1 8 package com.nightlabs.editor2d.tools; 9 10 import org.apache.log4j.Logger; 11 import org.eclipse.draw2d.Ellipse; 12 import org.eclipse.draw2d.PositionConstants; 13 import org.eclipse.draw2d.geometry.Point; 14 import org.eclipse.draw2d.geometry.PrecisionRectangle; 15 import org.eclipse.draw2d.geometry.Rectangle; 16 import org.eclipse.gef.SnapToHelper; 17 import org.eclipse.gef.requests.CreationFactory; 18 19 import com.nightlabs.editor2d.command.CreateShapeCommand; 20 import com.nightlabs.editor2d.j2d.GeneralShape; 21 import com.nightlabs.editor2d.request.EditorCreateRequest; 22 import com.nightlabs.editor2d.util.EditorGeneralShapeFactory; 23 24 25 public class EllipseTool 26 extends EditorCreationTool 27 { 28 public static final Logger LOGGER = Logger.getLogger(EllipseTool.class); 29 30 public EllipseTool(CreationFactory aFactory) 31 { 32 super(aFactory); 33 } 34 35 protected Ellipse ellipse; 36 37 40 public void deactivate() 41 { 42 super.deactivate(); 43 helper = null; 44 } 45 46 protected boolean handleButtonDown(int button) 47 { 48 if (button == 1) 49 { 50 Point p = getLocation(); 51 getEditorCreateRequest().setLocation(p); 52 lockTargetEditPart(getTargetEditPart()); 53 helper = (SnapToHelper)getTargetEditPart().getAdapter(SnapToHelper.class); 55 56 ellipse = new Ellipse(); 57 Rectangle bounds = new Rectangle(p.x, p.y, p.x+1, p.y+1); 58 ellipse.setBounds(bounds); 59 getEditorCreateRequest().setUseShape(true); 60 getEditorCreateRequest().setShape(ellipse); 61 62 stateTransition(STATE_INITIAL, STATE_DRAG_IN_PROGRESS); 63 setCursor(getDefaultCursor()); 64 } 65 return true; 66 } 67 68 protected void updateTargetRequest() 69 { 70 EditorCreateRequest req = (EditorCreateRequest) getCreateRequest(); 71 if (isInState(STATE_DRAG_IN_PROGRESS)) 72 { 73 Point loq = getStartLocation(); 74 Rectangle bounds = new Rectangle(loq, loq); 75 bounds.union(loq.getTranslated(getDragMoveDelta())); 76 req.setSize(bounds.getSize()); 77 req.setLocation(bounds.getLocation()); 78 req.getExtendedData().clear(); 79 if (!getCurrentInput().isAltKeyDown() && helper != null) { 80 PrecisionRectangle baseRect = new PrecisionRectangle(bounds); 81 PrecisionRectangle result = baseRect.getPreciseCopy(); 82 helper.snapRectangle(req, PositionConstants.NSEW, 83 baseRect, result); 84 req.setLocation(result.getLocation()); 85 req.setSize(result.getSize()); 86 } 87 } else { 88 req.setSize(null); 89 req.setLocation(getLocation()); 90 } 91 } 92 93 public void performCreation(int button) 94 { 95 GeneralShape gs = EditorGeneralShapeFactory.createEllipse(ellipse.getBounds()); 96 getEditorCreateRequest().setUseShape(false); 97 if (getCurrentCommand() instanceof CreateShapeCommand) { 98 CreateShapeCommand command = (CreateShapeCommand) getCurrentCommand(); 99 command.setGeneralShape(gs); 100 } 101 102 super.performCreation(button); 103 } 104 } 105 | Popular Tags |