blob: 6f9dda488af5ace58f46e09913efd313963e2fda [file] [log] [blame]
/*******************************************************************************
* <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.algorithm.node;
import java.util.ArrayList;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.fmc.blockdiagram.editor.util.FMCUtil;
import org.eclipse.graphiti.mm.GraphicsAlgorithmContainer;
import org.eclipse.graphiti.mm.algorithms.Ellipse;
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.styles.Color;
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.IPeCreateService;
/**
*
* @author Benjamin Schmeling
*
*/
public class HumanAgentAlgorithm extends RectangleAgentAlgorithm {
/**
* Changes the type of the agent from human agent to (rectangle) agent,
* removes the next human figure that is found. If there is more than one
* human figure only one is removed.
*
* @param element
* The shape containing the human figure.
*/
public void removeHumanFigure(PictogramElement element) {
if (element instanceof ContainerShape) {
ContainerShape container = (ContainerShape) element;
for (Shape shape : new ArrayList<Shape>(container.getChildren())) {
if (shape instanceof ContainerShape
&& shape.getGraphicsAlgorithm()
.getGraphicsAlgorithmChildren().size() == 5) {
container.getChildren().remove(shape);
break;
}
}
;
}
}
/**
* Checks if the agent is a human agent / has a human figure.
*
* @param element
* The shape of the agent.
* @return True if the agent shape contains a human figure.
*/
public boolean hasHumanFigure(PictogramElement element) {
if (element instanceof ContainerShape) {
ContainerShape container = (ContainerShape) element;
for (Shape shape : new ArrayList<Shape>(container.getChildren())) {
if (shape instanceof ContainerShape
&& shape.getGraphicsAlgorithm()
.getGraphicsAlgorithmChildren().size() == 5) {
return true;
}
}
;
}
return false;
}
/**
* Retrieves the human agent's container shape with the human figure, if
* existing.
*
* @param element
* The shape of the agent.
* @return The container shape with the human figure, else null.
*/
public ContainerShape getHumanFigure(PictogramElement element) {
if (element instanceof ContainerShape) {
ContainerShape container = (ContainerShape) element;
for (Shape shape : new ArrayList<Shape>(container.getChildren())) {
if (shape instanceof ContainerShape
&& shape.getGraphicsAlgorithm()
.getGraphicsAlgorithmChildren().size() == 5) {
return (ContainerShape) shape;
}
}
;
}
return null;
}
/**
* Changes the type of the agent to human agent, adds a human figure.
*
* @param diag
* The current Blockdiagram.
* @param container
* The shape containing the graphics algorithms of the agent.
* @param width
* The width of the agent.
*/
public void addHumanFigure(Diagram diag, ContainerShape container, int width) {
int startPointX = width / 2 - 12;
IPeCreateService pe = Graphiti.getPeCreateService();
Color black = ga.manageColor(diag, 0, 0, 0);
ContainerShape figure = pe.createContainerShape(container, true);
int headX = 6;
int headSize = 10;
int bodyX = headSize;
int bodyY = headSize - 1;
int bodySize = 18;
int lArmX = bodyX + 1;
int rArmX = bodyX - 1;
int armsY = 18;
int armEndX = 13;
int armEndY = 9;
int legY = headSize * 2 + bodySize - 2;
int rLegX = headSize * 2 + 1;
int centerOfLegsX = headSize;
int centerOfLegsY = -headSize - 1;
// link(figure, FMCType.HumanAgentIcon);
Rectangle invRec = ga.createInvisibleRectangle(figure);
ga.setLocationAndSize(invRec, startPointX, 10, 25, bodySize + 2
* headSize);
Ellipse head = ga.createEllipse(invRec);
head.setFilled(false);
head.setForeground(black);
head.setParentGraphicsAlgorithm(invRec);
ga.setLocationAndSize(head, headX, 0, headSize, headSize);
Polyline leftArm = ga.createPolyline(invRec, new int[] { 0, 0,
-armEndX, -armEndY });
leftArm.setForeground(black);
leftArm.setParentGraphicsAlgorithm(invRec);
ga.setLocation(leftArm, lArmX, armsY);
Polyline rightArm = ga.createPolyline(invRec, new int[] { 0, 0,
armEndX, -armEndY });
rightArm.setForeground(black);
rightArm.setParentGraphicsAlgorithm(invRec);
ga.setLocation(rightArm, rArmX, armsY);
Polyline body = ga.createPolyline(invRec,
new int[] { 0, 0, 0, bodySize });
body.setForeground(black);
body.setParentGraphicsAlgorithm(invRec);
ga.setLocation(body, bodyX, bodyY);
Polyline legs = ga.createPolyline(invRec, new int[] { 0, 0,
centerOfLegsX, centerOfLegsY, rLegX, 0 });
legs.setForeground(black);
legs.setParentGraphicsAlgorithm(invRec);
ga.setLocation(legs, 0, legY);
// Shape headShape = pe.createShape(figure, false);
// Ellipse head = ga.createEllipse(headShape);
// head.setFilled(false);
// head.setForeground(black);
// ga.setLocationAndSize(head, headX, 0, headSize, headSize);
// Shape leftArmShape = pe.createShape(figure, false);
// Polyline leftArm = ga.createPolyline(leftArmShape, new int[] { 0, 0,
// -armEndX, -armEndY });
// leftArm.setForeground(black);
// ga.setLocation(leftArm, lArmX, armsY);
// Shape rightArmShape = pe.createShape(figure, false);
// Polyline rightArm = ga.createPolyline(rightArmShape, new int[] { 0,
// 0,
// armEndX, -armEndY });
// rightArm.setForeground(black);
// ga.setLocation(rightArm, rArmX, armsY);
// Shape bodyShape = pe.createShape(figure, false);
// Polyline body = ga.createPolyline(bodyShape, new int[] { 0, 0, 0,
// bodySize});
// body.setForeground(black);
// ga.setLocation(body, bodyX, bodyY);
// Shape legsShape = pe.createShape(figure, false);
// Polyline legs = ga.createPolyline(legsShape, new int[] { 0, 0,
// centerOfLegsX, centerOfLegsY,
// rLegX, 0 });
// legs.setForeground(black);
// ga.setLocation(legs, 0, legY);
}
// ----------------------------------------
// ----------- Graphics Node --------------
@Override
/*
* (non-Javadoc)
*
* @see
* org.eclipse.fmc.blockdiagram.editor.node.RectangleAgentAlgorithm#createGraphics
* (org.eclipse.graphiti.mm.pictograms.Diagram,
* org.eclipse.graphiti.mm.GraphicsAlgorithmContainer, int, int, int, int)
*/
public GraphicsAlgorithm createGraphics(Diagram diag,
GraphicsAlgorithmContainer container, int x, int y, int width,
int height) {
GraphicsAlgorithm rectangle = super.createGraphics(diag, container, x,
y, width, height);
if (container instanceof ContainerShape)
addHumanFigure(diag, (ContainerShape) container, width);
return rectangle;
}
// ----------------------------------------
// ----------- Resizable Node -------------
@Override
/*
* (non-Javadoc)
*
* @see
* org.eclipse.fmc.blockdiagram.editor.node.RectangleAgentAlgorithm#resize(org.eclipse
* .graphiti.mm.algorithms.GraphicsAlgorithm, int, int, int, int)
*/
public void resize(GraphicsAlgorithm algorithm, int x, int y, int width,
int height) {
super.resize(algorithm, x, y, width, height);
ContainerShape container = this.getContainerShape(algorithm);
removeHumanFigure(container);
addHumanFigure(FMCUtil.getActiveEditor().getDiagramTypeProvider()
.getDiagram(), container, width);
}
// ----------------------------------------
// ----------- Container Node -------------
@Override
/*
* (non-Javadoc)
*
* @see
* org.eclipse.fmc.blockdiagram.editor.node.AbstractNode#getContainedShapes(org.
* eclipse.graphiti.mm.pictograms.ContainerShape)
*/
public EList<Shape> getContainedShapes(ContainerShape cs) {
EList<Shape> results = new BasicEList<Shape>();
Shape humanFigureShape = this.getHumanFigure(cs);
if (humanFigureShape != null) {
for (Shape shape : super.getContainedShapes(cs)) {
if (!(shape.equals(humanFigureShape)))
results.add(shape);
}
}
return results;
}
}