]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/RelationshipNode2.java
Merge commit 'd186091'
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / RelationshipNode2.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.scenegraph.g2d.nodes;\r
13 \r
14 import java.awt.Color;\r
15 import java.awt.Graphics2D;\r
16 import java.awt.Stroke;\r
17 import java.awt.geom.Line2D;\r
18 import java.awt.geom.Point2D;\r
19 import java.awt.geom.Rectangle2D;\r
20 \r
21 import org.simantics.scenegraph.g2d.G2DNode;\r
22 import org.simantics.scenegraph.utils.GeometryUtils;\r
23 \r
24 /**\r
25  * @author Tuukka Lehtonen\r
26  */\r
27 public class RelationshipNode2 extends G2DNode {\r
28 \r
29     private static final long serialVersionUID = 3517235896431855937L;\r
30 \r
31     public static transient double PADDING = 1;\r
32 \r
33     protected Stroke boxStroke = null;\r
34     protected Stroke lineStroke = null;\r
35     protected Color boxColor = null;\r
36     protected Color lineColor = null;\r
37     protected Point2D p1 = null;\r
38     protected Point2D p2 = null;\r
39 \r
40     private transient Rectangle2D boundsInLocal = null;\r
41     private transient float phase = 0;\r
42     private transient Line2D line = new Line2D.Double();\r
43 \r
44     @SyncField({"linestroke", "linecolor", "p1", "p2"})\r
45     public void init(Stroke lineStroke, Color lineColor, Point2D p1, Point2D p2) {\r
46         this.lineStroke = lineStroke;\r
47         this.lineColor = lineColor;\r
48         this.p1 = p1;\r
49         this.p2 = p2;\r
50         this.boundsInLocal = calculateLocalBounds();\r
51     }\r
52 \r
53     @Override\r
54     public void render(Graphics2D g) {\r
55         if (p1 == null || p2 == null)\r
56             return;\r
57 \r
58         phase += 0.2;\r
59         if (phase > 10)\r
60             phase -= 10;\r
61 \r
62         float scale = (float) (1.0 / GeometryUtils.getScale(g.getTransform()));\r
63         float scaledPhase = scale * phase;\r
64 \r
65         if (lineStroke != null && lineColor != null) {\r
66             if (lineStroke != boxStroke)\r
67                 g.setStroke(GeometryUtils.scaleAndOffsetStroke(lineStroke, scale, scaledPhase));\r
68             if (lineColor != boxColor)\r
69                 g.setColor(lineColor);\r
70             line.setLine(p1, p2);\r
71             g.draw(line);\r
72         }\r
73     }\r
74 \r
75     @Override\r
76     public Rectangle2D getBoundsInLocal() {\r
77         return boundsInLocal;\r
78     }\r
79 \r
80     private Rectangle2D calculateLocalBounds() {\r
81         if (p1 == null || p2 == null)\r
82             return null;\r
83         Rectangle2D r = new Rectangle2D.Double();\r
84         r.setFrameFromDiagonal(p1, p2);\r
85         return r;\r
86     }\r
87 \r
88 }\r