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