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.IJavaElement;
16 import net.sourceforge.phpdt.core.JavaModelException;
20 * Destroys a working copy (remove it from its cache if it is shared)
21 * and signal its removal through a delta.
23 public class DestroyWorkingCopyOperation extends JavaModelOperation {
25 public DestroyWorkingCopyOperation(IJavaElement workingCopy) {
26 super(new IJavaElement[] {workingCopy});
29 * @exception JavaModelException if setting the source
30 * of the original compilation unit fails
32 protected void executeOperation() throws JavaModelException {
33 WorkingCopy workingCopy = getWorkingCopy();
36 // if original element is not on classpath flush it from the cache
37 IJavaElement originalElement = workingCopy.getOriginalElement();
38 if (!workingCopy.getParent().exists()) {
39 ((CompilationUnit)originalElement).close();
42 // remove working copy from the cache if it is shared
43 JavaModelManager manager = JavaModelManager.getJavaModelManager();
45 // In order to be shared, working copies have to denote the same compilation unit
46 // AND use the same buffer factory.
47 // Assuming there is a little set of buffer factories, then use a 2 level Map cache.
48 Map sharedWorkingCopies = manager.sharedWorkingCopies;
50 Map perFactoryWorkingCopies = (Map) sharedWorkingCopies.get(workingCopy.bufferFactory);
51 if (perFactoryWorkingCopies != null){
52 if (perFactoryWorkingCopies.remove(originalElement) != null
53 && CompilationUnit.SHARED_WC_VERBOSE) {
54 System.out.println("Destroying shared working copy " + workingCopy.toStringWithAncestors());//$NON-NLS-1$
58 // report removed java delta
59 JavaElementDelta delta = new JavaElementDelta(this.getJavaModel());
60 delta.removed(workingCopy);
62 removeReconcileDelta(workingCopy);
65 * Returns the working copy this operation is working on.
67 protected WorkingCopy getWorkingCopy() {
68 return (WorkingCopy)getElementToProcess();
71 * @see JavaModelOperation#isReadOnly
73 public boolean isReadOnly() {