3m9 compatible;
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / DiscardWorkingCopyOperation.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.IJavaElement;
14 import net.sourceforge.phpdt.core.IJavaElementDelta;
15 import net.sourceforge.phpdt.core.JavaModelException;
16
17 /**
18  * Discards a working copy (decrement its use count and remove its working copy info if the use count is 0)
19  * and signal its removal through a delta.
20  */
21 public class DiscardWorkingCopyOperation extends JavaModelOperation {
22         
23         public DiscardWorkingCopyOperation(IJavaElement workingCopy) {
24                 super(new IJavaElement[] {workingCopy});
25         }
26         protected void executeOperation() throws JavaModelException {
27                 CompilationUnit workingCopy = getWorkingCopy();
28                 
29                 int useCount = JavaModelManager.getJavaModelManager().discardPerWorkingCopyInfo(workingCopy);
30                 if (useCount == 0) {
31                         if (!workingCopy.isPrimary()) {
32                                 // report removed java delta for a non-primary working copy
33                                 JavaElementDelta delta = new JavaElementDelta(this.getJavaModel());
34                                 delta.removed(workingCopy);
35                                 addDelta(delta);
36                                 removeReconcileDelta(workingCopy);
37                         } else {
38                                 if (workingCopy.getResource().isAccessible()) {
39                                         // report a F_PRIMARY_WORKING_COPY change delta for a primary working copy
40                                         JavaElementDelta delta = new JavaElementDelta(this.getJavaModel());
41                                         delta.changed(workingCopy, IJavaElementDelta.F_PRIMARY_WORKING_COPY);
42                                         addDelta(delta);
43                                 } else {
44                                         // report a REMOVED delta
45                                         JavaElementDelta delta = new JavaElementDelta(this.getJavaModel());
46                                         delta.removed(workingCopy, IJavaElementDelta.F_PRIMARY_WORKING_COPY);
47                                         addDelta(delta);
48                                 }
49                         }
50                 }
51         }
52         /**
53          * Returns the working copy this operation is working on.
54          */
55         protected CompilationUnit getWorkingCopy() {
56                 return (CompilationUnit)getElementToProcess();
57         }
58         /**
59          * @see JavaModelOperation#isReadOnly
60          */
61         public boolean isReadOnly() {
62                 return true;
63         }
64 }