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.scenegraph.g2d.nodes;
\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
21 import org.simantics.scenegraph.g2d.G2DNode;
\r
22 import org.simantics.scenegraph.utils.GeometryUtils;
\r
25 * @author Tuukka Lehtonen
\r
27 public class RelationshipNode2 extends G2DNode {
\r
29 private static final long serialVersionUID = 3517235896431855937L;
\r
31 public static transient double PADDING = 1;
\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
40 private transient Rectangle2D boundsInLocal = null;
\r
41 private transient float phase = 0;
\r
42 private transient Line2D line = new Line2D.Double();
\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
50 this.boundsInLocal = calculateLocalBounds();
\r
54 public void render(Graphics2D g) {
\r
55 if (p1 == null || p2 == null)
\r
62 float scale = (float) (1.0 / GeometryUtils.getScale(g.getTransform()));
\r
63 float scaledPhase = scale * phase;
\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
76 public Rectangle2D getBoundsInLocal() {
\r
77 return boundsInLocal;
\r
80 private Rectangle2D calculateLocalBounds() {
\r
81 if (p1 == null || p2 == null)
\r
83 Rectangle2D r = new Rectangle2D.Double();
\r
84 r.setFrameFromDiagonal(p1, p2);
\r