/*
   * @see org.eclipse.swt.dnd.DragSourceListener#dragStart
   */
  @Override
  public void dragStart(DragSourceEvent event) {
    fEditorInputDatas = new ArrayList<EditorInputData>();

    ISelection selection = fProvider.getSelection();
    if (selection instanceof IStructuredSelection) {
      IStructuredSelection structuredSelection = (IStructuredSelection) selection;
      for (Iterator<?> iter = structuredSelection.iterator(); iter.hasNext(); ) {
        Object element = iter.next();
        IEditorInput editorInput = EditorUtility.getEditorInput(element);
        if (editorInput != null && editorInput.getPersistable() != null) {
          try {
            String editorId = EditorUtility.getEditorID(editorInput);
            // see org.eclipse.ui.internal.ide.EditorAreaDropAdapter.openNonExternalEditor(..):
            IEditorRegistry editorReg = PlatformUI.getWorkbench().getEditorRegistry();
            IEditorDescriptor editorDesc = editorReg.findEditor(editorId);
            if (editorDesc != null && !editorDesc.isOpenExternal()) {
              fEditorInputDatas.add(
                  EditorInputTransfer.createEditorInputData(editorId, editorInput));
            }
          } catch (PartInitException e) {
            JavaPlugin.log(e);
          }
        }
      }
    }

    event.doit = fEditorInputDatas.size() > 0;
  }
 public static void openEditor(IFile file, int offset, int length) {
   IEditorPart ep = null;
   try {
     ep = EditorUtility.openInEditor(file);
   } catch (PartInitException e) {
     e.printStackTrace();
   }
   if (ep != null) EditorUtility.revealInEditor(ep, offset, length);
 }
 /**
  * Links to editor (if option enabled)
  *
  * @param selection the selection
  */
 private void linkToEditor(ISelection selection) {
   Object obj = SelectionUtil.getSingleElement(selection);
   if (obj != null) {
     IEditorPart part = EditorUtility.isOpenInEditor(obj);
     if (part != null) {
       IWorkbenchPage page = getSite().getPage();
       page.bringToTop(part);
       if (obj instanceof IJavaElement) EditorUtility.revealInEditor(part, (IJavaElement) obj);
     }
   }
 }
Beispiel #4
0
 public IEditorPart openInEditor(Object object, boolean activate)
     throws PartInitException, CoreException {
   if (object instanceof IJavaElement) {
     IEditorPart editor = EditorUtility.openInEditor(object, activate);
     if (editor != null) {
       EditorUtility.revealInEditor(editor, (IJavaElement) object);
     }
     return editor;
   }
   return null;
 }
 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
   if (!(targetPart instanceof JavaEditor)) {
     return;
   }
   JavaEditor javaEditor = (JavaEditor) targetPart;
   javaProject = EditorUtility.getJavaProject(javaEditor.getEditorInput());
 }
  public JavaEditor openJavaEditor(IJavaElement javaElement) {
    IEditorPart part = EditorUtility.isOpenInEditor(javaElement);
    if (part == null) {
      try {
        part = JavaUI.openInEditor(javaElement);
      } catch (PartInitException e) {
        // ignore
      } catch (JavaModelException e) {
        // ignore
      }
    }

    IWorkbenchPage page = JavaPlugin.getActivePage();
    if (page != null && part != null) {
      page.bringToTop(part);
    }
    if (part != null) {
      part.setFocus();
    }
    if (part instanceof JavaEditor) {
      return (JavaEditor) part;
    }

    return null;
  }
    @Override
    public JavaContentAssistInvocationContext apply(IInvocationContext context) {
      ICompilationUnit cu = context.getCompilationUnit();
      int offset = context.getSelectionOffset();
      try {
        cu.codeComplete(
            offset,
            new CompletionRequestor() {
              @Override
              public void acceptContext(CompletionContext context) {
                internalContext = context;
              }

              @Override
              public boolean isExtendedContextRequired() {
                return true;
              }

              @Override
              public void accept(CompletionProposal proposal) {}
            });
      } catch (JavaModelException e) {
        propagate(e);
      }

      JavaEditor editor = cast(EditorUtility.isOpenInEditor(cu));
      ISourceViewer viewer = editor.getViewer();
      return new JavaContentAssistInvocationContext(viewer, offset, editor) {

        @Override
        public CompletionContext getCoreContext() {
          return internalContext;
        }
      };
    }
Beispiel #8
0
  /**
   * 將所要變更的內容寫回Edit中 (New)
   *
   * @param msg
   */
  protected void applyChange(ASTRewrite rewrite) {
    try {
      // 參考org.eclipse.jdt.internal.ui.text.correction.CorrectionMarkerResolutionGenerator run
      // org.eclipse.jdt.internal.ui.text.correction.ChangeCorrectionProposal apply與performChange
      ICompilationUnit cu = (ICompilationUnit) actOpenable;
      IEditorPart part = EditorUtility.isOpenInEditor(cu);
      IEditorInput input = part.getEditorInput();
      IDocument doc =
          JavaPlugin.getDefault().getCompilationUnitDocumentProvider().getDocument(input);

      performChange(JavaPlugin.getActivePage().getActiveEditor(), doc, rewrite);
    } catch (CoreException e) {
      logger.error("[Core Exception] EXCEPTION ", 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);
    }
  }