/******************************************************************************* | |
* <copyright> | |
* | |
* Copyright (c) 2013, 2013 SAP AG. | |
* All rights reserved. This program and the accompanying materials | |
* are made available under the terms of the Eclipse Public License v1.0 | |
* which accompanies this distribution, and is available at | |
* http://www.eclipse.org/legal/epl-v10.html | |
* | |
* Contributors: | |
* SAP AG - initial API, implementation and documentation | |
* | |
* </copyright> | |
* | |
*******************************************************************************/ | |
package org.eclipse.fmc.blockdiagram.editor.meta.features.add; | |
import org.eclipse.fmc.blockdiagram.editor.meta.BlockDiagramMetaFeatureProvider; | |
import org.eclipse.fmc.blockdiagram.editor.meta.util.AddConnectionHelper; | |
import org.eclipse.graphiti.features.IFeatureProvider; | |
import org.eclipse.graphiti.features.context.IAddContext; | |
import org.eclipse.graphiti.features.context.IContext; | |
import org.eclipse.graphiti.mm.pictograms.ContainerShape; | |
import org.eclipse.graphiti.mm.pictograms.PictogramElement; | |
import org.eclipse.fmc.blockdiagram.editor.algorithm.node.FMCNodeAlgorithm; | |
import org.eclipse.fmc.blockdiagram.editor.features.add.AgentAddFeature; | |
import org.eclipse.fmc.blockdiagram.editor.model.ShapeStyle; | |
import org.eclipse.fmc.blockdiagram.editor.util.FMCUtil; | |
import org.eclipse.fmc.mm.FMCNode; | |
import org.eclipse.fmc.mm.MultiplicityType; | |
/** | |
* Feature Class for adding the graphical representation of an existing agent | |
* business object. | |
* | |
* @author Patrick Jahnke | |
* | |
*/ | |
public class AgentAddMetaFeature extends AgentAddFeature { | |
/** | |
* Object for creating all connections which are existing in the meta model. | |
*/ | |
AddConnectionHelper connHelper = null; | |
/** | |
* Constructor just calls the super constructor. | |
* | |
* @param fp | |
*/ | |
public AgentAddMetaFeature(IFeatureProvider fp) { | |
super(fp); | |
} | |
/** | |
* Constructor just calls the super constructor. | |
* | |
* @param fp | |
* @param text | |
* @param width | |
* @param height | |
* @param angle | |
*/ | |
public AgentAddMetaFeature(IFeatureProvider fp, String text, int width, | |
int height, int angle) { | |
super(fp, text, width, height, angle); | |
} | |
/** | |
* Constructor just calls the super constructor. | |
* | |
* @param fp | |
* @param style | |
*/ | |
public AgentAddMetaFeature(IFeatureProvider fp, ShapeStyle style) { | |
super(fp, style); | |
} | |
/** | |
* Call the AddConnectionHelper for creating all connections which are | |
* existing in the meta model and draw the right multiplicity. | |
*/ | |
@Override | |
public PictogramElement add(IAddContext context) { | |
PictogramElement picEl = super.add(context); | |
// When the Model has connections and channels to Elements in the | |
// current Diagram we must add them. | |
connHelper = new AddConnectionHelper(); | |
connHelper.addAgentConnections(picEl, | |
(BlockDiagramMetaFeatureProvider) this.getFeatureProvider(), | |
getDiagram()); | |
checkMultiplicity(context, picEl); | |
return picEl; | |
} | |
/** | |
* Create the right graphical multiplicity representation for the given | |
* PictogramElement object. | |
* | |
* @param context | |
* @param picEl | |
*/ | |
private void checkMultiplicity(IAddContext context, PictogramElement picEl) { | |
ContainerShape container = (ContainerShape) picEl; | |
FMCNode node = (FMCNode) FMCUtil.getBO(container); | |
FMCNodeAlgorithm algorithm = factory.getShape(container); | |
if (algorithm == null) | |
return; | |
boolean isMultiInstance = algorithm.isMultipleInstances(container); | |
if ((node.getMultiplicity() == MultiplicityType.MANY) != isMultiInstance | |
&& !isContainerMultiPart(algorithm, container)) | |
algorithm.setMultipleInstances((ContainerShape) picEl, | |
getFeatureProvider(), | |
node.getMultiplicity() == MultiplicityType.MANY); | |
} | |
/** | |
* Checks if the given container is a container multi part. | |
* | |
* @param algorithm | |
* the FMCNodeAlgorithm. | |
* @param container | |
* the ContainerShape. | |
* @return true, if container is a container multi part. | |
*/ | |
protected boolean isContainerMultiPart(FMCNodeAlgorithm algorithm, | |
ContainerShape container) { | |
// TODO: Code copy from FMCNodeUpdateFeature; | |
if (container.eContainer() == null | |
|| !(container.eContainer() instanceof ContainerShape)) | |
return false; | |
ContainerShape containerParent = (ContainerShape) container | |
.eContainer(); | |
return container.getGraphicsAlgorithm().equals( | |
algorithm.getMultiInstanceChild(containerParent, true)) | |
|| container.getGraphicsAlgorithm() | |
.equals(algorithm.getMultiInstanceChild( | |
containerParent, false)); | |
} | |
/** | |
* Decides if the feature of the connection helper or the current feature | |
* (if the feature of the connection helper is null) is available with the | |
* given context. | |
*/ | |
@Override | |
public boolean isAvailable(IContext context) { | |
if (connHelper == null) | |
return super.isAvailable(context); | |
if (!connHelper.isAvailable(context)) | |
return false; | |
return super.isAvailable(context); | |
} | |
/** | |
* Decides if the feature of the connection helper or the current feature | |
* (if the feature of the connection helper is null) is available with the | |
* given context. | |
*/ | |
@Override | |
public boolean canExecute(IContext context) { | |
if (connHelper == null) | |
return super.canExecute(context); | |
if (!connHelper.canExecute(context)) | |
return false; | |
return super.canExecute(context); | |
} | |
/** | |
* Executes the current feature and the builded feature of the connection | |
* helper with the given context. | |
*/ | |
@Override | |
public void execute(IContext context) { | |
super.execute(context); | |
if (connHelper != null) | |
connHelper.execute(context); | |
} | |
/** | |
* Decides if the feature of the connection helper or the current feature | |
* (if the feature of the connection helper is null) can be undone - this is | |
* the undo of the execute operation. | |
*/ | |
@Override | |
public boolean canUndo(IContext context) { | |
if (connHelper == null) | |
return super.canUndo(context); | |
if (!connHelper.canUndo(context)) | |
return false; | |
return super.canUndo(context); | |
} | |
/** | |
* Check if this feature or the connection helper feature has done changes. | |
*/ | |
@Override | |
public boolean hasDoneChanges() { | |
if (connHelper == null) | |
return super.hasDoneChanges(); | |
return (connHelper.hasDoneChanges() || super.hasDoneChanges()); | |
} | |
} |