]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.elementclass.connection;\r
13 \r
14 import java.awt.Rectangle;\r
15 import java.awt.Shape;\r
16 import java.awt.geom.AffineTransform;\r
17 import java.awt.geom.Path2D;\r
18 import java.awt.geom.PathIterator;\r
19 import java.awt.geom.Point2D;\r
20 import java.awt.geom.Rectangle2D;\r
21 \r
22 public class Path2DOutlineShape implements Shape {\r
23 \r
24         Path2D path;\r
25         \r
26         public Path2DOutlineShape(Path2D path) {\r
27                 this.path = path;\r
28         }\r
29 \r
30         @Override\r
31         public boolean contains(Point2D p) {\r
32                 return false;\r
33         }\r
34 \r
35         @Override\r
36         public boolean contains(Rectangle2D r) {\r
37                 return false;\r
38         }\r
39 \r
40         @Override\r
41         public boolean contains(double x, double y) {           \r
42                 return false;\r
43         }\r
44 \r
45         @Override\r
46         public boolean contains(double x, double y, double w, double h) {               \r
47                 return false;\r
48         }\r
49 \r
50         @Override\r
51         public Rectangle getBounds() {\r
52                 return path.getBounds();\r
53         }\r
54 \r
55         @Override\r
56         public Rectangle2D getBounds2D() {\r
57                 return path.getBounds2D();\r
58         }\r
59 \r
60         @Override\r
61         public PathIterator getPathIterator(AffineTransform at) {\r
62                 return path.getPathIterator(at);\r
63         }\r
64 \r
65         @Override\r
66         public PathIterator getPathIterator(AffineTransform at, double flatness) {\r
67                 return path.getPathIterator(at, flatness);\r
68         }\r
69 \r
70         @Override\r
71         public boolean intersects(Rectangle2D r) {\r
72                 return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());\r
73         }\r
74 \r
75         @Override\r
76         public boolean intersects(double x, double y, double w, double h) {\r
77                 PathIterator it = path.getPathIterator(new AffineTransform(), 1.0);\r
78                 double[] coords = new double[2];\r
79                 double x0, y0, x1=0.0, y1=0.0;\r
80                 while(!it.isDone()) {\r
81                         x0 = x1;\r
82                         y0 = y1;\r
83                         switch(it.currentSegment(coords)) {\r
84                         case PathIterator.SEG_MOVETO:\r
85                                 x1 = coords[0];\r
86                                 y1 = coords[1];\r
87                                 break;\r
88                                 \r
89                         case PathIterator.SEG_LINETO:\r
90                                 x1 = coords[0];\r
91                                 y1 = coords[1];\r
92                                 \r
93                                 // Lines are approximated by their bounding boxes. This works for\r
94                                 // orthogonal paths.\r
95                                 if(x0 < x1) {\r
96                                         if(x > x1 || x+w < x0)\r
97                                                 break;                                          \r
98                                 }\r
99                                 else {\r
100                                         if(x > x0 || x+w < x1)\r
101                                                 break;\r
102                                 }\r
103 \r
104                                 if(y0 < y1) {\r
105                                         if(y > y1 || y+h < y0)\r
106                                                 break;                                          \r
107                                 }\r
108                                 else {\r
109                                         if(y > y0 || y+h < y1)\r
110                                                 break;\r
111                                 }       \r
112                                 return true;\r
113                         }                       \r
114                         it.next();\r
115                 }\r
116                 return false;\r
117         }\r
118         \r
119 }\r