]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/connection/Path2DOutlineShape.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / elementclass / connection / Path2DOutlineShape.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/connection/Path2DOutlineShape.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/connection/Path2DOutlineShape.java
new file mode 100644 (file)
index 0000000..e04de9c
--- /dev/null
@@ -0,0 +1,119 @@
+/*******************************************************************************\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.g2d.elementclass.connection;\r
+\r
+import java.awt.Rectangle;\r
+import java.awt.Shape;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Path2D;\r
+import java.awt.geom.PathIterator;\r
+import java.awt.geom.Point2D;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+public class Path2DOutlineShape implements Shape {\r
+\r
+       Path2D path;\r
+       \r
+       public Path2DOutlineShape(Path2D path) {\r
+               this.path = path;\r
+       }\r
+\r
+       @Override\r
+       public boolean contains(Point2D p) {\r
+               return false;\r
+       }\r
+\r
+       @Override\r
+       public boolean contains(Rectangle2D r) {\r
+               return false;\r
+       }\r
+\r
+       @Override\r
+       public boolean contains(double x, double y) {           \r
+               return false;\r
+       }\r
+\r
+       @Override\r
+       public boolean contains(double x, double y, double w, double h) {               \r
+               return false;\r
+       }\r
+\r
+       @Override\r
+       public Rectangle getBounds() {\r
+               return path.getBounds();\r
+       }\r
+\r
+       @Override\r
+       public Rectangle2D getBounds2D() {\r
+               return path.getBounds2D();\r
+       }\r
+\r
+       @Override\r
+       public PathIterator getPathIterator(AffineTransform at) {\r
+               return path.getPathIterator(at);\r
+       }\r
+\r
+       @Override\r
+       public PathIterator getPathIterator(AffineTransform at, double flatness) {\r
+               return path.getPathIterator(at, flatness);\r
+       }\r
+\r
+       @Override\r
+       public boolean intersects(Rectangle2D r) {\r
+               return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());\r
+       }\r
+\r
+       @Override\r
+       public boolean intersects(double x, double y, double w, double h) {\r
+               PathIterator it = path.getPathIterator(new AffineTransform(), 1.0);\r
+               double[] coords = new double[2];\r
+               double x0, y0, x1=0.0, y1=0.0;\r
+               while(!it.isDone()) {\r
+                       x0 = x1;\r
+                       y0 = y1;\r
+                       switch(it.currentSegment(coords)) {\r
+                       case PathIterator.SEG_MOVETO:\r
+                               x1 = coords[0];\r
+                               y1 = coords[1];\r
+                               break;\r
+                               \r
+                       case PathIterator.SEG_LINETO:\r
+                               x1 = coords[0];\r
+                               y1 = coords[1];\r
+                               \r
+                               // Lines are approximated by their bounding boxes. This works for\r
+                               // orthogonal paths.\r
+                               if(x0 < x1) {\r
+                                       if(x > x1 || x+w < x0)\r
+                                               break;                                          \r
+                               }\r
+                               else {\r
+                                       if(x > x0 || x+w < x1)\r
+                                               break;\r
+                               }\r
+\r
+                               if(y0 < y1) {\r
+                                       if(y > y1 || y+h < y0)\r
+                                               break;                                          \r
+                               }\r
+                               else {\r
+                                       if(y > y0 || y+h < y1)\r
+                                               break;\r
+                               }       \r
+                               return true;\r
+                       }                       \r
+                       it.next();\r
+               }\r
+               return false;\r
+       }\r
+       \r
+}\r