/*
   * (non-Javadoc)
   *
   * @see org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator#
   * isSelectedValid(java.lang.Object)
   */
  @Override
  public boolean isSelectedValid(Object element) {
    boolean isValid = false;
    try {
      if (element instanceof IJavaProject) {
        IJavaProject jproject = (IJavaProject) element;
        IPath path = jproject.getProject().getFullPath();
        isValid = (jproject.findPackageFragmentRoot(path) != null);
      } else if (element instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) element;

        boolean isSrc = (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE);
        boolean isGen =
            packageFragmentRoot.getElementName().equals(IAndroidConstants.GEN_SRC_FOLDER)
                && (packageFragmentRoot.getParent() instanceof IJavaProject);

        isValid = isSrc && !isGen;
      } else {
        isValid = true;
      }
    } catch (JavaModelException e) {
      AndmoreLogger.error(ElementTreeValidator.class, e.getLocalizedMessage(), e);
    }
    return isValid;
  }
 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);
     }
   }
 }
 /*
  * Create the list of focused jars or projects.
  */
 private static IJavaElement[] getFocusedElementsAndTypes(
     SearchPattern pattern, IJavaElement focusElement, ObjectVector superTypes)
     throws JavaModelException {
   if (pattern instanceof MethodPattern) {
     // For method pattern, it needs to walk along the focus type super hierarchy
     // and add jars/projects of all the encountered types.
     IType type = (IType) pattern.focus.getAncestor(IJavaElement.TYPE);
     MethodPattern methodPattern = (MethodPattern) pattern;
     String selector = new String(methodPattern.selector);
     int parameterCount = methodPattern.parameterCount;
     ITypeHierarchy superHierarchy = type.newSupertypeHierarchy(null);
     IType[] allTypes = superHierarchy.getAllSupertypes(type);
     int length = allTypes.length;
     SimpleSet focusSet = new SimpleSet(length + 1);
     if (focusElement != null) focusSet.add(focusElement);
     for (int i = 0; i < length; i++) {
       IMethod[] methods = allTypes[i].getMethods();
       int mLength = methods.length;
       for (int m = 0; m < mLength; m++) {
         if (parameterCount == methods[m].getNumberOfParameters()
             && methods[m].getElementName().equals(selector)) {
           IPackageFragmentRoot root =
               (IPackageFragmentRoot) allTypes[i].getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
           IJavaElement element = root.isArchive() ? root : root.getParent();
           focusSet.add(element);
           if (superTypes != null) superTypes.add(allTypes[i]);
           break;
         }
       }
     }
     // Rebuilt a contiguous array
     IJavaElement[] focuses = new IJavaElement[focusSet.elementSize];
     Object[] values = focusSet.values;
     int count = 0;
     for (int i = values.length; --i >= 0; ) {
       if (values[i] != null) {
         focuses[count++] = (IJavaElement) values[i];
       }
     }
     return focuses;
   }
   if (focusElement == null) return new IJavaElement[0];
   return new IJavaElement[] {focusElement};
 }