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