/**
   * Creates and opens a dialog enabling the user to select one or more elements from the specified
   * input tree.
   *
   * @param shell The parent shell for the dialog.
   * @param input The input for the dialog's tree.
   * @param allowMultipleSelections Whether or not to allow multiple items to be selected. If false,
   *     then only one item may be selected from the tree, otherwise the tree's selection will be
   *     determined by checkboxes.
   * @return The result of the dialog, usually either {@link Window#OK} or {@link Window#CANCEL}.
   */
  public int openDialog(Shell shell, Object input, boolean allowMultipleSelections) {

    // Create the dialog if necessary.
    final SelectionDialog dialog;

    // Set up the content and label providers. These are required for
    // any TreeViewer.
    ITreeContentProvider contentProvider = createContentProvider();
    ILabelProvider labelProvider = createLabelProvider();

    // Create a dialog with or without checkboxes in the tree.
    if (allowMultipleSelections) {
      dialog = createCheckedTreeSelectionDialog(shell, labelProvider, contentProvider);
    } else {
      dialog = createElementTreeSelectionDialog(shell, labelProvider, contentProvider);
    }

    // Set its title and message.
    dialog.setTitle(title);
    dialog.setMessage(message);

    setInput(input);

    // Set the input and refresh the viewer.
    if (allowMultipleSelections) {
      CheckedTreeSelectionDialog treeDialog = (CheckedTreeSelectionDialog) dialog;
      treeDialog.setInput(root);
      if (!initialSelection.isEmpty()) {
        treeDialog.setInitialSelections(initialSelection.toArray());
        newSelection.addAll(initialSelection);
      }
    } else {
      ElementTreeSelectionDialog treeDialog = (ElementTreeSelectionDialog) dialog;
      treeDialog.setInput(root);
      if (!initialSelection.isEmpty()) {
        Node initialNode = initialSelection.iterator().next();
        treeDialog.setInitialSelection(initialNode);
        newSelection.add(initialNode);
      }
    }

    // Return the result of opening the dialog.
    int result = dialog.open();

    // If the dialog was closed or cancelled, reset the selection to the
    // initial one.
    if (result != Window.OK) {
      newSelection.clear();
      for (Node node : initialSelection) {
        newSelection.add(node);
      }
    }

    return result;
  }
Example #2
0
 /**
  * @param shell Shell for the window
  * @param superTypeName supertype to search for
  * @param project project to look in
  * @return IType the type created
  * @throws JavaModelException exception thrown
  */
 public IType selectType(Shell shell, String superTypeName, IProject project)
     throws JavaModelException {
   IJavaSearchScope searchScope = null;
   if (project == null) {
     ISelection selection =
         PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
     IStructuredSelection selectionToPass = StructuredSelection.EMPTY;
     if (selection instanceof IStructuredSelection) {
       selectionToPass = (IStructuredSelection) selection;
       if (selectionToPass.getFirstElement() instanceof IFile) {
         project = ((IFile) selectionToPass.getFirstElement()).getProject();
       }
     }
   }
   if (superTypeName != null && !superTypeName.equals("java.lang.Object")) { // $NON-NLS-1$
     if (project == null) {
       project = model.getProject();
     }
     IJavaProject javaProject = JavaCore.create(project);
     IType superType = javaProject.findType(superTypeName);
     if (superType != null) {
       searchScope =
           SearchEngine.createStrictHierarchyScope(javaProject, superType, true, false, null);
     }
   } else {
     searchScope = SearchEngine.createWorkspaceScope();
   }
   SelectionDialog dialog =
       JavaUI.createTypeDialog(
           shell,
           new ProgressMonitorDialog(shell),
           searchScope,
           IJavaElementSearchConstants.CONSIDER_CLASSES_AND_INTERFACES,
           false);
   dialog.setTitle("Select Class");
   dialog.setMessage("Matching items");
   if (dialog.open() == IDialogConstants.CANCEL_ID) {
     return null;
   }
   Object[] types = dialog.getResult();
   if (types == null || types.length == 0) {
     return null;
   }
   return (IType) types[0];
 }
  private void handleBrowsePackages(Shell dialogParrentShell) {
    try {
      IResource sourceContainer =
          ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(getSourceFolder()));
      IJavaProject javaProject = JDTUtil.getJavaProject(sourceContainer.getProject().getName());

      SelectionDialog dialog = JavaUI.createPackageDialog(dialogParrentShell, javaProject, 0);
      dialog.setTitle("Package selection");
      dialog.setMessage("&Choose a package:");

      if (dialog.open() == SelectionDialog.OK) {
        Object[] selectedPackages = dialog.getResult();
        if (selectedPackages.length == 1) {
          m_packageNameText.setText(((IPackageFragment) selectedPackages[0]).getElementName());
        }
      }
    } catch (JavaModelException e) {
      updateStatus("Failed to list packages.");
    }
  }
  private void addType() {
    Shell shell = fAddTypeButton.getDisplay().getActiveShell();
    SelectionDialog dialog = null;
    try {
      dialog =
          JavaUI.createTypeDialog(
              shell,
              PlatformUI.getWorkbench().getProgressService(),
              SearchEngine.createJavaSearchScope(
                  new IJavaElement[] {fEditor.getJavaProject()}, true),
              IJavaElementSearchConstants.CONSIDER_ALL_TYPES,
              false);
    } catch (JavaModelException jme) {
      String title =
          SnippetMessages.getString("SelectImportsDialog.Add_Type_as_Import_12"); // $NON-NLS-1$
      String message =
          SnippetMessages.getString(
              "SelectImportsDialog.Could_not_open_class_selection_dialog_13"); //$NON-NLS-1$
      ExceptionHandler.handle(jme, title, message);
      return;
    }

    dialog.setTitle(
        SnippetMessages.getString("SelectImportsDialog.Add_Type_as_Import_12")); // $NON-NLS-1$
    dialog.setMessage(
        SnippetMessages.getString(
            "SelectImportsDialog.&Select_a_type_to_add_to_add_as_an_import_15")); //$NON-NLS-1$
    if (dialog.open() == IDialogConstants.CANCEL_ID) {
      return;
    }

    Object[] types = dialog.getResult();
    if (types != null && types.length > 0) {
      IType type = (IType) types[0];
      fImportContentProvider.addImport(type.getFullyQualifiedName());
    }
  }
  /**
   * Opens a type selection dialog. If the user selects a type (and does not cancel), an editor is
   * opened on the selected type.
   *
   * @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event)
   */
  @Override
  public void runWithEvent(Event e) {
    Shell parent = DartToolsPlugin.getActiveWorkbenchShell();
    if (!doCreateProjectFirstOnEmptyWorkspace(parent)) {
      return;
    }

    SelectionDialog dialog =
        new OpenTypeSelectionDialog(
            parent,
            true,
            PlatformUI.getWorkbench().getProgressService(),
            null,
            SEARCH_ELEMENT_KINDS);
    dialog.setTitle(DartUIMessages.OpenTypeAction_dialogTitle);
    dialog.setMessage(DartUIMessages.OpenTypeAction_dialogMessage);

    int result = dialog.open();
    if (result != IDialogConstants.OK_ID) {
      return;
    }

    Object[] types = dialog.getResult();
    if (types == null || types.length == 0) {
      return;
    }

    if (types.length == 1) {
      try {
        DartUI.openInEditor((Element) types[0], true, true);
      } catch (CoreException x) {
        ExceptionHandler.handle(
            x,
            DartUIMessages.OpenTypeAction_errorTitle,
            DartUIMessages.OpenTypeAction_errorMessage);
      }
      return;
    }

    final IWorkbenchPage workbenchPage = DartToolsPlugin.getActivePage();
    if (workbenchPage == null) {
      IStatus status =
          new Status(
              IStatus.ERROR,
              DartToolsPlugin.getPluginId(),
              DartUIMessages.OpenTypeAction_no_active_WorkbenchPage);
      ExceptionHandler.handle(
          status,
          DartUIMessages.OpenTypeAction_errorTitle,
          DartUIMessages.OpenTypeAction_errorMessage);
      return;
    }

    MultiStatus multiStatus =
        new MultiStatus(
            DartToolsPlugin.getPluginId(),
            DartStatusConstants.INTERNAL_ERROR,
            DartUIMessages.OpenTypeAction_multiStatusMessage,
            null);

    for (int i = 0; i < types.length; i++) {
      Type type = (Type) types[i];
      try {
        DartUI.openInEditor(type, true, true);
      } catch (CoreException x) {
        multiStatus.merge(x.getStatus());
      }
    }

    if (!multiStatus.isOK()) {
      ExceptionHandler.handle(
          multiStatus,
          DartUIMessages.OpenTypeAction_errorTitle,
          DartUIMessages.OpenTypeAction_errorMessage);
    }
  }
  @Override
  public String browse(final Presentation context) {
    final Property property = property();

    final EnumSet<JavaTypeKind> kinds = EnumSet.noneOf(JavaTypeKind.class);

    if (this.paramKinds != null) {
      for (String kindString : this.paramKinds.split(",")) {
        kindString = kindString.trim();

        if (kindString.equalsIgnoreCase(JavaTypeKind.CLASS.name())) {
          kinds.add(JavaTypeKind.CLASS);
        } else if (kindString.equalsIgnoreCase(JavaTypeKind.ABSTRACT_CLASS.name())) {
          kinds.add(JavaTypeKind.ABSTRACT_CLASS);
        } else if (kindString.equalsIgnoreCase(JavaTypeKind.INTERFACE.name())) {
          kinds.add(JavaTypeKind.INTERFACE);
        } else if (kindString.equalsIgnoreCase(JavaTypeKind.ANNOTATION.name())) {
          kinds.add(JavaTypeKind.ANNOTATION);
        } else if (kindString.equalsIgnoreCase(JavaTypeKind.ENUM.name())) {
          kinds.add(JavaTypeKind.ENUM);
        } else {
          final String msg = typeKindNotRecognized.format(kindString);
          Sapphire.service(LoggingService.class).logError(msg);
        }
      }
    } else {
      final JavaTypeConstraintService javaTypeConstraintService =
          property.service(JavaTypeConstraintService.class);

      if (javaTypeConstraintService != null) {
        kinds.addAll(javaTypeConstraintService.kinds());
      }
    }

    int browseDialogStyle = IJavaElementSearchConstants.CONSIDER_ALL_TYPES;
    int count = kinds.size();

    if (count == 1) {
      final JavaTypeKind kind = kinds.iterator().next();

      switch (kind) {
        case CLASS:
          browseDialogStyle = IJavaElementSearchConstants.CONSIDER_CLASSES;
          break;
        case ABSTRACT_CLASS:
          browseDialogStyle = IJavaElementSearchConstants.CONSIDER_CLASSES;
          break;
        case INTERFACE:
          browseDialogStyle = IJavaElementSearchConstants.CONSIDER_INTERFACES;
          break;
        case ANNOTATION:
          browseDialogStyle = IJavaElementSearchConstants.CONSIDER_ANNOTATION_TYPES;
          break;
        case ENUM:
          browseDialogStyle = IJavaElementSearchConstants.CONSIDER_ENUMS;
          break;
        default:
          throw new IllegalStateException();
      }
    } else if (count == 2) {
      if (kinds.contains(JavaTypeKind.CLASS) || kinds.contains(JavaTypeKind.ABSTRACT_CLASS)) {
        if (kinds.contains(JavaTypeKind.INTERFACE)) {
          browseDialogStyle = IJavaElementSearchConstants.CONSIDER_CLASSES_AND_INTERFACES;
        } else if (kinds.contains(JavaTypeKind.ENUM)) {
          browseDialogStyle = IJavaElementSearchConstants.CONSIDER_CLASSES_AND_ENUMS;
        }
      }
    }

    final IProject project = property.element().adapt(IProject.class);

    try {
      final SelectionDialog dlg =
          JavaUI.createTypeDialog(
              ((FormComponentPresentation) context).shell(),
              null,
              project,
              browseDialogStyle,
              false);

      dlg.setTitle(
          dialogTitle.format(
              property.definition().getLabel(true, CapitalizationType.TITLE_STYLE, false)));

      if (dlg.open() == SelectionDialog.OK) {
        Object results[] = dlg.getResult();
        assert results != null && results.length == 1;
        if (results[0] instanceof IType) {
          return ((IType) results[0]).getFullyQualifiedName();
        }
      }
    } catch (JavaModelException e) {
      Sapphire.service(LoggingService.class).log(e);
    }

    return null;
  }