]> gerrit.simantics Code Review - simantics/platform.git/blob - impl/TransitionFunctionPainter.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / impl / TransitionFunctionPainter.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.element.handler.impl;\r
13 \r
14 import java.awt.BasicStroke;\r
15 import java.awt.Color;\r
16 import java.awt.geom.AffineTransform;\r
17 import java.awt.geom.Path2D;\r
18 import java.awt.geom.Rectangle2D;\r
19 \r
20 import org.simantics.g2d.element.ElementUtils;\r
21 import org.simantics.g2d.element.IElement;\r
22 import org.simantics.g2d.element.SceneGraphNodeKey;\r
23 import org.simantics.g2d.element.handler.SceneGraph;\r
24 import org.simantics.g2d.multileveldiagram.TransitionFunction;\r
25 import org.simantics.scenegraph.Node;\r
26 import org.simantics.scenegraph.g2d.G2DParentNode;\r
27 import org.simantics.scenegraph.g2d.nodes.ShapeNode;\r
28 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
29 \r
30 /**\r
31  * @See {@link TransitionFunction}\r
32  * @author Toni Kalajainen\r
33  */\r
34 public class TransitionFunctionPainter implements SceneGraph {\r
35         /**\r
36          * \r
37          */\r
38         private static final long serialVersionUID = 218125859645762515L;\r
39         TransitionFunction func;\r
40         Path2D path;\r
41         final static BasicStroke STROKE = new BasicStroke(1.0f);\r
42         \r
43     public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");\r
44 \r
45         public TransitionFunctionPainter(TransitionFunction func) {\r
46                 this.func = func;\r
47                 path = new Path2D.Double();\r
48                 for (int i=0; i<101; i++)\r
49                 {\r
50                         double x = ((double)i)/100;\r
51                         double y = func.f(x);\r
52                         if (i==0)\r
53                                 path.moveTo(x, 1-y);\r
54                         else\r
55                                 path.lineTo(x, 1-y);\r
56                 }               \r
57         }\r
58 \r
59         @Override\r
60         public void cleanup(IElement e) {\r
61        Node node = e.removeHint(SG_NODE);\r
62        if (node != null)\r
63            node.remove();\r
64         }\r
65 \r
66         @Override\r
67         public void init(IElement e, G2DParentNode parent) {\r
68                 ShapeNode node = (ShapeNode) e.getHint(SG_NODE);\r
69                 if (node == null) {\r
70                         node = parent.addNode(ShapeNode.class);\r
71                 e.setHint(SG_NODE, node);\r
72                 }\r
73                 \r
74                 Rectangle2D bounds = ElementUtils.getElementBounds(e);\r
75                 \r
76                 AffineTransform transform = AffineTransform.getTranslateInstance(-bounds.getMinX(), -bounds.getMinY());\r
77                 transform.scale(bounds.getWidth(), bounds.getHeight());\r
78                 \r
79                 node.setTransform(transform);\r
80                 node.setStroke(STROKE);\r
81                 node.setScaleStroke(true);\r
82                 node.setColor(Color.BLACK);\r
83                 node.setShape(path);\r
84         }\r
85 }\r