public void initFromSelection() {
   IJavaElement je = getSelectedJavaElement(selection);
   if (je instanceof IJavaProject) {
     IJavaProject jp = (IJavaProject) je;
     if (jp.isOpen()) {
       // default to the first source dir
       // we find in the selected project
       try {
         for (IPackageFragmentRoot pfr : jp.getAllPackageFragmentRoots()) {
           if (!pfr.isExternal() && !pfr.isArchive()) {
             je = pfr;
             break;
           }
         }
       } catch (JavaModelException e) {
       }
     }
   }
   if (je instanceof IPackageFragmentRoot) {
     sourceDir = (IPackageFragmentRoot) je;
     packageFragment = sourceDir.getPackageFragment("");
     packageName = packageFragment.getElementName();
   } else if (je instanceof IPackageFragment) {
     packageFragment = (IPackageFragment) je;
     packageName = packageFragment.getElementName();
     sourceDir = (IPackageFragmentRoot) packageFragment.getAncestor(PACKAGE_FRAGMENT_ROOT);
   }
 }
 /**
  * Get the source folder for the project
  *
  * @param project
  */
 private IPackageFragmentRoot[] getPackageFragmentRoots(IProject project) {
   IJavaProject p = JavaCore.create(project);
   List<IPackageFragmentRoot> result = new ArrayList<IPackageFragmentRoot>();
   try {
     IPackageFragmentRoot[] candidates = p.getAllPackageFragmentRoots();
     for (IPackageFragmentRoot candidate : candidates) {
       if (candidate.getKind() == IPackageFragmentRoot.K_SOURCE) {
         result.add(candidate);
       }
     }
   } catch (JavaModelException e) {
   }
   return result.toArray(new IPackageFragmentRoot[] {});
 }
Example #3
0
  public static List<ICompilationUnit> collectCompilationUnits(IJavaProject project)
      throws JavaModelException {

    List<ICompilationUnit> result = new ArrayList<ICompilationUnit>();

    IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
    for (int i = 0; i < roots.length; ++i) {
      IPackageFragmentRoot root = roots[i];
      if (IPackageFragmentRoot.K_SOURCE == root.getKind()) {
        collectCompilationUnits(result, root);
      }
    }

    return result;
  }
 /** Attempts to set the defaults values from the given {@link UriPathTemplateCategory} */
 private void setDefaultValues(final UriPathTemplateCategory category) {
   final IJavaProject javaProject = category.getJavaProject();
   try {
     final IPackageFragmentRoot[] packageFragmentRoots = javaProject.getAllPackageFragmentRoots();
     for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) {
       if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
         setPackageFragmentRoot(packageFragmentRoot, true);
         break;
       }
     }
   } catch (JavaModelException e) {
     Logger.error(
         "Failed to set the default values from project '" + javaProject.getElementName() + "'",
         e);
   }
 }
 private void addPackage() {
   Shell shell = fAddPackageButton.getDisplay().getActiveShell();
   ElementListSelectionDialog dialog = null;
   try {
     IJavaProject project = fEditor.getJavaProject();
     List<IJavaElement> projects = new ArrayList<IJavaElement>();
     projects.add(project);
     IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
     for (int i = 0; i < roots.length; i++) {
       IPackageFragmentRoot root = roots[i];
       projects.add(root.getParent());
     }
     dialog =
         JDIDebugUIPlugin.createAllPackagesDialog(
             shell, projects.toArray(new IJavaProject[projects.size()]), false);
   } catch (JavaModelException jme) {
     String title =
         SnippetMessages.getString("SelectImportsDialog.Add_package_as_import_7"); // $NON-NLS-1$
     String message =
         SnippetMessages.getString(
             "SelectImportsDialog.Could_not_open_package_selection_dialog_8"); //$NON-NLS-1$
     ExceptionHandler.handle(jme, title, message);
     return;
   }
   if (dialog == null) {
     return;
   }
   dialog.setTitle(
       SnippetMessages.getString("SelectImportsDialog.Add_package_as_import_7")); // $NON-NLS-1$
   dialog.setMessage(
       SnippetMessages.getString(
           "SelectImportsDialog.&Select_a_package_to_add_as_an_Import_10")); //$NON-NLS-1$
   dialog.setMultipleSelection(true);
   if (dialog.open() == IDialogConstants.CANCEL_ID) {
     return;
   }
   Object[] packages = dialog.getResult();
   if (packages != null) {
     for (int i = 0; i < packages.length; i++) {
       IJavaElement pkg = (IJavaElement) packages[i];
       String filter = pkg.getElementName();
       filter += ".*"; // $NON-NLS-1$
       fImportContentProvider.addImport(filter);
     }
   }
 }