| 1 26 27 package org.nightlabs.editor2d.viewer.util; 28 29 import java.awt.Rectangle ; 30 import java.awt.geom.Area ; 31 import java.awt.geom.Rectangle2D ; 32 import java.util.Collection ; 33 import java.util.Iterator ; 34 import java.util.LinkedList ; 35 import java.util.List ; 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 74 protected static int hitTolerance = 3; 75 76 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 return contains(sdc, x, y) ? sdc : null; 111 } 112 else { 113 return dc.getBounds().contains(x, y) ? dc : null; 114 } 115 } 116 117 128 public static List findObjectsAt(DrawComponent dc, int x, int y, 129 IDrawComponentConditional conditional, Collection excludeList) 130 { 131 List objects = findObjectsAt(dc, x, y); 132 for (Iterator 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 157 public static DrawComponent findObjectAt(DrawComponent dc, int x, int y, 158 IDrawComponentConditional conditional, Collection excludeList) 159 { 160 List objects = findObjectsAt(dc, x, y, conditional, excludeList); 161 return !objects.isEmpty() ? (DrawComponent) objects.get(0) : null; 162 } 163 164 176 public static List findObjectsAt(DrawComponent dc, int x, int y) 177 { 178 List l = new LinkedList (); 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 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 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 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 outerBounds = TransformUtil.expand(sdc.getBounds(), hitTolerance, hitTolerance, true); 227 Rectangle 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 outlineArea = new Area (outerGS); 233 Area innerArea = new Area (innerGS); 234 outlineArea.exclusiveOr(innerArea); 235 return outlineArea.contains(x,y); 236 } 237 } 238 239 245 public static boolean intersects(ShapeDrawComponent sdc, Rectangle2D r) 246 { 247 return sdc.getGeneralShape().intersects(r); 248 } 249 250 256 public static List findObjectsAt(DrawComponent dc, Rectangle2D r) 257 { 258 List l = new LinkedList (); 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 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 302 public static List findObjectsAt(DrawComponent dc, Rectangle2D r, 303 IDrawComponentConditional conditional, Collection excludeList) 304 { 305 List objects = findObjectsAt(dc, r); 306 for (Iterator 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 |