KickJava   Java API By Example, From Geeks To Geeks.

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


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: 23.02.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.tools;
9
10 import org.eclipse.draw2d.ColorConstants;
11 import org.eclipse.draw2d.Figure;
12 import org.eclipse.draw2d.Graphics;
13 import org.eclipse.draw2d.IFigure;
14 import org.eclipse.draw2d.geometry.Rectangle;
15 import org.eclipse.gef.EditPartViewer;
16 import org.eclipse.gef.GraphicalViewer;
17 import org.eclipse.gef.SharedCursors;
18 import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
19 import org.eclipse.gef.editparts.ScalableRootEditPart;
20 import org.eclipse.gef.editparts.ZoomManager;
21 import org.eclipse.gef.tools.AbstractTool;
22 import org.eclipse.swt.widgets.Display;
23
24 import com.nightlabs.editor2d.request.EditorRequestConstants;
25 import com.nightlabs.editor2d.util.EditorUtil;
26
27
28 public class ZoomTool
29 extends AbstractTool
30 implements EditorRequestConstants
31 {
32   protected IFigure zoomRectangleFigure;
33   
34   protected ZoomManager zoomManager;
35   
36   public ZoomTool()
37   {
38     setDefaultCursor(SharedCursors.CROSS);
39   }
40   
41   protected String JavaDoc getCommandName()
42   {
43     return REQ_ZOOM_RECT;
44   }
45
46   private void eraseZoomFeedback()
47   {
48     if (zoomRectangleFigure != null) {
49         removeFeedback(zoomRectangleFigure);
50         zoomRectangleFigure = null;
51     }
52   }
53   
54   /**
55    * Erases feedback if necessary and puts the tool into the terminal state.
56    */

57   public void deactivate()
58   {
59     if (isInState(STATE_DRAG_IN_PROGRESS)) {
60         eraseZoomFeedback();
61     }
62     super.deactivate();
63     setState(STATE_TERMINAL);
64   }
65   
66   /**
67    * @see org.eclipse.gef.tools.AbstractTool#getDebugName()
68    */

69   protected String JavaDoc getDebugName()
70   {
71     return "Zoom Tool";//$NON-NLS-1$
72
}
73   
74   protected IFigure getZoomFeedbackFigure()
75   {
76     if (zoomRectangleFigure == null) {
77         zoomRectangleFigure = new ZoomRectangleFigure();
78         addFeedback(zoomRectangleFigure);
79     }
80     return zoomRectangleFigure;
81   }
82   
83   protected Rectangle getZoomSelectionRectangle()
84   {
85     return new Rectangle(getStartLocation(), getLocation());
86   }
87   
88   protected ZoomManager getZoomManager()
89   {
90     if (getCurrentViewer().getContents().getRoot() instanceof ScalableRootEditPart)
91     {
92       if (zoomManager == null) {
93         zoomManager = ((ScalableRootEditPart) getCurrentViewer().getContents().getRoot()).getZoomManager();
94       }
95       return zoomManager;
96     }
97     else if (getCurrentViewer().getContents().getRoot() instanceof ScalableFreeformRootEditPart)
98     {
99       if (zoomManager == null) {
100         zoomManager = ((ScalableFreeformRootEditPart) getCurrentViewer().getContents().getRoot()).getZoomManager();
101       }
102       return zoomManager;
103     }
104     return null;
105   }
106     
107   protected boolean isGraphicalViewer()
108   {
109     return getCurrentViewer() instanceof GraphicalViewer;
110   }
111   
112   /**
113    * @see org.eclipse.gef.tools.AbstractTool#handleButtonDown(int)
114    */

115   protected boolean handleButtonDown(int button)
116   {
117     if (!isGraphicalViewer())
118         return true;
119     if (button != 1) {
120         setState(STATE_INVALID);
121         handleInvalidInput();
122     }
123     if (stateTransition(STATE_INITIAL, STATE_DRAG_IN_PROGRESS))
124     {
125       // TODO: if shift is pressed draw symetric Rectangle
126
// if (getCurrentInput().isControlKeyDown())
127
// setSelectionMode(TOGGLE_MODE);
128
// else if (getCurrentInput().isShiftKeyDown())
129
// setSelectionMode(APPEND_MODE);
130
}
131     return true;
132   }
133   
134   /**
135    * @see org.eclipse.gef.tools.AbstractTool#handleButtonUp(int)
136    */

137   protected boolean handleButtonUp(int button)
138   {
139     if (stateTransition(STATE_DRAG_IN_PROGRESS, STATE_TERMINAL)) {
140         eraseZoomFeedback();
141         performZoom();
142     }
143     handleFinished();
144     return true;
145   }
146   
147   /**
148    * @see org.eclipse.gef.tools.AbstractTool#handleDragInProgress()
149    */

150   protected boolean handleDragInProgress()
151   {
152     if (isInState(STATE_DRAG | STATE_DRAG_IN_PROGRESS)) {
153         showZoomFeedback();
154     }
155     return true;
156   }
157   
158   /**
159    * @see org.eclipse.gef.tools.AbstractTool#handleFocusLost()
160    */

161   protected boolean handleFocusLost()
162   {
163     if (isInState(STATE_DRAG | STATE_DRAG_IN_PROGRESS)) {
164         handleFinished();
165         return true;
166     }
167     return false;
168   }
169
170   /**
171    * This method is called when mouse or keyboard input is invalid and erases the feedback.
172    * @return <code>true</code>
173    */

174   protected boolean handleInvalidInput()
175   {
176     eraseZoomFeedback();
177     return true;
178   }
179   
180   /**
181    * @see org.eclipse.gef.Tool#setViewer(org.eclipse.gef.EditPartViewer)
182    */

183   public void setViewer(EditPartViewer viewer)
184   {
185     if (viewer == getCurrentViewer())
186         return;
187     super.setViewer(viewer);
188     if (viewer instanceof GraphicalViewer)
189         setDefaultCursor(SharedCursors.CROSS);
190     else
191         setDefaultCursor(SharedCursors.NO);
192   }
193     
194   protected void performZoom()
195   {
196 // EditorUtil.zoomToRelativeRect(getZoomSelectionRectangle().getCopy(), getZoomManager());
197
EditorUtil.zoomToAbsoluteRect(getZoomSelectionRectangle().getCopy(), getZoomManager());
198   }
199   
200   protected void showZoomFeedback()
201   {
202     Rectangle rect = getZoomSelectionRectangle().getCopy();
203     getZoomFeedbackFigure().translateToRelative(rect);
204     getZoomFeedbackFigure().setBounds(rect);
205   }
206     
207 class ZoomRectangleFigure
208 extends Figure
209 {
210   private int offset = 0;
211   private boolean schedulePaint = true;
212   private static final int DELAY = 110; //animation delay in millisecond
213

214   /**
215    * @see org.eclipse.draw2d.Figure#paintFigure(org.eclipse.draw2d.Graphics)
216    */

217   protected void paintFigure(Graphics graphics)
218   {
219     Rectangle bounds = getBounds().getCopy();
220     graphics.translate(getLocation());
221     
222     graphics.setXORMode(true);
223     graphics.setForegroundColor(ColorConstants.white);
224     graphics.setBackgroundColor(ColorConstants.black);
225     
226     graphics.setLineStyle(Graphics.LINE_DOT);
227     
228     int[] points = new int[6];
229     
230     points[0] = 0 + offset;
231     points[1] = 0;
232     points[2] = bounds.width - 1;
233     points[3] = 0;
234     points[4] = bounds.width - 1;
235     points[5] = bounds.height - 1;
236     
237     graphics.drawPolyline(points);
238     
239     points[0] = 0;
240     points[1] = 0 + offset;
241     points[2] = 0;
242     points[3] = bounds.height - 1;
243     points[4] = bounds.width - 1;
244     points[5] = bounds.height - 1;
245     
246     graphics.drawPolyline(points);
247     
248     graphics.translate(getLocation().getNegated());
249     
250     if (schedulePaint) {
251         Display.getCurrent().timerExec(DELAY, new Runnable JavaDoc() {
252             public void run() {
253                 offset++;
254                 if (offset > 5)
255                     offset = 0;
256                 
257                 schedulePaint = true;
258                 repaint();
259             }
260         });
261     }
262     
263     schedulePaint = false;
264   }
265     
266 } // class ZoomRectangleFigure
267

268 }
269
Popular Tags