3m9 compatible;
[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.*;
14 import net.sourceforge.phpdt.core.IJavaElement;
15 import net.sourceforge.phpdt.core.JavaModelException;
16
17 /**
18  * Switch and ICompilationUnit to working copy mode
19  * and signal the working copy addition through a delta.
20  */
21 public class BecomeWorkingCopyOperation extends JavaModelOperation {
22         
23         IProblemRequestor problemRequestor;
24         
25         /*
26          * Creates a BecomeWorkingCopyOperation for the given working copy.
27          * perOwnerWorkingCopies map is not null if the working copy is a shared working copy.
28          */
29         public BecomeWorkingCopyOperation(CompilationUnit workingCopy, IProblemRequestor problemRequestor) {
30                 super(new IJavaElement[] {workingCopy});
31                 this.problemRequestor = problemRequestor;
32         }
33         protected void executeOperation() throws JavaModelException {
34
35                 // open the working copy now to ensure contents are that of the current state of this element
36                 CompilationUnit workingCopy = getWorkingCopy();
37                 JavaModelManager.getJavaModelManager().getPerWorkingCopyInfo(workingCopy, true/*create if needed*/, true/*record usage*/, this.problemRequestor);
38                 workingCopy.openWhenClosed(workingCopy.createElementInfo(), this.progressMonitor);
39
40                 if (!workingCopy.isPrimary()) {
41                         // report added java delta for a non-primary working copy
42                         JavaElementDelta delta = new JavaElementDelta(this.getJavaModel());
43                         delta.added(workingCopy);
44                         addDelta(delta);
45                 } else {
46                         if (workingCopy.getResource().isAccessible()) {
47                                 // report a F_PRIMARY_WORKING_COPY change delta for a primary working copy
48                                 JavaElementDelta delta = new JavaElementDelta(this.getJavaModel());
49                                 delta.changed(workingCopy, IJavaElementDelta.F_PRIMARY_WORKING_COPY);
50                                 addDelta(delta);
51                         } else {
52                                 // report an ADDED delta
53                                 JavaElementDelta delta = new JavaElementDelta(this.getJavaModel());
54                                 delta.added(workingCopy, IJavaElementDelta.F_PRIMARY_WORKING_COPY);
55                                 addDelta(delta);
56                         }
57                 }
58
59                 this.resultElements = new IJavaElement[] {workingCopy};
60         }
61         /*
62          * Returns the working copy this operation is working on.
63          */
64         protected CompilationUnit getWorkingCopy() {
65                 return (CompilationUnit)getElementToProcess();
66         }
67         /*
68          * @see JavaModelOperation#isReadOnly
69          */
70         public boolean isReadOnly() {
71                 return true;
72         }
73
74 }