Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Problem with EReference
Problem with EReference [message #1384979] Tue, 03 June 2014 08:42
hafidhi imen is currently offline hafidhi imen
Messages: 8
Registered: April 2014
Junior Member
Hello,

I have a problem when adding an ERefernce to my editor. In fact, when opening the editor, the Ereference is already added to palette,then, when i try to draw a connection from StartEvent to EndEvent, the arrow cannot be visualized in the editor (see image 1)

this is my code

The AddConnection.java:
import org.eclipse.emf.ecore.EReference;
import org.eclipse.graphiti.datatypes.IDimension;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.IAddConnectionContext;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.features.impl.AbstractAddFeature;
import org.eclipse.graphiti.mm.GraphicsAlgorithmContainer;
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
import org.eclipse.graphiti.mm.algorithms.Polyline;
import org.eclipse.graphiti.mm.algorithms.Text;
import org.eclipse.graphiti.mm.pictograms.Connection;
import org.eclipse.graphiti.mm.pictograms.ConnectionDecorator;
import org.eclipse.graphiti.mm.pictograms.FreeFormConnection;
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.services.IPeService;
import org.eclipse.graphiti.util.ColorConstant;
import org.eclipse.graphiti.util.IColorConstant;

import eclipse.emf.editeurgraphique.model.editeurgraphique.SequenceFlow;

public class AddConnection extends AbstractAddFeature {
IPeService peService = Graphiti.getPeService();
     static IGaService gaService = Graphiti.getGaService();
private static final IColorConstant E_REFERENCE_FOREGROUND = new ColorConstant(98, 131, 167);
 
    public  AddConnection (IFeatureProvider fp) {
        super(fp);
    }
    private Polyline createArrow(GraphicsAlgorithmContainer gaContainer) {
        IGaService gaService = Graphiti.getGaService();
        Polyline polyline =
            gaService.createPolyline(gaContainer, new int[] { -15, 10, 0, 0, -15,
                    -10 });
        polyline.setForeground(manageColor(E_REFERENCE_FOREGROUND));
        polyline.setLineWidth(2);
        return polyline;
    }
 
    public PictogramElement add(IAddContext context) {
        IAddConnectionContext addConContext = (IAddConnectionContext) context;
        EReference addedEReference = (EReference) context.getNewObject();
        IPeCreateService peCreateService = Graphiti.getPeCreateService();
        
        // CONNECTION WITH POLYLINE
        Connection connection = peCreateService
            .createFreeFormConnection(getDiagram());
        connection.setStart(addConContext.getSourceAnchor());
        connection.setEnd(addConContext.getTargetAnchor());
     
        Polyline polyline = gaService.createPolyline(connection);
        //polyline.setLineWidth(2);
        polyline.setForeground(manageColor(E_REFERENCE_FOREGROUND));
        peService.createChopboxAnchor(connection);
        //addFixedPointAnchors(connection,polyline);
        // create link and wire it
        link(connection, addedEReference);
     // add dynamic text decorator for the association name 
        ConnectionDecorator textDecorator =
            peCreateService.createConnectionDecorator(connection, true,
            0.5, true);
        Text text = gaService.createDefaultText(getDiagram(),textDecorator);
        text.setForeground(manageColor(IColorConstant.BLACK));
        gaService.setLocation(text, 10, 0);
        // set reference name in the text decorator
        EReference eReference = (EReference) context.getNewObject();
        text.setValue(eReference.getName());
    
        // add static graphical decorator (composition and navigable)
        ConnectionDecorator cd;
        cd = peCreateService
              .createConnectionDecorator(connection, false, 1.0, true);
        createArrow(cd);
        return connection;
    }
     
   
public boolean canAdd(IAddContext context) {
        // return true if given business object is an EReference
        // note, that the context must be an instance of IAddConnectionContext
        if (context instanceof IAddConnectionContext)
           // && context.getNewObject() instanceof EReference)
        {
            return true;
        }
        return false;
    }
}


The CreateConnection.java :
package eclipse.emf.editeurgraphique.risque.features;

import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.ICreateConnectionContext;
import org.eclipse.graphiti.features.context.impl.AddConnectionContext;
import org.eclipse.graphiti.features.impl.AbstractCreateConnectionFeature;
import org.eclipse.graphiti.mm.pictograms.Anchor;
import org.eclipse.graphiti.mm.pictograms.Connection;

import eclipse.emf.editeurgraphique.model.editeurgraphique.Activity;
import eclipse.emf.editeurgraphique.model.editeurgraphique.ComplexGateway;
import eclipse.emf.editeurgraphique.model.editeurgraphique.EditeurgraphiqueFactory;
import eclipse.emf.editeurgraphique.model.editeurgraphique.EndEvent;
import eclipse.emf.editeurgraphique.model.editeurgraphique.ExclusiveGateway;
import eclipse.emf.editeurgraphique.model.editeurgraphique.InclusiveGateway;
import eclipse.emf.editeurgraphique.model.editeurgraphique.ParallelGateway;
import eclipse.emf.editeurgraphique.model.editeurgraphique.SequenceFlow;
import eclipse.emf.editeurgraphique.model.editeurgraphique.StartEvent;
import org.eclipse.emf.ecore.ETypedElement;

import org.eclipse.emf.ecore.EStructuralFeature;
public class CreateConnection extends AbstractCreateConnectionFeature {

private Anchor sourceAnchor = null;
private Anchor targetAnchor = null;

public CreateConnection(IFeatureProvider fp) {
 super(fp, "EReference", "Create EReference");
}

@Override
public boolean canStartConnection(ICreateConnectionContext context) {
if ((getStartEvent(context.getSourceAnchor()) != null)
|| (getEndEvent(context.getSourceAnchor()) != null))
//(getActivity(context.getSourceAnchor()) != null)
//(getInclusiveGateway(context.getSourceAnchor()) != null)||
//(getExclusiveGateway(context.getSourceAnchor()) != null)||
//(getParallelGateway(context.getSourceAnchor()) != null)
//|| (getComplexGateway(context.getSourceAnchor()) != null)) 
{
return true;
}

return false;
}

@Override
public boolean canCreate(ICreateConnectionContext context) {
// get anchors
sourceAnchor = context.getSourceAnchor();
targetAnchor = context.getTargetAnchor();

StartEvent sourceStartEvent = getStartEvent(context.getSourceAnchor());
StartEvent targetStartEvent = getStartEvent(context.getTargetAnchor());

EndEvent sourceEndEvent = getEndEvent(context.getSourceAnchor());
EndEvent targetEndEvent = getEndEvent(context.getTargetAnchor());
Activity sourceActivity  = getActivity(context.getSourceAnchor());
Activity  targetActivity = getActivity (context.getTargetAnchor());
InclusiveGateway sourceInclusiveGateway  = getInclusiveGateway (context.getSourceAnchor());
InclusiveGateway  targetInclusiveGateway  = getInclusiveGateway (context.getTargetAnchor());
ExclusiveGateway  sourceExclusiveGateway = getExclusiveGateway(context.getSourceAnchor());
ExclusiveGateway targetExclusiveGateway = getExclusiveGateway(context.getTargetAnchor());
ParallelGateway sourceParallelGateway = getParallelGateway(context.getSourceAnchor());
ParallelGateway targetParallelGateway = getParallelGateway(context.getTargetAnchor());
ComplexGateway sourceComplexGateway = getComplexGateway(context.getSourceAnchor());
ComplexGateway targetComplexGateway = getComplexGateway(context.getTargetAnchor());
if ((sourceStartEvent != null && targetStartEvent != null && sourceStartEvent !=targetStartEvent)
|| (sourceEndEvent != null && targetEndEvent != null && sourceEndEvent!= targetEndEvent)||
(sourceExclusiveGateway != null && targetExclusiveGateway != null && sourceExclusiveGateway!=targetExclusiveGateway)||
(sourceParallelGateway != null && targetParallelGateway != null && sourceParallelGateway !=targetParallelGateway)||
(sourceActivity != null && targetActivity != null && sourceActivity!= targetActivity )
||(sourceComplexGateway != null && targetComplexGateway != null && sourceComplexGateway != targetComplexGateway)||
(sourceInclusiveGateway != null && targetInclusiveGateway != null && sourceInclusiveGateway != targetInclusiveGateway ))
{
return true;
}

return false;
}

@Override
public Connection create(ICreateConnectionContext context) {
Connection newConnection = null;

StartEvent sourceStartEvent = getStartEvent(sourceAnchor);
StartEvent targetStartEvent = getStartEvent(targetAnchor);
EndEvent targetEndEvent = getEndEvent(targetAnchor);
EndEvent sourceEndEvent = getEndEvent(sourceAnchor);
Activity targetActivity = getActivity(targetAnchor);
Activity sourceActivity = getActivity(sourceAnchor);
InclusiveGateway targetInclusiveGateway = getInclusiveGateway(targetAnchor);
InclusiveGateway sourceInclusiveGateway = getInclusiveGateway(sourceAnchor);
ExclusiveGateway targetExclusiveGateway = getExclusiveGateway(targetAnchor);
ExclusiveGateway sourceExclusiveGateway = getExclusiveGateway(sourceAnchor);
ParallelGateway targetParallelGateway = getParallelGateway(targetAnchor);
ParallelGateway sourceParallelGateway = getParallelGateway(sourceAnchor);
ComplexGateway targetComplexGateway = getComplexGateway(targetAnchor);
ComplexGateway sourceComplexGateway= getComplexGateway(sourceAnchor);
if ((targetStartEvent != null && sourceEndEvent!= null)
|| (sourceStartEvent != null && targetEndEvent != null)) {
//SequenceFlow flow = EditeurgraphiqueFactory.eINSTANCE
// .createSequenceFlow();
 EReference eReference = createEReference(sourceEndEvent,targetStartEvent);
AddConnectionContext addContext = new AddConnectionContext(
sourceAnchor, targetAnchor);
addContext.setNewObject(eReference);
newConnection = (Connection) getFeatureProvider().addIfPossible(
addContext);
//System.out.println("New Connection: " + newConnection.toString());
}
else if ((targetInclusiveGateway != null && sourceExclusiveGateway!= null)
|| (sourceInclusiveGateway != null && targetExclusiveGateway != null)) {
 EReference eReference = createEReference(sourceExclusiveGateway,targetInclusiveGateway);
AddConnectionContext addContext = new AddConnectionContext(
sourceAnchor, targetAnchor);
addContext.setNewObject(eReference);
newConnection = (Connection) getFeatureProvider().addIfPossible(
addContext);
//System.out.println("New Connection: " + newConnection.toString());
}
else if   ((targetParallelGateway != null && sourceComplexGateway!= null)
|| (sourceParallelGateway != null && targetComplexGateway != null)) {
 EReference eReference = createEReference(sourceParallelGateway,targetComplexGateway);
AddConnectionContext addContext = new AddConnectionContext(
sourceAnchor, targetAnchor);
addContext.setNewObject(eReference);
newConnection = (Connection) getFeatureProvider().addIfPossible(
addContext);
//System.out.println("New Connection: " + newConnection.toString());
}
return newConnection;
}

private StartEvent getStartEvent(Anchor anchor) {
if (anchor != null) {
Object object = getBusinessObjectForPictogramElement(anchor
.getParent());
if (object instanceof StartEvent) {
return (StartEvent) object;
}
}
return null;
}

private EndEvent getEndEvent(Anchor anchor) {
if (anchor != null) {
Object object = getBusinessObjectForPictogramElement(anchor
.getParent());
if (object instanceof EndEvent) {
return (EndEvent) object;
}
}
return null;
}
private Activity getActivity(Anchor anchor) {
if (anchor != null) {
Object object = getBusinessObjectForPictogramElement(anchor
.getParent());
if (object instanceof Activity) {
return (Activity) object;
}
}
return null;
}
private InclusiveGateway getInclusiveGateway(Anchor anchor) {
if (anchor != null) {
Object object = getBusinessObjectForPictogramElement(anchor
.getParent());
if (object instanceof InclusiveGateway) {
return (InclusiveGateway) object;
}
}
return null;
}
private ExclusiveGateway getExclusiveGateway(Anchor anchor) {
if (anchor != null) {
Object object = getBusinessObjectForPictogramElement(anchor
.getParent());
if (object instanceof ExclusiveGateway) {
return (ExclusiveGateway) object;
}
}
return null;
}
private ParallelGateway getParallelGateway(Anchor anchor) {
if (anchor != null) {
Object object = getBusinessObjectForPictogramElement(anchor
.getParent());
if (object instanceof ParallelGateway) {
return (ParallelGateway) object;
}
}
return null;
}
private ComplexGateway getComplexGateway(Anchor anchor) {
if (anchor != null) {
Object object = getBusinessObjectForPictogramElement(anchor
.getParent());
if (object instanceof ComplexGateway) {
return (ComplexGateway) object;
}
}
return null;
}
private EReference createEReference(EndEvent source, StartEvent target) {
       EReference eReference = EcoreFactory.eINSTANCE.createEReference();
       eReference.setName("new EReference");
       //eReference.setEType(target);
       eReference.setLowerBound(0);
       eReference.setUpperBound(1);
      source.getEStructuralFeatures().add(eReference);
       return eReference;
  }
private EReference createEReference(ExclusiveGateway source, InclusiveGateway target) {
       EReference eReference = EcoreFactory.eINSTANCE.createEReference();
       eReference.setName("new EReference");
       //eReference.setEType(target);
       eReference.setLowerBound(0);
       eReference.setUpperBound(1);
      // source.getEStructuralFeatures().add(eReference);
       return eReference;
  }
private EReference createEReference(  ParallelGateway source, ComplexGateway target) {
       EReference eReference = EcoreFactory.eINSTANCE.createEReference();
       eReference.setName("new EReference");
       //eReference.setEType(target);
       eReference.setLowerBound(0);
       eReference.setUpperBound(1);
      // source.getEStructuralFeatures().add(eReference);
       return eReference;
  }




Could anyone explain me what is the matter ?
Thanks in advance.
Previous Topic:Undoing direct editing leads to multiple undos
Next Topic:Problem with EReference
Goto Forum:
  


Current Time: Mon Sep 01 07:29:08 EDT 2014

Powered by FUDForum. Page generated in 0.01714 seconds