Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Iterating GMF diagram
Iterating GMF diagram [message #725051] Tue, 13 September 2011 19:39 Go to next message
janpojer is currently offline janpojerFriend
Messages: 40
Registered: July 2011
Member
Hi,

I am trying to iterate through elements modeled in diagram. What is the best way to approach that?

Thanks.

Jan
Re: Iterating GMF diagram [message #725055 is a reply to message #725051] Tue, 13 September 2011 19:55 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33216
Registered: July 2009
Senior Member
Resource.getAllContents can be quite handy.


On 13/09/2011 12:39 PM, janpojer wrote:
> Hi,
>
> I am trying to iterate through elements modeled in diagram. What is
> the best way to approach that?
>
> Thanks.
>
> Jan


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Iterating GMF diagram [message #725056 is a reply to message #725055] Tue, 13 September 2011 20:13 Go to previous messageGo to next message
janpojer is currently offline janpojerFriend
Messages: 40
Registered: July 2011
Member
But does that get the actual modelled element (if i want to erase it)?
Re: Iterating GMF diagram [message #725059 is a reply to message #725056] Tue, 13 September 2011 20:18 Go to previous messageGo to next message
janpojer is currently offline janpojerFriend
Messages: 40
Registered: July 2011
Member
This is how I iterate the diagram resource:

try {
	for (Iterator<Object> iter = EcoreUtil.getAllContents(res, true); iter.hasNext();) {
		EObject eObject = (EObject) iter.next();
		sw.doSwitch(eObject);
	}
} catch (RuntimeException e) {
	FnDiagramEditorPlugin.getInstance().logError("Element initialization failed", e); 
}


And I am trying to figure out how to remove certain items from the resource (or add) based on some external conditions.

Thanks
Re: Iterating GMF diagram [message #725073 is a reply to message #725056] Tue, 13 September 2011 21:19 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33216
Registered: July 2009
Senior Member
The graphical elements have references to the underlying model; it's not
clear if you keep both in the same resource or in separate resources.
You'll want to use commands if you're planning to make changes to the
diagram or the model.


On 13/09/2011 1:13 PM, janpojer wrote:
> But does that get the actual modelled element (if i want to erase it)?
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Iterating GMF diagram [message #725082 is a reply to message #725073] Tue, 13 September 2011 21:48 Go to previous messageGo to next message
janpojer is currently offline janpojerFriend
Messages: 40
Registered: July 2011
Member
I have only one diagram file, so i persume that all necessary info is in one resource file. I am able to create custom command. What i struggle with is to get the proper view of the iterated elements from resource file so i can pass that to the command and erase it.
Re: Iterating GMF diagram [message #725087 is a reply to message #725082] Tue, 13 September 2011 21:58 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33216
Registered: July 2009
Senior Member
So the code you're show will definitely visit every diagram element and
also every element of your model. Looking at the serialized XMI should
give you a good sense of the hierarchical structure over which you're
iterating...


On 13/09/2011 2:48 PM, janpojer wrote:
> I have only one diagram file, so i persume that all necessary info is
> in one resource file. I am able to create custom command. What i
> struggle with is to get the proper view of the iterated elements from
> resource file so i can pass that to the command and erase it.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Iterating GMF diagram [message #725088 is a reply to message #725087] Tue, 13 September 2011 22:30 Go to previous messageGo to next message
janpojer is currently offline janpojerFriend
Messages: 40
Registered: July 2011
Member
Right, I can see the structure... i have there the emf domain elements:
core.fn.impl.RequiredPortImpl@1c4fe21 (name: Challenge/Response CAS/DME_Signal_RQ_0, id: 996f0adc-2337-4524-976c-c8264d11978f)

and the gmf elements (the notation):
org.eclipse.gmf.runtime.notation.impl.NodeImpl@15687d9 (visible: true, type: 2003, mutable: false)

My question is how to remove both the notation and the domain element at once.

Or in oder words how to remove the domain element along with its gmf notation in the diagram...

[Updated on: Tue, 13 September 2011 22:31]

Report message to a moderator

Re: Iterating GMF diagram [message #725089 is a reply to message #725088] Tue, 13 September 2011 22:39 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33216
Registered: July 2009
Senior Member
Your question is slowly evolving...

I'm not an expert on how GMF is implemented. I imagine there's some
type of remove/delete command (as there is in EMF.Edit) that's invoked
when you do this the normal way in the generated editor. You'll need to
learn how to create such commands and execute them in the editor's
command stack. Maybe it's better if you ask such a question directly
(after looking at documentation and using Google) rather than as part of
an evolving "iterating GMF diagram" question...


On 13/09/2011 3:30 PM, janpojer wrote:
> Right, I can see the structure... i have there the emf domain elements:
> core.fn.impl.RequiredPortImpl@1c4fe21 (name: Challenge/Response
> CAS/DME_Signal_RQ_0, id: 996f0adc-2337-4524-976c-c8264d11978f)
> and the gmf elements the notation:
> org.eclipse.gmf.runtime.notation.impl.NodeImpl@15687d9 (visible: true,
> type: 2003, mutable: false)
> My question is how to remove both the notation and the domain element
> at once.
>
> Or in oder words how to remove the domain element along with its gmf
> notation in the diagram...


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Iterating GMF diagram [message #725093 is a reply to message #725089] Tue, 13 September 2011 22:59 Go to previous messageGo to next message
janpojer is currently offline janpojerFriend
Messages: 40
Registered: July 2011
Member
Thanks,

I tried, but noone answered so i tried more global approach.

Re: Iterating GMF diagram [message #725094 is a reply to message #725093] Tue, 13 September 2011 23:09 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33216
Registered: July 2009
Senior Member
Helpful people seem to be in short supply around here. Best you try to
help yourself by looking at how things work under the covers.


On 13/09/2011 3:59 PM, janpojer wrote:
> Thanks,
>
> I tried, but noone answered so i tried more global approach.
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Iterating GMF diagram [message #725200 is a reply to message #725059] Wed, 14 September 2011 08:14 Go to previous messageGo to next message
Mickael Istria is currently offline Mickael IstriaFriend
Messages: 865
Registered: July 2009
Location: Grenoble, France
Senior Member

On 13/09/2011 22:18, janpojer wrote:
> This is how I iterate the diagram resource:
>
>
> try {
> for (Iterator<Object> iter = EcoreUtil.getAllContents(res, true);
> iter.hasNext();) {
> EObject eObject = (EObject) iter.next();
> sw.doSwitch(eObject);
> }
> } catch (RuntimeException e) {
> FnDiagramEditorPlugin.getInstance().logError("Element initialization
> failed", e); }


I think you'd rather first get your root semantic element by retrieving
it from DiagramEditPart.resolveSemanticElement() - you can access the
DiagramEditPart either using iterating on getParent from any EditPart,
either by retrieving it from your GraphicalViewer - and then process it.

> And I am trying to figure out how to remove certain items from the
> resource (or add) based on some external conditions.

You should use the DeleteCommand for that, otherwise, you may cause
inconsistencies.

If you are trying to achieve it without opening an Editor, you should
use the OffscreenEditPartFactory that allows you to perform "normal" GMF
operations without seeing an editor open.

--
http://mickaelistria.wordpress.com
http://twitter.com/#!/mickaelistria
http://www.petalslink.com
Re: Iterating GMF diagram [message #725451 is a reply to message #725200] Wed, 14 September 2011 20:23 Go to previous messageGo to next message
janpojer is currently offline janpojerFriend
Messages: 40
Registered: July 2011
Member
Thnaks for hints. This is what i managed to put together. it works as expected - meaning that it deletes the particular element.

/**get the graphical viewer*/
		IWorkbenchPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
		IDiagramWorkbenchPart diagramPart = (IDiagramWorkbenchPart) part.getAdapter(IDiagramWorkbenchPart.class);
		DiagramEditPart diagramEditPart = diagramPart.getDiagramEditPart();
		
		Diagram diagram = diagramEditPart.getDiagramView();
		
		List<NodeImpl> nodesToDelete = new ArrayList<NodeImpl>();
		for (Object v : diagram.getChildren()) {
			NodeImpl node = (NodeImpl) v;
			for (Object o : node.getChildren()) {
				if (o instanceof NodeImpl) {
					NodeImpl chNode = (NodeImpl) o;
					EObject obj = chNode.basicGetElement();
					if (obj instanceof ProvidedPort) {
						ProvidedPort pp = (ProvidedPort) obj;
						if (pp.getName().equals("Challenge/Response CAS/DME_Signal_RQ_2")) {
							nodesToDelete.add(chNode);
						}
					}
				}
			}
		}
		
		CommandStack stack = viewer.getEditDomain().getCommandStack();
		Command deleteViewCommand = new ICommandProxy(new DeleteCommand( TransactionUtil.getEditingDomain(toDelete), nodesToDelete.get(0)));
		stack.execute(deleteViewCommand);


But!

It only deletes its notation (the element on screen). And not the domain model record. Do I really now have to call
EcoreUtil.remove(EObject);
on the particular object?

Isnt there a way to do that all together? Also when i call the remove method it gives me write transaction error. I have to enclose the remove call into specific block?

Thanks for the final help...

Re: Iterating GMF diagram [message #725472 is a reply to message #725451] Wed, 14 September 2011 21:08 Go to previous message
janpojer is currently offline janpojerFriend
Messages: 40
Registered: July 2011
Member
Ok,
so I got it. All what needs to be done is to remove the underlaying domain model element with following:
                DeleteAction del = new DeleteAction( TransactionUtil.getEditingDomain(toDelete), true);
		org.eclipse.emf.common.command.Command dCmd =  del.createCommand(objToDelete);
		TransactionUtil.getEditingDomain(toDelete).getCommandStack().execute(dCmd);

And it will remove all needed dependencies.

Thanks to all that helped me out.

[Updated on: Wed, 14 September 2011 21:08]

Report message to a moderator

Previous Topic:Views+GMF editor on a webbrowser
Next Topic:Create Diagram Node from code
Goto Forum:
  


Current Time: Fri Sep 20 21:29:56 GMT 2024

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

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

Back to the top