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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
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;
25 * Creates a new working copy and signal its addition through a delta.
27 public class CreateWorkingCopyOperation extends JavaModelOperation {
29 Map perFactoryWorkingCopies;
30 IBufferFactory factory;
31 IProblemRequestor problemRequestor;
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.
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;
43 protected void executeOperation() throws JavaModelException {
44 ICompilationUnit cu = getCompilationUnit();
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);
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$
57 // report added java delta
58 JavaElementDelta delta = new JavaElementDelta(this.getJavaModel());
59 delta.added(workingCopy);
62 fResultElements = new IJavaElement[] {workingCopy};
65 * Returns the compilation unit this operation is working on.
67 protected ICompilationUnit getCompilationUnit() {
68 return (ICompilationUnit)getElementToProcess();
71 * @see JavaModelOperation#isReadOnly
73 public boolean isReadOnly() {