KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.jdt.core.IClasspathEntry;
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IJavaModel;
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.core.JavaModelException;
24 import org.eclipse.jdt.internal.core.util.Util;
25
26 public class SetVariablesOperation extends ChangeClasspathOperation {
27     
28     String JavaDoc[] variableNames;
29     IPath[] variablePaths;
30     boolean updatePreferences;
31         
32     /*
33      * Creates a new SetVariablesOperation for the given variable values (null path meaning removal), allowing to change multiple variable values at once.
34      */

35     public SetVariablesOperation(String JavaDoc[] variableNames, IPath[] variablePaths, boolean updatePreferences) {
36         super(new IJavaElement[] {JavaModelManager.getJavaModelManager().getJavaModel()}, !ResourcesPlugin.getWorkspace().isTreeLocked());
37         this.variableNames = variableNames;
38         this.variablePaths = variablePaths;
39         this.updatePreferences = updatePreferences;
40     }
41
42     protected void executeOperation() throws JavaModelException {
43         checkCanceled();
44         try {
45             beginTask("", 1); //$NON-NLS-1$
46
if (JavaModelManager.CP_RESOLVE_VERBOSE)
47                 verbose_set_variables();
48             
49             JavaModelManager manager = JavaModelManager.getJavaModelManager();
50             if (manager.variablePutIfInitializingWithSameValue(this.variableNames, this.variablePaths))
51                 return;
52     
53             int varLength = this.variableNames.length;
54             
55             // gather classpath information for updating
56
final HashMap JavaDoc affectedProjectClasspaths = new HashMap JavaDoc(5);
57             IJavaModel model = getJavaModel();
58         
59             // filter out unmodified variables
60
int discardCount = 0;
61             for (int i = 0; i < varLength; i++){
62                 String JavaDoc variableName = this.variableNames[i];
63                 IPath oldPath = manager.variableGet(variableName); // if reentering will provide previous session value
64
if (oldPath == JavaModelManager.VARIABLE_INITIALIZATION_IN_PROGRESS) {
65                     oldPath = null; //33695 - cannot filter out restored variable, must update affected project to reset cached CP
66
}
67                 if (oldPath != null && oldPath.equals(this.variablePaths[i])){
68                     this.variableNames[i] = null;
69                     discardCount++;
70                 }
71             }
72             if (discardCount > 0){
73                 if (discardCount == varLength) return;
74                 int changedLength = varLength - discardCount;
75                 String JavaDoc[] changedVariableNames = new String JavaDoc[changedLength];
76                 IPath[] changedVariablePaths = new IPath[changedLength];
77                 for (int i = 0, index = 0; i < varLength; i++){
78                     if (this.variableNames[i] != null){
79                         changedVariableNames[index] = this.variableNames[i];
80                         changedVariablePaths[index] = this.variablePaths[i];
81                         index++;
82                     }
83                 }
84                 this.variableNames = changedVariableNames;
85                 this.variablePaths = changedVariablePaths;
86                 varLength = changedLength;
87             }
88             
89             if (isCanceled())
90                 return;
91     
92             IJavaProject[] projects = model.getJavaProjects();
93             nextProject : for (int i = 0, projectLength = projects.length; i < projectLength; i++){
94                 JavaProject project = (JavaProject) projects[i];
95                         
96                 // check to see if any of the modified variables is present on the classpath
97
IClasspathEntry[] classpath = project.getRawClasspath();
98                 for (int j = 0, cpLength = classpath.length; j < cpLength; j++){
99                     
100                     IClasspathEntry entry = classpath[j];
101                     for (int k = 0; k < varLength; k++){
102     
103                         String JavaDoc variableName = this.variableNames[k];
104                         if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE){
105     
106                             if (variableName.equals(entry.getPath().segment(0))){
107                                 affectedProjectClasspaths.put(project, project.getResolvedClasspath());
108                                 continue nextProject;
109                             }
110                             IPath sourcePath, sourceRootPath;
111                             if (((sourcePath = entry.getSourceAttachmentPath()) != null && variableName.equals(sourcePath.segment(0)))
112                                 || ((sourceRootPath = entry.getSourceAttachmentRootPath()) != null && variableName.equals(sourceRootPath.segment(0)))) {
113     
114                                 affectedProjectClasspaths.put(project, project.getResolvedClasspath());
115                                 continue nextProject;
116                             }
117                         }
118                     }
119                 }
120             }
121
122             // update variables
123
for (int i = 0; i < varLength; i++){
124                 manager.variablePut(this.variableNames[i], this.variablePaths[i]);
125                 if (this.updatePreferences)
126                     manager.variablePreferencesPut(this.variableNames[i], this.variablePaths[i]);
127             }
128                     
129             // update affected project classpaths
130
if (!affectedProjectClasspaths.isEmpty()) {
131                 String JavaDoc[] dbgVariableNames = this.variableNames;
132                 try {
133                     // propagate classpath change
134
Iterator JavaDoc projectsToUpdate = affectedProjectClasspaths.keySet().iterator();
135                     while (projectsToUpdate.hasNext()) {
136     
137                         if (this.progressMonitor != null && this.progressMonitor.isCanceled()) return;
138     
139                         JavaProject affectedProject = (JavaProject) projectsToUpdate.next();
140     
141                         // force resolved classpath to be recomputed
142
if (JavaModelManager.CP_RESOLVE_VERBOSE_ADVANCED)
143                             verbose_update_project(dbgVariableNames, affectedProject);
144                         affectedProject.getPerProjectInfo().resetResolvedClasspath();
145                         
146                         // if needed, generate delta, update project ref, create markers, ...
147
classpathChanged(affectedProject);
148
149                         if (this.canChangeResources) {
150                             // touch project to force a build if needed
151
affectedProject.getProject().touch(this.progressMonitor);
152                         }
153                     }
154                 } catch (CoreException e) {
155                     if (JavaModelManager.CP_RESOLVE_VERBOSE){
156                         verbose_failure(dbgVariableNames);
157                         e.printStackTrace();
158                     }
159                     if (e instanceof JavaModelException) {
160                         throw (JavaModelException)e;
161                     } else {
162                         throw new JavaModelException(e);
163                     }
164                 }
165             }
166         } finally {
167             done();
168         }
169     }
170
171     private void verbose_failure(String JavaDoc[] dbgVariableNames) {
172         Util.verbose(
173             "CPVariable SET - FAILED DUE TO EXCEPTION\n" + //$NON-NLS-1$
174
" variables: " + org.eclipse.jdt.internal.compiler.util.Util.toString(dbgVariableNames), //$NON-NLS-1$
175
System.err);
176     }
177
178     private void verbose_update_project(String JavaDoc[] dbgVariableNames,
179             JavaProject affectedProject) {
180         Util.verbose(
181             "CPVariable SET - updating affected project due to setting variables\n" + //$NON-NLS-1$
182
" project: " + affectedProject.getElementName() + '\n' + //$NON-NLS-1$
183
" variables: " + org.eclipse.jdt.internal.compiler.util.Util.toString(dbgVariableNames)); //$NON-NLS-1$
184
}
185
186     private void verbose_set_variables() {
187         Util.verbose(
188             "CPVariable SET - setting variables\n" + //$NON-NLS-1$
189
" variables: " + org.eclipse.jdt.internal.compiler.util.Util.toString(this.variableNames) + '\n' +//$NON-NLS-1$
190
" values: " + org.eclipse.jdt.internal.compiler.util.Util.toString(this.variablePaths)); //$NON-NLS-1$
191
}
192
193 }
194
Popular Tags