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