KickJava   Java API By Example, From Geeks To Geeks.

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


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

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