KickJava   Java API By Example, From Geeks To Geeks.

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


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.eclipse.draw2d.ColorConstants;
31 import org.eclipse.draw2d.Figure;
32 import org.eclipse.draw2d.Graphics;
33 import org.eclipse.draw2d.IFigure;
34 import org.eclipse.draw2d.geometry.Rectangle;
35 import org.eclipse.gef.EditPartViewer;
36 import org.eclipse.gef.GraphicalViewer;
37 import org.eclipse.gef.SharedCursors;
38 import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
39 import org.eclipse.gef.editparts.ScalableRootEditPart;
40 import org.eclipse.gef.editparts.ZoomManager;
41 import org.eclipse.gef.tools.AbstractTool;
42 import org.eclipse.swt.widgets.Display;
43
44 import org.nightlabs.editor2d.request.EditorRequestConstants;
45 import org.nightlabs.editor2d.util.EditorUtil;
46
47
48 public class ZoomTool
49 extends AbstractTool
50 implements EditorRequestConstants
51 {
52   protected IFigure zoomRectangleFigure;
53   
54   protected ZoomManager zoomManager;
55   
56   public ZoomTool()
57   {
58     setDefaultCursor(SharedCursors.CROSS);
59   }
60   
61   protected String JavaDoc getCommandName()
62   {
63     return REQ_ZOOM_RECT;
64   }
65
66   private void eraseZoomFeedback()
67   {
68     if (zoomRectangleFigure != null) {
69         removeFeedback(zoomRectangleFigure);
70         zoomRectangleFigure = null;
71     }
72   }
73   
74   /**
75    * Erases feedback if necessary and puts the tool into the terminal state.
76    */

77   public void deactivate()
78   {
79     if (isInState(STATE_DRAG_IN_PROGRESS)) {
80         eraseZoomFeedback();
81     }
82     super.deactivate();
83     setState(STATE_TERMINAL);
84   }
85   
86   /**
87    * @see org.eclipse.gef.tools.AbstractTool#getDebugName()
88    */

89   protected String JavaDoc getDebugName()
90   {
91     return "Zoom Tool";//$NON-NLS-1$
92
}
93   
94   protected IFigure getZoomFeedbackFigure()
95   {
96     if (zoomRectangleFigure == null) {
97         zoomRectangleFigure = new ZoomRectangleFigure();
98         addFeedback(zoomRectangleFigure);
99     }
100     return zoomRectangleFigure;
101   }
102   
103   protected Rectangle getZoomSelectionRectangle()
104   {
105     return new Rectangle(getStartLocation(), getLocation());
106   }
107   
108   protected ZoomManager getZoomManager()
109   {
110     if (getCurrentViewer().getContents().getRoot() instanceof ScalableRootEditPart)
111     {
112       if (zoomManager == null) {
113         zoomManager = ((ScalableRootEditPart) getCurrentViewer().getContents().getRoot()).getZoomManager();
114       }
115       return zoomManager;
116     }
117     else if (getCurrentViewer().getContents().getRoot() instanceof ScalableFreeformRootEditPart)
118     {
119       if (zoomManager == null) {
120         zoomManager = ((ScalableFreeformRootEditPart) getCurrentViewer().getContents().getRoot()).getZoomManager();
121       }
122       return zoomManager;
123     }
124     return null;
125   }
126     
127   protected boolean isGraphicalViewer()
128   {
129     return getCurrentViewer() instanceof GraphicalViewer;
130   }
131   
132   /**
133    * @see org.eclipse.gef.tools.AbstractTool#handleButtonDown(int)
134    */

135   protected boolean handleButtonDown(int button)
136   {
137     if (!isGraphicalViewer())
138         return true;
139     if (button != 1) {
140         setState(STATE_INVALID);
141         handleInvalidInput();
142     }
143     if (stateTransition(STATE_INITIAL, STATE_DRAG_IN_PROGRESS))
144     {
145       // TODO: if shift is pressed draw symetric Rectangle
146
// if (getCurrentInput().isControlKeyDown())
147
// setSelectionMode(TOGGLE_MODE);
148
// else if (getCurrentInput().isShiftKeyDown())
149
// setSelectionMode(APPEND_MODE);
150
}
151     return true;
152   }
153   
154   /**
155    * @see org.eclipse.gef.tools.AbstractTool#handleButtonUp(int)
156    */

157   protected boolean handleButtonUp(int button)
158   {
159     if (stateTransition(STATE_DRAG_IN_PROGRESS, STATE_TERMINAL)) {
160         eraseZoomFeedback();
161         performZoom();
162     }
163     handleFinished();
164     return true;
165   }
166   
167   /**
168    * @see org.eclipse.gef.tools.AbstractTool#handleDragInProgress()
169    */

170   protected boolean handleDragInProgress()
171   {
172     if (isInState(STATE_DRAG | STATE_DRAG_IN_PROGRESS)) {
173         showZoomFeedback();
174     }
175     return true;
176   }
177   
178   /**
179    * @see org.eclipse.gef.tools.AbstractTool#handleFocusLost()
180    */

181   protected boolean handleFocusLost()
182   {
183     if (isInState(STATE_DRAG | STATE_DRAG_IN_PROGRESS)) {
184         handleFinished();
185         return true;
186     }
187     return false;
188   }
189
190   /**
191    * This method is called when mouse or keyboard input is invalid and erases the feedback.
192    * @return <code>true</code>
193    */

194   protected boolean handleInvalidInput()
195   {
196     eraseZoomFeedback();
197     return true;
198   }
199   
200   /**
201    * @see org.eclipse.gef.Tool#setViewer(org.eclipse.gef.EditPartViewer)
202    */

203   public void setViewer(EditPartViewer viewer)
204   {
205     if (viewer == getCurrentViewer())
206         return;
207     super.setViewer(viewer);
208     if (viewer instanceof GraphicalViewer)
209         setDefaultCursor(SharedCursors.CROSS);
210     else
211         setDefaultCursor(SharedCursors.NO);
212   }
213     
214   protected void performZoom()
215   {
216 // EditorUtil.zoomToRelativeRect(getZoomSelectionRectangle().getCopy(), getZoomManager());
217
EditorUtil.zoomToAbsoluteRect(getZoomSelectionRectangle().getCopy(), getZoomManager());
218   }
219   
220   protected void showZoomFeedback()
221   {
222     Rectangle rect = getZoomSelectionRectangle().getCopy();
223     getZoomFeedbackFigure().translateToRelative(rect);
224     getZoomFeedbackFigure().setBounds(rect);
225   }
226     
227 class ZoomRectangleFigure
228 extends Figure
229 {
230   private int offset = 0;
231   private boolean schedulePaint = true;
232   private static final int DELAY = 110; //animation delay in millisecond
233

234   /**
235    * @see org.eclipse.draw2d.Figure#paintFigure(org.eclipse.draw2d.Graphics)
236    */

237   protected void paintFigure(Graphics graphics)
238   {
239     Rectangle bounds = getBounds().getCopy();
240     graphics.translate(getLocation());
241     
242     graphics.setXORMode(true);
243     graphics.setForegroundColor(ColorConstants.white);
244     graphics.setBackgroundColor(ColorConstants.black);
245     
246     graphics.setLineStyle(Graphics.LINE_DOT);
247     
248     int[] points = new int[6];
249     
250     points[0] = 0 + offset;
251     points[1] = 0;
252     points[2] = bounds.width - 1;
253     points[3] = 0;
254     points[4] = bounds.width - 1;
255     points[5] = bounds.height - 1;
256     
257     graphics.drawPolyline(points);
258     
259     points[0] = 0;
260     points[1] = 0 + offset;
261     points[2] = 0;
262     points[3] = bounds.height - 1;
263     points[4] = bounds.width - 1;
264     points[5] = bounds.height - 1;
265     
266     graphics.drawPolyline(points);
267     
268     graphics.translate(getLocation().getNegated());
269     
270     if (schedulePaint) {
271         Display.getCurrent().timerExec(DELAY, new Runnable JavaDoc() {
272             public void run() {
273                 offset++;
274                 if (offset > 5)
275                     offset = 0;
276                 
277                 schedulePaint = true;
278                 repaint();
279             }
280         });
281     }
282     
283     schedulePaint = false;
284   }
285     
286 } // class ZoomRectangleFigure
287

288 }
289
Popular Tags