KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * Project author: Daniel Mazurek <Daniel.Mazurek [at] nightlabs [dot] org> *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin St, Fifth Floor, *
20  * Boston, MA 02110-1301 USA *
21  * *
22  * Or get it online : *
23  * http://www.gnu.org/copyleft/lesser.html *
24  * *
25  * *
26  ******************************************************************************/

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   /**
56    * @see org.eclipse.gef.Tool#deactivate()
57    */

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         // Snap only when size on drop is employed
73
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