KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.core.resources.ResourcesPlugin;
14 import org.eclipse.core.runtime.jobs.ISchedulingRule;
15 import org.eclipse.jdt.core.IJavaElement;
16 import org.eclipse.jdt.core.JavaModelException;
17
18 /*
19  * Abstract class for operations that change the classpath
20  */

21 public abstract class ChangeClasspathOperation extends JavaModelOperation {
22
23     protected boolean canChangeResources;
24
25     public ChangeClasspathOperation(IJavaElement[] elements, boolean canChangeResources) {
26         super(elements);
27         this.canChangeResources = canChangeResources;
28     }
29
30     protected boolean canModifyRoots() {
31         // changing the classpath can modify roots
32
return true;
33     }
34     
35     /*
36      * The resolved classpath of the given project may have changed:
37      * - generate a delta
38      * - trigger indexing
39      * - update project references
40      * - create resolved classpath markers
41      */

42     protected void classpathChanged(JavaProject project) throws JavaModelException {
43         DeltaProcessingState state = JavaModelManager.getJavaModelManager().deltaState;
44         DeltaProcessor deltaProcessor = state.getDeltaProcessor();
45         ClasspathChange change = (ClasspathChange) deltaProcessor.classpathChanges.get(project.getProject());
46         if (this.canChangeResources) {
47             // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=177922
48
if (isTopLevelOperation() && !ResourcesPlugin.getWorkspace().isTreeLocked()) {
49                 new ClasspathValidation(project).validate();
50             }
51                 
52             // delta, indexing and classpath markers are going to be created by the delta processor
53
// while handling the .classpath file change
54

55             // however ensure project references are updated
56
// since some clients rely on the project references when run inside an IWorkspaceRunnable
57
new ProjectReferenceChange(project, change.oldResolvedClasspath).updateProjectReferencesIfNecessary();
58         } else {
59             JavaElementDelta delta = new JavaElementDelta(getJavaModel());
60             int result = change.generateDelta(delta);
61             if ((result & ClasspathChange.HAS_DELTA) != 0) {
62                 // create delta
63
addDelta(delta);
64                 
65                 // ensure indexes are updated
66
change.requestIndexing();
67                 
68                 // ensure classpath is validated on next build
69
state.addClasspathValidation(project);
70             }
71             if ((result & ClasspathChange.HAS_PROJECT_CHANGE) != 0) {
72                 // ensure project references are updated on next build
73
state.addProjectReferenceChange(project, change.oldResolvedClasspath);
74             }
75         }
76     }
77
78     protected ISchedulingRule getSchedulingRule() {
79         return null; // no lock taken while changing classpath
80
}
81     
82     public boolean isReadOnly() {
83         return !this.canChangeResources;
84     }
85
86 }
87
Popular Tags