KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > editor2d > tools > RectangleTool


1 /**
2  * <p> Project: com.nightlabs.editor2d </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 17.02.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.tools;
9
10 import org.apache.log4j.Logger;
11 import org.eclipse.draw2d.PositionConstants;
12 import org.eclipse.draw2d.RectangleFigure;
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 RectangleTool
26 extends EditorCreationTool
27 {
28   public static final Logger LOGGER = Logger.getLogger(RectangleTool.class);
29   
30   public RectangleTool(CreationFactory aFactory)
31   {
32     super(aFactory);
33   }
34   
35   /**
36    * @see org.eclipse.gef.Tool#deactivate()
37    */

38   public void deactivate()
39   {
40     super.deactivate();
41     helper = null;
42   }
43   
44   protected Rectangle bounds;
45   protected boolean handleButtonDown(int button)
46   {
47     if (button == 1)
48     {
49         Point p = getLocation();
50         getEditorCreateRequest().setLocation(p);
51         lockTargetEditPart(getTargetEditPart());
52         // Snap only when size on drop is employed
53
helper = (SnapToHelper)getTargetEditPart().getAdapter(SnapToHelper.class);
54                                             
55         bounds = new Rectangle(p.x, p.y, p.x+1, p.y+1);
56         RectangleFigure rectFigure = new RectangleFigure();
57         rectFigure.setBounds(bounds);
58         getEditorCreateRequest().setUseShape(true);
59         getEditorCreateRequest().setShape(rectFigure);
60         
61         stateTransition(STATE_INITIAL, STATE_DRAG_IN_PROGRESS);
62         setCursor(getDefaultCursor());
63     }
64     return true;
65   }
66   
67   protected void updateTargetRequest()
68   {
69     EditorCreateRequest req = (EditorCreateRequest) getCreateRequest();
70     if (isInState(STATE_DRAG_IN_PROGRESS))
71     {
72         Point loq = getStartLocation();
73         Rectangle bounds = new Rectangle(loq, loq);
74         bounds.union(loq.getTranslated(getDragMoveDelta()));
75         req.setSize(bounds.getSize());
76         req.setLocation(bounds.getLocation());
77         req.getExtendedData().clear();
78         if (!getCurrentInput().isAltKeyDown() && helper != null) {
79             PrecisionRectangle baseRect = new PrecisionRectangle(bounds);
80             PrecisionRectangle result = baseRect.getPreciseCopy();
81             helper.snapRectangle(req, PositionConstants.NSEW,
82                 baseRect, result);
83             req.setLocation(result.getLocation());
84             req.setSize(result.getSize());
85         }
86     } else {
87         req.setSize(null);
88         req.setLocation(getLocation());
89     }
90   }
91   
92   public void performCreation(int button)
93   {
94     GeneralShape gs = EditorGeneralShapeFactory.createRectangle(bounds);
95         getEditorCreateRequest().setUseShape(false);
96         if (getCurrentCommand() instanceof CreateShapeCommand) {
97           CreateShapeCommand command = (CreateShapeCommand) getCurrentCommand();
98           command.setGeneralShape(gs);
99         }
100         
101         super.performCreation(button);
102   }
103 }
104
Popular Tags