1 18 package org.apache.batik.gvt; 19 20 import java.awt.Graphics2D ; 21 import java.awt.Shape ; 22 import java.awt.geom.Point2D ; 23 import java.awt.geom.Rectangle2D ; 24 25 import org.apache.batik.util.HaltingThread; 26 27 33 public class ShapeNode extends AbstractGraphicsNode { 34 35 38 protected Shape shape; 39 40 43 protected ShapePainter shapePainter; 44 45 48 private Rectangle2D primitiveBounds; 49 50 53 private Rectangle2D geometryBounds; 54 55 58 private Rectangle2D sensitiveBounds; 59 60 63 private Shape paintedArea; 64 65 68 private Shape sensitiveArea; 69 70 73 public ShapeNode() {} 74 75 79 84 public void setShape(Shape newShape) { 85 fireGraphicsNodeChangeStarted(); 86 invalidateGeometryCache(); 87 this.shape = newShape; 88 if(this.shapePainter != null){ 89 if (newShape != null) { 90 this.shapePainter.setShape(newShape); 91 } else { 92 this.shapePainter = null; 93 } 94 } 95 fireGraphicsNodeChangeCompleted(); 96 } 97 98 101 public Shape getShape() { 102 return shape; 103 } 104 105 111 public void setShapePainter(ShapePainter newShapePainter) { 112 if (shape == null) return; 114 fireGraphicsNodeChangeStarted(); 115 invalidateGeometryCache(); 116 this.shapePainter = newShapePainter; 117 if(shapePainter != null && shape != this.shapePainter.getShape()){ 118 shapePainter.setShape(shape); 119 } 120 fireGraphicsNodeChangeCompleted(); 121 } 122 123 127 public ShapePainter getShapePainter() { 128 return shapePainter; 129 } 130 131 135 140 public void paint(Graphics2D g2d) { 141 if (isVisible) 142 super.paint(g2d); 143 } 144 145 150 public void primitivePaint(Graphics2D g2d) { 151 if (shapePainter != null) { 152 shapePainter.paint(g2d); 153 } 154 } 155 156 160 165 protected void invalidateGeometryCache() { 166 super.invalidateGeometryCache(); 167 primitiveBounds = null; 168 geometryBounds = null; 169 sensitiveBounds = null; 170 paintedArea = null; 171 sensitiveArea = null; 172 } 173 174 public void setPointerEventType(int pointerEventType) { 175 super.setPointerEventType(pointerEventType); 176 sensitiveBounds = null; 177 sensitiveArea = null; 178 } 179 185 public boolean contains(Point2D p) { 186 switch(pointerEventType) { 187 case VISIBLE_PAINTED: 188 case VISIBLE_FILL: 189 case VISIBLE_STROKE: 190 case VISIBLE: 191 if (!isVisible) return false; 192 case PAINTED: 194 case FILL: 195 case STROKE: 196 case ALL: { 197 Rectangle2D b = getSensitiveBounds(); 198 if (b == null || !b.contains(p)) 199 return false; 200 201 return inSensitiveArea(p); 202 } 203 case NONE: 204 default: 205 return false; 206 } 207 } 208 209 215 public boolean intersects(Rectangle2D r) { 216 Rectangle2D b = getBounds(); 217 if (b != null) { 218 return (b.intersects(r) && 219 paintedArea != null && 220 paintedArea.intersects(r)); 221 } 222 return false; 223 } 224 225 228 public Rectangle2D getPrimitiveBounds() { 229 if (!isVisible) return null; 230 if (shape == null) return null; 231 if (primitiveBounds != null) 232 return primitiveBounds; 233 234 if (shapePainter == null) 235 primitiveBounds = shape.getBounds2D(); 236 else 237 primitiveBounds = shapePainter.getPaintedBounds2D(); 238 239 if (HaltingThread.hasBeenHalted()) { 241 invalidateGeometryCache(); 245 } 246 return primitiveBounds; 247 } 248 249 public boolean inSensitiveArea(Point2D pt) { 250 if (shapePainter == null) 251 return false; 252 253 ShapePainter strokeShapePainter = null; 255 ShapePainter fillShapePainter = null; 256 if (shapePainter instanceof StrokeShapePainter) { 257 strokeShapePainter = shapePainter; 258 } else if (shapePainter instanceof FillShapePainter) { 259 fillShapePainter = shapePainter; 260 } else if (shapePainter instanceof CompositeShapePainter) { 261 CompositeShapePainter cp = (CompositeShapePainter)shapePainter; 262 263 for (int i=0; i < cp.getShapePainterCount(); ++i) { 264 ShapePainter sp = cp.getShapePainter(i); 265 if (sp instanceof StrokeShapePainter) { 266 strokeShapePainter = sp; 267 } else if (sp instanceof FillShapePainter) { 268 fillShapePainter = sp; 269 } 270 } 271 } else { 272 return false; } 274 275 switch(pointerEventType) { 276 case VISIBLE_PAINTED: 277 case PAINTED: 278 return shapePainter.inPaintedArea(pt); 279 case VISIBLE: 280 case ALL: 281 return shapePainter.inSensitiveArea(pt); 282 case VISIBLE_FILL: 283 case FILL: 284 if (fillShapePainter != null) 285 return fillShapePainter.inSensitiveArea(pt); 286 break; 287 case VISIBLE_STROKE: 288 case STROKE: 289 if (strokeShapePainter != null) 290 return strokeShapePainter.inSensitiveArea(pt); 291 break; 292 case NONE: 293 default: 294 } 296 return false; 297 } 298 299 304 public Rectangle2D getSensitiveBounds() { 305 if (sensitiveBounds != null) 306 return sensitiveBounds; 307 308 if (shapePainter == null) 309 return null; 310 311 ShapePainter strokeShapePainter = null; 313 ShapePainter fillShapePainter = null; 314 if (shapePainter instanceof StrokeShapePainter) { 315 strokeShapePainter = shapePainter; 316 } else if (shapePainter instanceof FillShapePainter) { 317 fillShapePainter = shapePainter; 318 } else if (shapePainter instanceof CompositeShapePainter) { 319 CompositeShapePainter cp = (CompositeShapePainter)shapePainter; 320 321 for (int i=0; i < cp.getShapePainterCount(); ++i) { 322 ShapePainter sp = cp.getShapePainter(i); 323 if (sp instanceof StrokeShapePainter) { 324 strokeShapePainter = sp; 325 } else if (sp instanceof FillShapePainter) { 326 fillShapePainter = sp; 327 } 328 } 329 } else return null; 331 332 switch(pointerEventType) { 333 case VISIBLE_PAINTED: 334 case PAINTED: 335 sensitiveBounds = shapePainter.getPaintedBounds2D(); 336 break; 337 case VISIBLE_FILL: 338 case FILL: 339 if (fillShapePainter != null) { 340 sensitiveBounds = fillShapePainter.getSensitiveBounds2D(); 341 } 342 break; 343 case VISIBLE_STROKE: 344 case STROKE: 345 if (strokeShapePainter != null) { 346 sensitiveBounds = strokeShapePainter.getSensitiveBounds2D(); 347 } 348 break; 349 case VISIBLE: 350 case ALL: 351 sensitiveBounds = shapePainter.getSensitiveBounds2D(); 352 break; 353 case NONE: 354 default: 355 } 357 return sensitiveBounds; 358 } 359 360 364 public Shape getSensitiveArea() { 365 if (sensitiveArea != null) 366 return sensitiveArea; 367 if (shapePainter == null) 368 return null; 369 370 ShapePainter strokeShapePainter = null; 372 ShapePainter fillShapePainter = null; 373 if (shapePainter instanceof StrokeShapePainter) { 374 strokeShapePainter = shapePainter; 375 } else if (shapePainter instanceof FillShapePainter) { 376 fillShapePainter = shapePainter; 377 } else if (shapePainter instanceof CompositeShapePainter) { 378 CompositeShapePainter cp = (CompositeShapePainter)shapePainter; 379 380 for (int i=0; i < cp.getShapePainterCount(); ++i) { 381 ShapePainter sp = cp.getShapePainter(i); 382 if (sp instanceof StrokeShapePainter) { 383 strokeShapePainter = sp; 384 } else if (sp instanceof FillShapePainter) { 385 fillShapePainter = sp; 386 } 387 } 388 } else return null; 390 391 switch(pointerEventType) { 392 case VISIBLE_PAINTED: 393 case PAINTED: 394 sensitiveArea = shapePainter.getPaintedArea(); 395 break; 396 case VISIBLE_FILL: 397 case FILL: 398 if (fillShapePainter != null) { 399 sensitiveArea = fillShapePainter.getSensitiveArea(); 400 } 401 break; 402 case VISIBLE_STROKE: 403 case STROKE: 404 if (strokeShapePainter != null) { 405 sensitiveArea = strokeShapePainter.getSensitiveArea(); 406 } 407 break; 408 case VISIBLE: 409 case ALL: 410 sensitiveArea = shapePainter.getSensitiveArea(); 411 break; 412 case NONE: 413 default: 414 } 416 return sensitiveArea; 417 } 418 419 425 public Rectangle2D getGeometryBounds(){ 426 if (geometryBounds == null) { 427 if (shape == null) { 428 return null; 429 } 430 geometryBounds = normalizeRectangle(shape.getBounds2D()); 431 } 432 return geometryBounds; 433 } 434 435 438 public Shape getOutline() { 439 return shape; 440 } 441 } 442 | Popular Tags |