1 11 package org.eclipse.jdt.internal.ui.search; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Collection ; 16 import java.util.HashSet ; 17 import java.util.Set ; 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 { 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 (); 90 } 91 92 IWorkingSet[] workingSets= dialog.getSelection(); 93 if (workingSets.length > 0) 94 return workingSets; 95 return null; } 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 javaElements= new HashSet (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 javaElements= new HashSet (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 javaElements= new HashSet (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 [] projectNames, boolean includeJRE) { 151 return createJavaProjectSearchScope(projectNames, includeJRE ? NO_PROJ : NO_JRE_NO_PROJ); 152 } 153 154 public IJavaSearchScope createJavaProjectSearchScope(String [] projectNames, int includeMask) { 155 ArrayList res= new ArrayList (); 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 getWorkspaceScopeDescription(boolean includeJRE) { 190 return includeJRE ? SearchMessages.WorkspaceScope : SearchMessages.WorkspaceScopeNoJRE; 191 } 192 193 public String getWorkspaceScopeDescription(int includeMask) { 194 return getWorkspaceScopeDescription((includeMask & JRE) != 0); 195 } 196 197 public String getProjectScopeDescription(String [] projectNames, int includeMask) { 198 if (projectNames.length == 0) { 199 return SearchMessages.JavaSearchScopeFactory_undefined_projects; 200 } 201 boolean includeJRE= (includeMask & JRE) != 0; 202 String scopeDescription; 203 if (projectNames.length == 1) { 204 String label= includeJRE ? SearchMessages.EnclosingProjectScope : SearchMessages.EnclosingProjectScopeNoJRE; 205 scopeDescription= Messages.format(label, projectNames[0]); 206 } else if (projectNames.length == 2) { 207 String label= includeJRE ? SearchMessages.EnclosingProjectsScope2 : SearchMessages.EnclosingProjectsScope2NoJRE; 208 scopeDescription= Messages.format(label, new String [] { projectNames[0], projectNames[1]}); 209 } else { 210 String label= includeJRE ? SearchMessages.EnclosingProjectsScope : SearchMessages.EnclosingProjectsScopeNoJRE; 211 scopeDescription= Messages.format(label, new String [] { projectNames[0], projectNames[1]}); 212 } 213 return scopeDescription; 214 } 215 216 public String 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 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, ""); } 234 235 public String getHierarchyScopeDescription(IType type) { 236 return Messages.format(SearchMessages.HierarchyScope, new String [] { type.getElementName() }); 237 } 238 239 public String getSelectionScopeDescription(IJavaElement[] javaElements, int includeMask) { 240 return getSelectionScopeDescription(javaElements, (includeMask & JRE) != 0); 241 } 242 243 244 public String getSelectionScopeDescription(IJavaElement[] javaElements, boolean includeJRE) { 245 if (javaElements.length == 0) { 246 return SearchMessages.JavaSearchScopeFactory_undefined_selection; 247 } 248 String scopeDescription; 249 if (javaElements.length == 1) { 250 String label= includeJRE ? SearchMessages.SingleSelectionScope : SearchMessages.SingleSelectionScopeNoJRE; 251 scopeDescription= Messages.format(label, javaElements[0].getElementName()); 252 } else if (javaElements.length == 1) { 253 String label= includeJRE ? SearchMessages.DoubleSelectionScope : SearchMessages.DoubleSelectionScopeNoJRE; 254 scopeDescription= Messages.format(label, new String [] { javaElements[0].getElementName(), javaElements[1].getElementName()}); 255 } else { 256 String label= includeJRE ? SearchMessages.SelectionScope : SearchMessages.SelectionScopeNoJRE; 257 scopeDescription= Messages.format(label, new String [] { javaElements[0].getElementName(), javaElements[1].getElementName()}); 258 } 259 return scopeDescription; 260 } 261 262 public String getWorkingSetScopeDescription(IWorkingSet[] workingSets, int includeMask) { 263 return getWorkingSetScopeDescription(workingSets, (includeMask & JRE) != 0); 264 } 265 266 public String getWorkingSetScopeDescription(IWorkingSet[] workingSets, boolean includeJRE) { 267 if (workingSets.length == 0) { 268 return SearchMessages.JavaSearchScopeFactory_undefined_workingsets; 269 } 270 if (workingSets.length == 1) { 271 String 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 label= includeJRE ? SearchMessages.DoubleWorkingSetScope : SearchMessages.DoubleWorkingSetScopeNoJRE; 277 return Messages.format(label, new String [] { workingSets[0].getLabel(), workingSets[1].getLabel()}); 278 } 279 String label= includeJRE ? SearchMessages.WorkingSetsScope : SearchMessages.WorkingSetsScopeNoJRE; 280 return Messages.format(label, new String [] { workingSets[0].getLabel(), workingSets[1].getLabel()}); 281 } 282 283 public IProject[] getProjects(IJavaSearchScope scope) { 284 IPath[] paths= scope.enclosingProjectsAndJars(); 285 HashSet temp= new HashSet (); 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 [] elements) { 303 if (elements.length == 0) 304 return new IJavaElement[0]; 305 306 Set result= new HashSet (elements.length); 307 for (int i= 0; i < elements.length; i++) { 308 Object 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 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 javaElements, IResource[] resources) { 350 for (int i= 0; i < resources.length; i++) 351 addJavaElements(javaElements, resources[i]); 352 } 353 354 private void addJavaElements(Set javaElements, IResource resource) { 355 IJavaElement javaElement= (IJavaElement)resource.getAdapter(IJavaElement.class); 356 if (javaElement == null) 357 return; 359 360 if (javaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) { 361 try { 363 addJavaElements(javaElements, ((IFolder)resource).members()); 364 } catch (CoreException ex) { 365 } 367 } 368 369 javaElements.add(javaElement); 370 } 371 372 private void addJavaElements(Set javaElements, IJavaElement javaElement) { 373 javaElements.add(javaElement); 374 } 375 376 private void addJavaElements(Set 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 } 404 } 405 406 private void addJavaElements(Set 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 } 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; } 444 } 445 | Popular Tags |