private void openInCompare(ITypedElement ancestor, ITypedElement left, ITypedElement right) { IWorkbenchPage workBenchPage = getTargetPage(); CompareEditorInput input = new SaveablesCompareEditorInput(ancestor, left, right, workBenchPage); IEditorPart editor = CompareRevisionAction.findReusableCompareEditor( input, workBenchPage); if (editor != null) { IEditorInput otherInput = editor.getEditorInput(); if (otherInput.equals(input)) { // simply provide focus to editor workBenchPage.activate(editor); } else { // if editor is currently not open on that input either re-use // existing CompareUI.reuseCompareEditor(input, (IReusableEditor) editor); workBenchPage.activate(editor); } } else { CompareUI.openCompareEditor(input); } }
/** * Displays the given input in an existing compare editor, if there is already one. Otherwise, a new compare editor is * opened. * * @param input * the {@link GitFileDiffEditorInput} to be shown in the compare editor */ public static void openExistingCompareEditor(final GitFileDiffEditorInput input) { final IEditorPart existingEditor = findExistingCompareEditor(); if (existingEditor != null) { CompareUI.reuseCompareEditor(input, (IReusableEditor) existingEditor); WorkbenchUtil.activePage().activate(existingEditor); } else { openNewCompareEditor(input); } }
private static IEditorPart findExistingCompareEditor() { final IEditorReference[] editorRefs = WorkbenchUtil.activePage().getEditorReferences(); for (final IEditorReference editorRef : editorRefs) { final IEditorPart part = editorRef.getEditor(false); if (part != null && part.getEditorInput() instanceof GitFileDiffEditorInput && part instanceof IReusableEditor) { return part; } } return null; }
private EntryEditor openEntryEditor(TreeNode en) throws PartInitException { final EditorInput input = new EditorInput(this, en); final IWorkbenchPage activePage = Helper.getActiveWBPage(); IEditorPart editor = activePage.findEditor(input); if(editor != null && editor instanceof IReusableEditor) { editor.setFocus(); return (EntryEditor)editor; } else { editor = activePage.openEditor(input, EntryEditor.ID); final IEditorPart entryEditor = editor; editor.addPropertyListener(new IPropertyListener() { @Override public void propertyChanged(Object source, int propId) { if(propId == EntryEditor.PROP_DIRTY) { if(entryEditor.isDirty()) { markDirty(); } firePropertyChange(PROP_DIRTY); } } }); if(editor instanceof EntryEditor) { ((EntryEditor)editor).initExpand(); } return (EntryEditor)editor; } }
/** * For each editor page, set the new input. * Auxiliary to setInput() * @param input the new editor input; may be null */ protected void setPageInputs(IEditorInput input) { for (int i = 0; i < getPageCount(); i++) { IEditorPart editorPart = getEditor(i); if (editorPart instanceof IReusableEditor) { ((IReusableEditor) editorPart).setInput(input); } else { // should be an illegal state since we want all of our editors to be reusable throw new IllegalStateException("All editors in the multi page plan editor must implement IReusableEditor"); } } }
private IEditorPart showInEditor(IWorkbenchPage page, IEditorInput input, String editorId) { IEditorPart editor= page.findEditor(input); if (editor != null) { page.bringToTop(editor); return editor; } IEditorReference reusedEditorRef= fReusedEditor; if (reusedEditorRef != null) { boolean isOpen= reusedEditorRef.getEditor(false) != null; boolean canBeReused= isOpen && !reusedEditorRef.isDirty() && !reusedEditorRef.isPinned(); if (canBeReused) { boolean showsSameInputType= reusedEditorRef.getId().equals(editorId); if (!showsSameInputType) { page.closeEditors(new IEditorReference[] { reusedEditorRef }, false); fReusedEditor= null; } else { editor= reusedEditorRef.getEditor(true); if (editor instanceof IReusableEditor) { ((IReusableEditor) editor).setInput(input); page.bringToTop(editor); return editor; } } } } // could not reuse try { editor= page.openEditor(input, editorId, false); if (editor instanceof IReusableEditor) { IEditorReference reference= (IEditorReference) page.getReference(editor); fReusedEditor= reference; } else { fReusedEditor= null; } return editor; } catch (PartInitException ex) { MessageDialog.openError(JavaPlugin.getActiveWorkbenchShell(), SearchMessages.Search_Error_openEditor_title, SearchMessages.Search_Error_openEditor_message); return null; } }
public static ITextEditor openTextEditor(ITextEditor currentEditor, String editorId, IEditorInput newInput, OpenNewEditorMode openNewEditor) throws PartInitException, CoreException { IWorkbenchPage page; if(currentEditor == null) { page = WorkbenchUtils.getActivePage(); openNewEditor = OpenNewEditorMode.ALWAYS; } else { page = currentEditor.getEditorSite().getWorkbenchWindow().getActivePage(); } if(openNewEditor == OpenNewEditorMode.NEVER) { if(currentEditor.getEditorInput().equals(newInput)) { return currentEditor; } else if(currentEditor instanceof IReusableEditor) { IReusableEditor reusableEditor = (IReusableEditor) currentEditor; reusableEditor.setInput(newInput); return currentEditor; } else { openNewEditor = OpenNewEditorMode.ALWAYS; } } int matchFlags = openNewEditor == OpenNewEditorMode.ALWAYS ? IWorkbenchPage.MATCH_NONE : IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID; IEditorPart editor = page.openEditor(newInput, editorId, true, matchFlags); ITextEditor targetEditor = tryCast(editor, ITextEditor.class); if(targetEditor == null) { throw EclipseCore.createCoreException("Not a text editor", null); } return targetEditor; }
private IEditorPart showWithReuse(IFile file, IWorkbenchPage page, String editorId, boolean activate) throws PartInitException { IEditorInput input= new FileEditorInput(file); IEditorPart editor= page.findEditor(input); if (editor != null) { page.bringToTop(editor); if (activate) { page.activate(editor); } return editor; } IEditorReference reusedEditorRef= fReusedEditor; if (reusedEditorRef != null) { boolean isOpen= reusedEditorRef.getEditor(false) != null; boolean canBeReused= isOpen && !reusedEditorRef.isDirty() && !reusedEditorRef.isPinned(); if (canBeReused) { boolean showsSameInputType= reusedEditorRef.getId().equals(editorId); if (!showsSameInputType) { page.closeEditors(new IEditorReference[] { reusedEditorRef }, false); fReusedEditor= null; } else { editor= reusedEditorRef.getEditor(true); if (editor instanceof IReusableEditor) { ((IReusableEditor) editor).setInput(input); page.bringToTop(editor); if (activate) { page.activate(editor); } return editor; } } } } editor= page.openEditor(input, editorId, activate); if (editor instanceof IReusableEditor) { IEditorReference reference= (IEditorReference) page.getReference(editor); fReusedEditor= reference; } else { fReusedEditor= null; } return editor; }
private IEditorPart showWithReuse(IFile file, IWorkbenchPage page, String editorId, boolean activate) throws PartInitException { IEditorInput input = new FileEditorInput(file); IEditorPart editor = page.findEditor(input); if (editor != null) { page.bringToTop(editor); if (activate) { page.activate(editor); } return editor; } IEditorReference reusedEditorRef = fReusedEditor; if (reusedEditorRef != null) { boolean isOpen = reusedEditorRef.getEditor(false) != null; boolean canBeReused = isOpen && !reusedEditorRef.isDirty() && !reusedEditorRef.isPinned(); if (canBeReused) { boolean showsSameInputType = reusedEditorRef.getId().equals(editorId); if (!showsSameInputType) { page.closeEditors(new IEditorReference[] { reusedEditorRef }, false); fReusedEditor = null; } else { editor = reusedEditorRef.getEditor(true); if (editor instanceof IReusableEditor) { ((IReusableEditor) editor).setInput(input); page.bringToTop(editor); if (activate) { page.activate(editor); } return editor; } } } } editor = page.openEditor(input, editorId, activate); if (editor instanceof IReusableEditor) { IEditorReference reference = (IEditorReference) page.getReference(editor); fReusedEditor = reference; } else { fReusedEditor = null; } return editor; }