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