Adding an Editor Action that auto-loads a specific resource [message #418228] |
Mon, 07 April 2008 12:35 |
Felix Dorner Messages: 676 Registered: July 2009 |
Senior Member |
|
|
Hey,
my plugin wants to contribut to the UML Editor Plugin by offering a uml model library, which may be used by clients in their
own models. I want to add an Editor Action that does load this library, just like you normally do by "loadResource.." (I didn't find
the code that's invoked by the "LoadResource.." Dialog, otherwise it'd probably be just copy´n paste.
I think it's more a general EMF/Resource question so I post here. I somehow got it working, but would like to have some
feedback if my code is .. well okay.. (I have bad feelings...) I added some comments inline why I did certain things the way they are.
Thanks for any comments,
Felix
public class LoadLibraryAction implements IEditorActionDelegate{
protected IEditorPart editor;
@Override
public void run(IAction action) {
ResourceSet resource = getResourceSet();
Resource res = resource.createResource(URI.createPlatformPluginURI("/allure/model/allure.uml ", false));
try {
res.load(null); // without this, the resource appeared in the editor, but It was just a single element, not a tree.
} catch (IOException e){
e.printStackTrace();
}
}
//...
// the action can only be invoked if a "Model" element is selected.
// returns the resourceset that the client model is part of.
private ResourceSet getResourceSet(){
ISelectionProvider provider = editor.getEditorSite().getSelectionProvider();
IStructuredSelection selection = (IStructuredSelection) provider.getSelection();
// this cast is safe. the action is only enabled if a Model object
// is selected. see the action declaration in plugin.xml
return ((EObject) selection.getFirstElement()).eResource().getResourceSet();
}
}
|
|
|
Re: Adding an Editor Action that auto-loads a specific resource [message #418230 is a reply to message #418228] |
Mon, 07 April 2008 12:49 |
Ed Merks Messages: 33142 Registered: July 2009 |
Senior Member |
|
|
Felix,
Comments below.
Felix Dorner wrote:
> Hey,
>
> my plugin wants to contribut to the UML Editor Plugin by offering a
> uml model library, which may be used by clients in their
> own models. I want to add an Editor Action that does load this
> library, just like you normally do by "loadResource.." (I didn't find
> the code that's invoked by the "LoadResource.." Dialog, otherwise it'd
> probably be just copy´n paste.
Ctrl-Shift-T LoadResourceActionw ill find it.
>
> I think it's more a general EMF/Resource question so I post here. I
> somehow got it working, but would like to have some
> feedback if my code is .. well okay.. (I have bad feelings...) I added
> some comments inline why I did certain things the way they are.
>
> Thanks for any comments,
>
> Felix
>
> public class LoadLibraryAction implements IEditorActionDelegate{
>
> protected IEditorPart editor;
>
> @Override
> public void run(IAction action) {
> ResourceSet resource = getResourceSet();
> Resource res =
> resource.createResource(URI.createPlatformPluginURI("/allure/model/allure.uml ",
> false));
It's better to use "true" so that it's demand loaded too.
> try {
> res.load(null); // without this, the resource appeared in
> the editor, but It was just a single element, not a tree.
> } catch (IOException e){
> e.printStackTrace();
It's better to log exceptions that just to print them.
>
> }
> }
>
> //...
> // the action can only be invoked if a "Model" element is selected.
> // returns the resourceset that the client model is part of.
> private ResourceSet getResourceSet(){
> ISelectionProvider provider =
> editor.getEditorSite().getSelectionProvider();
The editor will implement IEditingDomainProvider which allows you to get
at the ending domain and hence at the resource set even if there is no
selection...
> IStructuredSelection selection = (IStructuredSelection)
> provider.getSelection();
> // this cast is safe. the action is only enabled if a Model
> object
> // is selected. see the action declaration in plugin.xml
> return ((EObject)
> selection.getFirstElement()).eResource().getResourceSet();
> }
>
> }
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Powered by
FUDForum. Page generated in 0.02847 seconds