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