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; } }; }
/** * 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); } } }
/** * 將所要變更的內容寫回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); } }