Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » correct way to open editors of various "types" (i.e., to create an EditorInput?)
correct way to open editors of various "types" (i.e., to create an EditorInput?) [message #330471] Tue, 29 July 2008 18:32 Go to next message
Paul E. Keyser is currently offline Paul E. KeyserFriend
Messages: 878
Registered: July 2009
Senior Member
R3.3, WinXP

We have currently two sorts of editors (call them "C" and "G") that
correspond to two Windoze-sorts of files; in the future, there will be
other sorts of editors/files. Each sort of editor is to be plugged in
(we use both the "org.eclipse.ui.editors" and the
"org.eclipse.ui.elementFactories" extension-points), and in a given
install any or all of these pluggable editors may be present or absent.

The editors are not all text-editors, but (so far) all use the
IPathEditorInput (or a subclass); the editors themselves could be of any
type, but I think that doesn't matter.

All these sorts of files are displayed in a kind of "Explorer" (not my
code), and it is desired to open the correct editor, when the user
double-clicks (or takes other actions, maybe activating a menu-item).

I've looked through items on these two newsgroups, and poked at the help
docs, and other web-pages (the
http://wiki.eclipse.org/index.php/JFaceSnippets is not really relevant,
of course, whereas the http://www.eclipse.org/articles/ which could be,
do not seem to have anything on this topic).


(A) ================================
Here is what I think is the correct Eclipse code to open an editor,
given the IEditorInput (wrapped in a try/catch (PartInitException) block):

IWorkbenchPage page =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage()
IPath path = thisWeKnowHowToGet();
IPathEditorInput input = getInputSomehowOrOther(path);
String editorID = getIdSomehowOrOther(path);
page.openEditor(input, editorID, true);

So, first, am I right about that?


(B) ================================
In order to allow the various sorts of editors to be pluggable, clearly
the open-file action has to have some way of mapping from the file-name
to the editor-input *and* to the editor-id.

For the editor-id, this snippet does work, and I have used it before,
but am not sure it is the best practice:

IEditorRegistry reg = PlatformUI.getWorkbench().getEditorRegistry();
IEditorDescriptor descr = reg.getDefaultEditor(path.toOSString());
String editorID = descr.getId();

Is that the right way?


(C) ================================

I did find one way to map the *editorInputFactoryID* to the correct
editor-input class, and it works, but (a) I'd still like a
plug-in-correct way of getting that id, and (b) anyway, my
kluge-detector buzzes, for various reasons: (i) the
IDocumentEditorInput.TAG_PATH is a shared value in our system, which is
what we plan to use in all of our implementations of IPathEditrInput,
but ... requiring that is fragile, and (ii) since I have to create a
Memento on the fly.

(The "Document" stuff is just DOM4J, being used to create an XML string.)

IPathEditorInput getPathEditorInput(final IPath path, final String
factoryId) throws WorkbenchException {
IElementFactory factory =
PlatformUI.getWorkbench().getElementFactory(factoryId);
if (null == factory) {
throw new WorkbenchException("msg1");
}

Document doc = DocumentHelper.createDocument();
doc.addElement("root"); //$NON-NLS-1$ // any tag will do
String xml = doc.asXML();
IMemento memento =
XMLMemento.createReadRoot(new StringReader(xml));
memento.putString(
IDocumentEditorInput.TAG_PATH, path.toOSString());

IAdaptable adaptable = factory.createElement(memento);
if (adaptable instanceof IPathEditorInput) {
return (IPathEditorInput) adaptable;
}
throw new WorkbenchException("msg2");
}

So -- how do I *correctly* get the appropriate instance of an
IEditorInput simply from the {editorID, file-path} using Eclipse methods?

thanks,
Paul

PS -- we do have what I believe to be a correct equals() method in our
PathEditorInput subclasses.
Re: correct way to open editors of various "types" (i.e., to create an EditorInput?) [message #330528 is a reply to message #330471] Thu, 31 July 2008 14:22 Go to previous messageGo to next message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Paul Th. Keyser wrote:
> R3.3, WinXP
>
> We have currently two sorts of editors (call them "C" and "G") that
> correspond to two Windoze-sorts of files; in the future, there will be
> other sorts of editors/files. Each sort of editor is to be plugged in
> (we use both the "org.eclipse.ui.editors" and the
> "org.eclipse.ui.elementFactories" extension-points), and in a given
> install any or all of these pluggable editors may be present or absent.
>
> The editors are not all text-editors, but (so far) all use the
> IPathEditorInput (or a subclass); the editors themselves could be of any
> type, but I think that doesn't matter.
>
> All these sorts of files are displayed in a kind of "Explorer" (not my
> code), and it is desired to open the correct editor, when the user
> double-clicks (or takes other actions, maybe activating a menu-item).
>
> I've looked through items on these two newsgroups, and poked at the help
> docs, and other web-pages (the
> http://wiki.eclipse.org/index.php/JFaceSnippets is not really relevant,
> of course, whereas the http://www.eclipse.org/articles/ which could be,
> do not seem to have anything on this topic).
>
>
> (A) ================================
> Here is what I think is the correct Eclipse code to open an editor,
> given the IEditorInput (wrapped in a try/catch (PartInitException) block):
>
> IWorkbenchPage page =
> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage()
> IPath path = thisWeKnowHowToGet();
> IPathEditorInput input = getInputSomehowOrOther(path);
> String editorID = getIdSomehowOrOther(path);
> page.openEditor(input, editorID, true);
>
> So, first, am I right about that?

Yes, with all of the magic happening in your
getIdSomehowOrOther(IEditorInput). For IFileEditorInputs, check out
some of the methods that start on org.eclipse.ui.ide.IDE They get
editor descriptors for IFiles and can open editors (which they must turn
into IFileEditorInputs), although I see a method that can open a URI.

> (B) ================================
> In order to allow the various sorts of editors to be pluggable, clearly
> the open-file action has to have some way of mapping from the file-name
> to the editor-input *and* to the editor-id.
>
> For the editor-id, this snippet does work, and I have used it before,
> but am not sure it is the best practice:
>
> IEditorRegistry reg = PlatformUI.getWorkbench().getEditorRegistry();
> IEditorDescriptor descr = reg.getDefaultEditor(path.toOSString());
> String editorID = descr.getId();
>
> Is that the right way?

This looks fine, although be default I'm not sure if it works with files
outside the workspace.


The lookup for matching an IFile to an editor.id is first, the extension
matching, and then the IContentType scanning to narrow it down further.

Later,
PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse. platform.doc.isv/guide/workbench.htm


Re: correct way to open editors of various "types" (i.e., to create an EditorInput?) [message #330544 is a reply to message #330528] Thu, 31 July 2008 23:08 Go to previous messageGo to next message
Paul E. Keyser is currently offline Paul E. KeyserFriend
Messages: 878
Registered: July 2009
Senior Member
Thanks for the answers to (A) and (B) ...

Paul Webster wrote:
> Paul Th. Keyser wrote:
>> (A) ================================
>> Here is what I think is the correct Eclipse code to open an editor,
>> given the IEditorInput (wrapped in a try/catch (PartInitException)
>> block):
>>
>> IWorkbenchPage page =
>> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage()
>> IPath path = thisWeKnowHowToGet();
>> IPathEditorInput input = getInputSomehowOrOther(path);
>> String editorID = getIdSomehowOrOther(path);
>> page.openEditor(input, editorID, true);
>>
>> So, first, am I right about that?
>
> Yes, with all of the magic happening in your
> getIdSomehowOrOther(IEditorInput). For IFileEditorInputs, check out
> some of the methods that start on org.eclipse.ui.ide.IDE They get
> editor descriptors for IFiles and can open editors (which they must turn
> into IFileEditorInputs), although I see a method that can open a URI.
>
Right, I did wind up going through those (maybe I missed something), and
see below ...

>> (B) ================================
>> In order to allow the various sorts of editors to be pluggable,
>> clearly the open-file action has to have some way of mapping from the
>> file-name to the editor-input *and* to the editor-id.
>>
>> For the editor-id, this snippet does work, and I have used it before,
>> but am not sure it is the best practice:
>>
>> IEditorRegistry reg = PlatformUI.getWorkbench().getEditorRegistry();
>> IEditorDescriptor descr = reg.getDefaultEditor(path.toOSString());
>> String editorID = descr.getId();
>>
>> Is that the right way?
>
> This looks fine, although be default I'm not sure if it works with files
> outside the workspace.
>
I think it does not work outside the WS, since IPath seems to be
WS-scope, as it were.

.... but what about (C)?

In the interval, I did (as mentioned above) go through the IDE calls
(well, some of them), and it seemed to me that what was happening was
that *all* files (whether my "C" or "G" or whatever) were being opened
using the *internal* Eclipse class FileEditorInput.

So my current solution is to have copied that (and its transitive
closure of interface and factory) and now we use the same "kluge" --
every file-type is opened using a com.mun.ist.FileEditorInput -- but I
still sort of think that having a specific factory registered for each
file-type might be better (maybe some files need some extra work, e.g.),
and that would then require some "registry" that mapped file-types to
factories ...

Does that exist?

thanks,
-Paul
Re: correct way to open editors of various "types" (i.e., to create an EditorInput?) [message #330545 is a reply to message #330544] Fri, 01 August 2008 02:47 Go to previous message
Eclipse UserFriend
Originally posted by: totolaricot.mac.com

Paul Th. Keyser wrote:
> Thanks for the answers to (A) and (B) ...
>
> Paul Webster wrote:
>> Paul Th. Keyser wrote:
>>> (A) ================================
>>> Here is what I think is the correct Eclipse code to open an editor,
>>> given the IEditorInput (wrapped in a try/catch (PartInitException)
>>> block):
>>>
>>> IWorkbenchPage page =
>>> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage()
>>> IPath path = thisWeKnowHowToGet();
>>> IPathEditorInput input = getInputSomehowOrOther(path);
>>> String editorID = getIdSomehowOrOther(path);
>>> page.openEditor(input, editorID, true);
>>>
>>> So, first, am I right about that?
>>
>> Yes, with all of the magic happening in your
>> getIdSomehowOrOther(IEditorInput). For IFileEditorInputs, check out
>> some of the methods that start on org.eclipse.ui.ide.IDE They get
>> editor descriptors for IFiles and can open editors (which they must
>> turn into IFileEditorInputs), although I see a method that can open a
>> URI.
>>
> Right, I did wind up going through those (maybe I missed something),
> and see below ...
>
>>> (B) ================================
>>> In order to allow the various sorts of editors to be pluggable,
>>> clearly the open-file action has to have some way of mapping from
>>> the file-name to the editor-input *and* to the editor-id.
>>>
>>> For the editor-id, this snippet does work, and I have used it
>>> before, but am not sure it is the best practice:
>>>
>>> IEditorRegistry reg = PlatformUI.getWorkbench().getEditorRegistry();
>>> IEditorDescriptor descr = reg.getDefaultEditor(path.toOSString());
>>> String editorID = descr.getId();
>>>
>>> Is that the right way?
>>
>> This looks fine, although be default I'm not sure if it works with
>> files outside the workspace.
>>
> I think it does not work outside the WS, since IPath seems to be
> WS-scope, as it were.
>
> ... but what about (C)?
>
> In the interval, I did (as mentioned above) go through the IDE calls
> (well, some of them), and it seemed to me that what was happening was
> that *all* files (whether my "C" or "G" or whatever) were being opened
> using the *internal* Eclipse class FileEditorInput.
>
> So my current solution is to have copied that (and its transitive
> closure of interface and factory) and now we use the same "kluge" --
> every file-type is opened using a com.mun.ist.FileEditorInput -- but I
> still sort of think that having a specific factory registered for each
> file-type might be better (maybe some files need some extra work,
> e.g.), and that would then require some "registry" that mapped
> file-types to factories ...
>
> Does that exist?
>
> thanks,
> -Paul
not that you should play with any of it, but for the curious:

org.eclipse.ui.internal.EditorManager {
public IEditorReference openEditor(String editorId, IEditorInput input,
boolean setVisible, IMemento editorState) throws
PartInitException {
if (editorId == null || input == null) {
throw new IllegalArgumentException();
}

IEditorRegistry reg = getEditorRegistry();
EditorDescriptor desc = (EditorDescriptor) reg.findEditor(editorId);
if (desc == null) {
throw new PartInitException(NLS.bind(
WorkbenchMessages.EditorManager_unknownEditorIDMessage,
editorId));
}

IEditorReference result = openEditorFromDescriptor(desc, input,
editorState);
return result;
}
}

org.eclipse.ui.internal.registry.EditorRegistry implements IEditorRegistry {

---> among maaaany other things:
private Map contentTypeToEditorMappings = new HashMap();

}

populated by the file ext/editor preference page.

cheers,

--
Laurent Mihalkovic, co-author SWT/JFace in Action (www.manning.com/scarpino)
Previous Topic:Wizard descriptions in 120 DPI
Next Topic:Negative search
Goto Forum:
  


Current Time: Thu Jul 25 13:55:58 GMT 2024

Powered by FUDForum. Page generated in 0.03687 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top