1 27 28 package org.nightlabs.editor2d.figures; 29 30 import java.awt.Graphics2D ; 31 import java.awt.geom.Area ; 32 33 import org.apache.log4j.Logger; 34 import org.eclipse.draw2d.Figure; 35 import org.eclipse.draw2d.Graphics; 36 import org.eclipse.draw2d.J2DGraphics; 37 import org.eclipse.draw2d.geometry.Rectangle; 38 import org.eclipse.gef.editparts.ZoomListener; 39 import org.nightlabs.editor2d.DrawComponent; 40 import org.nightlabs.editor2d.ShapeDrawComponent; 41 import org.nightlabs.editor2d.j2d.GeneralShape; 42 import org.nightlabs.editor2d.render.Renderer; 43 import org.nightlabs.editor2d.util.J2DUtil; 44 45 46 public class DrawComponentFigure 47 extends Figure 49 implements RendererFigure 50 { 51 public static final Logger LOGGER = Logger.getLogger(DrawComponentFigure.class.getName()); 52 53 protected J2DGraphics j2d; 54 protected Graphics2D g2d; 55 public void paint(Graphics graphics) 56 { 57 if (graphics instanceof J2DGraphics) { 58 j2d = (J2DGraphics) graphics; 59 g2d = j2d.createGraphics2D(); 60 g2d.setClip(null); 61 if (renderer != null) 62 renderer.paint(drawComponent, g2d); 63 64 g2d.dispose(); 65 } 66 } 67 68 public void paint(Graphics2D graphics) 69 { 70 if (renderer == null && drawComponent != null) 71 renderer = drawComponent.getRenderer(); 72 if (renderer != null) 73 renderer.paint(drawComponent, graphics); 74 } 75 76 protected Renderer renderer; 77 public void setRenderer(Renderer renderer) { 78 this.renderer = renderer; 79 } 80 81 protected DrawComponent drawComponent; 82 public void setDrawComponent(DrawComponent drawComponent) { 83 this.drawComponent = drawComponent; 84 } 85 86 public boolean containsPoint(int x, int y) 87 { 88 if (drawComponent != null) { 89 if (drawComponent instanceof ShapeDrawComponent) { 90 ShapeDrawComponent sdc = (ShapeDrawComponent) drawComponent; 91 if (sdc.isFill()) { 92 return sdc.getGeneralShape().contains(x,y); 93 } 94 else 95 { 96 if (outlineArea == null) 97 { 98 Rectangle outerBounds = getBounds().getCopy(); 99 Rectangle innerBounds = getBounds().getCopy(); 100 outerBounds.expand((int)hitTolerance, (int)hitTolerance); 101 innerBounds.shrink((int)hitTolerance, (int)hitTolerance); 102 GeneralShape outerGS = (GeneralShape) sdc.getGeneralShape().clone(); 103 GeneralShape innerGS = (GeneralShape) sdc.getGeneralShape().clone(); 104 J2DUtil.transformGeneralShape(outerGS, getBounds(), outerBounds); 105 J2DUtil.transformGeneralShape(innerGS, getBounds(), innerBounds); 106 outlineArea = new Area (outerGS); 107 Area innerArea = new Area (innerGS); 108 outlineArea.exclusiveOr(innerArea); 109 } 110 boolean contains = outlineArea.contains(x,y); 111 outlineArea = null; 112 return contains; 113 } 114 } 115 } 116 return super.containsPoint(x, y); 117 } 118 119 public static final double DEFAULT_HIT_TOLERANCE = 3; 120 protected double hitTolerance = DEFAULT_HIT_TOLERANCE; 121 public double getHitTolerance() { 122 return hitTolerance; 123 } 124 public void setHitTolerance(double hitTolerance) { 125 this.hitTolerance = hitTolerance; 126 } 127 128 protected Area outlineArea; 129 130 protected ZoomListener zoomListener = new ZoomListener() { 131 public void zoomChanged(double zoom) { 132 hitTolerance = DEFAULT_HIT_TOLERANCE / zoom; 133 } 134 }; 135 public ZoomListener getZoomListener() { 136 return zoomListener; 137 } 138 139 public Rectangle getBounds() { 140 if (drawComponent != null) 141 return J2DUtil.toDraw2D(drawComponent.getBounds()); 142 143 return super.getBounds(); 144 } 145 146 169 } 170 | Popular Tags |