public static String[] evaluateEnclosingProject(ISelection selection, IEditorPart activeEditor) {
   // always use the editor if active
   if (activeEditor != null) {
     String name = evaluateEnclosingProject(activeEditor.getEditorInput());
     if (name != null) {
       return new String[] {name};
     }
   } else if (selection instanceof IStructuredSelection) {
     HashSet res = new HashSet();
     for (Iterator iter = ((IStructuredSelection) selection).iterator(); iter.hasNext(); ) {
       Object curr = iter.next();
       if (curr instanceof IWorkingSet) {
         IWorkingSet workingSet = (IWorkingSet) curr;
         if (workingSet.isAggregateWorkingSet() && workingSet.isEmpty()) {
           IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
           for (int i = 0; i < projects.length; i++) {
             IProject proj = projects[i];
             if (proj.isOpen()) {
               res.add(proj.getName());
             }
           }
         } else {
           IAdaptable[] elements = workingSet.getElements();
           for (int i = 0; i < elements.length; i++) {
             String name = evaluateEnclosingProject(elements[i]);
             if (name != null) {
               res.add(name);
             }
           }
         }
       } else if (curr instanceof IAdaptable) {
         String name = evaluateEnclosingProject((IAdaptable) curr);
         if (name != null) {
           res.add(name);
         }
       }
     }
     if (!res.isEmpty()) {
       return (String[]) res.toArray(new String[res.size()]);
     }
   }
   return new String[0];
 }
Пример #2
0
 private FileTextSearchScope getSelectedResourcesScope() {
   HashSet<Object> resources = new HashSet<Object>();
   ISelection sel = getContainer().getSelection();
   if (sel instanceof IStructuredSelection && !sel.isEmpty()) {
     Iterator<?> iter = ((IStructuredSelection) sel).iterator();
     while (iter.hasNext()) {
       Object curr = iter.next();
       if (curr instanceof IWorkingSet) {
         IWorkingSet workingSet = (IWorkingSet) curr;
         if (workingSet.isAggregateWorkingSet() && workingSet.isEmpty()) {
           return FileTextSearchScope.newWorkspaceScope(getExtensions(), fSearchDerived);
         }
         IAdaptable[] elements = workingSet.getElements();
         for (int i = 0; i < elements.length; i++) {
           IResource resource = (IResource) elements[i].getAdapter(IResource.class);
           if (resource != null && resource.isAccessible()) {
             resources.add(resource);
           }
         }
       } else if (curr instanceof LineElement) {
         IResource resource = ((LineElement) curr).getParent();
         if (resource != null && resource.isAccessible()) {
           resources.add(resource);
         }
       } else if (curr instanceof IAdaptable) {
         IResource resource = (IResource) ((IAdaptable) curr).getAdapter(IResource.class);
         if (resource != null && resource.isAccessible()) {
           resources.add(resource);
         }
       }
     }
   } else if (getContainer().getActiveEditorInput() != null) {
     resources.add(getContainer().getActiveEditorInput().getAdapter(IFile.class));
   }
   IResource[] arr = resources.toArray(new IResource[resources.size()]);
   return FileTextSearchScope.newSearchScope(arr, getExtensions(), fSearchDerived);
 }