Modify an object from a different plugin [message #1219420] |
Tue, 03 December 2013 12:59  |
Eclipse User |
|
|
|
Hi,
We have a model in which the first few objects in the hierarchy can only be single instance. We need to modify (e.g change the name) of one of the elements. I think the code we need is similar to:
Client client = (Client) ContractSpecEditorPackage.eINSTANCE.getContact_Spec_Editor_The_client();
TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(client);
CommandStack stack = domain.getCommandStack();
// execute some change, just as in any EMF.Edit application
Command cmd = domain.createCommand(SetCommand.class,
new CommandParameter(client, ContractSpecEditorPackage.Literals.NODE__NAME, "Burt"));
stack.execute(cmd);
The problem is we get a runtime error such that the EReference can't be cast to the "Client" object.
I guess we need to somehow get the actual instance of the "client" object from the model.
How??????
Is there some way in GMF to get the root object of the model and then navigate from there for example?
Thanks,
Greg
|
|
|
|
|
Re: Modify an object from a different plugin [message #1220963 is a reply to message #1220384] |
Mon, 16 December 2013 06:27   |
Eclipse User |
|
|
|
Hi Ralph,
Thanks for the reply - I finally figured something out, although I'm not sure this is the best way to achieve what we want. This code (below) is called as a result of a right-click menu action in a custom plug-in UI component view (It's actually a treeview).
My basic approach is:
1) get the current GMF editor (and make sure it's the one we were expecting)
2) get ALL the elements in that editor
3) search through them ALL until we find the type we are looking for
4) as we know there's only ever one instance of that type, go ahead and use the command stack to change it's name.
My concern is still that if the diagram gets big, this could be quite a slow process?
This is my working code, called from a menu handler - any suggestions/improvements gratefully received.
Thanks,
Greg
public Object execute(ExecutionEvent event) throws ExecutionException {
// TODO Auto-generated method stub
IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (editorPart instanceof ContractSpecEditorDiagramEditor) {
System.out.println("Found Editor");
ContractSpecEditorDiagramEditor cSEDE = (ContractSpecEditorDiagramEditor) editorPart;
TreeIterator<EObject> allContents = cSEDE.getDiagram().eAllContents();
while (allContents.hasNext()) {
EObject next = allContents.next();
if (next instanceof NodeImpl) {
EObject obj = ((NodeImpl)next).getElement();
if (obj instanceof ClientImpl){
ClientImpl client = (ClientImpl) obj;
System.out.println("Found Client: " + client.getName());
EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(client);
Command cmnd = SetCommand.create(editingDomain, client, ContractSpecEditorPackage.eINSTANCE.getNode_Name(), "Bob");
try {
editingDomain.getCommandStack().execute(cmnd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("New Name is: " + client.getName());
}
}
}
}
return null;
}
}
|
|
|
Re: Modify an object from a different plugin [message #1222013 is a reply to message #1220963] |
Thu, 19 December 2013 06:21  |
Eclipse User |
|
|
|
Hi,
the question is how big, big is. I think if you get real big models you need to start tweaking your GMF editor anyway. What you could do is to build a hash map that caches the model and do your search / build up the cache if you do not find in this cache what you are looking for.
Ralph
|
|
|
Powered by
FUDForum. Page generated in 0.05442 seconds