Skip to content
This repository has been archived by the owner on Oct 7, 2021. It is now read-only.

Commit

Permalink
#289: pass icon info along in graphical model and to paste logic
Browse files Browse the repository at this point in the history
Signed-off-by: Erwin De Ley <erwindl0@gmail.com>
  • Loading branch information
erwindl0 committed Oct 9, 2017
1 parent d3ab322 commit 154574b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 58 deletions.
Expand Up @@ -27,8 +27,8 @@
*
*/
public enum BoCategory implements Category{
Actor(ptolemy.actor.Actor.class),
CompositeActor(ptolemy.actor.CompositeActor.class),
Actor(ptolemy.actor.Actor.class),
Director(ptolemy.actor.Director.class),
Parameter(ptolemy.data.expr.Parameter.class),
Attribute(ptolemy.kernel.util.Attribute.class),
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.graphiti.features.IDirectEditingInfo;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.features.context.IContext;
import org.eclipse.graphiti.features.impl.AbstractAddShapeFeature;
import org.eclipse.graphiti.mm.MmFactory;
import org.eclipse.graphiti.mm.Property;
Expand Down Expand Up @@ -69,7 +70,7 @@ public ActorAddFeature(IFeatureProvider fp) {
* @param businessObject
* @param categories
*/
protected void link(PictogramElement pe, Object businessObject, Category... categories) {
protected void link(IContext context, PictogramElement pe, Object businessObject, Category... categories) {
super.link(pe, businessObject);
// add property on the graphical model element, identifying the associated triq model element
// so we can easily distinguish and identify them later on for updates etc
Expand All @@ -78,6 +79,14 @@ protected void link(PictogramElement pe, Object businessObject, Category... cate
}
if (businessObject instanceof NamedObj) {
Graphiti.getPeService().setPropertyValue(pe, FeatureConstants.BO_NAME, ((NamedObj) businessObject).getName());
String iconResource = (String) context.getProperty("icon");
if(iconResource!=null) {
Graphiti.getPeService().setPropertyValue(pe, FeatureConstants.ICON, iconResource);
}
String iconType = (String) context.getProperty("iconType");
if(iconType!=null) {
Graphiti.getPeService().setPropertyValue(pe, FeatureConstants.ICON_TYPE, iconType);
}
}
Graphiti.getPeService().setPropertyValue(pe, FeatureConstants.BO_CLASS, businessObject.getClass().getName());
}
Expand Down Expand Up @@ -107,7 +116,7 @@ public PictogramElement add(IAddContext context) {
IPeCreateService peCreateService = Graphiti.getPeCreateService();
IGaService gaService = Graphiti.getGaService();
ContainerShape containerShape = peCreateService.createContainerShape(targetContainer, true);
link(containerShape, addedActor, BoCategory.Actor);
link(context, containerShape, addedActor, BoCategory.Actor);

GraphicsAlgorithm invisibleRectangle = null;
invisibleRectangle = gaService.createInvisibleRectangle(containerShape);
Expand All @@ -120,10 +129,10 @@ public PictogramElement add(IAddContext context) {
switch (iconType) {
case TriqFeatureProvider.ICONTYPE_SVG:
case TriqFeatureProvider.ICONTYPE_PTOLEMY:
actorShapeGA = buildExternallyDefinedShape(gaService, invisibleRectangle, containerShape, iconType, iconResource);
actorShapeGA = buildExternallyDefinedShape(context, gaService, invisibleRectangle, containerShape, iconType, iconResource);
break;
default:
actorShapeGA = buildDefaultShape(gaService, invisibleRectangle, containerShape, addedActor, iconResource);
actorShapeGA = buildDefaultShape(context, gaService, invisibleRectangle, containerShape, addedActor, iconResource);
}

int width = actorShapeGA.getWidth();
Expand Down Expand Up @@ -151,7 +160,7 @@ public PictogramElement add(IAddContext context) {
* @param iconResource
* @return
*/
protected GraphicsAlgorithm buildDefaultShape(IGaService gaService, GraphicsAlgorithm invisibleRectangle, ContainerShape containerShape, Entity addedActor,
protected GraphicsAlgorithm buildDefaultShape(IAddContext context, IGaService gaService, GraphicsAlgorithm invisibleRectangle, ContainerShape containerShape, Entity addedActor,
String iconResource) {

IPeCreateService peCreateService = Graphiti.getPeCreateService();
Expand All @@ -174,7 +183,7 @@ protected GraphicsAlgorithm buildDefaultShape(IGaService gaService, GraphicsAlgo
gaService.setLocationAndSize(image, ACTOR_ICON_X_MARGIN, ACTOR_ICON_Y_MARGIN, ACTOR_ICON_SIZE, ACTOR_ICON_SIZE);

// create link and wire it
link(shape, addedActor, BoCategory.Actor);
link(context, shape, addedActor, BoCategory.Actor);
} catch (Exception e) {
LOGGER.error(ErrorCode.MODEL_CONFIGURATION_ERROR + " - Error trying to add actor icon for " + addedActor, e);
}
Expand All @@ -191,7 +200,7 @@ protected GraphicsAlgorithm buildDefaultShape(IGaService gaService, GraphicsAlgo
polyline.setLineWidth(2);

// create link and wire it
link(shape, addedActor, BoCategory.Actor);
link(context, shape, addedActor, BoCategory.Actor);
}

// SHAPE WITH actor name as TEXT
Expand All @@ -208,7 +217,7 @@ protected GraphicsAlgorithm buildDefaultShape(IGaService gaService, GraphicsAlgo
gaService.setLocationAndSize(text, ACTOR_TEXT_X_MARGIN, ACTOR_Y_MARGIN, ACTOR_TEXT_WIDTH, ACTOR_TEXT_HEIGHT);

// create link and wire it
link(shape, addedActor, BoCategory.Actor);
link(context, shape, addedActor, BoCategory.Actor);

// provide information to support direct-editing directly
// after object creation (must be activated additionally)
Expand All @@ -234,7 +243,7 @@ protected GraphicsAlgorithm buildDefaultShape(IGaService gaService, GraphicsAlgo
* @param iconResource
* @return
*/
protected GraphicsAlgorithm buildExternallyDefinedShape(IGaService gaService, GraphicsAlgorithm invisibleRectangle, ContainerShape containerShape,
protected GraphicsAlgorithm buildExternallyDefinedShape(IAddContext context, IGaService gaService, GraphicsAlgorithm invisibleRectangle, ContainerShape containerShape,
String iconType, String iconResource) {

GraphicsAlgorithm extFigure = Graphiti.getGaCreateService().createPlatformGraphicsAlgorithm(invisibleRectangle, iconType);
Expand Down Expand Up @@ -270,7 +279,7 @@ private void createAnchorsAndPortShapesForDirection(IAddContext context, Contain
Port p = portList.get(i);
Anchor anchor = PortShapes.createAnchor(containerShape, direction, p, i, portCount);
PortShapes.createPortShape(getDiagram(), anchor, direction, p);
link(anchor, p, BoCategory.Port, PortCategory.valueOf(direction));
link(context, anchor, p, BoCategory.Port, PortCategory.valueOf(direction));
if (anchorMap != null) {
anchorMap.put(p.getFullName(), anchor);
}
Expand Down
Expand Up @@ -66,7 +66,7 @@ public PictogramElement add(IAddContext context) {
IPeCreateService peCreateService = Graphiti.getPeCreateService();
IGaService gaService = Graphiti.getGaService();
ContainerShape containerShape = peCreateService.createContainerShape(targetContainer, true);
link(containerShape, addedActor, BoCategory.CompositeActor);
link(context, containerShape, addedActor, BoCategory.CompositeActor);

GraphicsAlgorithm invisibleRectangle = null;
invisibleRectangle = gaService.createInvisibleRectangle(containerShape);
Expand All @@ -79,10 +79,10 @@ public PictogramElement add(IAddContext context) {
switch (iconType) {
case TriqFeatureProvider.ICONTYPE_SVG:
case TriqFeatureProvider.ICONTYPE_PTOLEMY:
actorShapeGA = buildExternallyDefinedShape(gaService, invisibleRectangle, containerShape, iconType, iconResource);
actorShapeGA = buildExternallyDefinedShape(context, gaService, invisibleRectangle, containerShape, iconType, iconResource);
break;
default:
actorShapeGA = buildDefaultShape(gaService, invisibleRectangle, containerShape, addedActor, iconResource);
actorShapeGA = buildDefaultShape(context, gaService, invisibleRectangle, containerShape, addedActor, iconResource);
}

int width = actorShapeGA.getWidth();
Expand All @@ -94,7 +94,8 @@ public PictogramElement add(IAddContext context) {
return containerShape;
}

protected GraphicsAlgorithm buildDefaultShape(IGaService gaService, GraphicsAlgorithm invisibleRectangle, ContainerShape containerShape, Entity addedActor,
@Override
protected GraphicsAlgorithm buildDefaultShape(IAddContext context, IGaService gaService, GraphicsAlgorithm invisibleRectangle, ContainerShape containerShape, Entity addedActor,
String iconResource) {

IPeCreateService peCreateService = Graphiti.getPeCreateService();
Expand All @@ -117,7 +118,7 @@ protected GraphicsAlgorithm buildDefaultShape(IGaService gaService, GraphicsAlgo
gaService.setLocationAndSize(image, ACTOR_ICON_X_MARGIN, ACTOR_ICON_Y_MARGIN, ACTOR_ICON_SIZE, ACTOR_ICON_SIZE);

// create link and wire it
link(shape, addedActor, BoCategory.Actor);
link(context, shape, addedActor, BoCategory.Actor);
} catch (Exception e) {
LOGGER.error(ErrorCode.MODEL_CONFIGURATION_ERROR + " - Error trying to add actor icon for " + addedActor, e);
}
Expand All @@ -134,7 +135,7 @@ protected GraphicsAlgorithm buildDefaultShape(IGaService gaService, GraphicsAlgo
polyline.setLineWidth(2);

// create link and wire it
link(shape, addedActor, BoCategory.Actor);
link(context, shape, addedActor, BoCategory.Actor);
}

// SHAPE WITH actor name as TEXT
Expand All @@ -151,7 +152,7 @@ protected GraphicsAlgorithm buildDefaultShape(IGaService gaService, GraphicsAlgo
gaService.setLocationAndSize(text, ACTOR_TEXT_X_MARGIN, ACTOR_Y_MARGIN, ACTOR_TEXT_WIDTH, ACTOR_TEXT_HEIGHT);

// create link and wire it
link(shape, addedActor, BoCategory.CompositeActor);
link(context, shape, addedActor, BoCategory.CompositeActor);

// provide information to support direct-editing directly
// after object creation (must be activated additionally)
Expand Down
Expand Up @@ -17,4 +17,7 @@ public interface FeatureConstants {

String BO_CLASS = "__BO_CLASS";
String BO_NAME = "__BO_NAME";

String ICON = "__ICON";
String ICON_TYPE = "__ICON_TYPE";
}
Expand Up @@ -118,6 +118,13 @@ public void paste(IPasteContext context) {
addCtxt.setTargetContainer(getDiagram());
getFeatureProvider().addIfPossible(new AddContext(addCtxt, clonedChild));
} else if (sourceBO instanceof NamedObj) {
// Handling the icon spec in this way is OK for making sure custom icons are maintained
// for copy/pasted model elements, on their top level only.
// I.e. once we must be able to look inside composite hierarchies, this is no longer sufficient.
// The basis of this limitation is the current approach to clone from the Ptolemy objects,
// meaning that we don't pass along all graphical info contained in the Graphiti model objects.
String iconType = Graphiti.getPeService().getPropertyValue(sourcePE, FeatureConstants.ICON_TYPE);
String iconResource = Graphiti.getPeService().getPropertyValue(sourcePE, FeatureConstants.ICON);
NamedObj sourceNO = (NamedObj) sourceBO;
// This fails due to the order in which contained things get initialized,
// before the parent has received its copied container, or something like that.
Expand All @@ -127,7 +134,7 @@ public void paste(IPasteContext context) {
// and then construct the Graphiti/EMF copies from there.
ptolemy.kernel.util.NamedObj sourcePtNO = sourceNO.getWrappedObject();
try {
cloneModelElement(targetModel, targetPtModel, sourcePtNO, newLocation, oldNewElementNames, anchorMap);
cloneModelElement(targetModel, targetPtModel, sourcePtNO, iconType, iconResource, newLocation, oldNewElementNames, anchorMap);
} catch (Exception e) {
// TODO how to fix/report paste errors that are not about missing elements?
LOGGER.error("Error pasting model elements", e);
Expand Down Expand Up @@ -163,7 +170,7 @@ public void paste(IPasteContext context) {
}

private void cloneModelElement(CompositeActor targetModel, ptolemy.kernel.util.NamedObj targetPtModel, ptolemy.kernel.util.NamedObj sourcePtNO,
double[] newLocation, Map<String, String> oldNewElementNames, Map<String, Anchor> anchorMap)
String iconType, String iconResource, double[] newLocation, Map<String, String> oldNewElementNames, Map<String, Anchor> anchorMap)
throws CloneNotSupportedException, IllegalActionException, NameDuplicationException {
ptolemy.kernel.util.NamedObj clonedPtNO = (ptolemy.kernel.util.NamedObj) sourcePtNO.clone(targetPtModel.workspace());
try {
Expand Down Expand Up @@ -194,7 +201,7 @@ private void cloneModelElement(CompositeActor targetModel, ptolemy.kernel.util.N
}
}
}
BuildDiagramElementsFromPtolemyElementCommand cmd = new BuildDiagramElementsFromPtolemyElementCommand(getDiagram(), clonedPtNO);
BuildDiagramElementsFromPtolemyElementCommand cmd = new BuildDiagramElementsFromPtolemyElementCommand(getDiagram(), clonedPtNO, iconType, iconResource);
cmd.execute();
anchorMap.putAll(cmd.getAnchorMap());
}
Expand Down Expand Up @@ -231,7 +238,7 @@ private boolean handleMissingElement(CompositeActor model, ptolemy.kernel.util.N

try {
// In Ptolemy II this is done via MoML change requests, but we're not using that (yet) in Triquetrum
cloneModelElement(model, ptModel, node, null, oldNewElementNames, anchorMap);
cloneModelElement(model, ptModel, node, null, null, null, oldNewElementNames, anchorMap);

// Rerun the validation in case there are other problem variables.
continueValidation = true;
Expand Down
Expand Up @@ -60,16 +60,23 @@ public class BuildDiagramElementsFromPtolemyElementCommand {
private Map<String, Anchor> anchorMap = new HashMap<>();
private Map<String, Relation> relationMap = new HashMap<>();

private String iconType;
private String iconResource;

/**
*
* @param diagram
* a Triq diagram that will be extended with new model elements
* @param ptolemyElement
* the model that must be imported into the diagram
* @param iconResource
* @param iconType
*/
public BuildDiagramElementsFromPtolemyElementCommand(Diagram diagram, NamedObj ptolemyElement) {
public BuildDiagramElementsFromPtolemyElementCommand(Diagram diagram, NamedObj ptolemyElement, String iconType, String iconResource) {
this.diagram = diagram;
this.ptolemyElement = ptolemyElement;
this.iconType = iconType;
this.iconResource = iconResource;
}

public void execute() {
Expand All @@ -79,41 +86,42 @@ public void execute() {
// get the root compositeactor of the diagram, where we must add the extra ptolemy element
CompositeActor model = (CompositeActor) featureProvider.getBusinessObjectForPictogramElement(diagram);

if (ptolemyElement instanceof ptolemy.actor.CompositeActor) {
ptolemy.actor.CompositeActor compPtElem = (ptolemy.actor.CompositeActor) ptolemyElement;
// Get the director as a first trial to add a new diagram element from ptolemy model elements
Director director = compPtElem.getDirector();
createModelElement(model, featureProvider, director, model);

for (IORelation rel : (List<IORelation>) compPtElem.relationList()) {
relationMap.put(rel.getFullName(), createRelation(model, featureProvider, rel));
}

for (IOPort p : (List<IOPort>) compPtElem.portList()) {
createModelElement(model, featureProvider, p, model);
}

for (Entity entity : compPtElem.entityList(Entity.class)) {
createModelElement(model, featureProvider, entity, model);
}

// we don't import all attributes as lots of them are ptolemy-internal
// TODO find something to resolve errors related to the order of adding parameters
// that contain expressions with references to other parameters that may be later in the list.

// First we take parameters ...
for (Parameter p : ptolemyElement.attributeList(Parameter.class)) {
createModelElement(model, featureProvider, p, model);
}
// ... and annotations (i.e. TextAttributes)
for (TextAttribute a : ptolemyElement.attributeList(TextAttribute.class)) {
createModelElement(model, featureProvider, a, model);
}

linkRelations(compPtElem, model, featureProvider);
} else {
// if (ptolemyElement instanceof ptolemy.actor.CompositeActor) {
// ptolemy.actor.CompositeActor compPtElem = (ptolemy.actor.CompositeActor) ptolemyElement;
// // Get the director as a first trial to add a new diagram element from ptolemy model elements
// Director director = compPtElem.getDirector();
// createModelElement(model, featureProvider, director, model);
//
//
// for (IORelation rel : (List<IORelation>) compPtElem.relationList()) {
// relationMap.put(rel.getFullName(), createRelation(model, featureProvider, rel));
// }
//
// for (IOPort p : (List<IOPort>) compPtElem.portList()) {
// createModelElement(model, featureProvider, p, model);
// }
//
// for (Entity entity : compPtElem.entityList(Entity.class)) {
// createModelElement(model, featureProvider, entity, model);
// }
//
// // we don't import all attributes as lots of them are ptolemy-internal
// // TODO find something to resolve errors related to the order of adding parameters
// // that contain expressions with references to other parameters that may be later in the list.
//
// // First we take parameters ...
// for (Parameter p : ptolemyElement.attributeList(Parameter.class)) {
// createModelElement(model, featureProvider, p, model);
// }
// // ... and annotations (i.e. TextAttributes)
// for (TextAttribute a : ptolemyElement.attributeList(TextAttribute.class)) {
// createModelElement(model, featureProvider, a, model);
// }
//
// linkRelations(compPtElem, model, featureProvider);
// } else {
createModelElement(model, featureProvider, ptolemyElement, model);
}
// }
}

public Map<String, Anchor> getAnchorMap() {
Expand Down Expand Up @@ -153,7 +161,7 @@ protected org.eclipse.triquetrum.workflow.model.NamedObj createModelElement(org.
// seems to be about a model element for which no preconfigured ModelElementCreateFeature was found
BoCategory meCategory = BoCategory.valueOf(ptObject.getClass());
if (meCategory != null) {
ModelElementCreateFeature mecf = new ModelElementCreateFeature(featureProvider, null, meCategory, ptObject.getName());
ModelElementCreateFeature mecf = new ModelElementCreateFeature(featureProvider, null, meCategory, ptObject.getName(), null, iconResource, iconType, null);
mecf.setWrappedObject(ptObject);
result = (org.eclipse.triquetrum.workflow.model.NamedObj) mecf.create(context)[0];
} else {
Expand Down

0 comments on commit 154574b

Please sign in to comment.