KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > search > JavaWorkspaceScope


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

11 package org.eclipse.jdt.internal.core.search;
12
13 import java.util.HashSet JavaDoc;
14
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.core.IJavaElementDelta;
18 import org.eclipse.jdt.core.JavaModelException;
19 import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
20 import org.eclipse.jdt.internal.core.JavaModelManager;
21 import org.eclipse.jdt.internal.core.JavaProject;
22 import org.eclipse.jdt.core.IJavaProject;
23
24 /**
25  * A Java-specific scope for searching the entire workspace.
26  * The scope can be configured to not search binaries. By default, binaries
27  * are included.
28  */

29 public class JavaWorkspaceScope extends JavaSearchScope {
30
31 protected boolean needsInitialize;
32
33 public boolean encloses(IJavaElement element) {
34     /*
35     if (this.needsInitialize) {
36         this.initialize();
37     }
38     return super.encloses(element);
39     */

40     /*A workspace scope encloses all java elements (this assumes that the index selector
41      * and thus enclosingProjectAndJars() returns indexes on the classpath only and that these
42      * indexes are consistent.)
43      * NOTE: Returning true gains 20% of a hierarchy build on Object
44      */

45     return true;
46 }
47 public boolean encloses(String JavaDoc resourcePathString) {
48     /*
49     if (this.needsInitialize) {
50         this.initialize();
51     }
52     return super.encloses(resourcePathString);
53     */

54     /*A workspace scope encloses all resources (this assumes that the index selector
55      * and thus enclosingProjectAndJars() returns indexes on the classpath only and that these
56      * indexes are consistent.)
57      * NOTE: Returning true gains 20% of a hierarchy build on Object
58      */

59     return true;
60 }
61 public IPath[] enclosingProjectsAndJars() {
62     if (this.needsInitialize) {
63         this.initialize(5);
64     }
65     return super.enclosingProjectsAndJars();
66 }
67 public boolean equals(Object JavaDoc o) {
68   return o instanceof JavaWorkspaceScope;
69 }
70 public AccessRuleSet getAccessRuleSet(String JavaDoc relativePath, String JavaDoc containerPath) {
71     if (this.pathRestrictions == null)
72         return null;
73     return super.getAccessRuleSet(relativePath, containerPath);
74 }
75 public int hashCode() {
76     return JavaWorkspaceScope.class.hashCode();
77 }
78 public void initialize(int size) {
79     super.initialize(size);
80     try {
81         IJavaProject[] projects = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects();
82         for (int i = 0, length = projects.length; i < length; i++) {
83             int includeMask = SOURCES | APPLICATION_LIBRARIES | SYSTEM_LIBRARIES;
84             add((JavaProject) projects[i], null, includeMask, new HashSet JavaDoc(length*2, 1), null);
85         }
86     } catch (JavaModelException ignored) {
87         // ignore
88
}
89     this.needsInitialize = false;
90 }
91 public void processDelta(IJavaElementDelta delta) {
92     if (this.needsInitialize) return;
93     IJavaElement element = delta.getElement();
94     switch (element.getElementType()) {
95         case IJavaElement.JAVA_MODEL:
96             IJavaElementDelta[] children = delta.getAffectedChildren();
97             for (int i = 0, length = children.length; i < length; i++) {
98                 IJavaElementDelta child = children[i];
99                 this.processDelta(child);
100             }
101             break;
102         case IJavaElement.JAVA_PROJECT:
103             int kind = delta.getKind();
104             switch (kind) {
105                 case IJavaElementDelta.ADDED:
106                 case IJavaElementDelta.REMOVED:
107                     this.needsInitialize = true;
108                     break;
109                 case IJavaElementDelta.CHANGED:
110                     int flags = delta.getFlags();
111                     if ((flags & IJavaElementDelta.F_CLOSED) != 0
112                             || (flags & IJavaElementDelta.F_OPENED) != 0) {
113                         this.needsInitialize = true;
114                     } else {
115                         children = delta.getAffectedChildren();
116                         for (int i = 0, length = children.length; i < length; i++) {
117                             IJavaElementDelta child = children[i];
118                             this.processDelta(child);
119                         }
120                     }
121                     break;
122             }
123             break;
124         case IJavaElement.PACKAGE_FRAGMENT_ROOT:
125             kind = delta.getKind();
126             switch (kind) {
127                 case IJavaElementDelta.ADDED:
128                 case IJavaElementDelta.REMOVED:
129                     this.needsInitialize = true;
130                     break;
131                 case IJavaElementDelta.CHANGED:
132                     int flags = delta.getFlags();
133                     if ((flags & IJavaElementDelta.F_ADDED_TO_CLASSPATH) > 0
134                         || (flags & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) > 0) {
135                         this.needsInitialize = true;
136                     }
137                     break;
138             }
139             break;
140     }
141 }
142 public String JavaDoc toString() {
143     return "JavaWorkspaceScope"; //$NON-NLS-1$
144
}
145 }
146
Popular Tags