]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/RelationshipNode.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / RelationshipNode.java
diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/RelationshipNode.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/RelationshipNode.java
new file mode 100644 (file)
index 0000000..f9b05b6
--- /dev/null
@@ -0,0 +1,106 @@
+/*******************************************************************************\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.Shape;\r
+import java.awt.Stroke;\r
+import java.awt.geom.Line2D;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.scenegraph.g2d.G2DNode;\r
+import org.simantics.scenegraph.utils.GeometryUtils;\r
+\r
+public class RelationshipNode 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 Shape bounds1 = null;\r
+    protected Shape bounds2 = null;\r
+\r
+    private transient Rectangle2D boundsInLocal = null;\r
+    private transient float phase = 0;\r
+\r
+    @SyncField({"boxstroke", "linestroke", "boxcolor", "linecolor", "bounds1", "bounds2"})\r
+    public void init(Stroke boxStroke, Stroke lineStroke, Color boxColor, Color lineColor, Shape bounds1, Shape bounds2) {\r
+        this.boxStroke = boxStroke;\r
+        this.lineStroke = lineStroke;\r
+        this.boxColor = boxColor;\r
+        this.lineColor = lineColor;\r
+        this.bounds1 = bounds1;\r
+        this.bounds2 = bounds2;\r
+        this.boundsInLocal = calculateLocalBounds();\r
+    }\r
+\r
+    @Override\r
+    public void render(Graphics2D g) {\r
+        if (bounds1 == null || bounds2 == 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
+        Rectangle2D rect = bounds1.getBounds2D();\r
+\r
+        if (boxStroke != null && boxColor != null) {\r
+            g.setStroke(GeometryUtils.scaleAndOffsetStroke(boxStroke, scale, scaledPhase));\r
+            g.setColor(boxColor);\r
+            GeometryUtils.expandRectangle(rect, PADDING, PADDING, PADDING, PADDING);\r
+            g.draw(rect);\r
+        }\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
+            Rectangle2D rect2 = bounds2.getBounds2D();\r
+            g.draw(new Line2D.Double(rect.getCenterX(), rect.getCenterY(), rect2.getCenterX(), rect2.getCenterY()));\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public Rectangle2D getBoundsInLocal() {\r
+        return boundsInLocal;\r
+    }\r
+\r
+    private Rectangle2D calculateLocalBounds() {\r
+        if (bounds1 == null || bounds2 == null)\r
+            return null;\r
+\r
+        Rectangle2D rect  = bounds1.getBounds2D();\r
+        Rectangle2D rect2 = bounds2.getBounds2D();\r
+\r
+        double x1 = rect.getCenterX();\r
+        double x2 = rect2.getCenterX();\r
+        double y1 = rect.getCenterY();\r
+        double y2 = rect2.getCenterY();\r
+        double minx = Math.min(x1, x2);\r
+        double maxx = Math.max(x1, x2);\r
+        double miny = Math.min(y1, y2);\r
+        double maxy = Math.max(y1, y2);\r
+\r
+        return new Rectangle2D.Double(minx, miny, maxx - minx, maxy - miny);\r
+    }\r
+\r
+}\r