78d052fdf84ec8e6d5268996e9d0db73eff40d02
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / CreateWorkingCopyOperation.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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 java.util.Map;
14
15 import net.sourceforge.phpdt.core.IBufferFactory;
16 import net.sourceforge.phpdt.core.ICompilationUnit;
17 import net.sourceforge.phpdt.core.IJavaElement;
18 import net.sourceforge.phpdt.core.IPackageFragment;
19 import net.sourceforge.phpdt.core.IProblemRequestor;
20 import net.sourceforge.phpdt.core.JavaModelException;
21
22
23
24 /**
25  * Creates a new working copy and signal its addition through a delta.
26  */
27 public class CreateWorkingCopyOperation extends JavaModelOperation {
28         
29         Map perFactoryWorkingCopies;
30         IBufferFactory factory;
31         IProblemRequestor problemRequestor;
32         
33         /*
34          * Creates a working copy from the given original cu and the given buffer factory.
35          * perFactoryWorkingCopies map is not null if the working copy is a shared working copy.
36          */
37         public CreateWorkingCopyOperation(ICompilationUnit originalElement, Map perFactoryWorkingCopies, IBufferFactory factory, IProblemRequestor problemRequestor) {
38                 super(new IJavaElement[] {originalElement});
39                 this.perFactoryWorkingCopies = perFactoryWorkingCopies;
40                 this.factory = factory;
41                 this.problemRequestor = problemRequestor;
42         }
43         protected void executeOperation() throws JavaModelException {
44                 ICompilationUnit cu = getCompilationUnit();
45
46                 WorkingCopy workingCopy = new WorkingCopy((IPackageFragment)cu.getParent(), cu.getElementName(), this.factory, this.problemRequestor);
47                 // open the working copy now to ensure contents are that of the current state of this element
48                 workingCopy.open(this.fMonitor);
49                 
50                 if (this.perFactoryWorkingCopies != null) {
51                         this.perFactoryWorkingCopies.put(cu, workingCopy);
52                         if (CompilationUnit.SHARED_WC_VERBOSE) {
53                                 System.out.println("Creating shared working copy " + workingCopy.toStringWithAncestors()); //$NON-NLS-1$
54                         }
55                 }
56
57                 // report added java delta
58                 JavaElementDelta delta = new JavaElementDelta(this.getJavaModel());
59                 delta.added(workingCopy);
60                 addDelta(delta);
61
62                 fResultElements = new IJavaElement[] {workingCopy};
63         }
64         /**
65          * Returns the compilation unit this operation is working on.
66          */
67         protected ICompilationUnit getCompilationUnit() {
68                 return (ICompilationUnit)getElementToProcess();
69         }
70         /**
71          * @see JavaModelOperation#isReadOnly
72          */
73         public boolean isReadOnly() {
74                 return true;
75         }
76
77 }