blob: 7fbd15cc76d1d2b31c9cc4cff254aee867539bb6 [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 org.eclipse.emf.common.util.EList;
import org.eclipse.fmc.blockdiagram.editor.util.StyleUtil;
import org.eclipse.graphiti.mm.GraphicsAlgorithmContainer;
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
import org.eclipse.graphiti.mm.algorithms.Polygon;
import org.eclipse.graphiti.mm.algorithms.styles.Point;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.Diagram;
/**
* This abstract class is the super class for all polygon algorithms and
* provides common methods for them. For example, there is a general rotate
* method and a standard resize method.
*
* @author Benjamin Schmeling
* @author Heiko Witteborg
*
*/
public abstract class AbstractPolygonAlgorithm extends AbstractNode implements
PolygonAlgorithm {
// ----------------------------------------
// ----------- Graphics Node --------------
@Override
/*
* (non-Javadoc)
*
* @see
* org.eclipse.fmc.blockdiagram.editor.node.GraphicsNode#createGraphics(org.eclipse
* .graphiti.mm.pictograms.Diagram,
* org.eclipse.graphiti.mm.GraphicsAlgorithmContainer, int, int, int, int)
*/
public Polygon createGraphics(Diagram diagram,
GraphicsAlgorithmContainer container, int x, int y, int width,
int height) {
// Create polygon
Polygon polygon = ga.createPolygon(container,
this.getInitialPoints(0, 0, width, height));
ga.setLocationAndSize(polygon, x, y, width, height);
polygon.setLineWidth(3);
polygon.setTransparency(null);
polygon.setStyle(StyleUtil.getStyle(diagram, StyleUtil.SHAPE));
// Create anchors
if (container instanceof GraphicsAlgorithm) {
ContainerShape containerShape = this.getContainerShape(polygon);
pe.createChopboxAnchor(containerShape);
createBoxAnchorSet(containerShape);
}
return polygon;
}
// ----------------------------------------
// ----------- Polygon Impl ---------------
@Override
/*
* (non-Javadoc)
*
* @see
* org.eclipse.fmc.blockdiagram.editor.node.PolygonAlgorithm#hasRoundedCorners()
*/
public boolean hasRoundedCorners() {
return false;
}
@Override
/*
* (non-Javadoc)
*
* @see
* org.eclipse.fmc.blockdiagram.editor.node.AbstractNode#synchronizeFirstLevelShape
* (org.eclipse.graphiti.mm.pictograms.ContainerShape)
*/
public void synchronizeFirstLevelShape(ContainerShape firstLevelContainer) {
Polygon firstLevelPolygon = (Polygon) firstLevelContainer
.getGraphicsAlgorithm();
Polygon secondLevelPolygon;
if (isMultipleInstances(firstLevelContainer))
secondLevelPolygon = (Polygon) getMultiInstanceChild(
firstLevelContainer, true);
else
secondLevelPolygon = (Polygon) firstLevelPolygon
.getGraphicsAlgorithmChildren().get(0);
int w = firstLevelPolygon.getWidth();
int h = firstLevelPolygon.getHeight();
int i = 0;
Point firstLevelPoint;
for (Point secondLevelPoint : secondLevelPolygon.getPoints()) {
firstLevelPoint = firstLevelPolygon.getPoints().get(i++);
// ensure that invisible first level shape has maximum width
if (secondLevelPoint.getX() != (w - MultipleNode.MULTI_INSTANCE_GAP))
firstLevelPoint.setX(secondLevelPoint.getX());
else
firstLevelPoint.setX(w);
// ensure that invisible first level shape has maximum height
if (secondLevelPoint.getY() != (h - MultipleNode.MULTI_INSTANCE_GAP))
firstLevelPoint.setY(secondLevelPoint.getY());
else
firstLevelPoint.setY(h);
firstLevelPoint.setAfter(secondLevelPoint.getAfter());
firstLevelPoint.setBefore(secondLevelPoint.getBefore());
}
}
// ----------------------------------------
// ----------- Rotatable Node -------------
@Override
/*
* (non-Javadoc)
*
* @see
* org.eclipse.fmc.blockdiagram.editor.node.RotatableNode#rotate90Right(org.eclipse
* .graphiti.mm.algorithms.GraphicsAlgorithm)
*/
public void rotate90Right(GraphicsAlgorithm graphicsAlgorithm) {
Polygon polygon = (Polygon) graphicsAlgorithm;
polygon.setX(polygon.getX()
+ (polygon.getWidth() - polygon.getHeight()) / 2);
polygon.setY(polygon.getY()
- (polygon.getWidth() - polygon.getHeight()) / 2);
doRotate(polygon);
}
private void doRotate(Polygon polygon) {
// Rotate graphicsAlgorithm
int oldWidth = polygon.getWidth();
int oldHeight = polygon.getHeight();
int oldX, oldY;
for (Point point : polygon.getPoints()) {
oldX = point.getX();
oldY = point.getY();
point.setX(oldHeight - oldY);
point.setY(oldX);
}
// Adapt the size of graphicsAlgorithm
polygon.setHeight(oldWidth);
polygon.setWidth(oldHeight);
// Relocate the text shape of the polygon
relocateText(polygon);
EList<GraphicsAlgorithm> graphicsAlgorithmChildren = polygon
.getGraphicsAlgorithmChildren();
for (int i = 0; i < graphicsAlgorithmChildren.size(); i++) {
// Flip polygon children
if (graphicsAlgorithmChildren.get(i) instanceof Polygon)
this.doRotate((Polygon) graphicsAlgorithmChildren.get(i));
// Update relative anchors when the ga modification is done
if (i == graphicsAlgorithmChildren.size() - 1)
createBoxAnchorSet(this.getContainerShape(polygon));
}
}
@Override
/*
* (non-Javadoc)
*
* @see
* org.eclipse.fmc.blockdiagram.editor.node.RotatableNode#flipHorizontally(org.eclipse
* .graphiti.mm.algorithms.GraphicsAlgorithm)
*/
public void flipHorizontally(GraphicsAlgorithm graphicsAlgorithm) {
Polygon polygon = (Polygon) graphicsAlgorithm;
// Flip graphicsAlgorithm
int width = polygon.getWidth();
for (Point point : polygon.getPoints()) {
point.setX(width - point.getX());
}
EList<GraphicsAlgorithm> graphicsAlgorithmChildren = graphicsAlgorithm
.getGraphicsAlgorithmChildren();
for (int i = 0; i < graphicsAlgorithmChildren.size(); i++) {
// Flip polygon children
if (graphicsAlgorithmChildren.get(i) instanceof Polygon)
this.flipHorizontally(graphicsAlgorithmChildren.get(i));
// Update relative anchors when the ga modification is done
if (i == graphicsAlgorithmChildren.size() - 1)
createBoxAnchorSet(this.getContainerShape(graphicsAlgorithm));
}
}
@Override
/*
* (non-Javadoc)
*
* @see
* org.eclipse.fmc.blockdiagram.editor.node.RotatableNode#flipVertically(org.eclipse
* .graphiti.mm.algorithms.GraphicsAlgorithm)
*/
public void flipVertically(GraphicsAlgorithm graphicsAlgorithm) {
Polygon polygon = (Polygon) graphicsAlgorithm;
// Flip graphicsAlgorithm
int height = polygon.getHeight();
for (Point point : polygon.getPoints()) {
point.setY(height - point.getY());
}
// Relocate the text shape of the polygon
relocateText(polygon);
EList<GraphicsAlgorithm> graphicsAlgorithmChildren = graphicsAlgorithm
.getGraphicsAlgorithmChildren();
for (int i = 0; i < graphicsAlgorithmChildren.size(); i++) {
// Flip polygon children
if (graphicsAlgorithmChildren.get(i) instanceof Polygon)
this.flipVertically(graphicsAlgorithmChildren.get(i));
// Update relative anchors when the ga modification is done
if (i == graphicsAlgorithmChildren.size() - 1)
createBoxAnchorSet(this.getContainerShape(graphicsAlgorithm));
}
}
// ----------------------------------------
// ----------- Resizable Node -------------
@Override
/*
* (non-Javadoc)
*
* @see
* org.eclipse.fmc.blockdiagram.editor.node.ResizableNode#resize(org.eclipse.graphiti
* .mm.algorithms.GraphicsAlgorithm, int, int, int, int)
*/
public void resize(GraphicsAlgorithm algorithm, int x, int y, int width,
int height) {
ga.setLocationAndSize(algorithm, x, y, width, height);
ContainerShape container = this.getContainerShape(algorithm);
resizeBoxAnchorSet(container, x, y, width, height);
}
// ----------------------------------------
// ----------- Multiple Node --------------
@Override
/*
* (non-Javadoc)
*
* @see org.eclipse.fmc.blockdiagram.editor.node.AbstractNode#
* synchronizeMultiInstanceChildren
* (org.eclipse.graphiti.mm.pictograms.ContainerShape)
*/
public void synchronizeMultiInstanceChildren(
ContainerShape firstLevelContainer) {
if (isMultipleInstances(firstLevelContainer)) {
GraphicsAlgorithm foregroundGa = getMultiInstanceChild(
firstLevelContainer, true);
GraphicsAlgorithm backgroundGa = getMultiInstanceChild(
firstLevelContainer, false);
if (foregroundGa != null && backgroundGa != null) {
if (foregroundGa instanceof Polygon) {
Polygon foregroundPolygon = (Polygon) foregroundGa;
Polygon backgroundPolygon = (Polygon) backgroundGa;
int i = 0;
Point backgroundPoint;
for (Point mainPoint : foregroundPolygon.getPoints()) {
backgroundPoint = backgroundPolygon.getPoints()
.get(i++);
backgroundPoint.setX(mainPoint.getX());
backgroundPoint.setY(mainPoint.getY());
backgroundPoint.setAfter(mainPoint.getAfter());
backgroundPoint.setBefore(mainPoint.getBefore());
}
}
}
}
}
}