KickJava   Java API By Example, From Geeks To Geeks.

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


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.custom;
9
10 import org.eclipse.draw2d.FigureUtilities;
11 import org.eclipse.gef.editparts.ZoomListener;
12 import org.eclipse.gef.editparts.ZoomManager;
13 import org.eclipse.gef.ui.actions.GEFActionConstants;
14 import org.eclipse.jface.action.ContributionItem;
15 import org.eclipse.jface.util.Assert;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.SWTException;
18 import org.eclipse.swt.events.FocusEvent;
19 import org.eclipse.swt.events.FocusListener;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.events.SelectionListener;
22 import org.eclipse.swt.widgets.Combo;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Menu;
26 import org.eclipse.swt.widgets.ToolBar;
27 import org.eclipse.swt.widgets.ToolItem;
28 import org.eclipse.ui.IPartListener;
29 import org.eclipse.ui.IPartService;
30 import org.eclipse.ui.IWorkbenchPart;
31
32
33 public class EditorZoomComboContributionItem
34 //extends ZoomComboContributionItem
35
extends ContributionItem
36 implements ZoomListener
37 {
38   protected Combo combo;
39   protected String JavaDoc[] initStrings;
40   protected ToolItem toolitem;
41   protected ZoomManager zoomManager;
42   protected IPartService service;
43   protected IPartListener partListener;
44   
45   public double[] zoomLevels = {.01, .05, .1, .25, .5, .75, 1.0, 1.5, 2.0, 2.5, 3, 4, 5};
46   
47   /**
48    * Constructor for ComboToolItem.
49    * @param partService used to add a PartListener
50    */

51   public EditorZoomComboContributionItem(IPartService partService)
52   {
53     this(partService, "8888%");//$NON-NLS-1$
54
}
55
56   /**
57    * Constructor for ComboToolItem.
58    * @param partService used to add a PartListener
59    * @param initString the initial string displayed in the combo
60    */

61   public EditorZoomComboContributionItem(IPartService partService, String JavaDoc initString)
62   {
63     this(partService, new String JavaDoc[] {initString});
64   }
65   
66   /**
67    * Constructor for ComboToolItem.
68    * @param partService used to add a PartListener
69    * @param initStrings the initial string displayed in the combo
70    */

71   public EditorZoomComboContributionItem(IPartService partService, String JavaDoc[] initStrings)
72   {
73     super(GEFActionConstants.ZOOM_TOOLBAR_WIDGET);
74 // super(partService, initStrings);
75
this.initStrings = initStrings;
76     service = partService;
77     Assert.isNotNull(partService);
78     partService.addPartListener(partListener = new IPartListener() {
79         public void partActivated(IWorkbenchPart part)
80         {
81           Object JavaDoc adapter = part.getAdapter(ZoomManager.class);
82           if (adapter != null && adapter instanceof ZoomManager)
83           {
84                 setZoomManager((ZoomManager)adapter);
85                 getZoomManager().setZoomLevels(zoomLevels);
86 // getZoomManager().setZoomAnimationStyle(ZoomManager.ANIMATE_ZOOM_IN_OUT);
87
refresh(true);
88           }
89         }
90         public void partBroughtToTop(IWorkbenchPart p) { }
91         public void partClosed(IWorkbenchPart p) { }
92         public void partDeactivated(IWorkbenchPart p) { }
93         public void partOpened(IWorkbenchPart p) { }
94     });
95   }
96   
97   protected void refresh(boolean repopulateCombo)
98   {
99     if (combo == null || combo.isDisposed())
100         return;
101     //$TODO GTK workaround
102
try {
103         if (zoomManager == null) {
104             combo.setEnabled(false);
105             combo.setText(""); //$NON-NLS-1$
106
} else {
107             if (repopulateCombo) {
108                 combo.setItems(getZoomManager().getZoomLevelsAsText());
109             }
110             String JavaDoc zoom = getZoomManager().getZoomAsText();
111             int index = combo.indexOf(zoom);
112             if (index != -1)
113                 combo.select(index);
114             else
115                 combo.setText(zoom);
116             combo.setEnabled(true);
117         }
118     } catch (SWTException exception) {
119         if (!SWT.getPlatform().equals("gtk")) //$NON-NLS-1$
120
throw exception;
121     }
122   }
123   
124   /**
125    * Returns the zoomManager.
126    * @return ZoomManager
127    */

128   public ZoomManager getZoomManager()
129   {
130     return zoomManager;
131   }
132
133   /**
134    * Sets the ZoomManager
135    * @param zm The ZoomManager
136    */

137   public void setZoomManager(ZoomManager zm)
138   {
139     if (zoomManager == zm)
140         return;
141     if (zoomManager != null)
142         zoomManager.removeZoomListener(this);
143
144     zoomManager = zm;
145     refresh(true);
146
147     if (zoomManager != null)
148         zoomManager.addZoomListener(this);
149   }
150   
151   /**
152    * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(SelectionEvent)
153    */

154   private void handleWidgetDefaultSelected(SelectionEvent event) {
155     handleWidgetSelected(event);
156   }
157
158   /**
159    * @see org.eclipse.swt.events.SelectionListener#widgetSelected(SelectionEvent)
160    */

161   private void handleWidgetSelected(SelectionEvent event) {
162     // TODO: GTK Workaround (fixed in 3.0) - see SWT bug #44345
163
if (zoomManager != null)
164         if (combo.getSelectionIndex() >= 0)
165             zoomManager.setZoomAsText(combo.getItem(combo.getSelectionIndex()));
166         else
167         {
168             zoomManager.setZoomAsText(combo.getText());
169         }
170     /*
171      * There are several cases where invoking setZoomAsText (above) will not result in
172      * zoomChanged being fired (the method below), such as when the user types "asdf" as
173      * the zoom level and hits enter, or when they type in 1%, which is below the minimum
174      * limit, and the current zoom is already at the minimum level. Hence, there is no
175      * guarantee that refresh() will always be invoked. But we need to invoke it to clear
176      * out the invalid text and show the current zoom level. Hence, an (often redundant)
177      * invocation to refresh() is made below.
178      */

179     refresh(false);
180   }
181
182   /**
183    * @see ZoomListener#zoomChanged(double)
184    */

185   public void zoomChanged(double zoom) {
186     refresh(false);
187   }
188   
189   /**
190    * Computes the width required by control
191    * @param control The control to compute width
192    * @return int The width required
193    */

194   protected int computeWidth(Control control) {
195     int width = control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x;
196     // $TODO: Windows workaround - Fixed in Eclipse 3.0
197
// Combo is not wide enough to show all text - add enough space for another character
198
if (SWT.getPlatform().equals("win32")) //$NON-NLS-1$
199
width += FigureUtilities.getTextWidth("8", control.getFont()); //$NON-NLS-1$
200
return width;
201   }
202
203   /**
204    * Creates and returns the control for this contribution item
205    * under the given parent composite.
206    *
207    * @param parent the parent composite
208    * @return the new control
209    */

210   protected Control createControl(Composite parent) {
211     combo = new Combo(parent, SWT.DROP_DOWN);
212     combo.addSelectionListener(new SelectionListener() {
213         public void widgetSelected(SelectionEvent e) {
214             handleWidgetSelected(e);
215         }
216         public void widgetDefaultSelected(SelectionEvent e) {
217             handleWidgetDefaultSelected(e);
218         }
219     });
220     combo.addFocusListener(new FocusListener() {
221         public void focusGained(FocusEvent e) {
222             // do nothing
223
}
224         public void focusLost(FocusEvent e) {
225             refresh(false);
226         }
227     });
228     
229     // Initialize width of combo
230
combo.setItems(initStrings);
231     toolitem.setWidth(computeWidth(combo));
232     refresh(true);
233     return combo;
234   }
235
236   /**
237    * @see org.eclipse.jface.action.ContributionItem#dispose()
238    */

239   public void dispose() {
240     if (partListener == null)
241         return;
242     service.removePartListener(partListener);
243     if (zoomManager != null) {
244         zoomManager.removeZoomListener(this);
245         zoomManager = null;
246     }
247     combo = null;
248     partListener = null;
249   }
250
251   /**
252    * The control item implementation of this <code>IContributionItem</code>
253    * method calls the <code>createControl</code> framework method to
254    * create a control under the given parent, and then creates
255    * a new tool item to hold it.
256    * Subclasses must implement <code>createControl</code> rather than
257    * overriding this method.
258    *
259    * @param parent The ToolBar to add the new control to
260    * @param index Index
261    */

262   public void fill(ToolBar parent, int index) {
263     toolitem = new ToolItem(parent, SWT.SEPARATOR, index);
264     Control control = createControl(parent);
265     toolitem.setControl(control);
266   }
267   
268   /**
269    * The control item implementation of this <code>IContributionItem</code>
270    * method calls the <code>createControl</code> framework method.
271    * Subclasses must implement <code>createControl</code> rather than
272    * overriding this method.
273    *
274    * @param parent The parent of the control to fill
275    */

276   public final void fill(Composite parent) {
277     createControl(parent);
278   }
279
280   /**
281    * The control item implementation of this <code>IContributionItem</code>
282    * method throws an exception since controls cannot be added to menus.
283    *
284    * @param parent The menu
285    * @param index Menu index
286    */

287   public final void fill(Menu parent, int index) {
288     Assert.isTrue(false, "Can't add a control to a menu");//$NON-NLS-1$
289
}
290 }
291
Popular Tags