]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/RelationshipNode2.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / RelationshipNode2.java
diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/RelationshipNode2.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/RelationshipNode2.java
new file mode 100644 (file)
index 0000000..c4fb1ae
--- /dev/null
@@ -0,0 +1,88 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.scenegraph.g2d.nodes;\r
+\r
+import java.awt.Color;\r
+import java.awt.Graphics2D;\r
+import java.awt.Stroke;\r
+import java.awt.geom.Line2D;\r
+import java.awt.geom.Point2D;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.scenegraph.g2d.G2DNode;\r
+import org.simantics.scenegraph.utils.GeometryUtils;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class RelationshipNode2 extends G2DNode {\r
+\r
+    private static final long serialVersionUID = 3517235896431855937L;\r
+\r
+    public static transient double PADDING = 1;\r
+\r
+    protected Stroke boxStroke = null;\r
+    protected Stroke lineStroke = null;\r
+    protected Color boxColor = null;\r
+    protected Color lineColor = null;\r
+    protected Point2D p1 = null;\r
+    protected Point2D p2 = null;\r
+\r
+    private transient Rectangle2D boundsInLocal = null;\r
+    private transient float phase = 0;\r
+    private transient Line2D line = new Line2D.Double();\r
+\r
+    @SyncField({"linestroke", "linecolor", "p1", "p2"})\r
+    public void init(Stroke lineStroke, Color lineColor, Point2D p1, Point2D p2) {\r
+        this.lineStroke = lineStroke;\r
+        this.lineColor = lineColor;\r
+        this.p1 = p1;\r
+        this.p2 = p2;\r
+        this.boundsInLocal = calculateLocalBounds();\r
+    }\r
+\r
+    @Override\r
+    public void render(Graphics2D g) {\r
+        if (p1 == null || p2 == null)\r
+            return;\r
+\r
+        phase += 0.2;\r
+        if (phase > 10)\r
+            phase -= 10;\r
+\r
+        float scale = (float) (1.0 / GeometryUtils.getScale(g.getTransform()));\r
+        float scaledPhase = scale * phase;\r
+\r
+        if (lineStroke != null && lineColor != null) {\r
+            if (lineStroke != boxStroke)\r
+                g.setStroke(GeometryUtils.scaleAndOffsetStroke(lineStroke, scale, scaledPhase));\r
+            if (lineColor != boxColor)\r
+                g.setColor(lineColor);\r
+            line.setLine(p1, p2);\r
+            g.draw(line);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public Rectangle2D getBoundsInLocal() {\r
+        return boundsInLocal;\r
+    }\r
+\r
+    private Rectangle2D calculateLocalBounds() {\r
+        if (p1 == null || p2 == null)\r
+            return null;\r
+        Rectangle2D r = new Rectangle2D.Double();\r
+        r.setFrameFromDiagonal(p1, p2);\r
+        return r;\r
+    }\r
+\r
+}\r