Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » creating new editor
creating new editor [message #896573] Thu, 19 July 2012 00:00 Go to next message
rama hana is currently offline rama hanaFriend
Messages: 10
Registered: July 2012
Junior Member
Hello,

I have already started with graphiti refering to the tutorial. Currently I want to implement my own Graphiti editor. My graphical editor is intended for the the M-Sigma metamodel which is an extension of the Ecore metamodel.

Source code of my editor is the following:
MANIFEST.MF :
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: M-SigmaEditor
Bundle-SymbolicName: org.eclipse.graphiti.M-Sigma.Editor;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: org.eclipse.graphiti.m_sigma.editor.Activator
Require-Bundle: org.eclipse.ui,
 org.eclipse.core.runtime,
 org.eclipse.graphiti;bundle-version="0.8.2",
 org.eclipse.graphiti.ui;bundle-version="0.8.2",
 org.eclipse.core.resources;bundle-version="3.7.101",
 org.eclipse.core.runtime.source;bundle-version="3.7.0",
 org.eclipse.core.runtime.compatibility.source;bundle-version="3.2.100",
 org.eclipse.core.runtime.compatibility.registry.source;bundle-version="3.5.0",
 org.eclipse.core.runtime.compatibility.auth.source;bundle-version="3.2.200",
 org.eclipse.core.runtime.compatibility.auth;bundle-version="3.2.200",
 org.eclipse.core.runtime.compatibility;bundle-version="3.2.100",
 org.eclipse.ui.views.properties.tabbed;bundle-version="3.5.200",
 org.eclipse.graphiti.examples.common;bundle-version="0.8.2"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.6

pluguin.xml :
<?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 M-Sigma Editor"
            id="org.eclipse.graphiti.M-Sigma.Editor.MSigmadiagramType"
            name="M-Sigma Diagram Type"
            [b]type="MSigma"[/b]>
      </diagramType>
   </extension>
   <extension
         point="org.eclipse.graphiti.ui.diagramTypeProviders">
      <diagramTypeProvider
            class="org.eclipse.graphiti.m_sigma.editor.MSigmaDiagramTypeProvider"
            description="This is M-Sigma editor for the Graphiti Editor"
            id="org.eclipse.graphiti.M-Sigma.Editor.MSigmadiagramTypeProvider"
            name="M-Sigma Editor">
      </diagramTypeProvider>
   </extension>
</plugin>


MSigmaDiagramTypeProvider.java
package org.eclipse.graphiti.m_sigma.editor;
import org.eclipse.graphiti.dt.AbstractDiagramTypeProvider;
import org.eclipse.graphiti.dt.IDiagramTypeProvider;

public class MSigmaDiagramTypeProvider extends AbstractDiagramTypeProvider
		implements IDiagramTypeProvider {

	public MSigmaDiagramTypeProvider() {
		 super();
		 setFeatureProvider(new MSigmaFeatureProvider(this));

	}

}


MSigmaFeatureProvider.java
package org.eclipse.graphiti.m_sigma.editor;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.graphiti.dt.IDiagramTypeProvider;
import org.eclipse.graphiti.features.IAddFeature;
import org.eclipse.graphiti.features.ICreateFeature;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.m_sigma.features.MSigmaAddEClassFeature;
import org.eclipse.graphiti.m_sigma.features.MSigmaCreateEClassFeature;
import org.eclipse.graphiti.ui.features.DefaultFeatureProvider;

public class MSigmaFeatureProvider extends DefaultFeatureProvider {

	public MSigmaFeatureProvider(IDiagramTypeProvider dtp) {
		super(dtp);
		// TODO Auto-generated constructor stub
	}
	public IAddFeature getAddFeature(IAddContext context) {
		//is object for add request a EClass?
		if (context.getNewObject() instanceof EClass) {
		return new MSigmaAddEClassFeature(this);
		} 

		return super.getAddFeature(context);
	}
	 @Override

	    public ICreateFeature[] getCreateFeatures() {

	        return new ICreateFeature[] { new MSigmaCreateEClassFeature(this) };

	    }


MSigmaAddEClassFeature.java
package org.eclipse.graphiti.m_sigma.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.GraphicsAlgorithm;
import org.eclipse.graphiti.mm.algorithms.Polyline;
import org.eclipse.graphiti.mm.algorithms.Rectangle;
import org.eclipse.graphiti.mm.algorithms.RoundedRectangle;
import org.eclipse.graphiti.mm.algorithms.Text;
import org.eclipse.graphiti.mm.algorithms.styles.Orientation;
import org.eclipse.graphiti.mm.pictograms.BoxRelativeAnchor;
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.mm.pictograms.Shape;
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 MSigmaAddEClassFeature extends AbstractAddShapeFeature {

	 private static final IColorConstant CLASS_TEXT_FOREGROUND =

		        new ColorConstant(51, 51, 153);

		 

		    private static final IColorConstant CLASS_FOREGROUND =

		        new ColorConstant(255, 102, 0);

		 

		    private static final IColorConstant CLASS_BACKGROUND =

		        new ColorConstant(255, 204, 153);


	public MSigmaAddEClassFeature(IFeatureProvider fp) {
		super(fp);
		// TODO Auto-generated constructor stub
	}

	//The method canAdd has to check the given context and therefore it decides if a business object can be added

    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;

    }

 
    //The method add finally has to create the pictogram structure described above and has to establish the linkage to the business object
    @SuppressWarnings("deprecation")
	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));

            roundedRectangle.setBackground(manageColor(CLASS_BACKGROUND));

            roundedRectangle.setLineWidth(2);

            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);

        }

        // 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(CLASS_FOREGROUND));

            polyline.setLineWidth(2);

        }

 

        // SHAPE WITH TEXT

        {

            // create shape for text

            Shape shape = peCreateService.createShape(containerShape, false);

 

            // create and set text graphics algorithm

            Text text = gaService.createDefaultText(getDiagram(), shape,
                        addedClass.getName());

            text.setForeground(manageColor(CLASS_TEXT_FOREGROUND));

            text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER);

            text.setVerticalAlignment(Orientation.ALIGNMENT_CENTER);

            text.getFont().setBold(true);

            gaService.setLocationAndSize(text, 0, 0, width, 20);

            // create link and wire it

            link(shape, addedClass);

        }
        
     // add a chopbox anchor to the shape

        peCreateService.createChopboxAnchor(containerShape);

  

        // create an additional box relative anchor at middle-right

        final BoxRelativeAnchor boxAnchor = 

             peCreateService.createBoxRelativeAnchor(containerShape);

       
        boxAnchor.setRelativeWidth(1.0);

        boxAnchor.setRelativeHeight(0.5);


        // anchor references visible rectangle instead of invisible rectangle

        GraphicsAlgorithm roundedRectangle = null;
		boxAnchor.setReferencedGraphicsAlgorithm(roundedRectangle);

 
        // assign a graphics algorithm for the box relative anchor

        Rectangle rectangle = gaService.createRectangle(boxAnchor);

        rectangle.setFilled(true);

        gaService.setLocationAndSize(rectangle, -2 * w, -w, 2 * w, 2 * w);

        rectangle.setForeground(manageColor(CLASS_FOREGROUND));

        rectangle.setBackground(manageColor(CLASS_BACKGROUND));
 
        // add a chopbox anchor to the shape 

        peCreateService.createChopboxAnchor(containerShape);

        return containerShape;
    }
}



MSigmaCreateEClassFeature.java
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.graphiti.examples.common.ExampleUtil;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.ICreateContext;
import org.eclipse.graphiti.features.impl.AbstractCreateFeature;
import org.eclipse.graphiti.mm.pictograms.Diagram;


public class MSigmaCreateEClassFeature extends AbstractCreateFeature {

    private static final String TITLE = "Create class";

    private static final String USER_QUESTION = "Enter new class name";

    public MSigmaCreateEClassFeature(IFeatureProvider fp) {

        // set name and description of the creation feature

        super(fp, "EClass", "Create EClass");

    }

    public boolean canCreate(ICreateContext context) {

        return context.getTargetContainer() instanceof Diagram;

    }

    public Object[] create(ICreateContext context) {

        // ask user for EClass name

        String newClassName = ExampleUtil.askString(TITLE, USER_QUESTION, "");

        if (newClassName == null || newClassName.trim().length() == 0) {

            return EMPTY;

        }

        // create EClass

        EClass newClass = EcoreFactory.eINSTANCE.createEClass();

        // Add model element to resource.

        // We add the model element to the resource of the diagram for

        // simplicity's sake. Normally, a customer would use its own

        // model persistence layer for storing the business model separately.

        getDiagram().eResource().getContents().add(newClass);

        newClass.setName(newClassName);
 

        // do the add

        addGraphicalRepresentation(context, newClass);

        // return newly created business object(s)

        return new Object[] { newClass };

    }


Which seems strange, is when I have run the project, and during the creation of graphiti diagram, I didn't find the type that I have defined above ("MSigma"). There is only the types "tutorial" and "mytutorial" (type that I have created for the tutorial) as options. Hence, I couldn't create a diagram of type "MSigma". Is there any missing thing? What should I doing ?

Thanks in advance.

[Updated on: Sat, 21 July 2012 14:28]

Report message to a moderator

Re: creating new editor [message #897089 is a reply to message #896573] Sat, 21 July 2012 15:33 Go to previous messageGo to next message
rama hana is currently offline rama hanaFriend
Messages: 10
Registered: July 2012
Junior Member
Hello all,

Still no answer to my question ?

I have thought that I will have more than answer, because all the questions in the forum show that all the members have reached advanced steps in the development with graphiti.
Normally, the tutorial given should be supported by the forum (because there is no other graphiti tutorial anywhere). However, I couldn't have the solutions to the issues that I have always faced.

Let's help each other, so that everyone get involved in the community of this new framework of modeling.

So, is there any answer ?
Re: creating new editor [message #897091 is a reply to message #897089] Sat, 21 July 2012 15:44 Go to previous messageGo to next message
Aljoscha Hark is currently offline Aljoscha HarkFriend
Messages: 24
Registered: March 2012
Junior Member
i think: in your plugin.xml where you added diagramTypeProvider, you have to add the created diagramType to your diagramTypeProvider, such that
<plugin>
   <extension
         point="org.eclipse.graphiti.ui.diagramTypes">
      <diagramType
            description="This is the diagram type for M-Sigma Editor"
            id="org.eclipse.graphiti.M-Sigma.Editor.MSigmadiagramType"
            name="M-Sigma Diagram Type"
            type="MSigma">
      </diagramType>
   </extension>
   <extension
         point="org.eclipse.graphiti.ui.diagramTypeProviders">
      <diagramTypeProvider
            class="org.eclipse.graphiti.m_sigma.editor.MSigmaDiagramTypeProvider"
            description="This is M-Sigma editor for the Graphiti Editor"
            id="org.eclipse.graphiti.M-Sigma.Editor.MSigmadiagramTypeProvider"
            name="M-Sigma Editor">
         <diagramType id="org.eclipse.graphiti.M-Sigma.Editor.MSigmadiagramType"/>
      </diagramTypeProvider>
   </extension>
</plugin>


so that the framework knows, which diagram types can be edited by the diagram type provider.

greetings,
aljoscha
Re: creating new editor [message #897096 is a reply to message #897091] Sat, 21 July 2012 17:07 Go to previous messageGo to next message
rama hana is currently offline rama hanaFriend
Messages: 10
Registered: July 2012
Junior Member
Thank you aljoscha Smile

You are right, I have added it. But, always samething, I haven't found the type that I have defined "MSigma" Crying or Very Sad
Re: creating new editor [message #897099 is a reply to message #896573] Sat, 21 July 2012 18:37 Go to previous message
Aljoscha Hark is currently offline Aljoscha HarkFriend
Messages: 24
Registered: March 2012
Junior Member
do you see anything related in the error log?

i dont know if this could be a possible error, but personally i would avoid "-" characters inside identifiers in the plugin.xml...

the attached "minimal" project works fine and will showup a "org.eclipse.graphiti.examples.minimal.type" entry in the wizard. perhaps this could help...
Previous Topic:programmatic connection creation
Next Topic:Creating Connections Programtically
Goto Forum:
  


Current Time: Thu Apr 25 06:03:37 GMT 2024

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

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

Back to the top