Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Creating new diagram based on my project
Creating new diagram based on my project [message #1062672] Mon, 10 June 2013 10:30 Go to next message
John Cole is currently offline John ColeFriend
Messages: 66
Registered: June 2013
Member
Hello everyone,

I try to experiment with Graphiti, before starting a real project.
I've created a new plug-in project like described in the Graphiti-tutorial with the following steps:


Created new plug-in project with the name org.eclipse.graphiti.test.
Referenced the following plugins:
org.eclipse.graphiti, org.eclipse.graphiti.ui, org.eclipse.graphiti.examples.common, org.eclipse.core.resources, org.eclipse.core.runtime, org.eclipse.ui.views.properties.tabbed


Created a new class (ProjectDiagramTypeProvider) and implemented it as follows:
package org.eclipse.graphiti.test.diagram;

import org.eclipse.graphiti.dt.AbstractDiagramTypeProvider;

public class ProjectDiagramTypeProvider extends AbstractDiagramTypeProvider {
	
	public ProjectDiagramTypeProvider() {
		super();
	}
}


Created a new XML document plugin.xml and registered the Diagram Type Provider and Diagram Type as you can see in the following code:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
  <extension
      point="org.eclipse.graphiti.ui.diagramTypes">
    <diagramType
      description="This is the diagram type for my Graphiti project"
      id="org.eclipse.graphiti.test.diagram.ProjectDiagramType"
      name="My Graphiti Project Diagram Type"
      type="project">
    </diagramType>
  </extension>
 
  <extension
      point="org.eclipse.graphiti.ui.diagramTypeProviders">
    <diagramTypeProvider
      class="org.eclipse.graphiti.test.diagram.ProjectDiagramTypeProvider"
      description="This is my first editor for the Graphiti Project"
      id="org.eclipse.graphiti.test.diagram.MyTutorialDiagramTypeProvider"
      name="My project editor">
      <diagramType
        id="org.eclipse.graphiti.test.diagram.ProjectDiagramType">
      </diagramType>
    </diagramTypeProvider>
  </extension>
</plugin>


Created a Feature Provider with the following code:
package org.eclipse.graphiti.test.diagram;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.graphiti.dt.IDiagramTypeProvider;
import org.eclipse.graphiti.features.IAddFeature;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.test.features.ProjectAddEClassFeature;
import org.eclipse.graphiti.ui.features.DefaultFeatureProvider;

public class ProjectFeatureProvider extends DefaultFeatureProvider {

	public ProjectFeatureProvider(IDiagramTypeProvider dtp) {
		super(dtp);
	}
}


Made the ProjectFeatureProvider known to the ProjectDiagramProvider, with the following implementation:
package org.eclipse.graphiti.test.diagram;

import org.eclipse.graphiti.dt.AbstractDiagramTypeProvider;

public class ProjectDiagramTypeProvider extends AbstractDiagramTypeProvider {
	
	public ProjectDiagramTypeProvider() {
		super();
		// set the feature provider
		setFeatureProvider(new ProjectFeatureProvider(this));
	}
}


Created a Add Feature with a simple RoundedRectangle (without text and a line), see the following code:
package org.eclipse.graphiti.test.features;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.features.impl.AbstractAddShapeFeature;
import org.eclipse.graphiti.mm.algorithms.RoundedRectangle;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.Diagram;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.graphiti.services.IGaService;
import org.eclipse.graphiti.services.IPeCreateService;
import org.eclipse.graphiti.util.ColorConstant;
import org.eclipse.graphiti.util.IColorConstant;

public class ProjectAddEClassFeature extends AbstractAddShapeFeature {
    private static final IColorConstant CLASS_FOREGROUND =
        new ColorConstant(255, 102, 0);
    public ProjectAddEClassFeature(IFeatureProvider fp) {
        super(fp);
    }
    public boolean canAdd(IAddContext context) {
        // check if user wants to add a EClass
        if (context.getNewObject() instanceof EClass) {
                       // check if user wants to add to a diagram
            if (context.getTargetContainer() instanceof Diagram) {
                return true;
            }
        }
        return false;
    }
    public PictogramElement add(IAddContext context) {
        EClass addedClass = (EClass) context.getNewObject();
        Diagram targetDiagram = (Diagram) context.getTargetContainer();
        
        // CONTAINER SHAPE WITH ROUNDED RECTANGLE
        IPeCreateService peCreateService = Graphiti.getPeCreateService();
        ContainerShape containerShape =
             peCreateService.createContainerShape(targetDiagram, true);
        // define a default size for the shape
        int width = 100;
        int height = 50; 
        IGaService gaService = Graphiti.getGaService();
        {
            // create and set graphics algorithm
            RoundedRectangle roundedRectangle =
                gaService.createRoundedRectangle(containerShape, 5, 5);
            roundedRectangle.setForeground(manageColor(CLASS_FOREGROUND));
            // set location
            gaService.setLocationAndSize(roundedRectangle,
                context.getX(), context.getY(), width, height);
            // if added Class has no resource we add it to the resource 
            // of the diagram
            // in a real scenario the business model would have its own resource
            if (addedClass.eResource() == null) {
                     getDiagram().eResource().getContents().add(addedClass);
            }
            // create link and wire it
            link(containerShape, addedClass);
        }
        return containerShape;
    }
}


Added a getAddFeature() method to the class ProjectFeatureProvider like this:
package org.eclipse.graphiti.test.diagram;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.graphiti.dt.IDiagramTypeProvider;
import org.eclipse.graphiti.features.IAddFeature;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.test.features.ProjectAddEClassFeature;
import org.eclipse.graphiti.ui.features.DefaultFeatureProvider;

public class ProjectFeatureProvider extends DefaultFeatureProvider {

	public ProjectFeatureProvider(IDiagramTypeProvider dtp) {
		super(dtp);
	}
	
	@Override
	public IAddFeature getAddFeature(IAddContext context) {
	       //is object for add request a EClass?
	       if (context.getNewObject() instanceof EClass) {
	           return new ProjectAddEClassFeature(this);
	       }
	       return super.getAddFeature(context);
	}
}


Tryed to create a new graphiti diagram to test my newly created editor: Right click on the project folder > New > Other... > Graphiti Diagram) but unfortunately I can't choose my own diagram type because it is not listed in the selection (see Screenshot).

Have I done something in a wrong wrong?

By the way, I just want to create rectangles in my diagram with anchors on left and right side and connect them with EReferences.

I am grateful for any help!
  • Attachment: screen.png
    (Size: 27.46KB, Downloaded 265 times)
Re: Creating new diagram based on my project [message #1062855 is a reply to message #1062672] Tue, 11 June 2013 06:19 Go to previous messageGo to next message
John Cole is currently offline John ColeFriend
Messages: 66
Registered: June 2013
Member
// please ignore

[Updated on: Tue, 11 June 2013 08:49]

Report message to a moderator

Re: Creating new diagram based on my project [message #1062876 is a reply to message #1062672] Tue, 11 June 2013 08:48 Go to previous message
John Cole is currently offline John ColeFriend
Messages: 66
Registered: June 2013
Member
Finaly I found it out. I just had to start the plug-in project as a new Eclipse application. There I can choose the corresponding diagram type. Smile
Previous Topic:Generate Diagram from Domain model file
Next Topic:Diagrams without resource for layout data
Goto Forum:
  


Current Time: Thu Apr 25 21:33:34 GMT 2024

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

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

Back to the top