Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Modify an object from a different plugin(How to modify a particular object from outside the GMF Editor?)
Modify an object from a different plugin [message #1219420] Tue, 03 December 2013 17:59 Go to next message
Greg Dart is currently offline Greg Dart
Messages: 17
Registered: September 2013
Junior Member
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 #1220281 is a reply to message #1219420] Tue, 10 December 2013 19:53 Go to previous messageGo to next message
Greg Dart is currently offline Greg Dart
Messages: 17
Registered: September 2013
Junior Member
Guys,

I'm still struggling with this - any help/clues/guesses appreciated! To try to make myself clearer:

I'm trying to find all the instances of a particular object type and then search through the for a particular one.

How do I first of all get a list of all the instances (i.e. items on the diagram) of a particular type?

Thanks,
Greg
Re: Modify an object from a different plugin [message #1220384 is a reply to message #1220281] Wed, 11 December 2013 12:09 Go to previous messageGo to next message
Ralph Gerbig is currently offline Ralph Gerbig
Messages: 697
Registered: November 2009
Senior Member
Hi,

where do you place this code? Is it in an edit part? Do you have an instance of any object in the model you want to edit?

Ralph
Re: Modify an object from a different plugin [message #1220963 is a reply to message #1220384] Mon, 16 December 2013 11:27 Go to previous messageGo to next message
Greg Dart is currently offline Greg Dart
Messages: 17
Registered: September 2013
Junior Member
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 11:21 Go to previous message
Ralph Gerbig is currently offline Ralph Gerbig
Messages: 697
Registered: November 2009
Senior Member
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
Previous Topic:What is the problem in import GMF files in new eclipse workspace?
Next Topic:Add link first without links
Goto Forum:
  


Current Time: Wed Oct 22 23:59:48 GMT 2014

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

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