public class EditorUtility
extends java.lang.Object
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.
Modifier | Constructor and Description |
---|---|
protected |
EditorUtility()
Prevents direct instantiation by clients.
|
Modifier and Type | Method and Description |
---|---|
org.eclipse.ui.IEditorReference |
findEditor(org.eclipse.ui.IWorkbenchPage page,
java.lang.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, or
null
if the element has no corresponding buffer or if an exception occurs
while obtaining the buffer. |
org.eclipse.ui.IEditorInput |
getEditorInput(java.lang.Object element)
Returns the editor input for the given element, or
null
if no editor input corresponds to the element. |
void |
revealElement(org.eclipse.ui.IEditorPart editor,
java.lang.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.
|
protected EditorUtility()
DefaultEditorUtility.INSTANCE
if you need an instance
of the editor utility with the default behavior.public org.eclipse.ui.IEditorInput getEditorInput(java.lang.Object element)
null
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:
IResource
, the corresponding resource
is the element itself.
IElement
,
the corresponding resource is obtained via Elements.getResource(IElement)
.
IResource
via
ResourceUtil.getResource(Object)
.
element
- may be null
null
if nonepublic org.eclipse.ui.IEditorReference findEditor(org.eclipse.ui.IWorkbenchPage page, java.lang.Object element)
null
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 an IElement
and the adapter element is an ISourceElement
, it is
additionally required for matching editors which could be adapted to an
ITextEditor
that the text editor's document equals the document
of the source element's buffer
.
page
- not null
element
- not null
null
if nonepublic void revealElement(org.eclipse.ui.IEditorPart editor, java.lang.Object element)
If the given element could be adapted to an IElement
and the
adapter element is an ISourceElement
, and if the given editor
could be adapted to an ITextEditor
, 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's buffer
.
If all else fails, a structured selection containing a single object,
the given element, is passed to the selection provider of the given editor.
editor
- not null
element
- not null
public void revealTextRange(org.eclipse.ui.IEditorPart editor, int offset, int length, ISnapshot snapshot)
If the given editor could be adapted to an ITextEditor
, this
implementation calls ITextEditor.selectAndReveal(int, int)
.
Otherwise, if the given editor could be adapted to an IGotoMarker
,
this implementation creates a temporary text marker on the IFile
corresponding to the editor input (if such a file exists) and calls
IGotoMarker.gotoMarker(IMarker)
. As a fallback, a text selection
for the given range is passed to the selection provider of the given editor.
editor
- not null
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,
or null
if the snapshot is unknown or does not matterStaleSnapshotException
- if the given snapshot could be detected
to be staleprotected IBuffer getBuffer(ISourceElement element)
null
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 a CoreException
if necessary.
element
- not null
null
if noneCopyright (c) 2014, 2019 1C-Soft LLC and others. Made available under the Eclipse Public License 2.0