KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > viewer > util > ViewerUtil


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.viewer.util;
28
29 import java.awt.Rectangle JavaDoc;
30 import java.awt.geom.Area JavaDoc;
31 import java.awt.geom.Rectangle2D JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.LinkedList JavaDoc;
35 import java.util.List JavaDoc;
36
37 import org.nightlabs.editor2d.DrawComponent;
38 import org.nightlabs.editor2d.DrawComponentContainer;
39 import org.nightlabs.editor2d.Layer;
40 import org.nightlabs.editor2d.ShapeDrawComponent;
41 import org.nightlabs.editor2d.j2d.GeneralShape;
42 import org.nightlabs.editor2d.viewer.IDrawComponentConditional;
43
44 public class ViewerUtil
45 {
46 // /**
47
// *
48
// * @param mldc The MultiLayerDrawComponent to iterate through
49
// * @param x the x-Coordinate
50
// * @param y the y-Coordinate
51
// * @return the topmost DrawComponent in the Z-Order which contains x and y or null if
52
// * no drawComponent is found (invisible Layers are skipped)
53
// */
54
// public static DrawComponent findObjectAt(MultiLayerDrawComponent mldc, int x, int y)
55
// {
56
// int size = mldc.getDrawComponents().size();
57
// // Iterate through the layers from top to bottom and check if they are visible
58
// for (int i = size-1; i >= 0; i--)
59
// {
60
// Layer l = (Layer) mldc.getDrawComponents().get(i);
61
// if (l.isVisible() && l.getBounds().contains(x, y))
62
// {
63
// int layerSize = l.getDrawComponents().size();
64
// for (int j = layerSize - 1; j >= 0; j--)
65
// {
66
// DrawComponent dc = (DrawComponent) l.getDrawComponents().get(j);
67
// return findObjectAt(dc, x, y);
68
// }
69
// }
70
// }
71
// return null;
72
// }
73

74     protected static int hitTolerance = 3;
75     
76     /**
77      *
78      * @param dc the DrawComponent to iterate through
79      * @param x the x-Coordinate
80      * @param y the y-Coordinate
81      * @return the topmost DrawComponent in the Z-Order which contains x and y or null if
82      * no drawComponent is found
83      */

84     public static DrawComponent findObjectAt(DrawComponent dc, int x, int y)
85     {
86         if (dc instanceof DrawComponentContainer)
87         {
88             if (dc instanceof Layer)
89             {
90                 Layer layer = (Layer) dc;
91                 if (!layer.isVisible())
92                     return null;
93             }
94             DrawComponentContainer container = (DrawComponentContainer) dc;
95             int size = container.getDrawComponents().size();
96             if (container.getBounds().contains(x, y) && size != 0)
97             {
98                 for (int i = size - 1; i >= 0; i--) {
99                     DrawComponent child = (DrawComponent) container.getDrawComponents().get(i);
100                     if (findObjectAt(child, x, y) != null) {
101                         return findObjectAt(child, x, y);
102                     }
103                 }
104             }
105             return null;
106         }
107         if (dc instanceof ShapeDrawComponent) {
108             ShapeDrawComponent sdc = (ShapeDrawComponent) dc;
109             // TODO: calculate outlineArea for all not filled shapes once at initalization
110
return contains(sdc, x, y) ? sdc : null;
111         }
112         else {
113             return dc.getBounds().contains(x, y) ? dc : null;
114         }
115     }
116      
117     /**
118      *
119      * @param dc the DrawComponent to iterate through (if it is a DrawComponentContainer)
120      * @param x the x-Coordinate
121      * @param y the y-Coordinate
122      * @param conditional an optional (maybe null) IDrawComponentConditional to filter the returned Objects
123      * @param excludeList an optional (mybe null) Collection of excluded DrawComponents
124      * @return a List which contains all DrawComponents which contain x and y and fullfill
125      * the condition as well as are not included in the excludeList
126      * if no drawComponents are found an empty List is returned
127      */

128     public static List JavaDoc findObjectsAt(DrawComponent dc, int x, int y,
129             IDrawComponentConditional conditional, Collection JavaDoc excludeList)
130     {
131         List JavaDoc objects = findObjectsAt(dc, x, y);
132         for (Iterator JavaDoc it = objects.iterator(); it.hasNext(); )
133         {
134             DrawComponent drawComponent = (DrawComponent) it.next();
135             if (conditional != null) {
136                 if (!conditional.evalute(drawComponent))
137                     it.remove();
138             }
139             if (excludeList != null) {
140                 if (excludeList.contains(drawComponent))
141                     it.remove();
142             }
143         }
144         return objects;
145     }
146
147     /**
148      *
149      * @param dc the DrawComponent to iterate through
150      * @param x the x-Coordinate
151      * @param y the y-Coordinate
152      * @param conditional an optional (maybe null) IDrawComponentConditional to filter the returned Objects
153      * @param excludeList an optional (mybe null) Collection of excluded DrawComponents
154      * @return the topmost DrawComponent in the Z-Order which contains x and y and
155      * fullfills the condition and is not included in the excludeList
156      */

157     public static DrawComponent findObjectAt(DrawComponent dc, int x, int y,
158             IDrawComponentConditional conditional, Collection JavaDoc excludeList)
159     {
160         List JavaDoc objects = findObjectsAt(dc, x, y, conditional, excludeList);
161         return !objects.isEmpty() ? (DrawComponent) objects.get(0) : null;
162     }
163     
164     /**
165      * the order of the returned List represents the Z-Order of the hit-testing
166      * (first entry = topmost, last entry = bottommost)
167      *
168      *
169      * @param dc the DrawComponent to iterate through
170      * @param x the x-Coordinate
171      * @param y the y-Coordinate
172      * @return a List which contains all DrawComponents which contain x and y,
173      * if no drawComponents are found an empty List is returned
174      *
175      */

176     public static List JavaDoc findObjectsAt(DrawComponent dc, int x, int y)
177     {
178         List JavaDoc l = new LinkedList JavaDoc();
179         if (dc instanceof DrawComponentContainer)
180         {
181             if (dc instanceof Layer)
182             {
183                 Layer layer = (Layer) dc;
184                 if (!layer.isVisible())
185                     return l;
186             }
187             DrawComponentContainer container = (DrawComponentContainer) dc;
188             int size = container.getDrawComponents().size();
189             if (container.getBounds().contains(x, y) && size != 0)
190             {
191                 for (int i = size - 1; i >= 0; i--)
192                 {
193                     DrawComponent child = (DrawComponent) container.getDrawComponents().get(i);
194                     List JavaDoc childrenObjects = findObjectsAt(child, x, y);
195                     if (!childrenObjects.isEmpty()) {
196                         l.addAll(childrenObjects);
197                     }
198                 }
199             }
200         }
201         else {
202             if (dc instanceof ShapeDrawComponent) {
203                 ShapeDrawComponent sdc = (ShapeDrawComponent) dc;
204                 // TODO: calculate outlineArea for all not filled shapes once at initalization
205
if (contains(sdc, x, y))
206                     l.add(sdc);
207             }
208             else if (dc.getBounds().contains(x, y))
209              l.add(dc);
210         }
211         return l;
212     }
213     
214     /**
215      *
216      * @param sdc the ShapeDrawComponent to check for containment
217      * @param x the x-Coordinate
218      * @param y the y-Coordinate
219      * @return true if the ShapeDrawComponent contains x,y or false if not
220      */

221     public static boolean contains(ShapeDrawComponent sdc, double x, double y)
222     {
223         if (sdc.isFill())
224             return sdc.getGeneralShape().contains(x, y);
225         else {
226       Rectangle JavaDoc outerBounds = TransformUtil.expand(sdc.getBounds(), hitTolerance, hitTolerance, true);
227       Rectangle JavaDoc innerBounds = TransformUtil.shrink(sdc.getBounds(), hitTolerance, hitTolerance, true);
228       GeneralShape outerGS = (GeneralShape) sdc.getGeneralShape().clone();
229       GeneralShape innerGS = (GeneralShape) sdc.getGeneralShape().clone();
230       TransformUtil.transformGeneralShape(outerGS, sdc.getBounds(), outerBounds);
231       TransformUtil.transformGeneralShape(innerGS, sdc.getBounds(), innerBounds);
232       Area JavaDoc outlineArea = new Area JavaDoc(outerGS);
233       Area JavaDoc innerArea = new Area JavaDoc(innerGS);
234       outlineArea.exclusiveOr(innerArea);
235       return outlineArea.contains(x,y);
236         }
237     }
238     
239     /**
240      *
241      * @param sdc the ShapeDrawComponent to check for intersection
242      * @param r the Rectangle2D to check for
243      * @return true if the ShapeDrawComponent intersects r
244      */

245     public static boolean intersects(ShapeDrawComponent sdc, Rectangle2D JavaDoc r)
246     {
247         return sdc.getGeneralShape().intersects(r);
248     }
249     
250     /**
251      *
252      * @param dc the DrawComponent to iterate through (if it is a DrawComponentContainer)
253      * @param r the Rectangle to check for intersection
254      * @return a List of DrawCompoennts which intersects the Rectangle
255      */

256     public static List JavaDoc findObjectsAt(DrawComponent dc, Rectangle2D JavaDoc r)
257     {
258         List JavaDoc l = new LinkedList JavaDoc();
259         if (dc instanceof DrawComponentContainer)
260         {
261             if (dc instanceof Layer)
262             {
263                 Layer layer = (Layer) dc;
264                 if (!layer.isVisible())
265                     return l;
266             }
267             DrawComponentContainer container = (DrawComponentContainer) dc;
268             int size = container.getDrawComponents().size();
269             if (container.getBounds().intersects(r) && size != 0)
270             {
271                 for (int i = size - 1; i >= 0; i--)
272                 {
273                     DrawComponent child = (DrawComponent) container.getDrawComponents().get(i);
274                     List JavaDoc childrenObjects = findObjectsAt(child, r);
275                     if (!childrenObjects.isEmpty()) {
276                         l.addAll(childrenObjects);
277                     }
278                 }
279             }
280         }
281         else {
282             if (dc instanceof ShapeDrawComponent) {
283                 ShapeDrawComponent sdc = (ShapeDrawComponent) dc;
284                 if (intersects(sdc, r))
285                     l.add(sdc);
286             }
287             else if (dc.getBounds().intersects(r))
288              l.add(dc);
289         }
290         return l;
291     }
292     
293     /**
294      * @param dc the DrawComponent to iterate through (if it is a DrawComponentContainer)
295      * @param r the Rectangel to check for intersection
296      * @param conditional an optional (maybe null) IDrawComponentConditional to filter the returned Objects
297      * @param excludeList an optional (maybe null) Collection of excluded DrawComponents
298      * @return a List which contains all DrawComponents which intersect r and fullfill
299      * the condition as well as are not included in the excludeList
300      * if no drawComponents are found an empty List is returned
301      */

302     public static List JavaDoc findObjectsAt(DrawComponent dc, Rectangle2D JavaDoc r,
303             IDrawComponentConditional conditional, Collection JavaDoc excludeList)
304     {
305         List JavaDoc objects = findObjectsAt(dc, r);
306         for (Iterator JavaDoc it = objects.iterator(); it.hasNext(); )
307         {
308             DrawComponent drawComponent = (DrawComponent) it.next();
309             if (conditional != null) {
310                 if (!conditional.evalute(drawComponent))
311                     it.remove();
312             }
313             if (excludeList != null) {
314                 if (excludeList.contains(drawComponent))
315                     it.remove();
316             }
317         }
318         return objects;
319     }
320     
321 }
322
Popular Tags