Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Creating Connections Programtically(Creating Connections Programtically)
Creating Connections Programtically [message #896908] Fri, 20 July 2012 12:00 Go to next message
Surya Kathayat is currently offline Surya KathayatFriend
Messages: 44
Registered: July 2012
Member
Hi,

Similar topic has been discussed previously in here[http://www.eclipse.org/forums/index.php/m/896890/#msg_896890]. However I am not able to get it right. Pointers will be highly appreciatd.

Problem Description:

My EMF based domain model (partial) is here index.php/fa/10783/0/.
I have Task nodes and tasks are connected by Links, sounds simple!!

I can create Tasks nodes and show them on Graphiti editor programatically but the problem is in creating links between tasks.

In the following code (within a command that will be executed from some context menu in UI - eclipse RCP), I am trying to create two Task nodes, and a Link to connect these two tasks.

@Override
	protected void doExecute() {
		
		// Get all Tasks and links to the given diagram
		List tasks = new ArrayList();
		OrderedTask t1  = IdmmodelFactory.eINSTANCE.createOrderedTask();
		t1.setName("t1");
		tasks.add(t1);
		OrderedTask t2  = IdmmodelFactory.eINSTANCE.createOrderedTask();
		t2.setName("t2");
		tasks.add(t2);

		List links = new ArrayList();
		OrderLink l1 = IdmmodelFactory.eINSTANCE.createOrderLink();
		l1.setName("");
		l1.setTarget(t2);
		t1.getOutgoingLinks().add(l1);
		links.add(l1);
		
		// Create the diagram and its file
		Diagram diagram = Graphiti.getPeCreateService().createDiagram("sapidmtaskflow", diagramName, true); 		
		diagramFile = createResourceFile(diagramName) ; 
		
		URI uri = URI.createPlatformResourceURI(diagramFile.getFullPath().toString(), true);
		createdResource = editingDomain.getResourceSet().createResource(uri);
		createdResource.getContents().add(diagram);
		
		// create/get resource and save diagram to it
		IDiagramTypeProvider dtp = GraphitiUi.getExtensionManager().createDiagramTypeProvider(diagram,
				"com.sap.idm.ic.rcp.graphiti.diagrams.TaskFlowDiagramTypeProvider");
		IFeatureProvider featureProvider = dtp.getFeatureProvider();

		// Add all tasks to diagram
		int x = 20;
		int y = 20;
		for (int i = 0; i < tasks.size(); i++) {
			// Create the context information
			AddContext addContext = new AddContext();
			addContext.setNewObject(tasks.get(i));
			addContext.setTargetContainer(diagram);
			addContext.setX(x);
			addContext.setY(y);
			x = x + 20;
			y = y + 20;

			IAddFeature addFeature = featureProvider.getAddFeature(addContext);
			if (addFeature.canAdd(addContext)) {
				addFeature.add(addContext);
			}
		}
		

                 // if I commenteded out rest of the code, I can see two task nodes on the editor..fine
                 // if I add the following code, the following exception occurs...PROBLEM!!! 
                 // org.eclipse.emf.common.util.WrappedException: An exception was ignored during command execution
		PictogramLink plink = PictogramsFactory.eINSTANCE.createPictogramLink();
		diagram.setLink(plink);
		
		for (int i = 0; i < links.size(); i++) {
			// Create the context information
			OrderLink link = (OrderLink) links.get(i);
			
			plink.getBusinessObjects().add(link);

			CreateConnectionContext createContext = new CreateConnectionContext();
			
			Task src = link.getSource();
			Task tar = link.getTarget();
			
			ICreateConnectionFeature[] createCFeatureList = featureProvider.getCreateConnectionFeatures();
			ICreateConnectionFeature createCFeature = createCFeatureList[0];
			
			EList<PictogramLink> plinks = diagram.getPictogramLinks();
			
			createContext.setSourcePictogramElement(getAnchorFromPE(plinks, src));
			createContext.setSourceAnchor(createContext.getSourceAnchor());
			createContext.setTargetPictogramElement(getAnchorFromPE(plinks, tar));
			createContext.setTargetAnchor(createContext.getTargetAnchor());
			if(createCFeature.canCreate(createContext)){
				createCFeature.create(createContext);
			}
		}
	}

	
	private PictogramElement getAnchorFromPE(List plinks, Task task){
		
		PictogramElement a = null;
		
		for (int i = 0; i < plinks.size(); i++) {
			
			PictogramLink p = (PictogramLink) plinks.get(i);
			
			List list = p.getBusinessObjects();
			
			for (int j = 0; j < list.size(); j++) {
				EObject eobj = (EObject) list.get(i);				
				if(eobj instanceof Task){
					Task t = (Task)eobj;
					if(t.getName().equalsIgnoreCase(task.getName())){
						return p.getPictogramElement();
					}
				}
			}
			
		}
		
		return a;
		
	}


-Surya
  • Attachment: tasklink.bmp
    (Size: 388.57KB, Downloaded 599 times)
  • Attachment: tasklink.gif
    (Size: 23.25KB, Downloaded 234 times)
Re: Creating Connections Programtically [message #896951 is a reply to message #896908] Fri, 20 July 2012 13:55 Go to previous messageGo to next message
Surya Kathayat is currently offline Surya KathayatFriend
Messages: 44
Registered: July 2012
Member
I found the solution, thanks to Michael Wenz!!

    ContainerShape srcContainerShape = (ContainerShape) getAnchorFromPE(plinks, src);
    ContainerShape tarContainerShape = (ContainerShape) getAnchorFromPE(plinks, tar);
			
    createContext.setSourcePictogramElement(srcContainerShape);
    createContext.setSourceAnchor(srcContainerShape.getAnchors().get(0));
    createContext.setTargetPictogramElement(tarContainerShape);
    createContext.setTargetAnchor(tarContainerShape.getAnchors().get(0));

Re: Creating Connections Programtically [message #896975 is a reply to message #896951] Fri, 20 July 2012 15:53 Go to previous messageGo to next message
Aljoscha Hark is currently offline Aljoscha HarkFriend
Messages: 24
Registered: March 2012
Junior Member
the code from post #1 seems strange at different places:

- why do you add all the links to the PictogramLink of the diagram? _normally_ i expect that there is only one business object for each shape.
- it seems unusual using create features in an "initialize diagram command" - as stated in [1]. the model elements are already created and (i think) in the ICreateConnectionFeature "createCFeature" it will be created again. so you will have two links between the tasks (in the BO model).

perhaps the snippet in [1] will help: there, "getElement()" would be the element, you add your tasks into (as analogy) - the rest is done by the framework i think.

greets,
aljoscha

[1] [http] ://www.eclipse.org/forums/index.php/t/278904/
Re: Creating Connections Programtically [message #897226 is a reply to message #896975] Mon, 23 July 2012 09:37 Go to previous messageGo to next message
Surya Kathayat is currently offline Surya KathayatFriend
Messages: 44
Registered: July 2012
Member
HI Aljoscha,

Thanks for the response. Your first point seems to be correct.

Could you re-check the reference [1], I could not open that link.

-Surya

Re: Creating Connections Programtically [message #897235 is a reply to message #896908] Mon, 23 July 2012 10:39 Go to previous message
Aljoscha Hark is currently offline Aljoscha HarkFriend
Messages: 24
Registered: March 2012
Junior Member
just add the "http://" because i am not allowed to post links yet.

http://www.eclipse.org/forums/index.php/mv/msg/278904/896972/#msg_896972

sorry, should work now Smile

[Updated on: Mon, 23 July 2012 10:39]

Report message to a moderator

Previous Topic:creating new editor
Next Topic:Setting Auto/Default layout in the diagram
Goto Forum:
  


Current Time: Fri Apr 26 12:36:59 GMT 2024

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

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

Back to the top