Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Creating a new Diagram
Creating a new Diagram [message #1072355] Mon, 22 July 2013 15:29 Go to next message
Thomas Thonhofer is currently offline Thomas Thonhofer
Messages: 18
Registered: July 2013
Junior Member
Hello

I have created an editor with Graphiti. So far i have only tested it with the default Graphiti Wizard, creating the standard .diagram file. Now I want to use my own wizard to create a custom file, containing a diagram and open my editor. I already have a wizard that creates a file. What I don't know is, how I should create a new diagram of my existing diagram type, use the file as a resource for it and open my editor. I already have a subclass of DiagramEditor, but I have no idea how to use it for creating my custom diagram. I didn't find any real help so far.
Below is the performFinish method of my wizard. The diagram should be created within the createDiagram method of the DiagramCreator class. I just don't know what this method should look like.

public boolean performFinish() {
		
		final String fileName = page1.getFileName();
		final IResource container = page1.getDiagramContainer();


		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) throws InvocationTargetException {
				try {

					
					IPath path = container.getFullPath().append(fileName);
					URI uri = URI.createPlatformResourceURI(path.toString(), true);
					DiagramCreator.createDiagram(uri, targetNamespace);


				} catch (CoreException e) {
					throw new InvocationTargetException(e);
				} finally {
					monitor.done();
				}
			}
		};
		try {
			getContainer().run(true, false, op);
		} catch (InterruptedException e) {
			return false;
		} catch (InvocationTargetException e) {
			Throwable realException = e.getTargetException();
			MessageDialog.openError(getShell(), "Error", realException.getMessage());
			return false;
		}
		return true;
	}


I really hope someone can help me out. If possible, some code snippets or a comprehensible example would be very appreciated.

Thank you.
Re: Creating a new Diagram [message #1072775 is a reply to message #1072355] Tue, 23 July 2013 13:20 Go to previous messageGo to next message
Andrej K is currently offline Andrej K
Messages: 26
Registered: March 2013
Location: germany
Junior Member
hey i hope I can help you and understood your question right
here a snippet:

private static String diagramEditorId="...";//you should read this two from your plugin xml file
private static String diagramTypeId ="...";//ive find out how i can open it in graphiti examples

public static void openMyEditor(...) {

// i have my own editorinput, it is handeld in my extended "DiagramEditor" class, cant write more about it 
	IEditorInput input = new myEditorInput(...); 
	String newName= "name of the new diagram which should be created";
	...
	//create an Diagram 
	final Diagram diagram = Graphiti.getCreateService().createDiagram(diagramTypeId, newName, true);
	//Create an Graphiti File on given path
	URI fileURI = URI.createFileURI("Drive:\\Users\\...\\"+newName+".diagram");
	FileService.createEmfFileForDiagram(fileURI, diagram);
	//Open this Created file in Graphiti Editor
	try {
		page.openEditor(input, diagramEditorId);
	} catch (PartInitException e) {}
	
	//here you can write a method and add some element automaticaly, it works fine
				
}
Re: Creating a new Diagram [message #1073816 is a reply to message #1072355] Thu, 25 July 2013 14:39 Go to previous messageGo to next message
Thomas Thonhofer is currently offline Thomas Thonhofer
Messages: 18
Registered: July 2013
Junior Member
Thank you for your answer.

Based on that I have created a similar way for making a file and a diagram. The file is created and the editor opens. The problem is, that the pictogram elements I want to create afterwards, are not displayed. They are added to the diagram correctly, but I can not see them.

Any ideas what the problem could be?
Re: Creating a new Diagram [message #1073825 is a reply to message #1073816] Thu, 25 July 2013 14:56 Go to previous messageGo to next message
Andrej K is currently offline Andrej K
Messages: 26
Registered: March 2013
Location: germany
Junior Member
Hi!
how do you know that they are added correctly if you cant see them?
maybe that helps
after i created new diagram i make like this:(in extra method)
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart myEditor= page.getActiveEditor();
if (myEditor instanceof myExtendedDiagramEditor ){
putThingsToEditorHandler.addElementEntityToEditor(myEditor,elemenToAdd,null);

}

in my handler i create a context and ask if I can add, if i can add so i add this element like:
CreateContext context = new CreateContext();
		context.setTargetContainer(((myExtendedDiagramEditor ) myEditor).getDiagramTypeProvider().getDiagram());
//put additional stuff if you want to context like context.putProperty(...);

if(((myExtendedDiagramEditor ) myEditor).getDiagramTypeProvider().getFeatureProvider().getCreateFeatures()[featureCall].canCreate(context)){
			((myExtendedDiagramEditor ) myEditor).getDiagramTypeProvider().getFeatureProvider().getCreateFeatures()[featureCall].create(context);
		}


"[featureCall]" is some special element/create/feature number, maybe not the best solution but it works at first...
Re: Creating a new Diagram [message #1074236 is a reply to message #1072355] Fri, 26 July 2013 10:45 Go to previous messageGo to next message
Thomas Thonhofer is currently offline Thomas Thonhofer
Messages: 18
Registered: July 2013
Junior Member
Thanks, but unfortunately this does not solve my problem.

I know that the elements are created and added, because when I ask for the children of the diagram (by using diagram.getChildren() for example), I get the elements I created before.
Re: Creating a new Diagram [message #1075346 is a reply to message #1073816] Mon, 29 July 2013 09:09 Go to previous messageGo to next message
Michael Wenz is currently offline Michael Wenz
Messages: 1590
Registered: July 2009
Location: Walldorf, Germany
Senior Member
Thomas,

can you provide some code here that shows how you add the pictogram elements
and from where you do that?

Michael
Re: Creating a new Diagram [message #1077353 is a reply to message #1072355] Thu, 01 August 2013 15:26 Go to previous messageGo to next message
Thomas Thonhofer is currently offline Thomas Thonhofer
Messages: 18
Registered: July 2013
Junior Member
I use a normal add method for my pictogram elements, similar to the graphiti tutorial. It is called from the create method when I add a new Element.

@Override
	public PictogramElement add(IAddContext context) {


		final Activity addedClass = (Activity) context.getNewObject();
		final Diagram targetDiagram = (Diagram) context.getTargetContainer();

		if (addedClass.getId() == null){
		
		Activator activator = Activator.getDefault();
		Shell shell = activator.getWorkbench().getActiveWorkbenchWindow().getShell();
		InputDialog n = new InputDialog(shell, "ID input", "Please insert ID", "", null);
		n.open();
		addedClass.setId(n.getValue());
		
		}
		
		// CONTAINER SHAPE WITH ROUNDED RECTANGLE
		final IPeCreateService peCreateService = Graphiti.getPeCreateService();
		final ContainerShape containerShape = peCreateService.createContainerShape(targetDiagram, true);

		// check whether the context has a size (e.g. from a create feature)
		// otherwise define a default size for the shape
		final int width = context.getWidth() <= 0 ? 120 : context.getWidth();
		final int height = context.getHeight() <= 0 ? 70 : context.getHeight();
		IGaService gaService = Graphiti.getGaService();
		RoundedRectangle roundedRectangle; // need to access it later

		{

			// create and set graphics algorithm
			roundedRectangle = gaService.createRoundedRectangle(containerShape, 5, 5);
			roundedRectangle.setForeground(manageColor(FOREGROUND));
			roundedRectangle.setBackground(manageColor(BACKGROUND));
			roundedRectangle.setLineWidth(2);

			gaService.setLocationAndSize(roundedRectangle, context.getX(), context.getY(), width, height);


			// create link and wire it
			link(containerShape, addedClass);



			// SHAPE WITH LINE
			{
				// create shape for line
				Shape shape = peCreateService.createShape(containerShape, false);

				// create and set graphics algorithm
				Polyline polyline =	gaService.createPolyline(shape, new int[] { 0, 20, width, 20 });
				polyline.setForeground(manageColor(FOREGROUND));
				polyline.setLineWidth(2);

			}

			{
				// create shape for text
				Shape shape = peCreateService.createShape(containerShape, false);

				// create and set text graphics algorithm
				Text text = gaService.createText(shape, addedClass.getId());
				text.setForeground(manageColor(TEXT_FOREGROUND));
				text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER ); 
				// vertical alignment has as default value "center"
				text.setFont(gaService.manageDefaultFont(getDiagram(), false, true));
				gaService.setLocationAndSize(text, -40, 20, width, 20);

				// create link and wire it
				link(shape, addedClass);
			}

			// SHAPE WITH TEXT
			{
				// create shape for text
				Shape shape = peCreateService.createShape(containerShape, false);

				// create and set text graphics algorithm
				Text text = gaService.createText(shape, "Activity");
				text.setForeground(manageColor(TEXT_FOREGROUND));
				text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER ); 
				// vertical alignment has as default value "center"
				text.setFont(gaService.manageDefaultFont(getDiagram(), false, true));
				gaService.setLocationAndSize(text, 0, 0, width, 20);

				// create link and wire it
				link(shape, addedClass);
			}

			ChopboxAnchor ca = peCreateService.createChopboxAnchor(containerShape);
			link(ca, addedClass); 

			// call the layout feature
			layoutPictogramElement(containerShape);

			return containerShape;
		}


The method basically works when I use the default graphiti wizard. The problem of the pictogram elements not being added to the canvas, only occurs when I create and open my own diagram and file.
Re: Creating a new Diagram [message #1077942 is a reply to message #1077353] Fri, 02 August 2013 10:39 Go to previous messageGo to next message
Michael Wenz is currently offline Michael Wenz
Messages: 1590
Registered: July 2009
Location: Walldorf, Germany
Senior Member
Thomas,

can you provide the code how you create the new diagram and open the editor?

Michael
Re: Creating a new Diagram [message #1079905 is a reply to message #1072355] Mon, 05 August 2013 08:13 Go to previous messageGo to next message
Thomas Thonhofer is currently offline Thomas Thonhofer
Messages: 18
Registered: July 2013
Junior Member
For opening my editor I created my own PersistencyBehavior class and a loadDiagram method. For now I don't want to save the diagram persistently. I only save the business model and create a new diagram and its pictogram elements on opening the editor. So I create the new diagram within the loadDiagram Method. After that I create the root object of my business model (called EMFContainer) and link it with the diagram.

@Override
	public Diagram loadDiagram(URI uri) {

							
		Diagram diagram = Graphiti.getPeCreateService().createDiagram("infinica_process_engine_designer", uri.toString(), true);

EMFContainer c = ContainerFactory.eINSTANCE.createEMFContainer();
				
		PictogramsFactory pictogramsFactory = PictogramsFactory.eINSTANCE;
		PictogramLink link = pictogramsFactory.createPictogramLink();
		diagram.setLink(link);
		link.getBusinessObjects().add(c);




This method is executed correctly and the editor opens, but the pictogram elements I add afterwards, don't appear. This is only a temporary solution, but I don't see, why it should not work.
Here are also my DiagramEditor, DiagramEditorInput and DiagramBehavior classes, if thats any help.

public class InfinicaEditor extends DiagramEditor
{
	public static final String DIAGRAMID = "infinica_process_engine_designer.InfinicaDiagramType";
	public static final String EDITOR_ID = "infinica-process-engine-designer.editor.InfinicaEditor";

		
	@Override
	protected DiagramEditorInput convertToDiagramEditorInput(IEditorInput input)
			throws PartInitException
	{
		if(input instanceof IURIEditorInput)
		{
			return createNewInfinicaEditorInput((IEditorSite) getSite(), (IURIEditorInput) input);
		}
		else
		{
			return super.convertToDiagramEditorInput(input);
		}
	}
		
	private DiagramEditorInput createNewInfinicaEditorInput(IEditorSite site,
			IURIEditorInput input)
	{
		URI uri = URI.createURI(input.getURI().toString());
		InfinicaDiagramEditorInput infinput = new InfinicaDiagramEditorInput(uri);

		return infinput;
	}
	
	
	@Override
	protected InfinicaDiagramBehavior createDiagramBehavior()
	{
		return new InfinicaDiagramBehavior(this);
	}
}


public final class InfinicaDiagramEditorInput extends DiagramEditorInput {

	private URI uri;
	
	public InfinicaDiagramEditorInput(URI uri)
	{
		super(uri, InfinicaEditor.DIAGRAMID);
		
		this.uri = uri;
	}
	
	@Override
	public URI getUri()
	{
		return uri;
	}
	
	@Override
	public void updateUri(URI uri)
	{
		this.uri = uri;
	}
}


public class InfinicaDiagramBehavior extends DiagramBehavior 
{
			
	public InfinicaDiagramBehavior(IDiagramContainerUI diagramContainer)
	{
		super(diagramContainer);
	}

	@Override
	protected DefaultPersistencyBehavior createPersistencyBehavior() {
		return new InfinicaPersistencyBehavior(this);
			}
	
}
Re: Creating a new Diagram [message #1080046 is a reply to message #1079905] Mon, 05 August 2013 11:59 Go to previous messageGo to next message
Michael Wenz is currently offline Michael Wenz
Messages: 1590
Registered: July 2009
Location: Walldorf, Germany
Senior Member
Thomas,

maybe this is just missing from the code snippets you sent, but where do you
add the diagram you create in loadDiagram() to the resource set/editing
domain of the editor? The diagram needs to be part of a resource the editor
knows to be able to show it.

Michael
Re: Creating a new Diagram [message #1080878 is a reply to message #1072355] Tue, 06 August 2013 13:39 Go to previous message
Thomas Thonhofer is currently offline Thomas Thonhofer
Messages: 18
Registered: July 2013
Junior Member
Thank you very much Michael,

I finnally found the cause of my problem. "a resource the editor knows" was the tip I needed. My diagram was added to a resource and an editing domain, but they had no connection to the editor. I fixed that and now it works fine.

After this problem is out of the way, I already have another one:
I used the method "link(PictogramElement, BusinessObject)" of the feature provider in a few places, in order to link my Pictogram Elements. Now I realised that it is not working anymore and I have no idea why. The method is called and executed correctly as far as I can tell, but the link, that should be created doesn't exist afterwards.
I need this method because I have non-EMF objects and it uses an independenceSolver for linking them.

So I either need to find out, why the link method of my featureProvider isn't working or I need a new method that is able to link a PictogramElement with a non-EMF object (with or without an independenceSolver)

I hope someone can help me out with this.
Thank you.

[Updated on: Tue, 06 August 2013 13:41]

Report message to a moderator

Previous Topic:Simple gradient
Next Topic:Integrating Kieler algorithms into Graphiti editors
Goto Forum:
  


Current Time: Wed Sep 03 02:25:34 GMT 2014

Powered by FUDForum. Page generated in 0.02415 seconds