KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > DiscardWorkingCopyOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import org.eclipse.jdt.core.IJavaElement;
14 import org.eclipse.jdt.core.IJavaElementDelta;
15 import org.eclipse.jdt.core.IJavaProject;
16 import org.eclipse.jdt.core.JavaModelException;
17
18 /**
19  * Discards a working copy (decrement its use count and remove its working copy info if the use count is 0)
20  * and signal its removal through a delta.
21  */

22 public class DiscardWorkingCopyOperation extends JavaModelOperation {
23     
24     public DiscardWorkingCopyOperation(IJavaElement workingCopy) {
25         super(new IJavaElement[] {workingCopy});
26     }
27     protected void executeOperation() throws JavaModelException {
28         CompilationUnit workingCopy = getWorkingCopy();
29         
30         JavaModelManager manager = JavaModelManager.getJavaModelManager();
31         int useCount = manager.discardPerWorkingCopyInfo(workingCopy);
32         if (useCount == 0) {
33             IJavaProject javaProject = workingCopy.getJavaProject();
34             if (ExternalJavaProject.EXTERNAL_PROJECT_NAME.equals(javaProject.getElementName())) {
35                 manager.removePerProjectInfo((JavaProject) javaProject);
36                 manager.containerRemove(javaProject);
37             }
38             if (!workingCopy.isPrimary()) {
39                 // report removed java delta for a non-primary working copy
40
JavaElementDelta delta = new JavaElementDelta(getJavaModel());
41                 delta.removed(workingCopy);
42                 addDelta(delta);
43                 removeReconcileDelta(workingCopy);
44             } else {
45                 if (workingCopy.getResource().isAccessible()) {
46                     // report a F_PRIMARY_WORKING_COPY change delta for a primary working copy
47
JavaElementDelta delta = new JavaElementDelta(getJavaModel());
48                     delta.changed(workingCopy, IJavaElementDelta.F_PRIMARY_WORKING_COPY);
49                     addDelta(delta);
50                 } else {
51                     // report a REMOVED delta
52
JavaElementDelta delta = new JavaElementDelta(getJavaModel());
53                     delta.removed(workingCopy, IJavaElementDelta.F_PRIMARY_WORKING_COPY);
54                     addDelta(delta);
55                 }
56             }
57         }
58     }
59     /**
60      * Returns the working copy this operation is working on.
61      */

62     protected CompilationUnit getWorkingCopy() {
63         return (CompilationUnit)getElementToProcess();
64     }
65     /**
66      * @see JavaModelOperation#isReadOnly
67      */

68     public boolean isReadOnly() {
69         return true;
70     }
71 }
72
Popular Tags