RC2 compatibility
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / BecomeWorkingCopyOperation.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import net.sourceforge.phpdt.core.IJavaElement;
14 import net.sourceforge.phpdt.core.IJavaElementDelta;
15 import net.sourceforge.phpdt.core.IProblemRequestor;
16 import net.sourceforge.phpdt.core.JavaModelException;
17
18 /**
19  * Switch and ICompilationUnit to working copy mode
20  * and signal the working copy addition through a delta.
21  */
22 public class BecomeWorkingCopyOperation extends JavaModelOperation {
23         
24         IProblemRequestor problemRequestor;
25         
26         /*
27          * Creates a BecomeWorkingCopyOperation for the given working copy.
28          * perOwnerWorkingCopies map is not null if the working copy is a shared working copy.
29          */
30         public BecomeWorkingCopyOperation(CompilationUnit workingCopy, IProblemRequestor problemRequestor) {
31                 super(new IJavaElement[] {workingCopy});
32                 this.problemRequestor = problemRequestor;
33         }
34         protected void executeOperation() throws JavaModelException {
35
36                 // open the working copy now to ensure contents are that of the current state of this element
37                 CompilationUnit workingCopy = getWorkingCopy();
38                 JavaModelManager.getJavaModelManager().getPerWorkingCopyInfo(workingCopy, true/*create if needed*/, true/*record usage*/, this.problemRequestor);
39                 workingCopy.openWhenClosed(workingCopy.createElementInfo(), this.progressMonitor);
40
41                 if (!workingCopy.isPrimary()) {
42                         // report added java delta for a non-primary working copy
43                         JavaElementDelta delta = new JavaElementDelta(this.getJavaModel());
44                         delta.added(workingCopy);
45                         addDelta(delta);
46                 } else {
47                         if (workingCopy.getResource().isAccessible()) {
48                                 // report a F_PRIMARY_WORKING_COPY change delta for a primary working copy
49                                 JavaElementDelta delta = new JavaElementDelta(this.getJavaModel());
50                                 delta.changed(workingCopy, IJavaElementDelta.F_PRIMARY_WORKING_COPY);
51                                 addDelta(delta);
52                         } else {
53                                 // report an ADDED delta
54                                 JavaElementDelta delta = new JavaElementDelta(this.getJavaModel());
55                                 delta.added(workingCopy, IJavaElementDelta.F_PRIMARY_WORKING_COPY);
56                                 addDelta(delta);
57                         }
58                 }
59
60                 this.resultElements = new IJavaElement[] {workingCopy};
61         }
62         /*
63          * Returns the working copy this operation is working on.
64          */
65         protected CompilationUnit getWorkingCopy() {
66                 return (CompilationUnit)getElementToProcess();
67         }
68         /*
69          * @see JavaModelOperation#isReadOnly
70          */
71         public boolean isReadOnly() {
72                 return true;
73         }
74
75 }