Java Code Examples for org.eclipse.ui.IWorkbenchPage#getReference()

The following examples show how to use org.eclipse.ui.IWorkbenchPage#getReference() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IDeduplication.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Close all duplicates present in the array of IEditorReferences
 * 
 * @param page
 * @param refs
 * @return true if any editors were closed
 * @throws PartInitException
 */
public static boolean closeDuplicates(IWorkbenchPage page, IEditorReference[] refs) throws PartInitException {
	boolean closed = false;
	if (refs != null && refs.length > 1) {
		// Dereference if a multi-part editor, returns identity if not
		IEditorReference active = (IEditorReference) page.getReference(page.getActiveEditor());
		HashSet<IEditorReference> closers = new HashSet<IEditorReference>();
		// first get any that match the active editor
		closers.addAll(getOtherInstances(active, refs, 0));
		// then see if any other editors have duplicates within the array of references
		for (int i = 0; i < refs.length; i++) {
			IEditorReference current = refs[i];
			if (current != active && !closers.contains(current)) {
				closers.addAll(getOtherInstances(current, refs, i));
			}
		}
		closed = closeInstances(page, closers, active);
	}
	return closed;
}
 
Example 2
Source File: JavaSearchEditorOpener.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
	}
}
 
Example 3
Source File: CloseOtherInstancesHandler.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Close all other editors on the current editor's input
 * 
 * @param page
 * @throws PartInitException
 */
protected void closeOtherInstances(IWorkbenchPage page) throws PartInitException {
	if (page != null) {
		IEditorReference active = (IEditorReference) page.getReference(page.getActiveEditor());
		// Close the others, and guarantee proper (emacs+) activation
		IDeduplication.closeInstances(page, IDeduplication.getOtherInstances(active, page.getEditorReferences(), 0), active);
	}
}
 
Example 4
Source File: EditorOpener.java    From typescript.java with MIT License 4 votes vote down vote up
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;
}
 
Example 5
Source File: EditorOpener.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
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;
}