Replace deprecated org.eclipse.core.runtime.Platform.Run with org.eclipse.core.runtim...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / DeltaProcessor.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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 java.io.File;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Iterator;
19 import java.util.Map;
20
21 import net.sourceforge.phpdt.core.ElementChangedEvent;
22 import net.sourceforge.phpdt.core.IClasspathEntry;
23 import net.sourceforge.phpdt.core.IElementChangedListener;
24 import net.sourceforge.phpdt.core.IJavaElement;
25 import net.sourceforge.phpdt.core.IJavaElementDelta;
26 import net.sourceforge.phpdt.core.IJavaModel;
27 import net.sourceforge.phpdt.core.IJavaProject;
28 import net.sourceforge.phpdt.core.JavaCore;
29 import net.sourceforge.phpdt.core.JavaModelException;
30 import net.sourceforge.phpdt.internal.core.builder.PHPBuilder;
31 import net.sourceforge.phpdt.internal.core.util.Util;
32 import net.sourceforge.phpdt.internal.core.util.PHPFileUtil;
33 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
34
35 import org.eclipse.core.resources.IFile;
36 import org.eclipse.core.resources.IProject;
37 import org.eclipse.core.resources.IResource;
38 import org.eclipse.core.resources.IResourceChangeEvent;
39 import org.eclipse.core.resources.IResourceChangeListener;
40 import org.eclipse.core.resources.IResourceDelta;
41 import org.eclipse.core.resources.IResourceDeltaVisitor;
42 import org.eclipse.core.resources.IWorkspace;
43 import org.eclipse.core.resources.ResourcesPlugin;
44 import org.eclipse.core.runtime.CoreException;
45 import org.eclipse.core.runtime.IPath;
46 import org.eclipse.core.runtime.ISafeRunnable;
47 //import org.eclipse.core.runtime.Platform;
48 import org.eclipse.core.runtime.QualifiedName;
49 import org.eclipse.core.runtime.SafeRunner;
50
51 /**
52  * This class is used by <code>JavaModelManager</code> to convert
53  * <code>IResourceDelta</code>s into <code>IJavaElementDelta</code>s. It
54  * also does some processing on the <code>JavaElement</code>s involved (e.g.
55  * closing them or updating classpaths).
56  */
57 public class DeltaProcessor implements IResourceChangeListener {
58
59         final static int IGNORE = 0;
60
61         final static int SOURCE = 1;
62
63         final static int BINARY = 2;
64
65         final static String EXTERNAL_JAR_ADDED = "external jar added"; //$NON-NLS-1$
66
67         final static String EXTERNAL_JAR_REMOVED = "external jar removed"; //$NON-NLS-1$
68
69         final static String EXTERNAL_JAR_CHANGED = "external jar changed"; //$NON-NLS-1$
70
71         final static String EXTERNAL_JAR_UNCHANGED = "external jar unchanged"; //$NON-NLS-1$
72
73         final static String INTERNAL_JAR_IGNORE = "internal jar ignore"; //$NON-NLS-1$
74
75         private final static int NON_JAVA_RESOURCE = -1;
76
77         public static boolean DEBUG = false;
78
79         public static boolean VERBOSE = false;
80
81         public static final int DEFAULT_CHANGE_EVENT = 0; // must not collide with
82                                                                                                                 // ElementChangedEvent
83                                                                                                                 // event masks
84
85         /**
86          * The <code>JavaElementDelta</code> corresponding to the
87          * <code>IResourceDelta</code> being translated.
88          */
89         protected JavaElementDelta currentDelta;
90
91         // protected IndexManager indexManager = new IndexManager();
92
93         /* A table from IPath (from a classpath entry) to RootInfo */
94         public Map roots;
95
96         /*
97          * A table from IPath (from a classpath entry) to ArrayList of RootInfo Used
98          * when an IPath corresponds to more than one root
99          */
100         Map otherRoots;
101
102         /* Whether the roots tables should be recomputed */
103         public boolean rootsAreStale = true;
104
105         /*
106          * A table from IPath (from a classpath entry) to RootInfo from the last
107          * time the delta processor was invoked.
108          */
109         public Map oldRoots;
110
111         /*
112          * A table from IPath (from a classpath entry) to ArrayList of RootInfo from
113          * the last time the delta processor was invoked. Used when an IPath
114          * corresponds to more than one root
115          */
116         Map oldOtherRoots;
117
118         /*
119          * A table from IPath (a source attachment path from a classpath entry) to
120          * IPath (a root path)
121          */
122         Map sourceAttachments;
123
124         /*
125          * The java element that was last created (see createElement(IResource)).
126          * This is used as a stack of java elements (using getParent() to pop it,
127          * and using the various get*(...) to push it.
128          */
129         Openable currentElement;
130
131         /*
132          * Queue of deltas created explicily by the Java Model that have yet to be
133          * fired.
134          */
135         public ArrayList javaModelDeltas = new ArrayList();
136
137         /*
138          * Queue of reconcile deltas on working copies that have yet to be fired.
139          * This is a table form IWorkingCopy to IJavaElementDelta
140          */
141         public HashMap reconcileDeltas = new HashMap();
142
143         /*
144          * Turns delta firing on/off. By default it is on.
145          */
146         private boolean isFiring = true;
147
148         public HashMap externalTimeStamps = new HashMap();
149
150         public HashSet projectsToUpdate = new HashSet();
151
152         // list of root projects which namelookup caches need to be updated for
153         // dependents
154         // TODO: (jerome) is it needed? projectsToUpdate might be sufficient
155         public HashSet projectsForDependentNamelookupRefresh = new HashSet();
156
157         /*
158          * The global state of delta processing.
159          */
160         private DeltaProcessingState state;
161
162         /*
163          * The Java model manager
164          */
165         private JavaModelManager manager;
166
167         /*
168          * A table from IJavaProject to an array of IPackageFragmentRoot. This table
169          * contains the pkg fragment roots of the project that are being deleted.
170          */
171         Map removedRoots;
172
173         /*
174          * A list of IJavaElement used as a scope for external archives refresh
175          * during POST_CHANGE. This is null if no refresh is needed.
176          */
177         HashSet refreshedElements;
178
179         class OutputsInfo {
180                 IPath[] paths;
181
182                 int[] traverseModes;
183
184                 int outputCount;
185
186                 OutputsInfo(IPath[] paths, int[] traverseModes, int outputCount) {
187                         this.paths = paths;
188                         this.traverseModes = traverseModes;
189                         this.outputCount = outputCount;
190                 }
191
192                 public String toString() {
193                         if (this.paths == null)
194                                 return "<none>"; //$NON-NLS-1$
195                         StringBuffer buffer = new StringBuffer();
196                         for (int i = 0; i < this.outputCount; i++) {
197                                 buffer.append("path="); //$NON-NLS-1$
198                                 buffer.append(this.paths[i].toString());
199                                 buffer.append("\n->traverse="); //$NON-NLS-1$
200                                 switch (this.traverseModes[i]) {
201                                 case BINARY:
202                                         buffer.append("BINARY"); //$NON-NLS-1$
203                                         break;
204                                 case IGNORE:
205                                         buffer.append("IGNORE"); //$NON-NLS-1$
206                                         break;
207                                 case SOURCE:
208                                         buffer.append("SOURCE"); //$NON-NLS-1$
209                                         break;
210                                 default:
211                                         buffer.append("<unknown>"); //$NON-NLS-1$
212                                 }
213                                 if (i + 1 < this.outputCount) {
214                                         buffer.append('\n');
215                                 }
216                         }
217                         return buffer.toString();
218                 }
219         }
220
221         class RootInfo {
222                 IJavaProject project;
223
224                 IPath rootPath;
225
226                 char[][] exclusionPatterns;
227
228                 RootInfo(IJavaProject project, IPath rootPath,
229                                 char[][] exclusionPatterns) {
230                         this.project = project;
231                         this.rootPath = rootPath;
232                         this.exclusionPatterns = exclusionPatterns;
233                 }
234
235                 boolean isRootOfProject(IPath path) {
236                         return this.rootPath.equals(path)
237                                         && this.project.getProject().getFullPath().isPrefixOf(path);
238                 }
239
240                 public String toString() {
241                         StringBuffer buffer = new StringBuffer("project="); //$NON-NLS-1$
242                         if (this.project == null) {
243                                 buffer.append("null"); //$NON-NLS-1$
244                         } else {
245                                 buffer.append(this.project.getElementName());
246                         }
247                         buffer.append("\npath="); //$NON-NLS-1$
248                         if (this.rootPath == null) {
249                                 buffer.append("null"); //$NON-NLS-1$
250                         } else {
251                                 buffer.append(this.rootPath.toString());
252                         }
253                         buffer.append("\nexcluding="); //$NON-NLS-1$
254                         if (this.exclusionPatterns == null) {
255                                 buffer.append("null"); //$NON-NLS-1$
256                         } else {
257                                 for (int i = 0, length = this.exclusionPatterns.length; i < length; i++) {
258                                         buffer.append(new String(this.exclusionPatterns[i]));
259                                         if (i < length - 1) {
260                                                 buffer.append("|"); //$NON-NLS-1$
261                                         }
262                                 }
263                         }
264                         return buffer.toString();
265                 }
266         }
267
268         // DeltaProcessor(JavaModelManager manager) {
269         // this.manager = manager;
270         // }
271
272         /*
273          * Type of event that should be processed no matter what the real event type
274          * is.
275          */
276         public int overridenEventType = -1;
277
278         public DeltaProcessor(DeltaProcessingState state, JavaModelManager manager) {
279                 this.state = state;
280                 this.manager = manager;
281         }
282
283         /*
284          * Adds the dependents of the given project to the list of the projects to
285          * update.
286          */
287         // void addDependentProjects(IPath projectPath, HashSet result) {
288         // try {
289         // IJavaProject[] projects = this.manager.getJavaModel().getJavaProjects();
290         // for (int i = 0, length = projects.length; i < length; i++) {
291         // IJavaProject project = projects[i];
292         // IClasspathEntry[] classpath =
293         // ((JavaProject)project).getExpandedClasspath(true);
294         // for (int j = 0, length2 = classpath.length; j < length2; j++) {
295         // IClasspathEntry entry = classpath[j];
296         // if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT
297         // && entry.getPath().equals(projectPath)) {
298         // result.add(project);
299         // }
300         // }
301         // }
302         // } catch (JavaModelException e) {
303         // }
304         // }
305         /*
306          * Adds the given element to the list of elements used as a scope for
307          * external jars refresh.
308          */
309         public void addForRefresh(IJavaElement element) {
310                 if (this.refreshedElements == null) {
311                         this.refreshedElements = new HashSet();
312                 }
313                 this.refreshedElements.add(element);
314         }
315
316         /*
317          * Adds the given project and its dependents to the list of the projects to
318          * update.
319          */
320         void addToProjectsToUpdateWithDependents(IProject project) {
321                 this.projectsToUpdate.add(JavaCore.create(project));
322                 // this.addDependentProjects(project.getFullPath(),
323                 // this.projectsToUpdate);
324         }
325
326         /**
327          * Adds the given child handle to its parent's cache of children.
328          */
329         protected void addToParentInfo(Openable child) {
330
331                 Openable parent = (Openable) child.getParent();
332                 if (parent != null && parent.isOpen()) {
333                         try {
334                                 JavaElementInfo info = (JavaElementInfo) parent
335                                                 .getElementInfo();
336                                 info.addChild(child);
337                         } catch (JavaModelException e) {
338                                 // do nothing - we already checked if open
339                         }
340                 }
341         }
342
343         /**
344          * Check all external archive (referenced by given roots, projects or model)
345          * status and issue a corresponding root delta. Also triggers index updates
346          */
347         // public void checkExternalArchiveChanges(IJavaElement[] refreshedElements,
348         // IProgressMonitor monitor) throws JavaModelException {
349         // try {
350         // for (int i = 0, length = refreshedElements.length; i < length; i++) {
351         // this.addForRefresh(refreshedElements[i]);
352         // }
353         // boolean hasDelta = this.createExternalArchiveDelta(monitor);
354         // if (monitor != null && monitor.isCanceled()) return;
355         // if (hasDelta){
356         // // force classpath marker refresh of affected projects
357         // JavaModel.flushExternalFileCache();
358         // IJavaElementDelta[] projectDeltas =
359         // this.currentDelta.getAffectedChildren();
360         // for (int i = 0, length = projectDeltas.length; i < length; i++) {
361         // IJavaElementDelta delta = projectDeltas[i];
362         // ((JavaProject)delta.getElement()).getResolvedClasspath(
363         // true, // ignoreUnresolvedEntry
364         // true); // generateMarkerOnError
365         // }
366         // if (this.currentDelta != null) { // if delta has not been fired while
367         // creating markers
368         // this.manager.fire(this.currentDelta,
369         // JavaModelManager.DEFAULT_CHANGE_EVENT);
370         // }
371         // }
372         // } finally {
373         // this.currentDelta = null;
374         // if (monitor != null) monitor.done();
375         // }
376         // }
377         /*
378          * Check if external archives have changed and create the corresponding
379          * deltas. Returns whether at least on delta was created.
380          */
381         // public boolean createExternalArchiveDelta(IProgressMonitor monitor)
382         // throws JavaModelException {
383         //              
384         // if (this.refreshedElements == null) return false;
385         //                      
386         // HashMap externalArchivesStatus = new HashMap();
387         // boolean hasDelta = false;
388         //              
389         // // find JARs to refresh
390         // HashSet archivePathsToRefresh = new HashSet();
391         // try {
392         // Iterator iterator = this.refreshedElements.iterator();
393         // while (iterator.hasNext()) {
394         // IJavaElement element = (IJavaElement)iterator.next();
395         // switch(element.getElementType()){
396         // case IJavaElement.PACKAGE_FRAGMENT_ROOT :
397         // archivePathsToRefresh.add(element.getPath());
398         // break;
399         // case IJavaElement.JAVA_PROJECT :
400         // IJavaProject project = (IJavaProject) element;
401         // if (!JavaProject.hasJavaNature(project.getProject())) {
402         // // project is not accessible or has lost its Java nature
403         // break;
404         // }
405         // IClasspathEntry[] classpath = project.getResolvedClasspath(true);
406         // for (int j = 0, cpLength = classpath.length; j < cpLength; j++){
407         // if (classpath[j].getEntryKind() == IClasspathEntry.CPE_LIBRARY){
408         // archivePathsToRefresh.add(classpath[j].getPath());
409         // }
410         // }
411         // break;
412         // case IJavaElement.JAVA_MODEL :
413         // IJavaProject[] projects =
414         // manager.getJavaModel().getOldJavaProjectsList();
415         // for (int j = 0, projectsLength = projects.length; j < projectsLength;
416         // j++){
417         // project = projects[j];
418         // if (!JavaProject.hasJavaNature(project.getProject())) {
419         // // project is not accessible or has lost its Java nature
420         // continue;
421         // }
422         // classpath = project.getResolvedClasspath(true);
423         // for (int k = 0, cpLength = classpath.length; k < cpLength; k++){
424         // if (classpath[k].getEntryKind() == IClasspathEntry.CPE_LIBRARY){
425         // archivePathsToRefresh.add(classpath[k].getPath());
426         // }
427         // }
428         // }
429         // break;
430         // }
431         // }
432         // } finally {
433         // this.refreshedElements = null;
434         // }
435         //              
436         // // perform refresh
437         // IJavaProject[] projects =
438         // manager.getJavaModel().getOldJavaProjectsList();
439         // IWorkspaceRoot wksRoot = ResourcesPlugin.getWorkspace().getRoot();
440         // for (int i = 0, length = projects.length; i < length; i++) {
441         //                      
442         // if (monitor != null && monitor.isCanceled()) break;
443         //                      
444         // IJavaProject project = projects[i];
445         // if (!JavaProject.hasJavaNature(project.getProject())) {
446         // // project is not accessible or has lost its Java nature
447         // continue;
448         // }
449         // IClasspathEntry[] entries = project.getResolvedClasspath(true);
450         // for (int j = 0; j < entries.length; j++){
451         // if (entries[j].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
452         //                                      
453         // IPath entryPath = entries[j].getPath();
454         //                                      
455         // if (!archivePathsToRefresh.contains(entryPath)) continue; // not supposed
456         // to be refreshed
457         //                                      
458         // String status = (String)externalArchivesStatus.get(entryPath);
459         // if (status == null){
460         //                                              
461         // // compute shared status
462         // Object targetLibrary = JavaModel.getTarget(wksRoot, entryPath, true);
463         //              
464         // if (targetLibrary == null){ // missing JAR
465         // if (this.externalTimeStamps.containsKey(entryPath)){
466         // this.externalTimeStamps.remove(entryPath);
467         // externalArchivesStatus.put(entryPath, EXTERNAL_JAR_REMOVED);
468         // // the jar was physically removed: remove the index
469         // indexManager.removeIndex(entryPath);
470         // }
471         //              
472         // } else if (targetLibrary instanceof File){ // external JAR
473         //              
474         // File externalFile = (File)targetLibrary;
475         //                                                      
476         // // check timestamp to figure if JAR has changed in some way
477         // Long oldTimestamp =(Long) this.externalTimeStamps.get(entryPath);
478         // long newTimeStamp = getTimeStamp(externalFile);
479         // if (oldTimestamp != null){
480         //              
481         // if (newTimeStamp == 0){ // file doesn't exist
482         // externalArchivesStatus.put(entryPath, EXTERNAL_JAR_REMOVED);
483         // this.externalTimeStamps.remove(entryPath);
484         // // remove the index
485         // indexManager.removeIndex(entryPath);
486         //              
487         // } else if (oldTimestamp.longValue() != newTimeStamp){
488         // externalArchivesStatus.put(entryPath, EXTERNAL_JAR_CHANGED);
489         // this.externalTimeStamps.put(entryPath, new Long(newTimeStamp));
490         // // first remove the index so that it is forced to be re-indexed
491         // indexManager.removeIndex(entryPath);
492         // // then index the jar
493         // indexManager.indexLibrary(entryPath, project.getProject());
494         // } else {
495         // externalArchivesStatus.put(entryPath, EXTERNAL_JAR_UNCHANGED);
496         // }
497         // } else {
498         // if (newTimeStamp == 0){ // jar still doesn't exist
499         // externalArchivesStatus.put(entryPath, EXTERNAL_JAR_UNCHANGED);
500         // } else {
501         // externalArchivesStatus.put(entryPath, EXTERNAL_JAR_ADDED);
502         // this.externalTimeStamps.put(entryPath, new Long(newTimeStamp));
503         // // index the new jar
504         // indexManager.indexLibrary(entryPath, project.getProject());
505         // }
506         // }
507         // } else { // internal JAR
508         // externalArchivesStatus.put(entryPath, INTERNAL_JAR_IGNORE);
509         // }
510         // }
511         // // according to computed status, generate a delta
512         // status = (String)externalArchivesStatus.get(entryPath);
513         // if (status != null){
514         // if (status == EXTERNAL_JAR_ADDED){
515         // PackageFragmentRoot root =
516         // (PackageFragmentRoot)project.getPackageFragmentRoot(entryPath.toString());
517         // if (VERBOSE){
518         // System.out.println("- External JAR ADDED, affecting root:
519         // "+root.getElementName()); //$NON-NLS-1$
520         // }
521         // elementAdded(root, null, null);
522         // hasDelta = true;
523         // } else if (status == EXTERNAL_JAR_CHANGED) {
524         // PackageFragmentRoot root =
525         // (PackageFragmentRoot)project.getPackageFragmentRoot(entryPath.toString());
526         // if (VERBOSE){
527         // System.out.println("- External JAR CHANGED, affecting root:
528         // "+root.getElementName()); //$NON-NLS-1$
529         // }
530         // // reset the corresponding project built state, since the builder would
531         // miss this change
532         // this.manager.setLastBuiltState(project.getProject(), null /*no state*/);
533         // contentChanged(root, null);
534         // hasDelta = true;
535         // } else if (status == EXTERNAL_JAR_REMOVED) {
536         // PackageFragmentRoot root =
537         // (PackageFragmentRoot)project.getPackageFragmentRoot(entryPath.toString());
538         // if (VERBOSE){
539         // System.out.println("- External JAR REMOVED, affecting root:
540         // "+root.getElementName()); //$NON-NLS-1$
541         // }
542         // elementRemoved(root, null, null);
543         // hasDelta = true;
544         // }
545         // }
546         // }
547         // }
548         // }
549         // return hasDelta;
550         // }
551         JavaElementDelta currentDelta() {
552                 if (this.currentDelta == null) {
553                         this.currentDelta = new JavaElementDelta(this.manager
554                                         .getJavaModel());
555                 }
556                 return this.currentDelta;
557         }
558
559         /*
560          * Process the given delta and look for projects being added, opened, closed
561          * or with a java nature being added or removed. Note that projects being
562          * deleted are checked in deleting(IProject). In all cases, add the
563          * project's dependents to the list of projects to update so that the
564          * classpath related markers can be updated.
565          */
566         // public void checkProjectsBeingAddedOrRemoved(IResourceDelta delta) {
567         // IResource resource = delta.getResource();
568         // switch (resource.getType()) {
569         // case IResource.ROOT :
570         // // workaround for bug 15168 circular errors not reported
571         // if (this.manager.javaProjectsCache == null) {
572         // try {
573         // this.manager.javaProjectsCache =
574         // this.manager.getJavaModel().getJavaProjects();
575         // } catch (JavaModelException e) {
576         // }
577         // }
578         //                              
579         // IResourceDelta[] children = delta.getAffectedChildren();
580         // for (int i = 0, length = children.length; i < length; i++) {
581         // this.checkProjectsBeingAddedOrRemoved(children[i]);
582         // }
583         // break;
584         // case IResource.PROJECT :
585         // // NB: No need to check project's nature as if the project is not a java
586         // project:
587         // // - if the project is added or changed this is a noop for
588         // projectsBeingDeleted
589         // // - if the project is closed, it has already lost its java nature
590         // int deltaKind = delta.getKind();
591         // if (deltaKind == IResourceDelta.ADDED) {
592         // // remember project and its dependents
593         // IProject project = (IProject)resource;
594         // this.addToProjectsToUpdateWithDependents(project);
595         //                                      
596         // // workaround for bug 15168 circular errors not reported
597         // if (JavaProject.hasJavaNature(project)) {
598         // this.addToParentInfo((JavaProject)JavaCore.create(project));
599         // }
600         //
601         // } else if (deltaKind == IResourceDelta.CHANGED) {
602         // IProject project = (IProject)resource;
603         // if ((delta.getFlags() & IResourceDelta.OPEN) != 0) {
604         // // project opened or closed: remember project and its dependents
605         // this.addToProjectsToUpdateWithDependents(project);
606         //                                              
607         // // workaround for bug 15168 circular errors not reported
608         // if (project.isOpen()) {
609         // if (JavaProject.hasJavaNature(project)) {
610         // this.addToParentInfo((JavaProject)JavaCore.create(project));
611         // }
612         // } else {
613         // JavaProject javaProject =
614         // (JavaProject)this.manager.getJavaModel().findJavaProject(project);
615         // if (javaProject != null) {
616         // try {
617         // javaProject.close();
618         // } catch (JavaModelException e) {
619         // }
620         // this.removeFromParentInfo(javaProject);
621         // }
622         // }
623         // } else if ((delta.getFlags() & IResourceDelta.DESCRIPTION) != 0) {
624         // boolean wasJavaProject =
625         // this.manager.getJavaModel().findJavaProject(project) != null;
626         // boolean isJavaProject = JavaProject.hasJavaNature(project);
627         // if (wasJavaProject != isJavaProject) {
628         // // java nature added or removed: remember project and its dependents
629         // this.addToProjectsToUpdateWithDependents(project);
630         //
631         // // workaround for bug 15168 circular errors not reported
632         // if (isJavaProject) {
633         // this.addToParentInfo((JavaProject)JavaCore.create(project));
634         // } else {
635         // JavaProject javaProject = (JavaProject)JavaCore.create(project);
636         //                                                              
637         // // flush classpath markers
638         // javaProject.
639         // flushClasspathProblemMarkers(
640         // true, // flush cycle markers
641         // true //flush classpath format markers
642         // );
643         //                                                                      
644         // // remove problems and tasks created by the builder
645         // JavaBuilder.removeProblemsAndTasksFor(project);
646         //
647         // // close project
648         // try {
649         // javaProject.close();
650         // } catch (JavaModelException e) {
651         // }
652         // this.removeFromParentInfo(javaProject);
653         // }
654         // } else {
655         // // in case the project was removed then added then changed (see bug
656         // 19799)
657         // if (JavaProject.hasJavaNature(project)) { // need nature check - 18698
658         // this.addToParentInfo((JavaProject)JavaCore.create(project));
659         // }
660         // }
661         // } else {
662         // // workaround for bug 15168 circular errors not reported
663         // // in case the project was removed then added then changed
664         // if (JavaProject.hasJavaNature(project)) { // need nature check - 18698
665         // this.addToParentInfo((JavaProject)JavaCore.create(project));
666         // }
667         // }
668         // }
669         // break;
670         // }
671         // }
672         // private void checkSourceAttachmentChange(IResourceDelta delta, IResource
673         // res) {
674         // IPath rootPath = (IPath)this.sourceAttachments.get(res.getFullPath());
675         // if (rootPath != null) {
676         // RootInfo rootInfo = this.rootInfo(rootPath, delta.getKind());
677         // if (rootInfo != null) {
678         // IJavaProject projectOfRoot = rootInfo.project;
679         // IPackageFragmentRoot root = null;
680         // try {
681         // // close the root so that source attachement cache is flushed
682         // root = projectOfRoot.findPackageFragmentRoot(rootPath);
683         // if (root != null) {
684         // root.close();
685         // }
686         // } catch (JavaModelException e) {
687         // }
688         // if (root == null) return;
689         // switch (delta.getKind()) {
690         // case IResourceDelta.ADDED:
691         // currentDelta().sourceAttached(root);
692         // break;
693         // case IResourceDelta.CHANGED:
694         // currentDelta().sourceDetached(root);
695         // currentDelta().sourceAttached(root);
696         // break;
697         // case IResourceDelta.REMOVED:
698         // currentDelta().sourceDetached(root);
699         // break;
700         // }
701         // }
702         // }
703         // }
704         /**
705          * Closes the given element, which removes it from the cache of open
706          * elements.
707          */
708         // protected static void close(Openable element) {
709         //
710         // try {
711         // element.close();
712         // } catch (JavaModelException e) {
713         // // do nothing
714         // }
715         // }
716         /**
717          * Generic processing for elements with changed contents:
718          * <ul>
719          * <li>The element is closed such that any subsequent accesses will re-open
720          * the element reflecting its new structure.
721          * <li>An entry is made in the delta reporting a content change (K_CHANGE
722          * with F_CONTENT flag set).
723          * </ul>
724          * Delta argument could be null if processing an external JAR change
725          */
726         // protected void contentChanged(Openable element, IResourceDelta delta) {
727         //
728         // close(element);
729         // int flags = IJavaElementDelta.F_CONTENT;
730         // if (element instanceof JarPackageFragmentRoot){
731         // flags |= IJavaElementDelta.F_ARCHIVE_CONTENT_CHANGED;
732         // }
733         // currentDelta().changed(element, flags);
734         // }
735         //      
736         /**
737          * Creates the openables corresponding to this resource. Returns null if
738          * none was found.
739          */
740         // protected Openable createElement(IResource resource, int elementType,
741         // RootInfo rootInfo) {
742         // if (resource == null) return null;
743         //              
744         // IPath path = resource.getFullPath();
745         // IJavaElement element = null;
746         // switch (elementType) {
747         //                      
748         // case IJavaElement.JAVA_PROJECT:
749         //                      
750         // // note that non-java resources rooted at the project level will also
751         // enter this code with
752         // // an elementType JAVA_PROJECT (see #elementType(...)).
753         // if (resource instanceof IProject){
754         //
755         // this.popUntilPrefixOf(path);
756         //                                      
757         // if (this.currentElement != null
758         // && this.currentElement.getElementType() == IJavaElement.JAVA_PROJECT
759         // && ((IJavaProject)this.currentElement).getProject().equals(resource)) {
760         // return this.currentElement;
761         // }
762         // if (rootInfo != null && rootInfo.project.getProject().equals(resource)){
763         // element = (Openable)rootInfo.project;
764         // break;
765         // }
766         // IProject proj = (IProject)resource;
767         // if (JavaProject.hasJavaNature(proj)) {
768         // element = JavaCore.create(proj);
769         // } else {
770         // // java project may have been been closed or removed (look for
771         // // element amongst old java project s list).
772         // element = (Openable) manager.getJavaModel().findJavaProject(proj);
773         // }
774         // }
775         // break;
776         // case IJavaElement.PACKAGE_FRAGMENT_ROOT:
777         // element = rootInfo == null ? JavaCore.create(resource) :
778         // rootInfo.project.getPackageFragmentRoot(resource);
779         // break;
780         // case IJavaElement.PACKAGE_FRAGMENT:
781         // // find the element that encloses the resource
782         // this.popUntilPrefixOf(path);
783         //                              
784         // if (this.currentElement == null) {
785         // element = rootInfo == null ? JavaCore.create(resource) :
786         // JavaModelManager.create(resource, rootInfo.project);
787         // } else {
788         // // find the root
789         // IPackageFragmentRoot root = this.currentElement.getPackageFragmentRoot();
790         // if (root == null) {
791         // element = rootInfo == null ? JavaCore.create(resource) :
792         // JavaModelManager.create(resource, rootInfo.project);
793         // } else if (((JavaProject)root.getJavaProject()).contains(resource)) {
794         // // create package handle
795         // IPath pkgPath = path.removeFirstSegments(root.getPath().segmentCount());
796         // String pkg = ProjectPrefUtil.packageName(pkgPath);
797         // if (pkg == null) return null;
798         // element = root.getPackageFragment(pkg);
799         // }
800         // }
801         // break;
802         // case IJavaElement.COMPILATION_UNIT:
803         // case IJavaElement.CLASS_FILE:
804         // // find the element that encloses the resource
805         // this.popUntilPrefixOf(path);
806         //                              
807         // if (this.currentElement == null) {
808         // element = rootInfo == null ? JavaCore.create(resource) :
809         // JavaModelManager.create(resource, rootInfo.project);
810         // } else {
811         // // find the package
812         // IPackageFragment pkgFragment = null;
813         // switch (this.currentElement.getElementType()) {
814         // case IJavaElement.PACKAGE_FRAGMENT_ROOT:
815         // IPackageFragmentRoot root = (IPackageFragmentRoot)this.currentElement;
816         // IPath rootPath = root.getPath();
817         // IPath pkgPath = path.removeLastSegments(1);
818         // String pkgName =
819         // ProjectPrefUtil.packageName(pkgPath.removeFirstSegments(rootPath.segmentCount()));
820         // if (pkgName != null) {
821         // pkgFragment = root.getPackageFragment(pkgName);
822         // }
823         // break;
824         // case IJavaElement.PACKAGE_FRAGMENT:
825         // Openable pkg = (Openable)this.currentElement;
826         // if (pkg.getPath().equals(path.removeLastSegments(1))) {
827         // pkgFragment = (IPackageFragment)pkg;
828         // } // else case of package x which is a prefix of x.y
829         // break;
830         // case IJavaElement.COMPILATION_UNIT:
831         // case IJavaElement.CLASS_FILE:
832         // pkgFragment = (IPackageFragment)this.currentElement.getParent();
833         // break;
834         // }
835         // if (pkgFragment == null) {
836         // element = rootInfo == null ? JavaCore.create(resource) :
837         // JavaModelManager.create(resource, rootInfo.project);
838         // } else {
839         // if (elementType == IJavaElement.COMPILATION_UNIT) {
840         // // create compilation unit handle
841         // // fileName validation has been done in elementType(IResourceDelta, int,
842         // boolean)
843         // String fileName = path.lastSegment();
844         // element = pkgFragment.getCompilationUnit(fileName);
845         // } else {
846         // // create class file handle
847         // // fileName validation has been done in elementType(IResourceDelta, int,
848         // boolean)
849         // String fileName = path.lastSegment();
850         // element = pkgFragment.getClassFile(fileName);
851         // }
852         // }
853         // }
854         // break;
855         // }
856         // if (element == null) {
857         // return null;
858         // } else {
859         // this.currentElement = (Openable)element;
860         // return this.currentElement;
861         // }
862         // }
863         /**
864          * Note that the project is about to be deleted.
865          */
866         // public void deleting(IProject project) {
867         //              
868         // try {
869         // // discard indexing jobs that belong to this project so that the project
870         // can be
871         // // deleted without interferences from the index manager
872         // this.indexManager.discardJobs(project.getName());
873         //
874         // JavaProject javaProject = (JavaProject)JavaCore.create(project);
875         //                      
876         // // remember roots of this project
877         // if (this.removedRoots == null) {
878         // this.removedRoots = new HashMap();
879         // }
880         // if (javaProject.isOpen()) {
881         // this.removedRoots.put(javaProject,
882         // javaProject.getPackageFragmentRoots());
883         // } else {
884         // // compute roots without opening project
885         // this.removedRoots.put(
886         // javaProject,
887         // javaProject.computePackageFragmentRoots(
888         // javaProject.getResolvedClasspath(true),
889         // false));
890         // }
891         //                      
892         // javaProject.close();
893         //
894         // // workaround for bug 15168 circular errors not reported
895         // if (this.manager.javaProjectsCache == null) {
896         // this.manager.javaProjectsCache =
897         // this.manager.getJavaModel().getJavaProjects();
898         // }
899         // this.removeFromParentInfo(javaProject);
900         //
901         // } catch (JavaModelException e) {
902         // }
903         //              
904         // this.addDependentProjects(project.getFullPath(), this.projectsToUpdate);
905         // }
906
907         /**
908          * Processing for an element that has been added:
909          * <ul>
910          * <li>If the element is a project, do nothing, and do not process
911          * children, as when a project is created it does not yet have any natures -
912          * specifically a java nature.
913          * <li>If the elemet is not a project, process it as added (see
914          * <code>basicElementAdded</code>.
915          * </ul>
916          * Delta argument could be null if processing an external JAR change
917          */
918         // protected void elementAdded(Openable element, IResourceDelta delta,
919         // RootInfo rootInfo) {
920         // int elementType = element.getElementType();
921         //              
922         // if (elementType == IJavaElement.JAVA_PROJECT) {
923         // // project add is handled by JavaProject.configure() because
924         // // when a project is created, it does not yet have a java nature
925         // if (delta != null &&
926         // JavaProject.hasJavaNature((IProject)delta.getResource())) {
927         // addToParentInfo(element);
928         // if ((delta.getFlags() & IResourceDelta.MOVED_FROM) != 0) {
929         // Openable movedFromElement =
930         // (Openable)element.getJavaModel().getJavaProject(delta.getMovedFromPath().lastSegment());
931         // currentDelta().movedTo(element, movedFromElement);
932         // } else {
933         // currentDelta().added(element);
934         // }
935         // this.projectsToUpdate.add(element);
936         // this.updateRoots(element.getPath(), delta);
937         // this.projectsForDependentNamelookupRefresh.add((JavaProject) element);
938         // }
939         // } else {
940         // addToParentInfo(element);
941         //                      
942         // // Force the element to be closed as it might have been opened
943         // // before the resource modification came in and it might have a new child
944         // // For example, in an IWorkspaceRunnable:
945         // // 1. create a package fragment p using a java model operation
946         // // 2. open package p
947         // // 3. add file X.java in folder p
948         // // When the resource delta comes in, only the addition of p is notified,
949         // // but the package p is already opened, thus its children are not
950         // recomputed
951         // // and it appears empty.
952         // close(element);
953         //                      
954         // if (delta != null && (delta.getFlags() & IResourceDelta.MOVED_FROM) != 0)
955         // {
956         // IPath movedFromPath = delta.getMovedFromPath();
957         // IResource res = delta.getResource();
958         // IResource movedFromRes;
959         // if (res instanceof IFile) {
960         // movedFromRes = res.getWorkspace().getRoot().getFile(movedFromPath);
961         // } else {
962         // movedFromRes = res.getWorkspace().getRoot().getFolder(movedFromPath);
963         // }
964         //                              
965         // // find the element type of the moved from element
966         // RootInfo movedFromInfo = this.enclosingRootInfo(movedFromPath,
967         // IResourceDelta.REMOVED);
968         // int movedFromType =
969         // this.elementType(
970         // movedFromRes,
971         // IResourceDelta.REMOVED,
972         // element.getParent().getElementType(),
973         // movedFromInfo);
974         //                              
975         // // reset current element as it might be inside a nested root
976         // (popUntilPrefixOf() may use the outer root)
977         // this.currentElement = null;
978         //                      
979         // // create the moved from element
980         // Openable movedFromElement =
981         // elementType != IJavaElement.JAVA_PROJECT && movedFromType ==
982         // IJavaElement.JAVA_PROJECT ?
983         // null : // outside classpath
984         // this.createElement(movedFromRes, movedFromType, movedFromInfo);
985         // if (movedFromElement == null) {
986         // // moved from outside classpath
987         // currentDelta().added(element);
988         // } else {
989         // currentDelta().movedTo(element, movedFromElement);
990         // }
991         // } else {
992         // currentDelta().added(element);
993         // }
994         //                      
995         // switch (elementType) {
996         // case IJavaElement.PACKAGE_FRAGMENT_ROOT :
997         // // when a root is added, and is on the classpath, the project must be
998         // updated
999         // JavaProject project = (JavaProject) element.getJavaProject();
1000         // this.projectsToUpdate.add(project);
1001         // this.projectsForDependentNamelookupRefresh.add(project);
1002         //                                      
1003         // break;
1004         // case IJavaElement.PACKAGE_FRAGMENT :
1005         // // get rid of namelookup since it holds onto obsolete cached info
1006         // project = (JavaProject) element.getJavaProject();
1007         // try {
1008         // project.getJavaProjectElementInfo().setNameLookup(null);
1009         // this.projectsForDependentNamelookupRefresh.add(project);
1010         // } catch (JavaModelException e) {
1011         // }
1012         // // add subpackages
1013         // if (delta != null){
1014         // PackageFragmentRoot root = element.getPackageFragmentRoot();
1015         // String name = element.getElementName();
1016         // IResourceDelta[] children = delta.getAffectedChildren();
1017         // for (int i = 0, length = children.length; i < length; i++) {
1018         // IResourceDelta child = children[i];
1019         // IResource resource = child.getResource();
1020         // if (resource instanceof IFolder) {
1021         // String folderName = resource.getName();
1022         // if (ProjectPrefUtil.isValidFolderNameForPackage(folderName)) {
1023         // String subpkgName =
1024         // name.length() == 0 ?
1025         // folderName :
1026         // name + "." + folderName; //$NON-NLS-1$
1027         // Openable subpkg = (Openable)root.getPackageFragment(subpkgName);
1028         // this.updateIndex(subpkg, child);
1029         // this.elementAdded(subpkg, child, rootInfo);
1030         // }
1031         // }
1032         // }
1033         // }
1034         // break;
1035         // }
1036         // }
1037         // }
1038         /**
1039          * Generic processing for a removed element:
1040          * <ul>
1041          * <li>Close the element, removing its structure from the cache
1042          * <li>Remove the element from its parent's cache of children
1043          * <li>Add a REMOVED entry in the delta
1044          * </ul>
1045          * Delta argument could be null if processing an external JAR change
1046          */
1047         // protected void elementRemoved(Openable element, IResourceDelta delta,
1048         // RootInfo rootInfo) {
1049         //              
1050         // if (element.isOpen()) {
1051         // close(element);
1052         // }
1053         // removeFromParentInfo(element);
1054         // int elementType = element.getElementType();
1055         // if (delta != null && (delta.getFlags() & IResourceDelta.MOVED_TO) != 0) {
1056         // IPath movedToPath = delta.getMovedToPath();
1057         // IResource res = delta.getResource();
1058         // IResource movedToRes;
1059         // switch (res.getType()) {
1060         // case IResource.PROJECT:
1061         // movedToRes =
1062         // res.getWorkspace().getRoot().getProject(movedToPath.lastSegment());
1063         // break;
1064         // case IResource.FOLDER:
1065         // movedToRes = res.getWorkspace().getRoot().getFolder(movedToPath);
1066         // break;
1067         // case IResource.FILE:
1068         // movedToRes = res.getWorkspace().getRoot().getFile(movedToPath);
1069         // break;
1070         // default:
1071         // return;
1072         // }
1073         //
1074         // // find the element type of the moved from element
1075         // RootInfo movedToInfo = this.enclosingRootInfo(movedToPath,
1076         // IResourceDelta.ADDED);
1077         // int movedToType =
1078         // this.elementType(
1079         // movedToRes,
1080         // IResourceDelta.ADDED,
1081         // element.getParent().getElementType(),
1082         // movedToInfo);
1083         //
1084         // // reset current element as it might be inside a nested root
1085         // (popUntilPrefixOf() may use the outer root)
1086         // this.currentElement = null;
1087         //                      
1088         // // create the moved To element
1089         // Openable movedToElement =
1090         // elementType != IJavaElement.JAVA_PROJECT && movedToType ==
1091         // IJavaElement.JAVA_PROJECT ?
1092         // null : // outside classpath
1093         // this.createElement(movedToRes, movedToType, movedToInfo);
1094         // if (movedToElement == null) {
1095         // // moved outside classpath
1096         // currentDelta().removed(element);
1097         // } else {
1098         // currentDelta().movedFrom(element, movedToElement);
1099         // }
1100         // } else {
1101         // currentDelta().removed(element);
1102         // }
1103         //
1104         // switch (elementType) {
1105         // case IJavaElement.JAVA_MODEL :
1106         // this.indexManager.reset();
1107         // break;
1108         // case IJavaElement.JAVA_PROJECT :
1109         // this.manager.removePerProjectInfo(
1110         // (JavaProject) element);
1111         // this.updateRoots(element.getPath(), delta);
1112         // this.projectsForDependentNamelookupRefresh.add((JavaProject) element);
1113         // break;
1114         // case IJavaElement.PACKAGE_FRAGMENT_ROOT :
1115         // JavaProject project = (JavaProject) element.getJavaProject();
1116         // this.projectsToUpdate.add(project);
1117         // this.projectsForDependentNamelookupRefresh.add(project);
1118         // break;
1119         // case IJavaElement.PACKAGE_FRAGMENT :
1120         // //1G1TW2T - get rid of namelookup since it holds onto obsolete cached
1121         // info
1122         // project = (JavaProject) element.getJavaProject();
1123         // try {
1124         // project.getJavaProjectElementInfo().setNameLookup(null);
1125         // this.projectsForDependentNamelookupRefresh.add(project);
1126         // } catch (JavaModelException e) {
1127         // }
1128         // // remove subpackages
1129         // if (delta != null){
1130         // PackageFragmentRoot root = element.getPackageFragmentRoot();
1131         // String name = element.getElementName();
1132         // IResourceDelta[] children = delta.getAffectedChildren();
1133         // for (int i = 0, length = children.length; i < length; i++) {
1134         // IResourceDelta child = children[i];
1135         // IResource resource = child.getResource();
1136         // if (resource instanceof IFolder) {
1137         // String folderName = resource.getName();
1138         // if (ProjectPrefUtil.isValidFolderNameForPackage(folderName)) {
1139         // String subpkgName =
1140         // name.length() == 0 ?
1141         // folderName :
1142         // name + "." + folderName; //$NON-NLS-1$
1143         // Openable subpkg = (Openable)root.getPackageFragment(subpkgName);
1144         // this.updateIndex(subpkg, child);
1145         // this.elementRemoved(subpkg, child, rootInfo);
1146         // }
1147         // }
1148         // }
1149         // }
1150         // break;
1151         // }
1152         // }
1153         /*
1154          * Returns the type of the java element the given delta matches to. Returns
1155          * NON_JAVA_RESOURCE if unknown (e.g. a non-java resource or excluded .java
1156          * file)
1157          */
1158         // private int elementType(IResource res, int kind, int parentType, RootInfo
1159         // rootInfo) {
1160         // switch (parentType) {
1161         // case IJavaElement.JAVA_MODEL:
1162         // // case of a movedTo or movedFrom project (other cases are handled in
1163         // processResourceDelta(...)
1164         // return IJavaElement.JAVA_PROJECT;
1165         // case NON_JAVA_RESOURCE:
1166         // case IJavaElement.JAVA_PROJECT:
1167         // if (rootInfo == null) {
1168         // rootInfo = this.enclosingRootInfo(res.getFullPath(), kind);
1169         // }
1170         // if (rootInfo != null && rootInfo.isRootOfProject(res.getFullPath())) {
1171         // return IJavaElement.PACKAGE_FRAGMENT_ROOT;
1172         // } else {
1173         // return NON_JAVA_RESOURCE; // not yet in a package fragment root or root
1174         // of another project
1175         // }
1176         // case IJavaElement.PACKAGE_FRAGMENT_ROOT:
1177         // case IJavaElement.PACKAGE_FRAGMENT:
1178         // if (rootInfo == null) {
1179         // rootInfo = this.enclosingRootInfo(res.getFullPath(), kind);
1180         // }
1181         // if (rootInfo == null || ProjectPrefUtil.isExcluded(res,
1182         // rootInfo.exclusionPatterns)) {
1183         // return NON_JAVA_RESOURCE;
1184         // }
1185         // if (res instanceof IFolder) {
1186         // if (ProjectPrefUtil.isValidFolderNameForPackage(res.getName())) {
1187         // return IJavaElement.PACKAGE_FRAGMENT;
1188         // } else {
1189         // return NON_JAVA_RESOURCE;
1190         // }
1191         // } else {
1192         // String fileName = res.getName();
1193         // if (ProjectPrefUtil.isValidCompilationUnitName(fileName)) {
1194         // return IJavaElement.COMPILATION_UNIT;
1195         // } else if (ProjectPrefUtil.isValidClassFileName(fileName)) {
1196         // return IJavaElement.CLASS_FILE;
1197         // } else if (this.rootInfo(res.getFullPath(), kind) != null) {
1198         // // case of proj=src=bin and resource is a jar file on the classpath
1199         // return IJavaElement.PACKAGE_FRAGMENT_ROOT;
1200         // } else {
1201         // return NON_JAVA_RESOURCE;
1202         // }
1203         // }
1204         // default:
1205         // return NON_JAVA_RESOURCE;
1206         // }
1207         // }
1208         /**
1209          * Answer a combination of the lastModified stamp and the size. Used for
1210          * detecting external JAR changes
1211          */
1212         public static long getTimeStamp(File file) {
1213                 return file.lastModified() + file.length();
1214         }
1215
1216         public void initializeRoots() {
1217                 // remember roots infos as old roots infos
1218                 this.oldRoots = this.roots == null ? new HashMap() : this.roots;
1219                 this.oldOtherRoots = this.otherRoots == null ? new HashMap()
1220                                 : this.otherRoots;
1221
1222                 // recompute root infos only if necessary
1223                 if (!rootsAreStale)
1224                         return;
1225
1226                 this.roots = new HashMap();
1227                 this.otherRoots = new HashMap();
1228                 this.sourceAttachments = new HashMap();
1229
1230                 IJavaModel model = this.manager.getJavaModel();
1231                 IJavaProject[] projects;
1232                 try {
1233                         projects = model.getJavaProjects();
1234                 } catch (JavaModelException e) {
1235                         // nothing can be done
1236                         return;
1237                 }
1238                 for (int i = 0, length = projects.length; i < length; i++) {
1239                         IJavaProject project = projects[i];
1240                         IClasspathEntry[] classpath;
1241                         try {
1242                                 classpath = project.getResolvedClasspath(true);
1243                         } catch (JavaModelException e) {
1244                                 // continue with next project
1245                                 continue;
1246                         }
1247                         for (int j = 0, classpathLength = classpath.length; j < classpathLength; j++) {
1248                                 IClasspathEntry entry = classpath[j];
1249                                 if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT)
1250                                         continue;
1251
1252                                 // root path
1253                                 IPath path = entry.getPath();
1254                                 if (this.roots.get(path) == null) {
1255                                         this.roots.put(path, new RootInfo(project, path,
1256                                                         ((ClasspathEntry) entry)
1257                                                                         .fullExclusionPatternChars()));
1258                                 } else {
1259                                         ArrayList rootList = (ArrayList) this.otherRoots.get(path);
1260                                         if (rootList == null) {
1261                                                 rootList = new ArrayList();
1262                                                 this.otherRoots.put(path, rootList);
1263                                         }
1264                                         rootList.add(new RootInfo(project, path,
1265                                                         ((ClasspathEntry) entry)
1266                                                                         .fullExclusionPatternChars()));
1267                                 }
1268
1269                                 // source attachment path
1270                                 if (entry.getEntryKind() != IClasspathEntry.CPE_LIBRARY)
1271                                         continue;
1272                                 QualifiedName qName = new QualifiedName(JavaCore.PLUGIN_ID,
1273                                                 "sourceattachment: " + path.toOSString()); //$NON-NLS-1$;
1274                                 String propertyString = null;
1275                                 try {
1276                                         propertyString = ResourcesPlugin.getWorkspace().getRoot()
1277                                                         .getPersistentProperty(qName);
1278                                 } catch (CoreException e) {
1279                                         continue;
1280                                 }
1281                                 IPath sourceAttachmentPath;
1282                                 // if (propertyString != null) {
1283                                 // int index=
1284                                 // propertyString.lastIndexOf(JarPackageFragmentRoot.ATTACHMENT_PROPERTY_DELIMITER);
1285                                 // sourceAttachmentPath = (index < 0) ? new Path(propertyString)
1286                                 // : new Path(propertyString.substring(0, index));
1287                                 // } else {
1288                                 sourceAttachmentPath = entry.getSourceAttachmentPath();
1289                                 // }
1290                                 if (sourceAttachmentPath != null) {
1291                                         this.sourceAttachments.put(sourceAttachmentPath, path);
1292                                 }
1293                         }
1294                 }
1295                 this.rootsAreStale = false;
1296         }
1297
1298         /*
1299          * Returns whether a given delta contains some information relevant to the
1300          * JavaModel, in particular it will not consider SYNC or MARKER only deltas.
1301          */
1302         public boolean isAffectedBy(IResourceDelta rootDelta) {
1303                 // if (rootDelta == null) System.out.println("NULL DELTA");
1304                 // long start = System.currentTimeMillis();
1305                 if (rootDelta != null) {
1306                         // use local exception to quickly escape from delta traversal
1307                         class FoundRelevantDeltaException extends RuntimeException {
1308                         }
1309                         try {
1310                                 rootDelta.accept(new IResourceDeltaVisitor() {
1311                                         public boolean visit(IResourceDelta delta)
1312                                                         throws CoreException {
1313                                                 switch (delta.getKind()) {
1314                                                 case IResourceDelta.ADDED:
1315                                                 case IResourceDelta.REMOVED:
1316                                                         throw new FoundRelevantDeltaException();
1317                                                 case IResourceDelta.CHANGED:
1318                                                         // if any flag is set but SYNC or MARKER, this delta
1319                                                         // should be considered
1320                                                         if (delta.getAffectedChildren().length == 0 // only
1321                                                                                                                                                 // check
1322                                                                                                                                                 // leaf
1323                                                                                                                                                 // delta
1324                                                                                                                                                 // nodes
1325                                                                         && (delta.getFlags() & ~(IResourceDelta.SYNC | IResourceDelta.MARKERS)) != 0) {
1326                                                                 throw new FoundRelevantDeltaException();
1327                                                         }
1328                                                 }
1329                                                 return true;
1330                                         }
1331                                 });
1332                         } catch (FoundRelevantDeltaException e) {
1333                                 // System.out.println("RELEVANT DELTA detected in: "+
1334                                 // (System.currentTimeMillis() - start));
1335                                 return true;
1336                         } catch (CoreException e) { // ignore delta if not able to traverse
1337                         }
1338                 }
1339                 // System.out.println("IGNORE SYNC DELTA took: "+
1340                 // (System.currentTimeMillis() - start));
1341                 return false;
1342         }
1343
1344         /*
1345          * Returns whether the given resource is in one of the given output folders
1346          * and if it is filtered out from this output folder.
1347          */
1348         private boolean isResFilteredFromOutput(OutputsInfo info, IResource res,
1349                         int elementType) {
1350                 if (info != null) {
1351                         IPath resPath = res.getFullPath();
1352                         for (int i = 0; i < info.outputCount; i++) {
1353                                 if (info.paths[i].isPrefixOf(resPath)) {
1354                                         if (info.traverseModes[i] != IGNORE) {
1355                                                 // case of bin=src
1356                                                 if (info.traverseModes[i] == SOURCE
1357                                                                 && elementType == IJavaElement.CLASS_FILE) {
1358                                                         return true;
1359                                                 } else {
1360                                                         // case of .class file under project and no source
1361                                                         // folder
1362                                                         // proj=bin
1363                                                         if (elementType == IJavaElement.JAVA_PROJECT
1364                                                                         && res instanceof IFile
1365                                                                         && PHPFileUtil.isPHPFile((IFile) res)) {
1366                                                                 return true;
1367                                                         }
1368                                                 }
1369                                         } else {
1370                                                 return true;
1371                                         }
1372                                 }
1373                         }
1374                 }
1375                 return false;
1376         }
1377
1378         /*
1379          * Merges all awaiting deltas.
1380          */
1381         private IJavaElementDelta mergeDeltas(Collection deltas) {
1382                 if (deltas.size() == 0)
1383                         return null;
1384                 if (deltas.size() == 1)
1385                         return (IJavaElementDelta) deltas.iterator().next();
1386
1387                 if (VERBOSE) {
1388                         System.out
1389                                         .println("MERGING " + deltas.size() + " DELTAS [" + Thread.currentThread() + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
1390                 }
1391
1392                 Iterator iterator = deltas.iterator();
1393                 JavaElementDelta rootDelta = new JavaElementDelta(
1394                                 this.manager.javaModel);
1395                 boolean insertedTree = false;
1396                 while (iterator.hasNext()) {
1397                         JavaElementDelta delta = (JavaElementDelta) iterator.next();
1398                         if (VERBOSE) {
1399                                 System.out.println(delta.toString());
1400                         }
1401                         IJavaElement element = delta.getElement();
1402                         if (this.manager.javaModel.equals(element)) {
1403                                 IJavaElementDelta[] children = delta.getAffectedChildren();
1404                                 for (int j = 0; j < children.length; j++) {
1405                                         JavaElementDelta projectDelta = (JavaElementDelta) children[j];
1406                                         rootDelta.insertDeltaTree(projectDelta.getElement(),
1407                                                         projectDelta);
1408                                         insertedTree = true;
1409                                 }
1410                                 IResourceDelta[] resourceDeltas = delta.getResourceDeltas();
1411                                 if (resourceDeltas != null) {
1412                                         for (int i = 0, length = resourceDeltas.length; i < length; i++) {
1413                                                 rootDelta.addResourceDelta(resourceDeltas[i]);
1414                                                 insertedTree = true;
1415                                         }
1416                                 }
1417                         } else {
1418                                 rootDelta.insertDeltaTree(element, delta);
1419                                 insertedTree = true;
1420                         }
1421                 }
1422                 if (insertedTree)
1423                         return rootDelta;
1424                 return null;
1425         }
1426
1427         /**
1428          * Check whether the updated file is affecting some of the properties of a
1429          * given project (like its classpath persisted as a file). Also force
1430          * classpath problems to be refresh if not running in autobuild mode. NOTE:
1431          * It can induce resource changes, and cannot be called during POST_CHANGE
1432          * notification.
1433          * 
1434          */
1435         // public void performPreBuildCheck(
1436         // IResourceDelta delta,
1437         // IJavaElement parent) {
1438         //      
1439         // IResource resource = delta.getResource();
1440         // IJavaElement element = null;
1441         // boolean processChildren = false;
1442         //      
1443         // switch (resource.getType()) {
1444         //      
1445         // case IResource.ROOT :
1446         // if (delta.getKind() == IResourceDelta.CHANGED) {
1447         // element = JavaCore.create(resource);
1448         // processChildren = true;
1449         // }
1450         // break;
1451         // case IResource.PROJECT :
1452         // int kind = delta.getKind();
1453         // switch (kind) {
1454         // case IResourceDelta.CHANGED:
1455         // // do not visit non-java projects (see bug 16140 Non-java project gets
1456         // .classpath)
1457         // IProject project = (IProject)resource;
1458         // if (JavaProject.hasJavaNature(project)) {
1459         // element = JavaCore.create(resource);
1460         // processChildren = true;
1461         // } else if
1462         // (JavaModelManager.getJavaModelManager().getJavaModel().findJavaProject(project)
1463         // != null) {
1464         // // project had the java nature
1465         // this.rootsAreStale = true;
1466         //
1467         // // remove classpath cache so that initializeRoots() will not consider the
1468         // project has a classpath
1469         // this.manager.removePerProjectInfo((JavaProject)JavaCore.create(project));
1470         // }
1471         // break;
1472         // case IResourceDelta.ADDED:
1473         // this.rootsAreStale = true;
1474         // break;
1475         // case IResourceDelta.REMOVED:
1476         // // remove classpath cache so that initializeRoots() will not consider the
1477         // project has a classpath
1478         // this.manager.removePerProjectInfo((JavaProject)JavaCore.create(resource));
1479         //                                              
1480         // this.rootsAreStale = true;
1481         // break;
1482         // }
1483         // break;
1484         // case IResource.FILE :
1485         // if (parent.getElementType() == IJavaElement.JAVA_PROJECT) {
1486         // IFile file = (IFile) resource;
1487         // JavaProject project = (JavaProject) parent;
1488         //      
1489         // /* check classpath file change */
1490         // if (file.getName().equals(JavaProject.CLASSPATH_FILENAME)) {
1491         // reconcileClasspathFileUpdate(delta, file, project);
1492         // this.rootsAreStale = true;
1493         // break;
1494         // }
1495         // // /* check custom preference file change */
1496         // // if (file.getName().equals(JavaProject.PREF_FILENAME)) {
1497         // // reconcilePreferenceFileUpdate(delta, file, project);
1498         // // break;
1499         // // }
1500         // }
1501         // break;
1502         // }
1503         // if (processChildren) {
1504         // IResourceDelta[] children = delta.getAffectedChildren();
1505         // for (int i = 0; i < children.length; i++) {
1506         // performPreBuildCheck(children[i], element);
1507         // }
1508         // }
1509         // }
1510         // private void popUntilPrefixOf(IPath path) {
1511         // while (this.currentElement != null) {
1512         // IPath currentElementPath = null;
1513         // if (this.currentElement instanceof IPackageFragmentRoot) {
1514         // currentElementPath =
1515         // ((IPackageFragmentRoot)this.currentElement).getPath();
1516         // } else {
1517         // IResource currentElementResource = this.currentElement.getResource();
1518         // if (currentElementResource != null) {
1519         // currentElementPath = currentElementResource.getFullPath();
1520         // }
1521         // }
1522         // if (currentElementPath != null) {
1523         // if (this.currentElement instanceof IPackageFragment
1524         // && this.currentElement.getElementName().length() == 0
1525         // && currentElementPath.segmentCount() != path.segmentCount()-1) {
1526         // // default package and path is not a direct child
1527         // this.currentElement = (Openable)this.currentElement.getParent();
1528         // }
1529         // if (currentElementPath.isPrefixOf(path)) {
1530         // return;
1531         // }
1532         // }
1533         // this.currentElement = (Openable)this.currentElement.getParent();
1534         // }
1535         // }
1536         /**
1537          * Converts a <code>IResourceDelta</code> rooted in a
1538          * <code>Workspace</code> into the corresponding set of
1539          * <code>IJavaElementDelta</code>, rooted in the relevant
1540          * <code>JavaModel</code>s.
1541          */
1542
1543         /**
1544          * Update the JavaModel according to a .classpath file change. The file can
1545          * have changed as a result of a previous call to
1546          * JavaProject#setRawClasspath or as a result of some user update (through
1547          * repository)
1548          */
1549         // void reconcileClasspathFileUpdate(IResourceDelta delta, IFile file,
1550         // JavaProject project) {
1551         //                      
1552         // switch (delta.getKind()) {
1553         // case IResourceDelta.REMOVED : // recreate one based on in-memory
1554         // classpath
1555         // try {
1556         // JavaModelManager.PerProjectInfo info =
1557         // JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(project.getProject());
1558         // if (info.classpath != null) { // if there is an in-memory classpath
1559         // project.saveClasspath(info.classpath, info.outputLocation);
1560         // }
1561         // } catch (JavaModelException e) {
1562         // if (project.getProject().isAccessible()) {
1563         // ProjectPrefUtil.log(e, "Could not save classpath for "+
1564         // project.getPath()); //$NON-NLS-1$
1565         // }
1566         // }
1567         // break;
1568         // case IResourceDelta.CHANGED :
1569         // if ((delta.getFlags() & IResourceDelta.CONTENT) == 0 // only consider
1570         // content change
1571         // && (delta.getFlags() & IResourceDelta.MOVED_FROM) == 0) // and also move
1572         // and overide scenario (see
1573         // http://dev.eclipse.org/bugs/show_bug.cgi?id=21420)
1574         // break;
1575         // case IResourceDelta.ADDED :
1576         // // check if any actual difference
1577         // project.flushClasspathProblemMarkers(false, true);
1578         // boolean wasSuccessful = false; // flag recording if .classpath file
1579         // change got reflected
1580         // try {
1581         // // force to (re)read the property file
1582         // IClasspathEntry[] fileEntries = project.readClasspathFile(true/*create
1583         // markers*/, false/*don't log problems*/);
1584         // if (fileEntries == null)
1585         // break; // could not read, ignore
1586         // JavaModelManager.PerProjectInfo info =
1587         // JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(project.getProject());
1588         // if (info.classpath != null) { // if there is an in-memory classpath
1589         // if (project.isClasspathEqualsTo(info.classpath, info.outputLocation,
1590         // fileEntries)) {
1591         // wasSuccessful = true;
1592         // break;
1593         // }
1594         // }
1595         //              
1596         // // will force an update of the classpath/output location based on the
1597         // file information
1598         // // extract out the output location
1599         // IPath outputLocation = null;
1600         // if (fileEntries != null && fileEntries.length > 0) {
1601         // IClasspathEntry entry = fileEntries[fileEntries.length - 1];
1602         // if (entry.getContentKind() == ClasspathEntry.K_OUTPUT) {
1603         // outputLocation = entry.getPath();
1604         // IClasspathEntry[] copy = new IClasspathEntry[fileEntries.length - 1];
1605         // System.arraycopy(fileEntries, 0, copy, 0, copy.length);
1606         // fileEntries = copy;
1607         // }
1608         // }
1609         // // restore output location
1610         // if (outputLocation == null) {
1611         // outputLocation = SetClasspathOperation.ReuseOutputLocation;
1612         // // clean mode will also default to reusing current one
1613         // }
1614         // project.setRawClasspath(
1615         // fileEntries,
1616         // outputLocation,
1617         // null, // monitor
1618         // true, // canChangeResource
1619         // project.getResolvedClasspath(true), // ignoreUnresolvedVariable
1620         // true, // needValidation
1621         // false); // no need to save
1622         //                                      
1623         // // if reach that far, the classpath file change got absorbed
1624         // wasSuccessful = true;
1625         // } catch (RuntimeException e) {
1626         // // setRawClasspath might fire a delta, and a listener may throw an
1627         // exception
1628         // if (project.getProject().isAccessible()) {
1629         // ProjectPrefUtil.log(e, "Could not set classpath for "+
1630         // project.getPath()); //$NON-NLS-1$
1631         // }
1632         // break;
1633         // } catch (JavaModelException e) { // CP failed validation
1634         // if (project.getProject().isAccessible()) {
1635         // if (e.getJavaModelStatus().getException() instanceof CoreException) {
1636         // // happens if the .classpath could not be written to disk
1637         // project.createClasspathProblemMarker(new JavaModelStatus(
1638         // IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT,
1639         // ProjectPrefUtil.bind("classpath.couldNotWriteClasspathFile",
1640         // project.getElementName(), e.getMessage()))); //$NON-NLS-1$
1641         // } else {
1642         // project.createClasspathProblemMarker(new JavaModelStatus(
1643         // IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT,
1644         // ProjectPrefUtil.bind("classpath.invalidClasspathInClasspathFile",
1645         // project.getElementName(), e.getMessage()))); //$NON-NLS-1$
1646         // }
1647         // }
1648         // break;
1649         // } finally {
1650         // if (!wasSuccessful) {
1651         // try {
1652         // project.setRawClasspath0(JavaProject.INVALID_CLASSPATH);
1653         // project.updatePackageFragmentRoots();
1654         // } catch (JavaModelException e) {
1655         // }
1656         // }
1657         // }
1658         // }
1659         // }
1660         /**
1661          * Update the JavaModel according to a .jprefs file change. The file can
1662          * have changed as a result of a previous call to JavaProject#setOptions or
1663          * as a result of some user update (through repository) Unused until
1664          * preference file get shared (.jpref)
1665          */
1666         // void reconcilePreferenceFileUpdate(IResourceDelta delta, IFile file,
1667         // JavaProject project) {
1668         //                      
1669         // switch (delta.getKind()) {
1670         // case IResourceDelta.REMOVED : // flush project custom settings
1671         // project.setOptions(null);
1672         // return;
1673         // case IResourceDelta.CHANGED :
1674         // if ((delta.getFlags() & IResourceDelta.CONTENT) == 0 // only consider
1675         // content change
1676         // && (delta.getFlags() & IResourceDelta.MOVED_FROM) == 0) // and also move
1677         // and overide scenario
1678         // break;
1679         // identityCheck : { // check if any actual difference
1680         // // force to (re)read the property file
1681         // Preferences filePreferences = project.loadPreferences();
1682         // if (filePreferences == null){
1683         // project.setOptions(null); // should have got removed delta.
1684         // return;
1685         // }
1686         // Preferences projectPreferences = project.getPreferences();
1687         // if (projectPreferences == null) return; // not a Java project
1688         //                                              
1689         // // compare preferences set to their default
1690         // String[] defaultProjectPropertyNames =
1691         // projectPreferences.defaultPropertyNames();
1692         // String[] defaultFilePropertyNames =
1693         // filePreferences.defaultPropertyNames();
1694         // if (defaultProjectPropertyNames.length ==
1695         // defaultFilePropertyNames.length) {
1696         // for (int i = 0; i < defaultProjectPropertyNames.length; i++){
1697         // String propertyName = defaultProjectPropertyNames[i];
1698         // if
1699         // (!projectPreferences.getString(propertyName).trim().equals(filePreferences.getString(propertyName).trim())){
1700         // break identityCheck;
1701         // }
1702         // }
1703         // } else break identityCheck;
1704         //
1705         // // compare custom preferences not set to their default
1706         // String[] projectPropertyNames = projectPreferences.propertyNames();
1707         // String[] filePropertyNames = filePreferences.propertyNames();
1708         // if (projectPropertyNames.length == filePropertyNames.length) {
1709         // for (int i = 0; i < projectPropertyNames.length; i++){
1710         // String propertyName = projectPropertyNames[i];
1711         // if
1712         // (!projectPreferences.getString(propertyName).trim().equals(filePreferences.getString(propertyName).trim())){
1713         // break identityCheck;
1714         // }
1715         // }
1716         // } else break identityCheck;
1717         //                                      
1718         // // identical - do nothing
1719         // return;
1720         // }
1721         // case IResourceDelta.ADDED :
1722         // // not identical, create delta and reset cached preferences
1723         // project.setPreferences(null);
1724         // // create delta
1725         // //fCurrentDelta.changed(project, IJavaElementDelta.F_OPTIONS_CHANGED);
1726         // }
1727         // }
1728         /*
1729          * Registers the given delta with this delta processor.
1730          */
1731         public void registerJavaModelDelta(IJavaElementDelta delta) {
1732                 this.javaModelDeltas.add(delta);
1733         }
1734
1735         /**
1736          * Removes the given element from its parents cache of children. If the
1737          * element does not have a parent, or the parent is not currently open, this
1738          * has no effect.
1739          */
1740         protected void removeFromParentInfo(Openable child) {
1741
1742                 Openable parent = (Openable) child.getParent();
1743                 if (parent != null && parent.isOpen()) {
1744                         try {
1745                                 JavaElementInfo info = (JavaElementInfo) parent
1746                                                 .getElementInfo();
1747                                 info.removeChild(child);
1748                         } catch (JavaModelException e) {
1749                                 // do nothing - we already checked if open
1750                         }
1751                 }
1752         }
1753
1754         /**
1755          * Notification that some resource changes have happened on the platform,
1756          * and that the Java Model should update any required internal structures
1757          * such that its elements remain consistent. Translates
1758          * <code>IResourceDeltas</code> into <code>IJavaElementDeltas</code>.
1759          * 
1760          * @see IResourceDelta
1761          * @see IResource
1762          */
1763         public void resourceChanged(IResourceChangeEvent event) {
1764                 if (event.getSource() instanceof IWorkspace) {
1765                         int eventType = this.overridenEventType == -1 ? event.getType()
1766                                         : this.overridenEventType;
1767                         IResource resource = event.getResource();
1768                         IResourceDelta delta = event.getDelta();
1769
1770                         switch (eventType) {
1771                         case IResourceChangeEvent.PRE_DELETE:
1772                                 try {
1773                                         if (resource.getType() == IResource.PROJECT
1774                                                         && ((IProject) resource)
1775                                                                         .hasNature(PHPeclipsePlugin.PHP_NATURE_ID)) {
1776
1777                                                 deleting((IProject) resource);
1778                                         }
1779                                 } catch (CoreException e) {
1780                                         // project doesn't exist or is not open: ignore
1781                                 }
1782                                 return;
1783
1784                         case IResourceChangeEvent.POST_CHANGE:
1785                                 if (isAffectedBy(delta)) { // avoid populating for SYNC or
1786                                                                                         // MARKER deltas
1787                                         try {
1788                                                 try {
1789                                                         stopDeltas();
1790                                                         // checkProjectsBeingAddedOrRemoved(delta);
1791                                                         // if (this.refreshedElements != null) {
1792                                                         // createExternalArchiveDelta(null);
1793                                                         // }
1794                                                         IJavaElementDelta translatedDelta = processResourceDelta(delta);
1795                                                         if (translatedDelta != null) {
1796                                                                 registerJavaModelDelta(translatedDelta);
1797                                                         }
1798                                                 } finally {
1799                                                         startDeltas();
1800                                                 }
1801                                                 // notifyTypeHierarchies(this.state.elementChangedListeners,
1802                                                 // this.state.elementChangedListenerCount);
1803                                                 fire(null, ElementChangedEvent.POST_CHANGE);
1804                                         } finally {
1805                                                 // workaround for bug 15168 circular errors not reported
1806                                                 this.state.modelProjectsCache = null;
1807                                                 this.removedRoots = null;
1808                                         }
1809                                 }
1810                                 return;
1811
1812                         case IResourceChangeEvent.PRE_BUILD:
1813                                 DeltaProcessingState.ProjectUpdateInfo[] updates = this.state
1814                                                 .removeAllProjectUpdates();
1815                                 if (updates != null) {
1816                                         for (int i = 0, length = updates.length; i < length; i++) {
1817                                                 try {
1818                                                         updates[i].updateProjectReferencesIfNecessary();
1819                                                 } catch (JavaModelException e) {
1820                                                         // do nothing
1821                                                 }
1822                                         }
1823                                 }
1824                                 // this.processPostChange = false;
1825                                 if (isAffectedBy(delta)) { // avoid populating for SYNC or
1826                                                                                         // MARKER deltas
1827                                 // updateClasspathMarkers(delta);
1828                                         PHPBuilder.buildStarting();
1829                                 }
1830                                 // does not fire any deltas
1831                                 return;
1832
1833                         case IResourceChangeEvent.POST_BUILD:
1834                                 PHPBuilder.buildFinished();
1835                                 return;
1836                         }
1837                 }
1838                 // // jsurfer TODO compare 3.0 sources
1839                 // if (event.getSource() instanceof IWorkspace) {
1840                 // int eventType = this.overridenEventType == -1 ? event.getType() :
1841                 // this.overridenEventType;
1842                 // IResource resource = event.getResource();
1843                 // IResourceDelta delta = event.getDelta();
1844                 //                      
1845                 // switch(eventType){
1846                 // case IResourceChangeEvent.PRE_DELETE :
1847                 // try {
1848                 // if(resource.getType() == IResource.PROJECT
1849                 // && ((IProject) resource).hasNature(PHPeclipsePlugin.PHP_NATURE_ID)) {
1850                 // // TODO jsurfer temp-del
1851                 // // this.deleting((IProject)resource);
1852                 // }
1853                 // } catch(CoreException e){
1854                 // }
1855                 // return;
1856                 //                                      
1857                 // case IResourceChangeEvent.PRE_BUILD :
1858                 // // TODO jsurfer temp-del
1859                 // // if(isAffectedBy(delta)) { // avoid populating for SYNC or MARKER
1860                 // deltas
1861                 // // this.checkProjectsBeingAddedOrRemoved(delta);
1862                 // //
1863                 // // // update the classpath related markers
1864                 // // this.updateClasspathMarkers();
1865                 // //
1866                 // // // the following will close project if affected by the property
1867                 // file change
1868                 // // try {
1869                 // // // don't fire classpath change deltas right away, but batch them
1870                 // // this.manager.stopDeltas();
1871                 // // this.performPreBuildCheck(delta, null);
1872                 // // } finally {
1873                 // // this.manager.startDeltas();
1874                 // // }
1875                 // // }
1876                 // // only fire already computed deltas (resource ones will be processed
1877                 // in post change only)
1878                 // this.manager.fire(null, ElementChangedEvent.PRE_AUTO_BUILD);
1879                 // break;
1880                 //
1881                 // case IResourceChangeEvent.POST_BUILD :
1882                 // // TODO jsurfer temp-del
1883                 // // JavaBuilder.finishedBuilding(event);
1884                 // break;
1885                 //                                      
1886                 // case IResourceChangeEvent.POST_CHANGE :
1887                 // // TODO jsurfer temp-del
1888                 // // if (isAffectedBy(delta)) {
1889                 // // try {
1890                 // // if (this.refreshedElements != null) {
1891                 // // try {
1892                 // // createExternalArchiveDelta(null);
1893                 // // } catch (JavaModelException e) {
1894                 // // e.printStackTrace();
1895                 // // }
1896                 // // }
1897                 // // IJavaElementDelta translatedDelta =
1898                 // this.processResourceDelta(delta);
1899                 // // if (translatedDelta != null) {
1900                 // // this.manager.registerJavaModelDelta(translatedDelta);
1901                 // // }
1902                 // // this.manager.fire(null, ElementChangedEvent.POST_CHANGE);
1903                 // // } finally {
1904                 // // // workaround for bug 15168 circular errors not reported
1905                 // // this.manager.javaProjectsCache = null;
1906                 // // this.removedRoots = null;
1907                 // // }
1908                 // // }
1909                 // }
1910                 // }
1911         }
1912
1913         /*
1914          * Turns the firing mode to on. That is, deltas that are/have been
1915          * registered will be fired.
1916          */
1917         private void startDeltas() {
1918                 this.isFiring = true;
1919         }
1920
1921         /*
1922          * Turns the firing mode to off. That is, deltas that are/have been
1923          * registered will not be fired until deltas are started again.
1924          */
1925         private void stopDeltas() {
1926                 this.isFiring = false;
1927         }
1928
1929         /*
1930          * Note that the project is about to be deleted.
1931          */
1932         private void deleting(IProject project) {
1933
1934                 try {
1935                         // discard indexing jobs that belong to this project so that the
1936                         // project can be
1937                         // deleted without interferences from the index manager
1938                         // this.manager.indexManager.discardJobs(project.getName());
1939
1940                         JavaProject javaProject = (JavaProject) JavaCore.create(project);
1941
1942                         // remember roots of this project
1943                         if (this.removedRoots == null) {
1944                                 this.removedRoots = new HashMap();
1945                         }
1946                         if (javaProject.isOpen()) {
1947                                 this.removedRoots.put(javaProject, javaProject
1948                                                 .getPackageFragmentRoots());
1949                         } else {
1950                                 // compute roots without opening project
1951                                 // this.removedRoots.put(
1952                                 // javaProject,
1953                                 // javaProject.computePackageFragmentRoots(
1954                                 // javaProject.getResolvedClasspath(true/*ignoreUnresolvedEntry*/,
1955                                 // false/*don't generateMarkerOnError*/, false/*don't
1956                                 // returnResolutionInProgress*/),
1957                                 // false));
1958                         }
1959
1960                         javaProject.close();
1961
1962                         // workaround for bug 15168 circular errors not reported
1963                         if (this.state.modelProjectsCache == null) {
1964                                 this.state.modelProjectsCache = this.manager.getJavaModel()
1965                                                 .getJavaProjects();
1966                         }
1967                         this.removeFromParentInfo(javaProject);
1968
1969                 } catch (JavaModelException e) {
1970                         // java project doesn't exist: ignore
1971                 }
1972         }
1973
1974         /*
1975          * Converts a <code>IResourceDelta</code> rooted in a <code>Workspace</code>
1976          * into the corresponding set of <code>IJavaElementDelta</code>, rooted
1977          * in the relevant <code>JavaModel</code>s.
1978          */
1979         private IJavaElementDelta processResourceDelta(IResourceDelta changes) {
1980
1981                 try {
1982                         IJavaModel model = this.manager.getJavaModel();
1983                         if (!model.isOpen()) {
1984                                 // force opening of java model so that java element delta are
1985                                 // reported
1986                                 try {
1987                                         model.open(null);
1988                                 } catch (JavaModelException e) {
1989                                         if (VERBOSE) {
1990                                                 e.printStackTrace();
1991                                         }
1992                                         return null;
1993                                 }
1994                         }
1995                         this.state.initializeRoots();
1996                         this.currentElement = null;
1997
1998                         // get the workspace delta, and start processing there.
1999                         IResourceDelta[] deltas = changes.getAffectedChildren();
2000                         for (int i = 0; i < deltas.length; i++) {
2001                                 IResourceDelta delta = deltas[i];
2002                                 IResource res = delta.getResource();
2003
2004                                 // find out the element type
2005                                 // RootInfo rootInfo = null;
2006                                 int elementType;
2007                                 IProject proj = (IProject) res;
2008                                 boolean wasJavaProject = this.manager.getJavaModel()
2009                                                 .findJavaProject(proj) != null;
2010                                 boolean isJavaProject = JavaProject.hasJavaNature(proj);
2011                                 if (!wasJavaProject && !isJavaProject) {
2012                                         elementType = NON_JAVA_RESOURCE;
2013                                 } else {
2014                                         // rootInfo = this.enclosingRootInfo(res.getFullPath(),
2015                                         // delta.getKind());
2016                                         // if (rootInfo != null &&
2017                                         // rootInfo.isRootOfProject(res.getFullPath())) {
2018                                         // elementType = IJavaElement.PACKAGE_FRAGMENT_ROOT;
2019                                         // } else {
2020                                         elementType = IJavaElement.JAVA_PROJECT;
2021                                         // }
2022                                 }
2023
2024                                 // traverse delta
2025                                 // this.traverseDelta(delta, elementType, rootInfo, null);
2026
2027                                 if (elementType == NON_JAVA_RESOURCE
2028                                                 || (wasJavaProject != isJavaProject && (delta.getKind()) == IResourceDelta.CHANGED)) { // project
2029                                                                                                                                                                                                                                 // has
2030                                                                                                                                                                                                                                 // changed
2031                                                                                                                                                                                                                                 // nature
2032                                                                                                                                                                                                                                 // (description
2033                                                                                                                                                                                                                                 // or
2034                                                                                                                                                                                                                                 // open/closed)
2035                                         try {
2036                                                 // add child as non java resource
2037                                                 nonJavaResourcesChanged((JavaModel) model, delta);
2038                                         } catch (JavaModelException e) {
2039                                                 // java model could not be opened
2040                                         }
2041                                 }
2042
2043                         }
2044                         // refreshPackageFragmentRoots();
2045                         // resetProjectCaches();
2046
2047                         return this.currentDelta;
2048                 } finally {
2049                         this.currentDelta = null;
2050                         // this.rootsToRefresh.clear();
2051                         // this.projectCachesToReset.clear();
2052                 }
2053         }
2054
2055         /*
2056          * Converts an <code>IResourceDelta</code> and its children into the
2057          * corresponding <code>IJavaElementDelta</code>s.
2058          */
2059         // private void traverseDelta(
2060         // IResourceDelta delta,
2061         // int elementType,
2062         // RootInfo rootInfo,
2063         // OutputsInfo outputsInfo) {
2064         //                      
2065         // IResource res = delta.getResource();
2066         //      
2067         // // set stack of elements
2068         // if (this.currentElement == null && rootInfo != null) {
2069         // // this.currentElement = rootInfo.project;
2070         // }
2071         //              
2072         // // process current delta
2073         // boolean processChildren = true;
2074         // if (res instanceof IProject) {
2075         // processChildren =
2076         // this.updateCurrentDeltaAndIndex(
2077         // delta,
2078         // elementType == IJavaElement.PACKAGE_FRAGMENT_ROOT ?
2079         // IJavaElement.JAVA_PROJECT : // case of prj=src
2080         // elementType,
2081         // rootInfo);
2082         // } else if (rootInfo != null) {
2083         // processChildren = this.updateCurrentDeltaAndIndex(delta, elementType,
2084         // rootInfo);
2085         // } else {
2086         // // not yet inside a package fragment root
2087         // processChildren = true;
2088         // }
2089         //              
2090         // // get the project's output locations and traverse mode
2091         // if (outputsInfo == null) outputsInfo = this.outputsInfo(rootInfo, res);
2092         //      
2093         // // process children if needed
2094         // if (processChildren) {
2095         // IResourceDelta[] children = delta.getAffectedChildren();
2096         // boolean oneChildOnClasspath = false;
2097         // int length = children.length;
2098         // IResourceDelta[] orphanChildren = null;
2099         // Openable parent = null;
2100         // boolean isValidParent = true;
2101         // for (int i = 0; i < length; i++) {
2102         // IResourceDelta child = children[i];
2103         // IResource childRes = child.getResource();
2104         //      
2105         // // check source attachment change
2106         // this.checkSourceAttachmentChange(child, childRes);
2107         //                              
2108         // // find out whether the child is a package fragment root of the current
2109         // project
2110         // IPath childPath = childRes.getFullPath();
2111         // int childKind = child.getKind();
2112         // RootInfo childRootInfo = this.rootInfo(childPath, childKind);
2113         // if (childRootInfo != null && !childRootInfo.isRootOfProject(childPath)) {
2114         // // package fragment root of another project (dealt with later)
2115         // childRootInfo = null;
2116         // }
2117         //                              
2118         // // compute child type
2119         // int childType =
2120         // this.elementType(
2121         // childRes,
2122         // childKind,
2123         // elementType,
2124         // rootInfo == null ? childRootInfo : rootInfo
2125         // );
2126         //                                              
2127         // // is childRes in the output folder and is it filtered out ?
2128         // boolean isResFilteredFromOutput =
2129         // this.isResFilteredFromOutput(outputsInfo, childRes, childType);
2130         //
2131         // boolean isNestedRoot = rootInfo != null && childRootInfo != null;
2132         // if (!isResFilteredFromOutput
2133         // && !isNestedRoot) { // do not treat as non-java rsc if nested root
2134         //
2135         // this.traverseDelta(child, childType, rootInfo == null ? childRootInfo :
2136         // rootInfo, outputsInfo); // traverse delta for child in the same project
2137         //
2138         // if (childType == NON_JAVA_RESOURCE) {
2139         // if (rootInfo != null) { // if inside a package fragment root
2140         // if (!isValidParent) continue;
2141         // if (parent == null) {
2142         // // find the parent of the non-java resource to attach to
2143         // if (this.currentElement == null
2144         // || !rootInfo.project.equals(this.currentElement.getJavaProject())) { //
2145         // note if currentElement is the IJavaModel, getJavaProject() is null
2146         // // force the currentProject to be used
2147         // this.currentElement = rootInfo.project;
2148         // }
2149         // if (elementType == IJavaElement.JAVA_PROJECT
2150         // || (elementType == IJavaElement.PACKAGE_FRAGMENT_ROOT
2151         // && res instanceof IProject)) {
2152         // // NB: attach non-java resource to project (not to its package fragment
2153         // root)
2154         // parent = rootInfo.project;
2155         // } else {
2156         // parent = this.createElement(res, elementType, rootInfo);
2157         // }
2158         // if (parent == null) {
2159         // isValidParent = false;
2160         // continue;
2161         // }
2162         // }
2163         // // add child as non java resource
2164         // try {
2165         // nonJavaResourcesChanged(parent, child);
2166         // } catch (JavaModelException e) {
2167         // // ignore
2168         // }
2169         // } else {
2170         // // the non-java resource (or its parent folder) will be attached to the
2171         // java project
2172         // if (orphanChildren == null) orphanChildren = new IResourceDelta[length];
2173         // orphanChildren[i] = child;
2174         // }
2175         // } else {
2176         // oneChildOnClasspath = true;
2177         // }
2178         // } else {
2179         // oneChildOnClasspath = true; // to avoid reporting child delta as non-java
2180         // resource delta
2181         // }
2182         //                                                              
2183         // // if child is a nested root
2184         // // or if it is not a package fragment root of the current project
2185         // // but it is a package fragment root of another project, traverse delta
2186         // too
2187         // if (isNestedRoot
2188         // || (childRootInfo == null && (childRootInfo = this.rootInfo(childPath,
2189         // childKind)) != null)) {
2190         // this.traverseDelta(child, IJavaElement.PACKAGE_FRAGMENT_ROOT,
2191         // childRootInfo, null); // binary output of childRootInfo.project cannot be
2192         // this root
2193         // }
2194         //      
2195         // // if the child is a package fragment root of one or several other
2196         // projects
2197         // ArrayList rootList;
2198         // if ((rootList = this.otherRootsInfo(childPath, childKind)) != null) {
2199         // Iterator iterator = rootList.iterator();
2200         // while (iterator.hasNext()) {
2201         // childRootInfo = (RootInfo) iterator.next();
2202         // this.traverseDelta(child, IJavaElement.PACKAGE_FRAGMENT_ROOT,
2203         // childRootInfo, null); // binary output of childRootInfo.project cannot be
2204         // this root
2205         // }
2206         // }
2207         // }
2208         // if (orphanChildren != null
2209         // && (oneChildOnClasspath // orphan children are siblings of a package
2210         // fragment root
2211         // || res instanceof IProject)) { // non-java resource directly under a
2212         // project
2213         //                                              
2214         // // attach orphan children
2215         // IProject rscProject = res.getProject();
2216         // JavaProject adoptiveProject = (JavaProject)JavaCore.create(rscProject);
2217         // if (adoptiveProject != null
2218         // && JavaProject.hasJavaNature(rscProject)) { // delta iff Java project
2219         // (18698)
2220         // for (int i = 0; i < length; i++) {
2221         // if (orphanChildren[i] != null) {
2222         // try {
2223         // nonJavaResourcesChanged(adoptiveProject, orphanChildren[i]);
2224         // } catch (JavaModelException e) {
2225         // // ignore
2226         // }
2227         // }
2228         // }
2229         // }
2230         // } // else resource delta will be added by parent
2231         // } // else resource delta will be added by parent
2232         // }
2233         private void notifyListeners(IJavaElementDelta deltaToNotify,
2234                         int eventType, IElementChangedListener[] listeners,
2235                         int[] listenerMask, int listenerCount) {
2236                 final ElementChangedEvent extraEvent = new ElementChangedEvent(
2237                                 deltaToNotify, eventType);
2238                 for (int i = 0; i < listenerCount; i++) {
2239                         if ((listenerMask[i] & eventType) != 0) {
2240                                 final IElementChangedListener listener = listeners[i];
2241                                 long start = -1;
2242                                 if (VERBOSE) {
2243                                         System.out
2244                                                         .print("Listener #" + (i + 1) + "=" + listener.toString());//$NON-NLS-1$//$NON-NLS-2$
2245                                         start = System.currentTimeMillis();
2246                                 }
2247                                 // wrap callbacks with Safe runnable for subsequent listeners to
2248                                 // be called when some are causing grief
2249                                 SafeRunner.run(new ISafeRunnable() {
2250                                         public void handleException(Throwable exception) {
2251                                                 Util
2252                                                                 .log(exception,
2253                                                                                 "Exception occurred in listener of Java element change notification"); //$NON-NLS-1$
2254                                         }
2255
2256                                         public void run() throws Exception {
2257                                                 listener.elementChanged(extraEvent);
2258                                         }
2259                                 });
2260                                 if (VERBOSE) {
2261                                         System.out
2262                                                         .println(" -> " + (System.currentTimeMillis() - start) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
2263                                 }
2264                         }
2265                 }
2266         }
2267
2268         // private void notifyTypeHierarchies(IElementChangedListener[] listeners,
2269         // int listenerCount) {
2270         // for (int i= 0; i < listenerCount; i++) {
2271         // final IElementChangedListener listener = listeners[i];
2272         // if (!(listener instanceof TypeHierarchy)) continue;
2273         //
2274         // // wrap callbacks with Safe runnable for subsequent listeners to be
2275         // called when some are causing grief
2276         // Platform.run(new ISafeRunnable() {
2277         // public void handleException(Throwable exception) {
2278         // ProjectPrefUtil.log(exception, "Exception occurred in listener of Java
2279         // element change notification"); //$NON-NLS-1$
2280         // }
2281         // public void run() throws Exception {
2282         // TypeHierarchy typeHierarchy = (TypeHierarchy)listener;
2283         // if (typeHierarchy.hasFineGrainChanges()) {
2284         // // case of changes in primary working copies
2285         // typeHierarchy.needsRefresh = true;
2286         // typeHierarchy.fireChange();
2287         // }
2288         // }
2289         // });
2290         // }
2291         // }
2292         /*
2293          * Generic processing for elements with changed contents:<ul> <li>The
2294          * element is closed such that any subsequent accesses will re-open the
2295          * element reflecting its new structure. <li>An entry is made in the delta
2296          * reporting a content change (K_CHANGE with F_CONTENT flag set). </ul>
2297          */
2298         private void nonJavaResourcesChanged(Openable element, IResourceDelta delta)
2299                         throws JavaModelException {
2300
2301                 // reset non-java resources if element was open
2302                 if (element.isOpen()) {
2303                         JavaElementInfo info = (JavaElementInfo) element.getElementInfo();
2304                         switch (element.getElementType()) {
2305                         case IJavaElement.JAVA_MODEL:
2306                                 ((JavaModelInfo) info).nonJavaResources = null;
2307                                 currentDelta().addResourceDelta(delta);
2308                                 return;
2309                         case IJavaElement.JAVA_PROJECT:
2310                                 ((JavaProjectElementInfo) info).setNonJavaResources(null);
2311
2312                                 // if a package fragment root is the project, clear it too
2313                                 JavaProject project = (JavaProject) element;
2314                                 PackageFragmentRoot projectRoot = (PackageFragmentRoot) project
2315                                                 .getPackageFragmentRoot(project.getProject());
2316                                 if (projectRoot.isOpen()) {
2317                                         ((PackageFragmentRootInfo) projectRoot.getElementInfo())
2318                                                         .setNonJavaResources(null);
2319                                 }
2320                                 break;
2321                         case IJavaElement.PACKAGE_FRAGMENT:
2322                                 ((PackageFragmentInfo) info).setNonJavaResources(null);
2323                                 break;
2324                         case IJavaElement.PACKAGE_FRAGMENT_ROOT:
2325                                 ((PackageFragmentRootInfo) info).setNonJavaResources(null);
2326                         }
2327                 }
2328
2329                 JavaElementDelta current = currentDelta();
2330                 JavaElementDelta elementDelta = current.find(element);
2331                 if (elementDelta == null) {
2332                         // don't use find after creating the delta as it can be null (see
2333                         // https://bugs.eclipse.org/bugs/show_bug.cgi?id=63434)
2334                         elementDelta = current
2335                                         .changed(element, IJavaElementDelta.F_CONTENT);
2336                 }
2337                 elementDelta.addResourceDelta(delta);
2338         }
2339
2340         /*
2341          * Flushes all deltas without firing them.
2342          */
2343         public void flush() {
2344                 this.javaModelDeltas = new ArrayList();
2345         }
2346
2347         /*
2348          * Finds the root info this path is included in. Returns null if not found.
2349          */
2350         RootInfo enclosingRootInfo(IPath path, int kind) {
2351                 while (path != null && path.segmentCount() > 0) {
2352                         RootInfo rootInfo = this.rootInfo(path, kind);
2353                         if (rootInfo != null)
2354                                 return rootInfo;
2355                         path = path.removeLastSegments(1);
2356                 }
2357                 return null;
2358         }
2359
2360         /*
2361          * Fire Java Model delta, flushing them after the fact after post_change
2362          * notification. If the firing mode has been turned off, this has no effect.
2363          */
2364         public void fire(IJavaElementDelta customDelta, int eventType) {
2365                 if (!this.isFiring)
2366                         return;
2367
2368                 if (DEBUG) {
2369                         System.out
2370                                         .println("-----------------------------------------------------------------------------------------------------------------------");//$NON-NLS-1$
2371                 }
2372
2373                 IJavaElementDelta deltaToNotify;
2374                 if (customDelta == null) {
2375                         deltaToNotify = this.mergeDeltas(this.javaModelDeltas);
2376                 } else {
2377                         deltaToNotify = customDelta;
2378                 }
2379
2380                 // Refresh internal scopes
2381                 // if (deltaToNotify != null) {
2382                 // Iterator scopes = this.manager.searchScopes.keySet().iterator();
2383                 // while (scopes.hasNext()) {
2384                 // AbstractSearchScope scope = (AbstractSearchScope)scopes.next();
2385                 // scope.processDelta(deltaToNotify);
2386                 // }
2387                 // }
2388
2389                 // Notification
2390
2391                 // Important: if any listener reacts to notification by updating the
2392                 // listeners list or mask, these lists will
2393                 // be duplicated, so it is necessary to remember original lists in a
2394                 // variable (since field values may change under us)
2395                 IElementChangedListener[] listeners = this.state.elementChangedListeners;
2396                 int[] listenerMask = this.state.elementChangedListenerMasks;
2397                 int listenerCount = this.state.elementChangedListenerCount;
2398
2399                 switch (eventType) {
2400                 case DEFAULT_CHANGE_EVENT:
2401                         firePostChangeDelta(deltaToNotify, listeners, listenerMask,
2402                                         listenerCount);
2403                         fireReconcileDelta(listeners, listenerMask, listenerCount);
2404                         break;
2405                 case ElementChangedEvent.POST_CHANGE:
2406                         firePostChangeDelta(deltaToNotify, listeners, listenerMask,
2407                                         listenerCount);
2408                         fireReconcileDelta(listeners, listenerMask, listenerCount);
2409                         break;
2410                 }
2411         }
2412
2413         private void firePostChangeDelta(IJavaElementDelta deltaToNotify,
2414                         IElementChangedListener[] listeners, int[] listenerMask,
2415                         int listenerCount) {
2416
2417                 // post change deltas
2418                 if (DEBUG) {
2419                         System.out
2420                                         .println("FIRING POST_CHANGE Delta [" + Thread.currentThread() + "]:"); //$NON-NLS-1$//$NON-NLS-2$
2421                         System.out
2422                                         .println(deltaToNotify == null ? "<NONE>" : deltaToNotify.toString()); //$NON-NLS-1$
2423                 }
2424                 if (deltaToNotify != null) {
2425                         // flush now so as to keep listener reactions to post their own
2426                         // deltas for subsequent iteration
2427                         this.flush();
2428
2429                         notifyListeners(deltaToNotify, ElementChangedEvent.POST_CHANGE,
2430                                         listeners, listenerMask, listenerCount);
2431                 }
2432         }
2433
2434         private void fireReconcileDelta(IElementChangedListener[] listeners,
2435                         int[] listenerMask, int listenerCount) {
2436
2437                 IJavaElementDelta deltaToNotify = mergeDeltas(this.reconcileDeltas
2438                                 .values());
2439                 if (DEBUG) {
2440                         System.out
2441                                         .println("FIRING POST_RECONCILE Delta [" + Thread.currentThread() + "]:"); //$NON-NLS-1$//$NON-NLS-2$
2442                         System.out
2443                                         .println(deltaToNotify == null ? "<NONE>" : deltaToNotify.toString()); //$NON-NLS-1$
2444                 }
2445                 if (deltaToNotify != null) {
2446                         // flush now so as to keep listener reactions to post their own
2447                         // deltas for subsequent iteration
2448                         this.reconcileDeltas = new HashMap();
2449
2450                         notifyListeners(deltaToNotify, ElementChangedEvent.POST_RECONCILE,
2451                                         listeners, listenerMask, listenerCount);
2452                 }
2453         }
2454
2455         /*
2456          * Returns the root info for the given path. Look in the old roots table if
2457          * kind is REMOVED.
2458          */
2459         RootInfo rootInfo(IPath path, int kind) {
2460                 if (kind == IResourceDelta.REMOVED) {
2461                         return (RootInfo) this.oldRoots.get(path);
2462                 } else {
2463                         return (RootInfo) this.roots.get(path);
2464                 }
2465         }
2466
2467         /*
2468          * Returns the other root infos for the given path. Look in the old other
2469          * roots table if kind is REMOVED.
2470          */
2471         ArrayList otherRootsInfo(IPath path, int kind) {
2472                 if (kind == IResourceDelta.REMOVED) {
2473                         return (ArrayList) this.oldOtherRoots.get(path);
2474                 } else {
2475                         return (ArrayList) this.otherRoots.get(path);
2476                 }
2477         }
2478
2479         /**
2480          * Converts an <code>IResourceDelta</code> and its children into the
2481          * corresponding <code>IJavaElementDelta</code>s. Return whether the
2482          * delta corresponds to a java element. If it is not a java element, it will
2483          * be added as a non-java resource by the sender of this method.
2484          */
2485         // protected boolean traverseDelta(
2486         // IResourceDelta delta,
2487         // int elementType,
2488         // RootInfo rootInfo,
2489         // OutputsInfo outputsInfo) {
2490         //                      
2491         // IResource res = delta.getResource();
2492         //      
2493         // // set stack of elements
2494         // if (this.currentElement == null && rootInfo != null) {
2495         // this.currentElement = (Openable)rootInfo.project;
2496         // }
2497         //              
2498         // // process current delta
2499         // boolean processChildren = true;
2500         // if (res instanceof IProject) {
2501         // processChildren =
2502         // this.updateCurrentDeltaAndIndex(
2503         // delta,
2504         // elementType == IJavaElement.PACKAGE_FRAGMENT_ROOT ?
2505         // IJavaElement.JAVA_PROJECT : // case of prj=src
2506         // elementType,
2507         // rootInfo);
2508         // } else if (rootInfo != null) {
2509         // processChildren = this.updateCurrentDeltaAndIndex(delta, elementType,
2510         // rootInfo);
2511         // } else {
2512         // // not yet inside a package fragment root
2513         // processChildren = true;
2514         // }
2515         //              
2516         // // get the project's output locations and traverse mode
2517         // if (outputsInfo == null) outputsInfo = this.outputsInfo(rootInfo, res);
2518         //      
2519         // // process children if needed
2520         // if (processChildren) {
2521         // IResourceDelta[] children = delta.getAffectedChildren();
2522         // boolean oneChildOnClasspath = false;
2523         // int length = children.length;
2524         // IResourceDelta[] orphanChildren = null;
2525         // Openable parent = null;
2526         // boolean isValidParent = true;
2527         // for (int i = 0; i < length; i++) {
2528         // IResourceDelta child = children[i];
2529         // IResource childRes = child.getResource();
2530         //      
2531         // // check source attachment change
2532         // this.checkSourceAttachmentChange(child, childRes);
2533         //                              
2534         // // find out whether the child is a package fragment root of the current
2535         // project
2536         // IPath childPath = childRes.getFullPath();
2537         // int childKind = child.getKind();
2538         // RootInfo childRootInfo = this.rootInfo(childPath, childKind);
2539         // if (childRootInfo != null && !childRootInfo.isRootOfProject(childPath)) {
2540         // // package fragment root of another project (dealt with later)
2541         // childRootInfo = null;
2542         // }
2543         //                              
2544         // // compute child type
2545         // int childType =
2546         // this.elementType(
2547         // childRes,
2548         // childKind,
2549         // elementType,
2550         // rootInfo == null ? childRootInfo : rootInfo
2551         // );
2552         //                                              
2553         // // is childRes in the output folder and is it filtered out ?
2554         // boolean isResFilteredFromOutput =
2555         // this.isResFilteredFromOutput(outputsInfo, childRes, childType);
2556         //
2557         // boolean isNestedRoot = rootInfo != null && childRootInfo != null;
2558         // if (!isResFilteredFromOutput
2559         // && !isNestedRoot) { // do not treat as non-java rsc if nested root
2560         // if (!this.traverseDelta(child, childType, rootInfo == null ?
2561         // childRootInfo : rootInfo, outputsInfo)) { // traverse delta for child in
2562         // the same project
2563         // // it is a non-java resource
2564         // try {
2565         // if (rootInfo != null) { // if inside a package fragment root
2566         // if (!isValidParent) continue;
2567         // if (parent == null) {
2568         // // find the parent of the non-java resource to attach to
2569         // if (this.currentElement == null
2570         // || !this.currentElement.getJavaProject().equals(rootInfo.project)) {
2571         // // force the currentProject to be used
2572         // this.currentElement = (Openable)rootInfo.project;
2573         // }
2574         // if (elementType == IJavaElement.JAVA_PROJECT
2575         // || (elementType == IJavaElement.PACKAGE_FRAGMENT_ROOT
2576         // && res instanceof IProject)) {
2577         // // NB: attach non-java resource to project (not to its package fragment
2578         // root)
2579         // parent = (Openable)rootInfo.project;
2580         // } else {
2581         // parent = this.createElement(res, elementType, rootInfo);
2582         // }
2583         // if (parent == null) {
2584         // isValidParent = false;
2585         // continue;
2586         // }
2587         // }
2588         // // add child as non java resource
2589         // nonJavaResourcesChanged(parent, child);
2590         // } else {
2591         // // the non-java resource (or its parent folder) will be attached to the
2592         // java project
2593         // if (orphanChildren == null) orphanChildren = new IResourceDelta[length];
2594         // orphanChildren[i] = child;
2595         // }
2596         // } catch (JavaModelException e) {
2597         // }
2598         // } else {
2599         // oneChildOnClasspath = true;
2600         // }
2601         // } else {
2602         // oneChildOnClasspath = true; // to avoid reporting child delta as non-java
2603         // resource delta
2604         // }
2605         //                                                              
2606         // // if child is a nested root
2607         // // or if it is not a package fragment root of the current project
2608         // // but it is a package fragment root of another project, traverse delta
2609         // too
2610         // if (isNestedRoot
2611         // || (childRootInfo == null && (childRootInfo = this.rootInfo(childPath,
2612         // childKind)) != null)) {
2613         // this.traverseDelta(child, IJavaElement.PACKAGE_FRAGMENT_ROOT,
2614         // childRootInfo, null); // binary output of childRootInfo.project cannot be
2615         // this root
2616         // // NB: No need to check the return value as the child can only be on the
2617         // classpath
2618         // }
2619         //      
2620         // // if the child is a package fragment root of one or several other
2621         // projects
2622         // ArrayList rootList;
2623         // if ((rootList = this.otherRootsInfo(childPath, childKind)) != null) {
2624         // Iterator iterator = rootList.iterator();
2625         // while (iterator.hasNext()) {
2626         // childRootInfo = (RootInfo) iterator.next();
2627         // this.traverseDelta(child, IJavaElement.PACKAGE_FRAGMENT_ROOT,
2628         // childRootInfo, null); // binary output of childRootInfo.project cannot be
2629         // this root
2630         // }
2631         // }
2632         // }
2633         // if (orphanChildren != null
2634         // && (oneChildOnClasspath // orphan children are siblings of a package
2635         // fragment root
2636         // || res instanceof IProject)) { // non-java resource directly under a
2637         // project
2638         //                                              
2639         // // attach orphan children
2640         // IProject rscProject = res.getProject();
2641         // JavaProject adoptiveProject = (JavaProject)JavaCore.create(rscProject);
2642         // if (adoptiveProject != null
2643         // && JavaProject.hasJavaNature(rscProject)) { // delta iff Java project
2644         // (18698)
2645         // for (int i = 0; i < length; i++) {
2646         // if (orphanChildren[i] != null) {
2647         // try {
2648         // nonJavaResourcesChanged(adoptiveProject, orphanChildren[i]);
2649         // } catch (JavaModelException e) {
2650         // }
2651         // }
2652         // }
2653         // }
2654         // } // else resource delta will be added by parent
2655         // return elementType != NON_JAVA_RESOURCE; // TODO: (jerome) do we still
2656         // need to return? (check could be done by caller)
2657         // } else {
2658         // return elementType != NON_JAVA_RESOURCE;
2659         // }
2660         // }
2661         /**
2662          * Update the classpath markers and cycle markers for the projects to
2663          * update.
2664          */
2665         // void updateClasspathMarkers() {
2666         // try {
2667         // if (!ResourcesPlugin.getWorkspace().isAutoBuilding()) {
2668         // Iterator iterator = this.projectsToUpdate.iterator();
2669         // while (iterator.hasNext()) {
2670         // try {
2671         // JavaProject project = (JavaProject)iterator.next();
2672         //                                              
2673         // // force classpath marker refresh
2674         // project.getResolvedClasspath(
2675         // true, // ignoreUnresolvedEntry
2676         // true); // generateMarkerOnError
2677         //                                              
2678         // } catch (JavaModelException e) {
2679         // }
2680         // }
2681         // }
2682         // if (!this.projectsToUpdate.isEmpty()){
2683         // try {
2684         // // update all cycle markers
2685         // JavaProject.updateAllCycleMarkers();
2686         // } catch (JavaModelException e) {
2687         // }
2688         // }
2689         // } finally {
2690         // this.projectsToUpdate = new HashSet();
2691         // }
2692         // }
2693         /*
2694          * Update the current delta (ie. add/remove/change the given element) and
2695          * update the correponding index. Returns whether the children of the given
2696          * delta must be processed. @throws a JavaModelException if the delta
2697          * doesn't correspond to a java element of the given type.
2698          */
2699         // private boolean updateCurrentDeltaAndIndex(IResourceDelta delta, int
2700         // elementType, RootInfo rootInfo) {
2701         // Openable element;
2702         // switch (delta.getKind()) {
2703         // case IResourceDelta.ADDED :
2704         // IResource deltaRes = delta.getResource();
2705         // element = this.createElement(deltaRes, elementType, rootInfo);
2706         // if (element == null) {
2707         // // resource might be containing shared roots (see bug 19058)
2708         // this.updateRoots(deltaRes.getFullPath(), delta);
2709         // return false;
2710         // }
2711         // this.updateIndex(element, delta);
2712         // this.elementAdded(element, delta, rootInfo);
2713         // return false;
2714         // case IResourceDelta.REMOVED :
2715         // deltaRes = delta.getResource();
2716         // element = this.createElement(deltaRes, elementType, rootInfo);
2717         // if (element == null) {
2718         // // resource might be containing shared roots (see bug 19058)
2719         // this.updateRoots(deltaRes.getFullPath(), delta);
2720         // return false;
2721         // }
2722         // this.updateIndex(element, delta);
2723         // this.elementRemoved(element, delta, rootInfo);
2724         //      
2725         // if (deltaRes.getType() == IResource.PROJECT){
2726         // // reset the corresponding project built state, since cannot reuse if
2727         // added back
2728         // this.manager.setLastBuiltState((IProject)deltaRes, null /*no state*/);
2729         // }
2730         // return false;
2731         // case IResourceDelta.CHANGED :
2732         // int flags = delta.getFlags();
2733         // if ((flags & IResourceDelta.CONTENT) != 0) {
2734         // // content has changed
2735         // element = this.createElement(delta.getResource(), elementType, rootInfo);
2736         // if (element == null) return false;
2737         // this.updateIndex(element, delta);
2738         // this.contentChanged(element, delta);
2739         // } else if (elementType == IJavaElement.JAVA_PROJECT) {
2740         // if ((flags & IResourceDelta.OPEN) != 0) {
2741         // // project has been opened or closed
2742         // IProject res = (IProject)delta.getResource();
2743         // element = this.createElement(res, elementType, rootInfo);
2744         // if (element == null) {
2745         // // resource might be containing shared roots (see bug 19058)
2746         // this.updateRoots(res.getFullPath(), delta);
2747         // return false;
2748         // }
2749         // if (res.isOpen()) {
2750         // if (JavaProject.hasJavaNature(res)) {
2751         // this.elementAdded(element, delta, rootInfo);
2752         // this.indexManager.indexAll(res);
2753         // }
2754         // } else {
2755         // JavaModel javaModel = this.manager.getJavaModel();
2756         // boolean wasJavaProject = javaModel.findJavaProject(res) != null;
2757         // if (wasJavaProject) {
2758         // this.elementRemoved(element, delta, rootInfo);
2759         // this.indexManager.discardJobs(element.getElementName());
2760         // this.indexManager.removeIndexFamily(res.getFullPath());
2761         //                                                              
2762         // }
2763         // }
2764         // return false; // when a project is open/closed don't process children
2765         // }
2766         // if ((flags & IResourceDelta.DESCRIPTION) != 0) {
2767         // IProject res = (IProject)delta.getResource();
2768         // JavaModel javaModel = this.manager.getJavaModel();
2769         // boolean wasJavaProject = javaModel.findJavaProject(res) != null;
2770         // boolean isJavaProject = JavaProject.hasJavaNature(res);
2771         // if (wasJavaProject != isJavaProject) {
2772         // // project's nature has been added or removed
2773         // element = this.createElement(res, elementType, rootInfo);
2774         // if (element == null) return false; // note its resources are still
2775         // visible as roots to other projects
2776         // if (isJavaProject) {
2777         // this.elementAdded(element, delta, rootInfo);
2778         // this.indexManager.indexAll(res);
2779         // } else {
2780         // this.elementRemoved(element, delta, rootInfo);
2781         // this.indexManager.discardJobs(element.getElementName());
2782         // this.indexManager.removeIndexFamily(res.getFullPath());
2783         // // reset the corresponding project built state, since cannot reuse if
2784         // added back
2785         // this.manager.setLastBuiltState(res, null /*no state*/);
2786         // }
2787         // return false; // when a project's nature is added/removed don't process
2788         // children
2789         // }
2790         // }
2791         // }
2792         // return true;
2793         // }
2794         // return true;
2795         // }
2796         /**
2797          * Traverse the set of projects which have changed namespace, and refresh
2798          * their dependents
2799          */
2800         // public void updateDependentNamelookups() {
2801         // Iterator iterator;
2802         // // update namelookup of dependent projects
2803         // iterator = this.projectsForDependentNamelookupRefresh.iterator();
2804         // HashSet affectedDependents = new HashSet();
2805         // while (iterator.hasNext()) {
2806         // JavaProject project = (JavaProject)iterator.next();
2807         // addDependentProjects(project.getPath(), affectedDependents);
2808         // }
2809         // iterator = affectedDependents.iterator();
2810         // while (iterator.hasNext()) {
2811         // JavaProject project = (JavaProject) iterator.next();
2812         // if (project.isOpen()){
2813         // try {
2814         // ((JavaProjectElementInfo)project.getElementInfo()).setNameLookup(null);
2815         // } catch (JavaModelException e) {
2816         // }
2817         // }
2818         // }
2819         // }
2820         // protected void updateIndex(Openable element, IResourceDelta delta) {
2821         //
2822         // if (indexManager == null)
2823         // return;
2824         //
2825         // switch (element.getElementType()) {
2826         // case IJavaElement.JAVA_PROJECT :
2827         // switch (delta.getKind()) {
2828         // case IResourceDelta.ADDED :
2829         // this.indexManager.indexAll(element.getJavaProject().getProject());
2830         // break;
2831         // case IResourceDelta.REMOVED :
2832         // this.indexManager.removeIndexFamily(element.getJavaProject().getProject().getFullPath());
2833         // // NB: Discarding index jobs belonging to this project was done during
2834         // PRE_DELETE
2835         // break;
2836         // // NB: Update of index if project is opened, closed, or its java nature
2837         // is added or removed
2838         // // is done in updateCurrentDeltaAndIndex
2839         // }
2840         // break;
2841         // case IJavaElement.PACKAGE_FRAGMENT_ROOT :
2842         // if (element instanceof JarPackageFragmentRoot) {
2843         // JarPackageFragmentRoot root = (JarPackageFragmentRoot)element;
2844         // // index jar file only once (if the root is in its declaring project)
2845         // IPath jarPath = root.getPath();
2846         // switch (delta.getKind()) {
2847         // case IResourceDelta.ADDED:
2848         // // index the new jar
2849         // indexManager.indexLibrary(jarPath, root.getJavaProject().getProject());
2850         // break;
2851         // case IResourceDelta.CHANGED:
2852         // // first remove the index so that it is forced to be re-indexed
2853         // indexManager.removeIndex(jarPath);
2854         // // then index the jar
2855         // indexManager.indexLibrary(jarPath, root.getJavaProject().getProject());
2856         // break;
2857         // case IResourceDelta.REMOVED:
2858         // // the jar was physically removed: remove the index
2859         // this.indexManager.discardJobs(jarPath.toString());
2860         // this.indexManager.removeIndex(jarPath);
2861         // break;
2862         // }
2863         // break;
2864         // } else {
2865         // int kind = delta.getKind();
2866         // if (kind == IResourceDelta.ADDED || kind == IResourceDelta.REMOVED) {
2867         // IPackageFragmentRoot root = (IPackageFragmentRoot)element;
2868         // this.updateRootIndex(root, root.getPackageFragment(""), delta);
2869         // //$NON-NLS-1$
2870         // break;
2871         // }
2872         // }
2873         // // don't break as packages of the package fragment root can be indexed
2874         // below
2875         // case IJavaElement.PACKAGE_FRAGMENT :
2876         // switch (delta.getKind()) {
2877         // case IResourceDelta.ADDED:
2878         // case IResourceDelta.REMOVED:
2879         // IPackageFragment pkg = null;
2880         // if (element instanceof IPackageFragmentRoot) {
2881         // IPackageFragmentRoot root = (IPackageFragmentRoot)element;
2882         // pkg = root.getPackageFragment(""); //$NON-NLS-1$
2883         // } else {
2884         // pkg = (IPackageFragment)element;
2885         // }
2886         // IResourceDelta[] children = delta.getAffectedChildren();
2887         // for (int i = 0, length = children.length; i < length; i++) {
2888         // IResourceDelta child = children[i];
2889         // IResource resource = child.getResource();
2890         // if (resource instanceof IFile) {
2891         // String name = resource.getName();
2892         // if (ProjectPrefUtil.isJavaFileName(name)) {
2893         // Openable cu = (Openable)pkg.getCompilationUnit(name);
2894         // this.updateIndex(cu, child);
2895         // } else if (ProjectPrefUtil.isClassFileName(name)) {
2896         // Openable classFile = (Openable)pkg.getClassFile(name);
2897         // this.updateIndex(classFile, child);
2898         // }
2899         // }
2900         // }
2901         // break;
2902         // }
2903         // break;
2904         // case IJavaElement.CLASS_FILE :
2905         // IFile file = (IFile) delta.getResource();
2906         // IJavaProject project = element.getJavaProject();
2907         // IPath binaryFolderPath = element.getPackageFragmentRoot().getPath();
2908         // // if the class file is part of the binary output, it has been created by
2909         // // the java builder -> ignore
2910         // try {
2911         // if (binaryFolderPath.equals(project.getOutputLocation())) {
2912         // break;
2913         // }
2914         // } catch (JavaModelException e) {
2915         // }
2916         // switch (delta.getKind()) {
2917         // case IResourceDelta.CHANGED :
2918         // // no need to index if the content has not changed
2919         // if ((delta.getFlags() & IResourceDelta.CONTENT) == 0)
2920         // break;
2921         // case IResourceDelta.ADDED :
2922         // indexManager.addBinary(file, binaryFolderPath);
2923         // break;
2924         // case IResourceDelta.REMOVED :
2925         // indexManager.remove(file.getFullPath().toString(), binaryFolderPath);
2926         // break;
2927         // }
2928         // break;
2929         // case IJavaElement.COMPILATION_UNIT :
2930         // file = (IFile) delta.getResource();
2931         // switch (delta.getKind()) {
2932         // case IResourceDelta.CHANGED :
2933         // // no need to index if the content has not changed
2934         // if ((delta.getFlags() & IResourceDelta.CONTENT) == 0)
2935         // break;
2936         // case IResourceDelta.ADDED :
2937         // indexManager.addSource(file,
2938         // file.getProject().getProject().getFullPath());
2939         // break;
2940         // case IResourceDelta.REMOVED :
2941         // indexManager.remove(file.getFullPath().toString(),
2942         // file.getProject().getProject().getFullPath());
2943         // break;
2944         // }
2945         // }
2946         // }
2947         /**
2948          * Upadtes the index of the given root (assuming it's an addition or a
2949          * removal). This is done recusively, pkg being the current package.
2950          */
2951         // private void updateRootIndex(IPackageFragmentRoot root, IPackageFragment
2952         // pkg, IResourceDelta delta) {
2953         // this.updateIndex((Openable)pkg, delta);
2954         // IResourceDelta[] children = delta.getAffectedChildren();
2955         // String name = pkg.getElementName();
2956         // for (int i = 0, length = children.length; i < length; i++) {
2957         // IResourceDelta child = children[i];
2958         // IResource resource = child.getResource();
2959         // if (resource instanceof IFolder) {
2960         // String subpkgName =
2961         // name.length() == 0 ?
2962         // resource.getName() :
2963         // name + "." + resource.getName(); //$NON-NLS-1$
2964         // IPackageFragment subpkg = root.getPackageFragment(subpkgName);
2965         // this.updateRootIndex(root, subpkg, child);
2966         // }
2967         // }
2968         // }
2969         /*
2970          * Update the roots that are affected by the addition or the removal of the
2971          * given container resource.
2972          */
2973         // private void updateRoots(IPath containerPath, IResourceDelta
2974         // containerDelta) {
2975         // Map roots;
2976         // Map otherRoots;
2977         // if (containerDelta.getKind() == IResourceDelta.REMOVED) {
2978         // roots = this.oldRoots;
2979         // otherRoots = this.oldOtherRoots;
2980         // } else {
2981         // roots = this.roots;
2982         // otherRoots = this.otherRoots;
2983         // }
2984         // Iterator iterator = roots.keySet().iterator();
2985         // while (iterator.hasNext()) {
2986         // IPath path = (IPath)iterator.next();
2987         // if (containerPath.isPrefixOf(path) && !containerPath.equals(path)) {
2988         // IResourceDelta rootDelta =
2989         // containerDelta.findMember(path.removeFirstSegments(1));
2990         // if (rootDelta == null) continue;
2991         // RootInfo rootInfo = (RootInfo)roots.get(path);
2992         //
2993         // if (!rootInfo.project.getPath().isPrefixOf(path)) { // only consider
2994         // roots that are not included in the container
2995         // this.updateCurrentDeltaAndIndex(rootDelta,
2996         // IJavaElement.PACKAGE_FRAGMENT_ROOT, rootInfo);
2997         // }
2998         //                      
2999         // ArrayList rootList = (ArrayList)otherRoots.get(path);
3000         // if (rootList != null) {
3001         // Iterator otherProjects = rootList.iterator();
3002         // while (otherProjects.hasNext()) {
3003         // rootInfo = (RootInfo)otherProjects.next();
3004         // if (!rootInfo.project.getPath().isPrefixOf(path)) { // only consider
3005         // roots that are not included in the container
3006         // this.updateCurrentDeltaAndIndex(rootDelta,
3007         // IJavaElement.PACKAGE_FRAGMENT_ROOT, rootInfo);
3008         // }
3009         // }
3010         // }
3011         // }
3012         // }
3013         // }
3014 }