| 1 26 27 package org.nightlabs.editor2d.viewer.tool; 28 29 import java.awt.Color ; 30 import java.awt.Rectangle ; 31 import java.util.List ; 32 33 import org.apache.log4j.Logger; 34 import org.nightlabs.editor2d.DrawComponent; 35 import org.nightlabs.editor2d.RectangleDrawComponent; 36 import org.nightlabs.editor2d.ShapeDrawComponent; 37 import org.nightlabs.editor2d.impl.RectangleDrawComponentImpl; 38 import org.nightlabs.editor2d.impl.ShapeDrawComponentImpl; 39 import org.nightlabs.editor2d.j2d.GeneralShape; 40 import org.nightlabs.editor2d.viewer.IDrawComponentConditional; 41 import org.nightlabs.editor2d.viewer.event.MouseEvent; 42 43 44 public class SelectTool 45 extends AbstractTool 46 { 47 public static final Logger LOGGER = Logger.getLogger(SelectTool.class); 48 49 protected RectangleDrawComponent rect; 50 protected boolean rectAdded = false; 51 protected DrawComponent rollOverDC = null; 52 54 public void activate() 55 { 56 super.activate(); 57 initRectangle(); 58 } 59 60 public void deactivate() 61 { 62 super.deactivate(); 63 rect = null; 64 rollOverDC = null; 65 } 67 68 protected RectangleDrawComponent initRectangle() 69 { 70 rect = new RectangleDrawComponentImpl(); 71 Rectangle r = new Rectangle (0, 0, 10, 10); 72 rect.setGeneralShape(new GeneralShape(r)); 73 rect.setLineColor(Color.BLACK); 74 rect.setFill(false); 75 rect.setLineWidth(2); 76 rect.setLineStyle(2); 77 return rect; 78 } 79 80 public void mouseMoved(MouseEvent me) 81 { 82 super.mouseMoved(me); 83 84 int currentX = getRelativeX(currentPoint.x); 85 int currentY = getRelativeY(currentPoint.y); 86 87 if (leftPressed || rightPressed) 89 { 90 int startX = getRelativeX(startPoint.x); 91 int startY = getRelativeY(startPoint.y); 92 int width = (int) Math.abs(currentX - startX); 93 int height = (int) Math.abs(currentY - startY); 94 95 if (!rectAdded) { 96 initRectangle(); 97 rect.setLocation(startX, startY); 98 addToTempContent(rect); 99 rectAdded = true; 100 } 101 102 if (startX > currentX) 103 rect.setX(currentX); 104 if (width != 0) 105 rect.setWidth(width); 106 107 if (startY > currentY) 108 rect.setY(currentY); 109 if (height != 0) 110 rect.setHeight(height); 111 112 repaint(); 113 } 114 115 if (rollOverDC != null) { 117 removeTempContent(rollOverDC); 118 rollOverDC = null; 119 120 123 repaint(); 124 } 125 126 DrawComponent dc = getViewer().getHitTestManager().findObjectAt( 127 getViewer().getDrawComponent(), currentX, currentY, conditional, null); 128 129 if (dc != null) 130 { 131 rollOverDC = createRollOverDrawComponent(dc); 132 addToTempContent(rollOverDC); 133 134 137 repaint(); 138 } 139 } 140 141 public void mousePressed(MouseEvent me) 142 { 143 super.mousePressed(me); 144 int startX = getRelativeX(startPoint.x); 145 int startY = getRelativeY(startPoint.y); 146 checkDrawComponents(startX, startY); 147 } 148 149 public void mouseReleased(MouseEvent me) 150 { 151 super.mouseReleased(me); 152 rectAdded = false; 153 removeTempContent(rect); 154 if (rect != null) 155 { 156 if (leftPressed) 157 checkDrawComponents(rect.getBounds()); 158 if (rightPressed) 159 getViewer().getZoomSupport().zoomTo(rect.getBounds()); 160 } 161 leftPressed = false; 162 rightPressed = false; 163 rect = null; 164 } 165 166 protected void checkDrawComponents(int x, int y) 167 { 168 getViewer().getSelectionManager().clearSelection(true); 169 DrawComponent dc = getViewer().getHitTestManager().findObjectAt(getViewer().getDrawComponent(), 170 x, y, conditional, null); 171 if (dc != null) { 172 getViewer().getSelectionManager().addSelectedDrawComponent(dc); 173 } 174 } 175 176 protected void checkDrawComponents(Rectangle r) 177 { 178 getViewer().getSelectionManager().clearSelection(true); 179 List drawComponents = getViewer().getHitTestManager().findObjectsAt(getViewer().getDrawComponent(), 180 r, conditional, null); 181 if (!drawComponents.isEmpty()) { 182 getViewer().getSelectionManager().addSelectedDrawComponents(drawComponents); 183 } 184 } 185 186 protected IDrawComponentConditional conditional = null; 188 public void setConditional(IDrawComponentConditional conditional) { 189 this.conditional = conditional; 190 } 191 public IDrawComponentConditional getConditional() { 192 return conditional; 193 } 194 195 protected DrawComponent createRollOverDrawComponent(DrawComponent dc) 196 { 197 if (dc instanceof ShapeDrawComponent) { 198 ShapeDrawComponent original = (ShapeDrawComponent) dc; 199 ShapeDrawComponent sdc = new ShapeDrawComponentImpl(); 200 sdc.setGeneralShape((GeneralShape)original.getGeneralShape().clone()); 201 sdc.setLineWidth(3); 202 sdc.setFill(false); 203 sdc.setLineColor(Color.WHITE); 204 return sdc; 205 } 206 else { 207 RectangleDrawComponent rect = new RectangleDrawComponentImpl(); 208 rect.setGeneralShape(new GeneralShape(dc.getBounds())); 209 rect.setLineWidth(3); 210 rect.setFill(false); 211 rect.setLineColor(Color.WHITE); 212 return rect; 213 } 214 } 215 216 } 237 | Popular Tags |