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.RectangleFigure; 33 import org.eclipse.draw2d.geometry.Point; 34 import org.eclipse.draw2d.geometry.PrecisionRectangle; 35 import org.eclipse.draw2d.geometry.Rectangle; 36 import org.eclipse.gef.SnapToHelper; 37 import org.eclipse.gef.requests.CreationFactory; 38 39 import org.nightlabs.editor2d.command.CreateShapeCommand; 40 import org.nightlabs.editor2d.j2d.GeneralShape; 41 import org.nightlabs.editor2d.request.EditorCreateRequest; 42 import org.nightlabs.editor2d.util.EditorGeneralShapeFactory; 43 44 45 public class RectangleTool 46 extends EditorCreationTool 47 { 48 public static final Logger LOGGER = Logger.getLogger(RectangleTool.class); 49 50 public RectangleTool(CreationFactory aFactory) 51 { 52 super(aFactory); 53 } 54 55 58 public void deactivate() 59 { 60 super.deactivate(); 61 helper = null; 62 } 63 64 protected Rectangle bounds; 65 protected boolean handleButtonDown(int button) 66 { 67 if (button == 1) 68 { 69 Point p = getLocation(); 70 getEditorCreateRequest().setLocation(p); 71 lockTargetEditPart(getTargetEditPart()); 72 helper = (SnapToHelper)getTargetEditPart().getAdapter(SnapToHelper.class); 74 75 bounds = new Rectangle(p.x, p.y, p.x+1, p.y+1); 76 RectangleFigure rectFigure = new RectangleFigure(); 77 rectFigure.setBounds(bounds); 78 getEditorCreateRequest().setUseShape(true); 79 getEditorCreateRequest().setShape(rectFigure); 80 81 stateTransition(STATE_INITIAL, STATE_DRAG_IN_PROGRESS); 82 setCursor(getDefaultCursor()); 83 } 84 return true; 85 } 86 87 protected void updateTargetRequest() 88 { 89 EditorCreateRequest req = (EditorCreateRequest) getCreateRequest(); 90 if (isInState(STATE_DRAG_IN_PROGRESS)) 91 { 92 Point loq = getStartLocation(); 93 Rectangle bounds = new Rectangle(loq, loq); 94 bounds.union(loq.getTranslated(getDragMoveDelta())); 95 req.setSize(bounds.getSize()); 96 req.setLocation(bounds.getLocation()); 97 req.getExtendedData().clear(); 98 if (!getCurrentInput().isAltKeyDown() && helper != null) { 99 PrecisionRectangle baseRect = new PrecisionRectangle(bounds); 100 PrecisionRectangle result = baseRect.getPreciseCopy(); 101 helper.snapRectangle(req, PositionConstants.NSEW, 102 baseRect, result); 103 req.setLocation(result.getLocation()); 104 req.setSize(result.getSize()); 105 } 106 } else { 107 req.setSize(null); 108 req.setLocation(getLocation()); 109 } 110 } 111 112 public void performCreation(int button) 113 { 114 GeneralShape gs = EditorGeneralShapeFactory.createRectangle(bounds); 115 getEditorCreateRequest().setUseShape(false); 116 if (getCurrentCommand() instanceof CreateShapeCommand) { 117 CreateShapeCommand command = (CreateShapeCommand) getCurrentCommand(); 118 command.setGeneralShape(gs); 119 } 120 121 super.performCreation(button); 122 } 123 } 124 | Popular Tags |