new version with WorkingCopy Management
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / DestroyWorkingCopyOperation.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.IJavaElement;
16 import net.sourceforge.phpdt.core.JavaModelException;
17
18
19 /**
20  * Destroys a working copy (remove it from its cache if it is shared)
21  * and signal its removal through a delta.
22  */
23 public class DestroyWorkingCopyOperation extends JavaModelOperation {
24         
25         public DestroyWorkingCopyOperation(IJavaElement workingCopy) {
26                 super(new IJavaElement[] {workingCopy});
27         }
28         /**
29          * @exception JavaModelException if setting the source
30          *      of the original compilation unit fails
31          */
32         protected void executeOperation() throws JavaModelException {
33                 WorkingCopy workingCopy = getWorkingCopy();
34                 workingCopy.close();
35                 
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();
40                 }
41                 
42                 // remove working copy from the cache if it is shared
43                 JavaModelManager manager = JavaModelManager.getJavaModelManager();
44                 
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;
49                 
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$
55                         }
56                 }
57                 
58                 // report removed java delta
59                 JavaElementDelta delta = new JavaElementDelta(this.getJavaModel());
60                 delta.removed(workingCopy);
61                 addDelta(delta);
62                 removeReconcileDelta(workingCopy);
63         }
64         /**
65          * Returns the working copy this operation is working on.
66          */
67         protected WorkingCopy getWorkingCopy() {
68                 return (WorkingCopy)getElementToProcess();
69         }
70         /**
71          * @see JavaModelOperation#isReadOnly
72          */
73         public boolean isReadOnly() {
74                 return true;
75         }
76 }