KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > custom > EditorZoomComboContributionItem


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.custom;
29
30 import org.eclipse.draw2d.FigureUtilities;
31 import org.eclipse.gef.editparts.ZoomListener;
32 import org.eclipse.gef.editparts.ZoomManager;
33 import org.eclipse.gef.ui.actions.GEFActionConstants;
34 import org.eclipse.jface.action.ContributionItem;
35 import org.eclipse.jface.util.Assert;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.SWTException;
38 import org.eclipse.swt.events.FocusEvent;
39 import org.eclipse.swt.events.FocusListener;
40 import org.eclipse.swt.events.SelectionEvent;
41 import org.eclipse.swt.events.SelectionListener;
42 import org.eclipse.swt.widgets.Combo;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.swt.widgets.Control;
45 import org.eclipse.swt.widgets.Menu;
46 import org.eclipse.swt.widgets.ToolBar;
47 import org.eclipse.swt.widgets.ToolItem;
48 import org.eclipse.ui.IPartListener;
49 import org.eclipse.ui.IPartService;
50 import org.eclipse.ui.IWorkbenchPart;
51
52
53 public class EditorZoomComboContributionItem
54 //extends ZoomComboContributionItem
55
extends ContributionItem
56 implements ZoomListener
57 {
58   protected Combo combo;
59   protected String JavaDoc[] initStrings;
60   protected ToolItem toolitem;
61   protected ZoomManager zoomManager;
62   protected IPartService service;
63   protected IPartListener partListener;
64   
65   public double[] zoomLevels = {.01, .05, .1, .25, .5, .75, 1.0, 1.5, 2.0, 2.5, 3, 4, 5};
66   
67   /**
68    * Constructor for ComboToolItem.
69    * @param partService used to add a PartListener
70    */

71   public EditorZoomComboContributionItem(IPartService partService)
72   {
73     this(partService, "8888%");//$NON-NLS-1$
74
}
75
76   /**
77    * Constructor for ComboToolItem.
78    * @param partService used to add a PartListener
79    * @param initString the initial string displayed in the combo
80    */

81   public EditorZoomComboContributionItem(IPartService partService, String JavaDoc initString)
82   {
83     this(partService, new String JavaDoc[] {initString});
84   }
85   
86   /**
87    * Constructor for ComboToolItem.
88    * @param partService used to add a PartListener
89    * @param initStrings the initial string displayed in the combo
90    */

91   public EditorZoomComboContributionItem(IPartService partService, String JavaDoc[] initStrings)
92   {
93     super(GEFActionConstants.ZOOM_TOOLBAR_WIDGET);
94 // super(partService, initStrings);
95
this.initStrings = initStrings;
96     service = partService;
97     Assert.isNotNull(partService);
98     partService.addPartListener(partListener = new IPartListener() {
99         public void partActivated(IWorkbenchPart part)
100         {
101           Object JavaDoc adapter = part.getAdapter(ZoomManager.class);
102           if (adapter != null && adapter instanceof ZoomManager)
103           {
104                 setZoomManager((ZoomManager)adapter);
105                 getZoomManager().setZoomLevels(zoomLevels);
106 // getZoomManager().setZoomAnimationStyle(ZoomManager.ANIMATE_ZOOM_IN_OUT);
107
refresh(true);
108           }
109         }
110         public void partBroughtToTop(IWorkbenchPart p) { }
111         public void partClosed(IWorkbenchPart p) { }
112         public void partDeactivated(IWorkbenchPart p) { }
113         public void partOpened(IWorkbenchPart p) { }
114     });
115   }
116   
117   protected void refresh(boolean repopulateCombo)
118   {
119     if (combo == null || combo.isDisposed())
120         return;
121     //$TODO GTK workaround
122
try {
123         if (zoomManager == null) {
124             combo.setEnabled(false);
125             combo.setText(""); //$NON-NLS-1$
126
} else {
127             if (repopulateCombo) {
128                 combo.setItems(getZoomManager().getZoomLevelsAsText());
129             }
130             String JavaDoc zoom = getZoomManager().getZoomAsText();
131             int index = combo.indexOf(zoom);
132             if (index != -1)
133                 combo.select(index);
134             else
135                 combo.setText(zoom);
136             combo.setEnabled(true);
137         }
138     } catch (SWTException exception) {
139         if (!SWT.getPlatform().equals("gtk")) //$NON-NLS-1$
140
throw exception;
141     }
142   }
143   
144   /**
145    * Returns the zoomManager.
146    * @return ZoomManager
147    */

148   public ZoomManager getZoomManager()
149   {
150     return zoomManager;
151   }
152
153   /**
154    * Sets the ZoomManager
155    * @param zm The ZoomManager
156    */

157   public void setZoomManager(ZoomManager zm)
158   {
159     if (zoomManager == zm)
160         return;
161     if (zoomManager != null)
162         zoomManager.removeZoomListener(this);
163
164     zoomManager = zm;
165     refresh(true);
166
167     if (zoomManager != null)
168         zoomManager.addZoomListener(this);
169   }
170   
171   /**
172    * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(SelectionEvent)
173    */

174   private void handleWidgetDefaultSelected(SelectionEvent event) {
175     handleWidgetSelected(event);
176   }
177
178   /**
179    * @see org.eclipse.swt.events.SelectionListener#widgetSelected(SelectionEvent)
180    */

181   private void handleWidgetSelected(SelectionEvent event) {
182     // TODO: GTK Workaround (fixed in 3.0) - see SWT bug #44345
183
if (zoomManager != null)
184         if (combo.getSelectionIndex() >= 0)
185             zoomManager.setZoomAsText(combo.getItem(combo.getSelectionIndex()));
186         else
187         {
188             zoomManager.setZoomAsText(combo.getText());
189         }
190     /*
191      * There are several cases where invoking setZoomAsText (above) will not result in
192      * zoomChanged being fired (the method below), such as when the user types "asdf" as
193      * the zoom level and hits enter, or when they type in 1%, which is below the minimum
194      * limit, and the current zoom is already at the minimum level. Hence, there is no
195      * guarantee that refresh() will always be invoked. But we need to invoke it to clear
196      * out the invalid text and show the current zoom level. Hence, an (often redundant)
197      * invocation to refresh() is made below.
198      */

199     refresh(false);
200   }
201
202   /**
203    * @see ZoomListener#zoomChanged(double)
204    */

205   public void zoomChanged(double zoom) {
206     refresh(false);
207   }
208   
209   /**
210    * Computes the width required by control
211    * @param control The control to compute width
212    * @return int The width required
213    */

214   protected int computeWidth(Control control) {
215     int width = control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x;
216     // $TODO: Windows workaround - Fixed in Eclipse 3.0
217
// Combo is not wide enough to show all text - add enough space for another character
218
if (SWT.getPlatform().equals("win32")) //$NON-NLS-1$
219
width += FigureUtilities.getTextWidth("8", control.getFont()); //$NON-NLS-1$
220
return width;
221   }
222
223   /**
224    * Creates and returns the control for this contribution item
225    * under the given parent composite.
226    *
227    * @param parent the parent composite
228    * @return the new control
229    */

230   protected Control createControl(Composite parent) {
231     combo = new Combo(parent, SWT.DROP_DOWN);
232     combo.addSelectionListener(new SelectionListener() {
233         public void widgetSelected(SelectionEvent e) {
234             handleWidgetSelected(e);
235         }
236         public void widgetDefaultSelected(SelectionEvent e) {
237             handleWidgetDefaultSelected(e);
238         }
239     });
240     combo.addFocusListener(new FocusListener() {
241         public void focusGained(FocusEvent e) {
242             // do nothing
243
}
244         public void focusLost(FocusEvent e) {
245             refresh(false);
246         }
247     });
248     
249     // Initialize width of combo
250
combo.setItems(initStrings);
251     toolitem.setWidth(computeWidth(combo));
252     refresh(true);
253     return combo;
254   }
255
256   /**
257    * @see org.eclipse.jface.action.ContributionItem#dispose()
258    */

259   public void dispose() {
260     if (partListener == null)
261         return;
262     service.removePartListener(partListener);
263     if (zoomManager != null) {
264         zoomManager.removeZoomListener(this);
265         zoomManager = null;
266     }
267     combo = null;
268     partListener = null;
269   }
270
271   /**
272    * The control item implementation of this <code>IContributionItem</code>
273    * method calls the <code>createControl</code> framework method to
274    * create a control under the given parent, and then creates
275    * a new tool item to hold it.
276    * Subclasses must implement <code>createControl</code> rather than
277    * overriding this method.
278    *
279    * @param parent The ToolBar to add the new control to
280    * @param index Index
281    */

282   public void fill(ToolBar parent, int index) {
283     toolitem = new ToolItem(parent, SWT.SEPARATOR, index);
284     Control control = createControl(parent);
285     toolitem.setControl(control);
286   }
287   
288   /**
289    * The control item implementation of this <code>IContributionItem</code>
290    * method calls the <code>createControl</code> framework method.
291    * Subclasses must implement <code>createControl</code> rather than
292    * overriding this method.
293    *
294    * @param parent The parent of the control to fill
295    */

296   public final void fill(Composite parent) {
297     createControl(parent);
298   }
299
300   /**
301    * The control item implementation of this <code>IContributionItem</code>
302    * method throws an exception since controls cannot be added to menus.
303    *
304    * @param parent The menu
305    * @param index Menu index
306    */

307   public final void fill(Menu parent, int index) {
308     Assert.isTrue(false, "Can't add a control to a menu");//$NON-NLS-1$
309
}
310 }
311
Popular Tags