Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Create PictogramLink for Diagram object itself.(This topic discusses the possibility to link Diagram objects with business objects directly.)
Create PictogramLink for Diagram object itself. [message #735558] Wed, 12 October 2011 03:59 Go to next message
Georg Hackenberg is currently offline Georg Hackenberg
Messages: 12
Registered: October 2011
Junior Member
Dear Graphiti community,

I have been working on a graphical editor for an EMF model for some days now. The goal is to support creating and editing models of automata (containing states and transitions). So far, adding states and transitions to the diagram works fine. Now, the states need to be contained within an automaton object. The idea was to link the diagram itself with the automaton object.

The question is where and how to do that naturally inside the Graphiti framework. For example I could imagine overwriting the method creating new diagram objects and automatically adding the automaton object so that each new diagram is linked directly. Another idea I had was to provide a utility class that retrieves the automaton object from the diagram - and creates respectively links the both if necessary. Then I could call this method where ever I need the automaton object and the automaton object is created on the fly the first time.

I would be grateful to hear some other opinions on this. Maybe there is also a built-in mechanism to support this use case.

Best regards,
Georg Hackenberg
Re: Create PictogramLink for Diagram object itself. [message #735609 is a reply to message #735558] Wed, 12 October 2011 07:28 Go to previous messageGo to next message
Hernan Gonzalez is currently offline Hernan Gonzalez
Messages: 158
Registered: October 2010
Location: Buenos Aires, Argentina
Senior Member
IMHO that's a perfectly reasonable (and frequent) scenario: you have a "root" domain object, linked with the diagram, and all other domain objects are added inside it. I think the normal way is to create this root object (your "automaton") when you create the diagram. Your own code will tipically have an utility method (called tipically from a creation wizard) that creates the "full" diagram -no need to overwrite any method.
Re: Create PictogramLink for Diagram object itself. [message #753662 is a reply to message #735609] Fri, 28 October 2011 05:02 Go to previous messageGo to next message
Georg Hackenberg is currently offline Georg Hackenberg
Messages: 12
Registered: October 2011
Junior Member
Hmm, I managed to solve the problem with the following utility method:

	public static Automaton getAutomaton(Diagram diagram)
	{
		PictogramLink link = diagram.getLink();
		
		if (link == null)
		{
			System.out.println("Creating new automaton object");
			
			EditingDomain editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(diagram);
			Command command;
			
			// Create the automaton object
			Automaton automaton = IfeditFactory.eINSTANCE.createAutomaton();
			
			// Add the automaton object to the diagram resource
			command = new AddCommand(editingDomain, diagram.eResource().getContents(), automaton);
			editingDomain.getCommandStack().execute(command);
			
			// Create the pictogram link object			
			link = PictogramsFactory.eINSTANCE.createPictogramLink();
			link.getBusinessObjects().add(automaton);
			
			// Set the pictogram link object for the diagram object
			command = new SetCommand(editingDomain, diagram, PictogramsPackage.eINSTANCE.getDiagram().getEStructuralFeature(PictogramsPackage.DIAGRAM__LINK), link);
			editingDomain.getCommandStack().execute(command);
		}		
		
		if (link.getBusinessObjects().size() == 1 && link.getBusinessObjects().get(0) instanceof Automaton)
		{
			// Return the automaton object
			return (Automaton) link.getBusinessObjects().get(0);
		}
		else
		{
			throw new IllegalStateException();
		}
	}


Now, anywhere I want to access the automaton I need to use this method. The automaton is created and linked on the fly if not available yet.

This seems a pretty straight forward solution, however I would be interested in the creation wizard solution. Is there an example online where I can see how to implement that?
Re: Create PictogramLink for Diagram object itself. [message #778612 is a reply to message #753662] Fri, 13 January 2012 09:09 Go to previous messageGo to next message
Matthieu Wipliez is currently offline Matthieu Wipliez
Messages: 18
Registered: March 2010
Junior Member
Hi,

I know this topic is a bit old, but I just found myself in a similar situation, and ended up creating the link in my wizard (extends WizardNewFileCreationPage, this excerpt is from a method that overrides getInitialContents) to make it easier:

// create business object
Network network = DfFactory.eINSTANCE.createNetwork();
// ...

// create diagram
Diagram diagram = Graphiti.getPeCreateService().createDiagram("xdf", name, true);

// link diagram to network
PictogramLink link = PictogramsFactory.eINSTANCE.createPictogramLink();
link.getBusinessObjects().add(network);
diagram.setLink(link);


Then when I need to access the network in a feature, I just do the following:

Diagram diagram = getDiagram();
Network network = (Network) getBusinessObjectForPictogramElement(diagram);


Cheers
Matthieu
Re: Create PictogramLink for Diagram object itself. [message #779670 is a reply to message #778612] Mon, 16 January 2012 04:45 Go to previous message
Georg Hackenberg is currently offline Georg Hackenberg
Messages: 12
Registered: October 2011
Junior Member
Thanks Matthieu! That was exactly what I was looking for.
Previous Topic:Resolving references to external Files in Businessmodels
Next Topic:API not usable without editor in running eclipse UI?!
Goto Forum:
  


Current Time: Wed Apr 23 02:40:15 EDT 2014

Powered by FUDForum. Page generated in 0.01813 seconds