--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.g2d.element.handler.impl;\r
+\r
+import java.awt.BasicStroke;\r
+import java.awt.Color;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Path2D;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.g2d.element.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.SceneGraphNodeKey;\r
+import org.simantics.g2d.element.handler.SceneGraph;\r
+import org.simantics.g2d.multileveldiagram.TransitionFunction;\r
+import org.simantics.scenegraph.Node;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.g2d.nodes.ShapeNode;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+\r
+/**\r
+ * @See {@link TransitionFunction}\r
+ * @author Toni Kalajainen\r
+ */\r
+public class TransitionFunctionPainter implements SceneGraph {\r
+ /**\r
+ * \r
+ */\r
+ private static final long serialVersionUID = 218125859645762515L;\r
+ TransitionFunction func;\r
+ Path2D path;\r
+ final static BasicStroke STROKE = new BasicStroke(1.0f);\r
+ \r
+ public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");\r
+\r
+ public TransitionFunctionPainter(TransitionFunction func) {\r
+ this.func = func;\r
+ path = new Path2D.Double();\r
+ for (int i=0; i<101; i++)\r
+ {\r
+ double x = ((double)i)/100;\r
+ double y = func.f(x);\r
+ if (i==0)\r
+ path.moveTo(x, 1-y);\r
+ else\r
+ path.lineTo(x, 1-y);\r
+ } \r
+ }\r
+\r
+ @Override\r
+ public void cleanup(IElement e) {\r
+ Node node = e.removeHint(SG_NODE);\r
+ if (node != null)\r
+ node.remove();\r
+ }\r
+\r
+ @Override\r
+ public void init(IElement e, G2DParentNode parent) {\r
+ ShapeNode node = (ShapeNode) e.getHint(SG_NODE);\r
+ if (node == null) {\r
+ node = parent.addNode(ShapeNode.class);\r
+ e.setHint(SG_NODE, node);\r
+ }\r
+ \r
+ Rectangle2D bounds = ElementUtils.getElementBounds(e);\r
+ \r
+ AffineTransform transform = AffineTransform.getTranslateInstance(-bounds.getMinX(), -bounds.getMinY());\r
+ transform.scale(bounds.getWidth(), bounds.getHeight());\r
+ \r
+ node.setTransform(transform);\r
+ node.setStroke(STROKE);\r
+ node.setScaleStroke(true);\r
+ node.setColor(Color.BLACK);\r
+ node.setShape(path);\r
+ }\r
+}\r