KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > action > ToolBarManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.action;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.accessibility.ACC;
18 import org.eclipse.swt.accessibility.AccessibleAdapter;
19 import org.eclipse.swt.accessibility.AccessibleEvent;
20 import org.eclipse.swt.accessibility.AccessibleListener;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Menu;
24 import org.eclipse.swt.widgets.ToolBar;
25 import org.eclipse.swt.widgets.ToolItem;
26
27 /**
28  * A tool bar manager is a contribution manager which realizes itself and its
29  * items in a tool bar control.
30  * <p>
31  * This class may be instantiated; it may also be subclassed if a more
32  * sophisticated layout is required.
33  * </p>
34  */

35 public class ToolBarManager extends ContributionManager implements
36         IToolBarManager {
37
38     /**
39      * The tool bar items style; <code>SWT.NONE</code> by default.
40      */

41     private int itemStyle = SWT.NONE;
42
43     /**
44      * The tool bat control; <code>null</code> before creation and after
45      * disposal.
46      */

47     private ToolBar toolBar = null;
48
49     /**
50      * The menu manager to the context menu associated with the toolbar.
51      *
52      * @since 3.0
53      */

54     private MenuManager contextMenuManager = null;
55
56     /**
57      * Creates a new tool bar manager with the default SWT button style. Use the
58      * {@link #createControl(Composite)} method to create the tool bar control.
59      *
60      */

61     public ToolBarManager() {
62         //Do nothing if there are no parameters
63
}
64
65     /**
66      * Creates a tool bar manager with the given SWT button style. Use the
67      * <code>createControl</code> method to create the tool bar control.
68      *
69      * @param style
70      * the tool bar item style
71      * @see org.eclipse.swt.widgets.ToolBar for valid style bits
72      */

73     public ToolBarManager(int style) {
74         itemStyle = style;
75     }
76
77     /**
78      * Creates a tool bar manager for an existing tool bar control. This manager
79      * becomes responsible for the control, and will dispose of it when the
80      * manager is disposed.
81      * <strong>NOTE</strong> When creating a ToolBarManager from an existing
82      * {@link ToolBar} you will not get the accessible listener provided by
83      * JFace.
84      * @see #ToolBarManager()
85      * @see #ToolBarManager(int)
86      *
87      * @param toolbar
88      * the tool bar control
89      */

90     public ToolBarManager(ToolBar toolbar) {
91         this();
92         this.toolBar = toolbar;
93     }
94
95     /**
96      * Creates and returns this manager's tool bar control. Does not create a
97      * new control if one already exists. Also create an {@link AccessibleListener}
98      * for the {@link ToolBar}.
99      *
100      * @param parent
101      * the parent control
102      * @return the tool bar control
103      */

104     public ToolBar createControl(Composite parent) {
105         if (!toolBarExist() && parent != null) {
106             toolBar = new ToolBar(parent, itemStyle);
107             toolBar.setMenu(getContextMenuControl());
108             update(true);
109             
110             toolBar.getAccessible().addAccessibleListener(getAccessibleListener());
111         }
112
113         return toolBar;
114     }
115
116     /**
117      * Get the accessible listener for the tool bar.
118      *
119      * @return AccessibleListener
120      *
121      * @since 3.1
122      */

123     private AccessibleListener getAccessibleListener() {
124         return new AccessibleAdapter() {
125             public void getName(AccessibleEvent e) {
126                 if (e.childID != ACC.CHILDID_SELF) {
127                     ToolItem item = toolBar.getItem(e.childID);
128                     if (item != null) {
129                         String JavaDoc toolTip = item.getToolTipText();
130                         if (toolTip != null) {
131                             e.result = toolTip;
132                         }
133                     }
134                 }
135             }
136         };
137
138     }
139
140     /**
141      * Disposes of this tool bar manager and frees all allocated SWT resources.
142      * Notifies all contribution items of the dispose. Note that this method
143      * does not clean up references between this tool bar manager and its
144      * associated contribution items. Use <code>removeAll</code> for that
145      * purpose.
146      */

147     public void dispose() {
148
149         if (toolBarExist()) {
150             toolBar.dispose();
151         }
152         toolBar = null;
153
154         IContributionItem[] items = getItems();
155         for (int i = 0; i < items.length; i++) {
156             items[i].dispose();
157         }
158
159         if (getContextMenuManager() != null) {
160             getContextMenuManager().dispose();
161             setContextMenuManager(null);
162         }
163     }
164
165     /**
166      * Returns the tool bar control for this manager.
167      *
168      * @return the tool bar control, or <code>null</code> if none (before
169      * creating or after disposal)
170      */

171     public ToolBar getControl() {
172         return toolBar;
173     }
174
175     /**
176      * Re-lays out the tool bar.
177      * <p>
178      * The default implementation of this framework method re-lays out the
179      * parent when the number of items crosses the zero threshold. Subclasses
180      * should override this method to implement their own re-layout strategy
181      *
182      * @param layoutBar
183      * the tool bar control
184      * @param oldCount
185      * the old number of items
186      * @param newCount
187      * the new number of items
188      */

189     protected void relayout(ToolBar layoutBar, int oldCount, int newCount) {
190         if ((oldCount == 0) != (newCount == 0)) {
191             layoutBar.getParent().layout();
192         }
193     }
194
195     /**
196      * Returns whether the tool bar control is created and not disposed.
197      *
198      * @return <code>true</code> if the control is created and not disposed,
199      * <code>false</code> otherwise
200      */

201     private boolean toolBarExist() {
202         return toolBar != null && !toolBar.isDisposed();
203     }
204
205     /*
206      * (non-Javadoc) Method declared on IContributionManager.
207      */

208     public void update(boolean force) {
209
210         // long startTime= 0;
211
// if (DEBUG) {
212
// dumpStatistics();
213
// startTime= (new Date()).getTime();
214
// }
215

216         if (isDirty() || force) {
217
218             if (toolBarExist()) {
219
220                 int oldCount = toolBar.getItemCount();
221
222                 // clean contains all active items without double separators
223
IContributionItem[] items = getItems();
224                 ArrayList JavaDoc clean = new ArrayList JavaDoc(items.length);
225                 IContributionItem separator = null;
226                 // long cleanStartTime= 0;
227
// if (DEBUG) {
228
// cleanStartTime= (new Date()).getTime();
229
// }
230
for (int i = 0; i < items.length; ++i) {
231                     IContributionItem ci = items[i];
232                     if (!ci.isVisible()) {
233                         continue;
234                     }
235                     if (ci.isSeparator()) {
236                         // delay creation until necessary
237
// (handles both adjacent separators, and separator at
238
// end)
239
separator = ci;
240                     } else {
241                         if (separator != null) {
242                             if (clean.size() > 0) {
243                                 clean.add(separator);
244                             }
245                             separator = null;
246                         }
247                         clean.add(ci);
248                     }
249                 }
250                 // if (DEBUG) {
251
// System.out.println(" Time needed to build clean vector: " +
252
// ((new Date()).getTime() - cleanStartTime));
253
// }
254

255                 // determine obsolete items (removed or non active)
256
ToolItem[] mi = toolBar.getItems();
257                 ArrayList JavaDoc toRemove = new ArrayList JavaDoc(mi.length);
258                 for (int i = 0; i < mi.length; i++) {
259                     Object JavaDoc data = mi[i].getData();
260                     if (data == null
261                             || !clean.contains(data)
262                             || (data instanceof IContributionItem && ((IContributionItem) data)
263                                     .isDynamic())) {
264                         toRemove.add(mi[i]);
265                     }
266                 }
267
268                 // Turn redraw off if the number of items to be added
269
// is above a certain threshold, to minimize flicker,
270
// otherwise the toolbar can be seen to redraw after each item.
271
// Do this before any modifications are made.
272
// We assume each contribution item will contribute at least one
273
// toolbar item.
274
boolean useRedraw = (clean.size() - (mi.length - toRemove
275                         .size())) >= 3;
276                 try {
277                     if (useRedraw) {
278                         toolBar.setRedraw(false);
279                     }
280
281                     // remove obsolete items
282
for (int i = toRemove.size(); --i >= 0;) {
283                         ToolItem item = (ToolItem) toRemove.get(i);
284                         if (!item.isDisposed()) {
285                             Control ctrl = item.getControl();
286                             if (ctrl != null) {
287                                 item.setControl(null);
288                                 ctrl.dispose();
289                             }
290                             item.dispose();
291                         }
292                     }
293
294                     // add new items
295
IContributionItem src, dest;
296                     mi = toolBar.getItems();
297                     int srcIx = 0;
298                     int destIx = 0;
299                     for (Iterator JavaDoc e = clean.iterator(); e.hasNext();) {
300                         src = (IContributionItem) e.next();
301
302                         // get corresponding item in SWT widget
303
if (srcIx < mi.length) {
304                             dest = (IContributionItem) mi[srcIx].getData();
305                         } else {
306                             dest = null;
307                         }
308
309                         if (dest != null && src.equals(dest)) {
310                             srcIx++;
311                             destIx++;
312                             continue;
313                         }
314
315                         if (dest != null && dest.isSeparator()
316                                 && src.isSeparator()) {
317                             mi[srcIx].setData(src);
318                             srcIx++;
319                             destIx++;
320                             continue;
321                         }
322
323                         int start = toolBar.getItemCount();
324                         src.fill(toolBar, destIx);
325                         int newItems = toolBar.getItemCount() - start;
326                         for (int i = 0; i < newItems; i++) {
327                             ToolItem item = toolBar.getItem(destIx++);
328                             item.setData(src);
329                         }
330                     }
331
332                     // remove any old tool items not accounted for
333
for (int i = mi.length; --i >= srcIx;) {
334                         ToolItem item = mi[i];
335                         if (!item.isDisposed()) {
336                             Control ctrl = item.getControl();
337                             if (ctrl != null) {
338                                 item.setControl(null);
339                                 ctrl.dispose();
340                             }
341                             item.dispose();
342                         }
343                     }
344
345                     setDirty(false);
346
347                     // turn redraw back on if we turned it off above
348
} finally {
349                     if (useRedraw) {
350                         toolBar.setRedraw(true);
351                     }
352                 }
353
354                 int newCount = toolBar.getItemCount();
355                 relayout(toolBar, oldCount, newCount);
356             }
357
358         }
359
360         // if (DEBUG) {
361
// System.out.println(" Time needed for update: " + ((new
362
// Date()).getTime() - startTime));
363
// System.out.println();
364
// }
365
}
366
367     /**
368      * Returns the control of the Menu Manager. If the menu manager does not
369      * have a control then one is created.
370      *
371      * @return menu widget associated with manager
372      */

373     private Menu getContextMenuControl() {
374         if ((contextMenuManager != null) && (toolBar != null)) {
375             Menu menuWidget = contextMenuManager.getMenu();
376             if ((menuWidget == null) || (menuWidget.isDisposed())) {
377                 menuWidget = contextMenuManager.createContextMenu(toolBar);
378             }
379             return menuWidget;
380         }
381         return null;
382     }
383
384     /**
385      * Returns the context menu manager for this tool bar manager.
386      *
387      * @return the context menu manager, or <code>null</code> if none
388      * @since 3.0
389      */

390     public MenuManager getContextMenuManager() {
391         return contextMenuManager;
392     }
393
394     /**
395      * Sets the context menu manager for this tool bar manager to the given menu
396      * manager. If the tool bar control exists, it also adds the menu control to
397      * the tool bar.
398      *
399      * @param contextMenuManager
400      * the context menu manager, or <code>null</code> if none
401      * @since 3.0
402      */

403     public void setContextMenuManager(MenuManager contextMenuManager) {
404         this.contextMenuManager = contextMenuManager;
405         if (toolBar != null) {
406             toolBar.setMenu(getContextMenuControl());
407         }
408     }
409
410 }
411
Popular Tags