A massive organize imports and formatting of the sources using default Eclipse code...
[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
19  * info if the use count is 0) 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
27         protected void executeOperation() throws JavaModelException {
28                 CompilationUnit workingCopy = getWorkingCopy();
29
30                 int useCount = JavaModelManager.getJavaModelManager()
31                                 .discardPerWorkingCopyInfo(workingCopy);
32                 if (useCount == 0) {
33                         if (!workingCopy.isPrimary()) {
34                                 // report removed java delta for a non-primary working copy
35                                 JavaElementDelta delta = new JavaElementDelta(this
36                                                 .getJavaModel());
37                                 delta.removed(workingCopy);
38                                 addDelta(delta);
39                                 removeReconcileDelta(workingCopy);
40                         } else {
41                                 if (workingCopy.getResource().isAccessible()) {
42                                         // report a F_PRIMARY_WORKING_COPY change delta for a
43                                         // primary working copy
44                                         JavaElementDelta delta = new JavaElementDelta(this
45                                                         .getJavaModel());
46                                         delta.changed(workingCopy,
47                                                         IJavaElementDelta.F_PRIMARY_WORKING_COPY);
48                                         addDelta(delta);
49                                 } else {
50                                         // report a REMOVED delta
51                                         JavaElementDelta delta = new JavaElementDelta(this
52                                                         .getJavaModel());
53                                         delta.removed(workingCopy,
54                                                         IJavaElementDelta.F_PRIMARY_WORKING_COPY);
55                                         addDelta(delta);
56                                 }
57                         }
58                 }
59         }
60
61         /**
62          * Returns the working copy this operation is working on.
63          */
64         protected CompilationUnit getWorkingCopy() {
65                 return (CompilationUnit) getElementToProcess();
66         }
67
68         /**
69          * @see JavaModelOperation#isReadOnly
70          */
71         public boolean isReadOnly() {
72                 return true;
73         }
74 }