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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.element.handler.impl;
\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
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
31 * @See {@link TransitionFunction}
\r
32 * @author Toni Kalajainen
\r
34 public class TransitionFunctionPainter implements SceneGraph {
\r
38 private static final long serialVersionUID = 218125859645762515L;
\r
39 TransitionFunction func;
\r
41 final static BasicStroke STROKE = new BasicStroke(1.0f);
\r
43 public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");
\r
45 public TransitionFunctionPainter(TransitionFunction func) {
\r
47 path = new Path2D.Double();
\r
48 for (int i=0; i<101; i++)
\r
50 double x = ((double)i)/100;
\r
51 double y = func.f(x);
\r
53 path.moveTo(x, 1-y);
\r
55 path.lineTo(x, 1-y);
\r
60 public void cleanup(IElement e) {
\r
61 Node node = e.removeHint(SG_NODE);
\r
67 public void init(IElement e, G2DParentNode parent) {
\r
68 ShapeNode node = (ShapeNode) e.getHint(SG_NODE);
\r
70 node = parent.addNode(ShapeNode.class);
\r
71 e.setHint(SG_NODE, node);
\r
74 Rectangle2D bounds = ElementUtils.getElementBounds(e);
\r
76 AffineTransform transform = AffineTransform.getTranslateInstance(-bounds.getMinX(), -bounds.getMinY());
\r
77 transform.scale(bounds.getWidth(), bounds.getHeight());
\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