KickJava   Java API By Example, From Geeks To Geeks.

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


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.geometry.Point;
33 import org.eclipse.draw2d.geometry.PrecisionRectangle;
34 import org.eclipse.draw2d.geometry.Rectangle;
35 import org.eclipse.gef.Request;
36 import org.eclipse.gef.SnapToHelper;
37 import org.eclipse.gef.requests.CreationFactory;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.events.KeyEvent;
40
41 import org.nightlabs.editor2d.command.CreateShapeCommand;
42 import org.nightlabs.editor2d.j2d.GeneralShape;
43 import org.nightlabs.editor2d.request.EditorCreateRequest;
44 import org.nightlabs.editor2d.request.LineCreateRequest;
45 import org.nightlabs.editor2d.util.J2DUtil;
46
47
48 public class LineTool
49 extends EditorCreationTool
50 {
51   public static final Logger LOGGER = Logger.getLogger(LineTool.class);
52   
53   public LineTool(CreationFactory factory) {
54     super(factory);
55   }
56   
57   /**
58    * Creates a {@link EditorCreateRequest} and sets this tool's factory on the request.
59    * @see org.eclipse.gef.tools.TargetingTool#createTargetRequest()
60    */

61   protected Request createTargetRequest()
62   {
63     LineCreateRequest request = new LineCreateRequest();
64     request.setFactory(getFactory());
65     return request;
66   }
67   
68   protected LineCreateRequest getLineCreateRequest()
69   {
70     return (LineCreateRequest) getTargetRequest();
71   }
72   
73   /*
74    * the Real Location of a mousePoint including ScrollOffset
75    */

76   protected Point realLocation;
77   
78   /*
79    * the Location of a mousePoint excluding ScrollOffset
80    */

81   protected Point relativeLocation;
82   
83   protected GeneralShape feedbackGeneralShape;
84   
85   protected GeneralShape creationGeneralShape;
86   
87   protected Rectangle creationBounds;
88   
89   protected boolean handleButtonDown(int button)
90   {
91     if (button == 1)
92     {
93         if (isInState(STATE_DRAG | STATE_DRAG_IN_PROGRESS))
94         {
95           realLocation = getRealLocation();
96           relativeLocation = getLocation();
97           
98           feedbackGeneralShape.lineTo(realLocation.x, realLocation.y);
99           creationGeneralShape.lineTo(relativeLocation.x, relativeLocation.y);
100           
101           creationBounds.setBounds(J2DUtil.toDraw2D(creationGeneralShape.getBounds()));
102           getLineCreateRequest().setCreationBounds(creationBounds);
103         }
104         else
105         {
106             lockTargetEditPart(getTargetEditPart());
107             // Snap only when size on drop is employed
108
helper = (SnapToHelper)getTargetEditPart().getAdapter(SnapToHelper.class);
109             
110           Point p = getRealLocation();
111           Point p2 = getLocation();
112                                     
113             feedbackGeneralShape = new GeneralShape();
114             feedbackGeneralShape.moveTo(p.x, p.y);
115             feedbackGeneralShape.lineTo(p.x+1, p.y+1);
116
117             creationGeneralShape = new GeneralShape();
118             creationGeneralShape.moveTo(p2.x, p2.y);
119             creationGeneralShape.lineTo(p2.x+1, p2.y+1);
120                         
121             creationBounds = new Rectangle(p2.x, p2.y, p2.x+1, p2.y+1);
122             
123           getLineCreateRequest().setLocation(getLocation());
124           getLineCreateRequest().setMode(EditorCreateRequest.BOUNDS_FIX_MODE);
125             getLineCreateRequest().setCreationBounds(creationBounds);
126           
127             stateTransition(STATE_INITIAL, STATE_DRAG_IN_PROGRESS);
128             setCursor(getDefaultCursor());
129         }
130     }
131     return true;
132   }
133     
134   /**
135    * Does nothing all actions are performed by handleButtonDown(int) or
136    * updateTargetRequest
137    *
138    * @see org.eclipse.gef.tools.AbstractTool#handleButtonUp(int)
139    * @see handleButtonDown(int)
140    */

141   protected boolean handleButtonUp(int button)
142   {
143     return false;
144   }
145   
146   /**
147    * Sets the location (and size if the user is performing size-on-drop) of the request.
148    * @see org.eclipse.gef.tools.TargetingTool#updateTargetRequest()
149    */

150   protected void updateTargetRequest()
151   {
152     LineCreateRequest req = (LineCreateRequest) getLineCreateRequest();
153     if (isInState(STATE_DRAG_IN_PROGRESS))
154     {
155       // TODO: Optimize bounds calc, by getting rect which is a union of
156
// of all points in the PointList and getLocation
157
// and union Rectangle can be cached until next buttonDown
158
// Rectangle bounds = J2DUtil.toDraw2D(creationGeneralShape.getBounds());
159
Rectangle bounds = creationBounds.getCopy();
160       bounds.union(getLocation());
161         
162         req.setSize(bounds.getSize());
163         req.setLocation(bounds.getLocation());
164         req.setCreationBounds(creationBounds);
165 // req.setSize(creationBounds.getSize());
166
// req.setLocation(creationBounds.getLocation());
167

168         req.getExtendedData().clear();
169         
170         updateShape(req);
171         
172         if (!getCurrentInput().isAltKeyDown() && helper != null)
173         {
174             PrecisionRectangle baseRect = new PrecisionRectangle(bounds);
175             PrecisionRectangle result = baseRect.getPreciseCopy();
176             helper.snapRectangle(req, PositionConstants.NSEW,
177                 baseRect, result);
178             req.setLocation(result.getLocation());
179             req.setSize(result.getSize());
180
181             updateShape(req);
182         }
183     } else {
184         req.setSize(null);
185         req.setLocation(getLocation());
186     }
187   }
188   
189   protected boolean handleKeyDown(KeyEvent e)
190   {
191     if (e.character == SWT.ESC) {
192         if (stateTransition(STATE_DRAG | STATE_DRAG_IN_PROGRESS, STATE_TERMINAL))
193         {
194             eraseTargetFeedback();
195             unlockTargetEditPart();
196             performCreation(1);
197         }
198         setState(STATE_TERMINAL);
199         handleFinished();
200         return true;
201     }
202     return false;
203   }
204          
205   protected void updateShape(EditorCreateRequest request)
206   {
207       Point p = getRealLocation();
208       Point p2 = getLocation();
209         
210     feedbackGeneralShape.setLastPoint(p.x, p.y);
211     creationGeneralShape.setLastPoint(p2.x, p2.y);
212     if (request.getGeneralShape() == null)
213       request.setGeneralShape(feedbackGeneralShape);
214   }
215     
216   public void performCreation(int button)
217   {
218         if (getCurrentCommand() instanceof CreateShapeCommand)
219         {
220           CreateShapeCommand command = (CreateShapeCommand) getCurrentCommand();
221 // command.setGeneralShape(creationGeneralShape);
222
GeneralShape gs = J2DUtil.removePathSegment(creationGeneralShape, creationGeneralShape.getSize()-1);
223           command.setGeneralShape(gs);
224         }
225         
226         super.performCreation(button);
227   }
228     
229   /**
230    * @see org.eclipse.gef.Tool#deactivate()
231    */

232   public void deactivate()
233   {
234     super.deactivate();
235     helper = null;
236   }
237     
238 }
239
Popular Tags