/* (non-JavaDoc)
  * Method declared in SelectionDispatchAction.
  */
 @Override
 public final void run(ITextSelection ts) {
   ITypeRoot input = getEditorInput(fEditor);
   if (!ActionUtil.isProcessable(getShell(), input)) return;
   OccurrencesFinder finder = new OccurrencesFinder();
   FindOccurrencesEngine engine = FindOccurrencesEngine.create(finder);
   try {
     String result = engine.run(input, ts.getOffset(), ts.getLength());
     if (result != null) showMessage(getShell(), fEditor, result);
   } catch (JavaModelException e) {
     JavaPlugin.log(e);
   }
 }
 @Override
 public void run(IStructuredSelection selection) {
   IMember member = getMember(selection);
   if (!ActionUtil.isProcessable(getShell(), member)) return;
   FindOccurrencesEngine engine = FindOccurrencesEngine.create(new OccurrencesFinder());
   try {
     ISourceRange range = member.getNameRange();
     String result = engine.run(member.getTypeRoot(), range.getOffset(), range.getLength());
     if (result != null) showMessage(getShell(), fActionBars, result);
   } catch (JavaModelException e) {
     JavaPlugin.log(e);
   }
 }
  /**
   * Note: This method is for internal use only. Clients should not call this method.
   *
   * @param cu The compilation unit to process
   */
  public void run(ICompilationUnit cu) {
    if (!ElementValidator.check(
        cu, getShell(), ActionMessages.OrganizeImportsAction_error_title, fEditor != null)) return;
    if (!ActionUtil.isProcessable(getShell(), cu)) return;

    IEditingSupport helper = createViewerHelper();
    try {
      CodeGenerationSettings settings =
          JavaPreferencesSettings.getCodeGenerationSettings(cu.getJavaProject());

      if (fEditor == null && EditorUtility.isOpenInEditor(cu) == null) {
        IEditorPart editor = EditorUtility.openInEditor(cu);
        if (editor instanceof JavaEditor) {
          fEditor = (JavaEditor) editor;
        }
      }

      CompilationUnit astRoot =
          JavaPlugin.getDefault().getASTProvider().getAST(cu, ASTProvider.WAIT_ACTIVE_ONLY, null);

      AJOrganizeImportsOperation op =
          new AJOrganizeImportsOperation(
              cu,
              astRoot,
              settings.importIgnoreLowercase,
              !cu.isWorkingCopy(),
              true,
              createChooseImportQuery());

      IRewriteTarget target = null;
      if (fEditor != null) {
        target = (IRewriteTarget) fEditor.getAdapter(IRewriteTarget.class);
        if (target != null) {
          target.beginCompoundChange();
        }
      }

      IProgressService progressService = PlatformUI.getWorkbench().getProgressService();
      IRunnableContext context = getSite().getWorkbenchWindow();
      if (context == null) {
        context = progressService;
      }
      try {
        registerHelper(helper);
        progressService.runInUI(
            context, new WorkbenchRunnableAdapter(op, op.getScheduleRule()), op.getScheduleRule());
        IProblem parseError = op.getParseError();
        if (parseError != null) {
          String message =
              Messages.format(
                  ActionMessages.OrganizeImportsAction_single_error_parse, parseError.getMessage());
          MessageDialog.openInformation(
              getShell(), ActionMessages.OrganizeImportsAction_error_title, message);
          if (fEditor != null && parseError.getSourceStart() != -1) {
            fEditor.selectAndReveal(
                parseError.getSourceStart(),
                parseError.getSourceEnd() - parseError.getSourceStart() + 1);
          }
        } else {
          if (fEditor != null) {
            setStatusBarMessage(getOrganizeInfo(op));
          }
        }
      } catch (InvocationTargetException e) {
        ExceptionHandler.handle(
            e,
            getShell(),
            ActionMessages.OrganizeImportsAction_error_title,
            ActionMessages.OrganizeImportsAction_error_message);
      } catch (InterruptedException e) {
      } finally {
        deregisterHelper(helper);
        if (target != null) {
          target.endCompoundChange();
        }
      }
    } catch (CoreException e) {
      ExceptionHandler.handle(
          e,
          getShell(),
          ActionMessages.OrganizeImportsAction_error_title,
          ActionMessages.OrganizeImportsAction_error_message);
    }
  }