KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > search > JavaSearchScopeFactory


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

11 package org.eclipse.jdt.internal.ui.search;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IAdaptable;
21 import org.eclipse.core.runtime.IPath;
22
23 import org.eclipse.core.resources.IFolder;
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.resources.IWorkspaceRoot;
27 import org.eclipse.core.resources.ResourcesPlugin;
28
29 import org.eclipse.swt.widgets.Shell;
30
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.jface.window.Window;
34
35 import org.eclipse.ui.IEditorInput;
36 import org.eclipse.ui.IWorkingSet;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog;
39
40 import org.eclipse.jdt.core.IClasspathContainer;
41 import org.eclipse.jdt.core.IClasspathEntry;
42 import org.eclipse.jdt.core.IJavaElement;
43 import org.eclipse.jdt.core.IJavaProject;
44 import org.eclipse.jdt.core.IPackageFragment;
45 import org.eclipse.jdt.core.IPackageFragmentRoot;
46 import org.eclipse.jdt.core.IType;
47 import org.eclipse.jdt.core.JavaCore;
48 import org.eclipse.jdt.core.JavaModelException;
49 import org.eclipse.jdt.core.search.IJavaSearchScope;
50 import org.eclipse.jdt.core.search.SearchEngine;
51
52 import org.eclipse.jdt.internal.corext.util.Messages;
53
54 import org.eclipse.jdt.ui.JavaUI;
55
56 import org.eclipse.jdt.internal.ui.JavaPlugin;
57 import org.eclipse.jdt.internal.ui.browsing.LogicalPackage;
58
59 public class JavaSearchScopeFactory {
60     
61     public static final int JRE= IJavaSearchScope.SYSTEM_LIBRARIES;
62     public static final int LIBS= IJavaSearchScope.APPLICATION_LIBRARIES;
63     public static final int PROJECTS= IJavaSearchScope.REFERENCED_PROJECTS;
64     public static final int SOURCES= IJavaSearchScope.SOURCES;
65     
66     public static final int ALL= JRE | LIBS | PROJECTS | SOURCES;
67     public static final int NO_PROJ= JRE | LIBS | SOURCES;
68     public static final int NO_JRE= LIBS | PROJECTS | SOURCES;
69     public static final int NO_JRE_NO_PROJ= LIBS | PROJECTS | SOURCES;
70
71     private static JavaSearchScopeFactory fgInstance;
72     private final IJavaSearchScope EMPTY_SCOPE= SearchEngine.createJavaSearchScope(new IJavaElement[] {});
73     
74     private JavaSearchScopeFactory() {
75     }
76
77     public static JavaSearchScopeFactory getInstance() {
78         if (fgInstance == null)
79             fgInstance= new JavaSearchScopeFactory();
80         return fgInstance;
81     }
82
83     public IWorkingSet[] queryWorkingSets() throws JavaModelException, InterruptedException JavaDoc {
84         Shell shell= JavaPlugin.getActiveWorkbenchShell();
85         if (shell == null)
86             return null;
87         IWorkingSetSelectionDialog dialog= PlatformUI.getWorkbench().getWorkingSetManager().createWorkingSetSelectionDialog(shell, true);
88         if (dialog.open() != Window.OK) {
89             throw new InterruptedException JavaDoc();
90         }
91             
92         IWorkingSet[] workingSets= dialog.getSelection();
93         if (workingSets.length > 0)
94             return workingSets;
95         return null; // 'no working set' selected
96
}
97
98     public IJavaSearchScope createJavaSearchScope(IWorkingSet[] workingSets, boolean includeJRE) {
99         return createJavaSearchScope(workingSets, includeJRE ? ALL : NO_JRE);
100     }
101     
102     public IJavaSearchScope createJavaSearchScope(IWorkingSet[] workingSets, int includeMask) {
103         if (workingSets == null || workingSets.length < 1)
104             return EMPTY_SCOPE;
105
106         Set JavaDoc javaElements= new HashSet JavaDoc(workingSets.length * 10);
107         for (int i= 0; i < workingSets.length; i++) {
108             IWorkingSet workingSet= workingSets[i];
109             if (workingSet.isEmpty() && workingSet.isAggregateWorkingSet()) {
110                 return createWorkspaceScope(includeMask);
111             }
112             addJavaElements(javaElements, workingSet);
113         }
114         return createJavaSearchScope(javaElements, includeMask);
115     }
116     
117     public IJavaSearchScope createJavaSearchScope(IWorkingSet workingSet, boolean includeJRE) {
118         return createJavaSearchScope(workingSet, includeJRE ? NO_PROJ : NO_JRE_NO_PROJ);
119     }
120     
121     public IJavaSearchScope createJavaSearchScope(IWorkingSet workingSet, int includeMask) {
122         Set JavaDoc javaElements= new HashSet JavaDoc(10);
123         if (workingSet.isEmpty() && workingSet.isAggregateWorkingSet()) {
124             return createWorkspaceScope(includeMask);
125         }
126         addJavaElements(javaElements, workingSet);
127         return createJavaSearchScope(javaElements, includeMask);
128     }
129     
130     public IJavaSearchScope createJavaSearchScope(IResource[] resources, boolean includeJRE) {
131         return createJavaSearchScope(resources, includeJRE ? NO_PROJ : NO_JRE_NO_PROJ);
132     }
133
134     public IJavaSearchScope createJavaSearchScope(IResource[] resources, int includeMask) {
135         if (resources == null)
136             return EMPTY_SCOPE;
137         Set JavaDoc javaElements= new HashSet JavaDoc(resources.length);
138         addJavaElements(javaElements, resources);
139         return createJavaSearchScope(javaElements, includeMask);
140     }
141         
142     public IJavaSearchScope createJavaSearchScope(ISelection selection, boolean includeJRE) {
143         return createJavaSearchScope(selection, includeJRE ? NO_PROJ : NO_JRE_NO_PROJ);
144     }
145     
146     public IJavaSearchScope createJavaSearchScope(ISelection selection, int includeMask) {
147         return createJavaSearchScope(getJavaElements(selection), includeMask);
148     }
149     
150     public IJavaSearchScope createJavaProjectSearchScope(String JavaDoc[] projectNames, boolean includeJRE) {
151         return createJavaProjectSearchScope(projectNames, includeJRE ? NO_PROJ : NO_JRE_NO_PROJ);
152     }
153     
154     public IJavaSearchScope createJavaProjectSearchScope(String JavaDoc[] projectNames, int includeMask) {
155         ArrayList JavaDoc res= new ArrayList JavaDoc();
156         IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
157         for (int i= 0; i < projectNames.length; i++) {
158             IJavaProject project= JavaCore.create(root.getProject(projectNames[i]));
159             if (project.exists()) {
160                 res.add(project);
161             }
162         }
163         return createJavaSearchScope(res, includeMask);
164     }
165     
166     public IJavaSearchScope createJavaProjectSearchScope(IJavaProject project, boolean includeJRE) {
167         return createJavaProjectSearchScope(project, includeJRE ? NO_PROJ : NO_JRE_NO_PROJ);
168     }
169
170     public IJavaSearchScope createJavaProjectSearchScope(IJavaProject project, int includeMask) {
171         return SearchEngine.createJavaSearchScope(new IJavaElement[] { project }, getSearchFlags(includeMask));
172     }
173     
174     public IJavaSearchScope createJavaProjectSearchScope(IEditorInput editorInput, boolean includeJRE) {
175         return createJavaProjectSearchScope(editorInput, includeJRE ? ALL : NO_JRE);
176     }
177     
178     public IJavaSearchScope createJavaProjectSearchScope(IEditorInput editorInput, int includeMask) {
179         IJavaElement elem= JavaUI.getEditorInputJavaElement(editorInput);
180         if (elem != null) {
181             IJavaProject project= elem.getJavaProject();
182             if (project != null) {
183                 return createJavaProjectSearchScope(project, includeMask);
184             }
185         }
186         return EMPTY_SCOPE;
187     }
188     
189     public String JavaDoc getWorkspaceScopeDescription(boolean includeJRE) {
190         return includeJRE ? SearchMessages.WorkspaceScope : SearchMessages.WorkspaceScopeNoJRE;
191     }
192     
193     public String JavaDoc getWorkspaceScopeDescription(int includeMask) {
194         return getWorkspaceScopeDescription((includeMask & JRE) != 0);
195     }
196     
197     public String JavaDoc getProjectScopeDescription(String JavaDoc[] projectNames, int includeMask) {
198         if (projectNames.length == 0) {
199             return SearchMessages.JavaSearchScopeFactory_undefined_projects;
200         }
201         boolean includeJRE= (includeMask & JRE) != 0;
202         String JavaDoc scopeDescription;
203         if (projectNames.length == 1) {
204             String JavaDoc label= includeJRE ? SearchMessages.EnclosingProjectScope : SearchMessages.EnclosingProjectScopeNoJRE;
205             scopeDescription= Messages.format(label, projectNames[0]);
206         } else if (projectNames.length == 2) {
207             String JavaDoc label= includeJRE ? SearchMessages.EnclosingProjectsScope2 : SearchMessages.EnclosingProjectsScope2NoJRE;
208             scopeDescription= Messages.format(label, new String JavaDoc[] { projectNames[0], projectNames[1]});
209         } else {
210             String JavaDoc label= includeJRE ? SearchMessages.EnclosingProjectsScope : SearchMessages.EnclosingProjectsScopeNoJRE;
211             scopeDescription= Messages.format(label, new String JavaDoc[] { projectNames[0], projectNames[1]});
212         }
213         return scopeDescription;
214     }
215     
216     public String JavaDoc getProjectScopeDescription(IJavaProject project, boolean includeJRE) {
217         if (includeJRE) {
218             return Messages.format(SearchMessages.ProjectScope, project.getElementName());
219         } else {
220             return Messages.format(SearchMessages.ProjectScopeNoJRE, project.getElementName());
221         }
222     }
223     
224     public String JavaDoc getProjectScopeDescription(IEditorInput editorInput, boolean includeJRE) {
225         IJavaElement elem= JavaUI.getEditorInputJavaElement(editorInput);
226         if (elem != null) {
227             IJavaProject project= elem.getJavaProject();
228             if (project != null) {
229                 return getProjectScopeDescription(project, includeJRE);
230             }
231         }
232         return Messages.format(SearchMessages.ProjectScope, ""); //$NON-NLS-1$
233
}
234     
235     public String JavaDoc getHierarchyScopeDescription(IType type) {
236         return Messages.format(SearchMessages.HierarchyScope, new String JavaDoc[] { type.getElementName() });
237     }
238
239     public String JavaDoc getSelectionScopeDescription(IJavaElement[] javaElements, int includeMask) {
240         return getSelectionScopeDescription(javaElements, (includeMask & JRE) != 0);
241     }
242     
243
244     public String JavaDoc getSelectionScopeDescription(IJavaElement[] javaElements, boolean includeJRE) {
245         if (javaElements.length == 0) {
246             return SearchMessages.JavaSearchScopeFactory_undefined_selection;
247         }
248         String JavaDoc scopeDescription;
249         if (javaElements.length == 1) {
250             String JavaDoc label= includeJRE ? SearchMessages.SingleSelectionScope : SearchMessages.SingleSelectionScopeNoJRE;
251             scopeDescription= Messages.format(label, javaElements[0].getElementName());
252         } else if (javaElements.length == 1) {
253             String JavaDoc label= includeJRE ? SearchMessages.DoubleSelectionScope : SearchMessages.DoubleSelectionScopeNoJRE;
254             scopeDescription= Messages.format(label, new String JavaDoc[] { javaElements[0].getElementName(), javaElements[1].getElementName()});
255         } else {
256             String JavaDoc label= includeJRE ? SearchMessages.SelectionScope : SearchMessages.SelectionScopeNoJRE;
257             scopeDescription= Messages.format(label, new String JavaDoc[] { javaElements[0].getElementName(), javaElements[1].getElementName()});
258         }
259         return scopeDescription;
260     }
261     
262     public String JavaDoc getWorkingSetScopeDescription(IWorkingSet[] workingSets, int includeMask) {
263         return getWorkingSetScopeDescription(workingSets, (includeMask & JRE) != 0);
264     }
265     
266     public String JavaDoc getWorkingSetScopeDescription(IWorkingSet[] workingSets, boolean includeJRE) {
267         if (workingSets.length == 0) {
268             return SearchMessages.JavaSearchScopeFactory_undefined_workingsets;
269         }
270         if (workingSets.length == 1) {
271             String JavaDoc label= includeJRE ? SearchMessages.SingleWorkingSetScope : SearchMessages.SingleWorkingSetScopeNoJRE;
272             return Messages.format(label, workingSets[0].getLabel());
273         }
274         Arrays.sort(workingSets, new WorkingSetComparator());
275         if (workingSets.length == 2) {
276             String JavaDoc label= includeJRE ? SearchMessages.DoubleWorkingSetScope : SearchMessages.DoubleWorkingSetScopeNoJRE;
277             return Messages.format(label, new String JavaDoc[] { workingSets[0].getLabel(), workingSets[1].getLabel()});
278         }
279         String JavaDoc label= includeJRE ? SearchMessages.WorkingSetsScope : SearchMessages.WorkingSetsScopeNoJRE;
280         return Messages.format(label, new String JavaDoc[] { workingSets[0].getLabel(), workingSets[1].getLabel()});
281     }
282     
283     public IProject[] getProjects(IJavaSearchScope scope) {
284         IPath[] paths= scope.enclosingProjectsAndJars();
285         HashSet JavaDoc temp= new HashSet JavaDoc();
286         for (int i= 0; i < paths.length; i++) {
287             IResource resource= ResourcesPlugin.getWorkspace().getRoot().findMember(paths[i]);
288             if (resource != null && resource.getType() == IResource.PROJECT)
289                 temp.add(resource);
290         }
291         return (IProject[]) temp.toArray(new IProject[temp.size()]);
292     }
293
294     public IJavaElement[] getJavaElements(ISelection selection) {
295         if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
296             return getJavaElements(((IStructuredSelection)selection).toArray());
297         } else {
298             return new IJavaElement[0];
299         }
300     }
301
302     private IJavaElement[] getJavaElements(Object JavaDoc[] elements) {
303         if (elements.length == 0)
304             return new IJavaElement[0];
305         
306         Set JavaDoc result= new HashSet JavaDoc(elements.length);
307         for (int i= 0; i < elements.length; i++) {
308             Object JavaDoc selectedElement= elements[i];
309             if (selectedElement instanceof IJavaElement) {
310                 addJavaElements(result, (IJavaElement) selectedElement);
311             } else if (selectedElement instanceof IResource) {
312                 addJavaElements(result, (IResource) selectedElement);
313             } else if (selectedElement instanceof LogicalPackage) {
314                 addJavaElements(result, (LogicalPackage) selectedElement);
315             } else if (selectedElement instanceof IWorkingSet) {
316                 IWorkingSet ws= (IWorkingSet)selectedElement;
317                 addJavaElements(result, ws);
318             } else if (selectedElement instanceof IAdaptable) {
319                 IResource resource= (IResource) ((IAdaptable) selectedElement).getAdapter(IResource.class);
320                 if (resource != null)
321                     addJavaElements(result, resource);
322             }
323             
324         }
325         return (IJavaElement[]) result.toArray(new IJavaElement[result.size()]);
326     }
327     
328     public IJavaSearchScope createJavaSearchScope(IJavaElement[] javaElements, boolean includeJRE) {
329         return createJavaSearchScope(javaElements, includeJRE ? NO_PROJ : NO_JRE_NO_PROJ);
330     }
331     
332     public IJavaSearchScope createJavaSearchScope(IJavaElement[] javaElements, int includeMask) {
333         if (javaElements.length == 0)
334             return EMPTY_SCOPE;
335         return SearchEngine.createJavaSearchScope(javaElements, getSearchFlags(includeMask));
336     }
337
338     private IJavaSearchScope createJavaSearchScope(Collection JavaDoc javaElements, int includeMask) {
339         if (javaElements.isEmpty())
340             return EMPTY_SCOPE;
341         IJavaElement[] elementArray= (IJavaElement[]) javaElements.toArray(new IJavaElement[javaElements.size()]);
342         return SearchEngine.createJavaSearchScope(elementArray, getSearchFlags(includeMask));
343     }
344     
345     private static int getSearchFlags(int includeMask) {
346         return includeMask;
347     }
348
349     private void addJavaElements(Set JavaDoc javaElements, IResource[] resources) {
350         for (int i= 0; i < resources.length; i++)
351             addJavaElements(javaElements, resources[i]);
352     }
353
354     private void addJavaElements(Set JavaDoc javaElements, IResource resource) {
355         IJavaElement javaElement= (IJavaElement)resource.getAdapter(IJavaElement.class);
356         if (javaElement == null)
357             // not a Java resource
358
return;
359         
360         if (javaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
361             // add other possible package fragments
362
try {
363                 addJavaElements(javaElements, ((IFolder)resource).members());
364             } catch (CoreException ex) {
365                 // don't add elements
366
}
367         }
368             
369         javaElements.add(javaElement);
370     }
371
372     private void addJavaElements(Set JavaDoc javaElements, IJavaElement javaElement) {
373         javaElements.add(javaElement);
374     }
375     
376     private void addJavaElements(Set JavaDoc javaElements, IWorkingSet workingSet) {
377         if (workingSet == null)
378             return;
379         
380         if (workingSet.isAggregateWorkingSet() && workingSet.isEmpty()) {
381             try {
382                 IJavaProject[] projects= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
383                 javaElements.addAll(Arrays.asList(projects));
384             } catch (JavaModelException e) {
385                 JavaPlugin.log(e);
386             }
387             return;
388         }
389         
390         IAdaptable[] elements= workingSet.getElements();
391         for (int i= 0; i < elements.length; i++) {
392             IJavaElement javaElement=(IJavaElement) elements[i].getAdapter(IJavaElement.class);
393             if (javaElement != null) {
394                 addJavaElements(javaElements, javaElement);
395                 continue;
396             }
397             IResource resource= (IResource)elements[i].getAdapter(IResource.class);
398             if (resource != null) {
399                 addJavaElements(javaElements, resource);
400             }
401             
402             // else we don't know what to do with it, ignore.
403
}
404     }
405
406     private void addJavaElements(Set JavaDoc javaElements, LogicalPackage selectedElement) {
407         IPackageFragment[] packages= selectedElement.getFragments();
408         for (int i= 0; i < packages.length; i++)
409             addJavaElements(javaElements, packages[i]);
410     }
411     
412     public IJavaSearchScope createWorkspaceScope(boolean includeJRE) {
413         return createWorkspaceScope(includeJRE ? ALL : NO_JRE);
414     }
415     
416     public IJavaSearchScope createWorkspaceScope(int includeMask) {
417         if ((includeMask & NO_PROJ) != NO_PROJ) {
418             try {
419                 IJavaProject[] projects= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
420                 return SearchEngine.createJavaSearchScope(projects, getSearchFlags(includeMask));
421             } catch (JavaModelException e) {
422                 // ignore, use workspace scope instead
423
}
424         }
425         return SearchEngine.createWorkspaceScope();
426     }
427
428     public boolean isInsideJRE(IJavaElement element) {
429         IPackageFragmentRoot root= (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
430         if (root != null) {
431             try {
432                 IClasspathEntry entry= root.getRawClasspathEntry();
433                 if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
434                     IClasspathContainer container= JavaCore.getClasspathContainer(entry.getPath(), root.getJavaProject());
435                     return container != null && container.getKind() == IClasspathContainer.K_DEFAULT_SYSTEM;
436                 }
437                 return false;
438             } catch (JavaModelException e) {
439                 JavaPlugin.log(e);
440             }
441         }
442         return true; // include JRE in doubt
443
}
444 }
445
Popular Tags