1 27 28 package org.nightlabs.editor2d.tools; 29 30 import org.apache.log4j.Logger; 31 import org.eclipse.draw2d.PositionConstants; 32 import org.eclipse.draw2d.geometry.Point; 33 import org.eclipse.draw2d.geometry.PrecisionRectangle; 34 import org.eclipse.draw2d.geometry.Rectangle; 35 import org.eclipse.gef.Request; 36 import org.eclipse.gef.SnapToHelper; 37 import org.eclipse.gef.requests.CreationFactory; 38 import org.eclipse.swt.SWT; 39 import org.eclipse.swt.events.KeyEvent; 40 41 import org.nightlabs.editor2d.command.CreateShapeCommand; 42 import org.nightlabs.editor2d.j2d.GeneralShape; 43 import org.nightlabs.editor2d.request.EditorCreateRequest; 44 import org.nightlabs.editor2d.request.LineCreateRequest; 45 import org.nightlabs.editor2d.util.J2DUtil; 46 47 48 public class LineTool 49 extends EditorCreationTool 50 { 51 public static final Logger LOGGER = Logger.getLogger(LineTool.class); 52 53 public LineTool(CreationFactory factory) { 54 super(factory); 55 } 56 57 61 protected Request createTargetRequest() 62 { 63 LineCreateRequest request = new LineCreateRequest(); 64 request.setFactory(getFactory()); 65 return request; 66 } 67 68 protected LineCreateRequest getLineCreateRequest() 69 { 70 return (LineCreateRequest) getTargetRequest(); 71 } 72 73 76 protected Point realLocation; 77 78 81 protected Point relativeLocation; 82 83 protected GeneralShape feedbackGeneralShape; 84 85 protected GeneralShape creationGeneralShape; 86 87 protected Rectangle creationBounds; 88 89 protected boolean handleButtonDown(int button) 90 { 91 if (button == 1) 92 { 93 if (isInState(STATE_DRAG | STATE_DRAG_IN_PROGRESS)) 94 { 95 realLocation = getRealLocation(); 96 relativeLocation = getLocation(); 97 98 feedbackGeneralShape.lineTo(realLocation.x, realLocation.y); 99 creationGeneralShape.lineTo(relativeLocation.x, relativeLocation.y); 100 101 creationBounds.setBounds(J2DUtil.toDraw2D(creationGeneralShape.getBounds())); 102 getLineCreateRequest().setCreationBounds(creationBounds); 103 } 104 else 105 { 106 lockTargetEditPart(getTargetEditPart()); 107 helper = (SnapToHelper)getTargetEditPart().getAdapter(SnapToHelper.class); 109 110 Point p = getRealLocation(); 111 Point p2 = getLocation(); 112 113 feedbackGeneralShape = new GeneralShape(); 114 feedbackGeneralShape.moveTo(p.x, p.y); 115 feedbackGeneralShape.lineTo(p.x+1, p.y+1); 116 117 creationGeneralShape = new GeneralShape(); 118 creationGeneralShape.moveTo(p2.x, p2.y); 119 creationGeneralShape.lineTo(p2.x+1, p2.y+1); 120 121 creationBounds = new Rectangle(p2.x, p2.y, p2.x+1, p2.y+1); 122 123 getLineCreateRequest().setLocation(getLocation()); 124 getLineCreateRequest().setMode(EditorCreateRequest.BOUNDS_FIX_MODE); 125 getLineCreateRequest().setCreationBounds(creationBounds); 126 127 stateTransition(STATE_INITIAL, STATE_DRAG_IN_PROGRESS); 128 setCursor(getDefaultCursor()); 129 } 130 } 131 return true; 132 } 133 134 141 protected boolean handleButtonUp(int button) 142 { 143 return false; 144 } 145 146 150 protected void updateTargetRequest() 151 { 152 LineCreateRequest req = (LineCreateRequest) getLineCreateRequest(); 153 if (isInState(STATE_DRAG_IN_PROGRESS)) 154 { 155 Rectangle bounds = creationBounds.getCopy(); 160 bounds.union(getLocation()); 161 162 req.setSize(bounds.getSize()); 163 req.setLocation(bounds.getLocation()); 164 req.setCreationBounds(creationBounds); 165 168 req.getExtendedData().clear(); 169 170 updateShape(req); 171 172 if (!getCurrentInput().isAltKeyDown() && helper != null) 173 { 174 PrecisionRectangle baseRect = new PrecisionRectangle(bounds); 175 PrecisionRectangle result = baseRect.getPreciseCopy(); 176 helper.snapRectangle(req, PositionConstants.NSEW, 177 baseRect, result); 178 req.setLocation(result.getLocation()); 179 req.setSize(result.getSize()); 180 181 updateShape(req); 182 } 183 } else { 184 req.setSize(null); 185 req.setLocation(getLocation()); 186 } 187 } 188 189 protected boolean handleKeyDown(KeyEvent e) 190 { 191 if (e.character == SWT.ESC) { 192 if (stateTransition(STATE_DRAG | STATE_DRAG_IN_PROGRESS, STATE_TERMINAL)) 193 { 194 eraseTargetFeedback(); 195 unlockTargetEditPart(); 196 performCreation(1); 197 } 198 setState(STATE_TERMINAL); 199 handleFinished(); 200 return true; 201 } 202 return false; 203 } 204 205 protected void updateShape(EditorCreateRequest request) 206 { 207 Point p = getRealLocation(); 208 Point p2 = getLocation(); 209 210 feedbackGeneralShape.setLastPoint(p.x, p.y); 211 creationGeneralShape.setLastPoint(p2.x, p2.y); 212 if (request.getGeneralShape() == null) 213 request.setGeneralShape(feedbackGeneralShape); 214 } 215 216 public void performCreation(int button) 217 { 218 if (getCurrentCommand() instanceof CreateShapeCommand) 219 { 220 CreateShapeCommand command = (CreateShapeCommand) getCurrentCommand(); 221 GeneralShape gs = J2DUtil.removePathSegment(creationGeneralShape, creationGeneralShape.getSize()-1); 223 command.setGeneralShape(gs); 224 } 225 226 super.performCreation(button); 227 } 228 229 232 public void deactivate() 233 { 234 super.deactivate(); 235 helper = null; 236 } 237 238 } 239 | Popular Tags |