Class EditorUtility
- Direct Known Subclasses:
DefaultEditorUtility
The implementations of the methods in this class strive to provide a
reasonable default behavior and work fine for most cases. Clients can use
the default
instance of the editor
utility or may subclass this class if they need to specialize the default
behavior.
- Restriction:
- This class is not intended to be instantiated by clients.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Prevents direct instantiation by clients. -
Method Summary
Modifier and TypeMethodDescriptionorg.eclipse.ui.IEditorReference
findEditor
(org.eclipse.ui.IWorkbenchPage page, Object element) Given a workbench page, finds and returns the reference for an editor that matches the given element.protected IBuffer
getBuffer
(ISourceElement element) Returns the buffer for the given source element, ornull
if the element has no corresponding buffer or if an exception occurs while obtaining the buffer.org.eclipse.ui.IEditorInput
getEditorInput
(Object element) Returns the editor input for the given element, ornull
if no editor input corresponds to the element.void
revealElement
(org.eclipse.ui.IEditorPart editor, Object element) Reveals the given element in the given editor on a best effort basis.void
revealTextRange
(org.eclipse.ui.IEditorPart editor, int offset, int length, ISnapshot snapshot) Reveals the given text range in the given editor on a best effort basis.
-
Constructor Details
-
EditorUtility
protected EditorUtility()Prevents direct instantiation by clients. UseDefaultEditorUtility.INSTANCE
if you need an instance of the editor utility with the default behavior.
-
-
Method Details
-
getEditorInput
Returns the editor input for the given element, ornull
if no editor input corresponds to the element.If the given element is an editor input, this implementation returns the element itself. Otherwise, it attempts to find a resource that corresponds to the given element and, if the corresponding resource is a file, returns a
FileEditorInput
based on the resource. The corresponding resource is determined as follows:-
If the input element is an
IResource
, the corresponding resource is the element itself. -
Otherwise, if the given element could be adapted to an
IElement
, the corresponding resource is obtained viaElements.getResource(IElement)
. -
Otherwise, the given element is adapted to an
IResource
viaResourceUtil.getResource(Object)
.
- Parameters:
element
- may benull
- Returns:
- the corresponding editor input, or
null
if none
-
If the input element is an
-
findEditor
public org.eclipse.ui.IEditorReference findEditor(org.eclipse.ui.IWorkbenchPage page, Object element) Given a workbench page, finds and returns the reference for an editor that matches the given element. If several matching editors are found within the page, returns the reference for the 'most specific' editor, which would typically be the most recently used matching editor. Returnsnull
if there are no matching editors.This implementation asks the workbench page to find editor references that match the editor input provided for the given element by
getEditorInput(Object)
and returns the reference for the most recently used matching editor. If the given element could be adapted to anIElement
and the adapter element is anISourceElement
, it is additionally required for matching editors which could be adapted to anITextEditor
that the text editor's document equals the document of the source element'sbuffer
, if any.- Parameters:
page
- notnull
element
- notnull
- Returns:
- the matching editor reference, or
null
if none
-
revealElement
Reveals the given element in the given editor on a best effort basis.If the given element could be adapted to an
IElement
and the adapter element is anISourceElement
, and if the given editor could be adapted to anITextEditor
, this implementation attempts to select and reveal the source element's identifying range in the text editor, provided that the text editor's document equals the document of the source element'sbuffer
, if any. If all else fails, a structured selection containing a single object, the given element, is passed to the selection provider of the given editor.- Parameters:
editor
- notnull
element
- notnull
-
revealTextRange
public void revealTextRange(org.eclipse.ui.IEditorPart editor, int offset, int length, ISnapshot snapshot) Reveals the given text range in the given editor on a best effort basis.If the given editor could be adapted to an
ITextEditor
, this implementation callsITextEditor.selectAndReveal(int, int)
. Otherwise, if the given editor could be adapted to anIGotoMarker
, this implementation creates a temporary text marker on theIFile
corresponding to the editor input (if such a file exists) and callsIGotoMarker.gotoMarker(IMarker)
. As a fallback, a text selection for the given range is passed to the selection provider of the given editor.- Parameters:
editor
- notnull
offset
- the offset of the text range (not negative)length
- the length of the text range (not negative)snapshot
- a snapshot on which the given text range is based, ornull
if the snapshot is unknown or does not matter- Throws:
StaleSnapshotException
- if the given snapshot could be detected to be stale
-
getBuffer
Returns the buffer for the given source element, ornull
if the element has no corresponding buffer or if an exception occurs while obtaining the buffer.If the given element is contained in a source file, this implementation delegates to
Elements.getBuffer(ISourceFile)
, suppressing and logging aCoreException
if necessary.- Parameters:
element
- notnull
- Returns:
- the corresponding buffer, or
null
if none
-