KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > menus > LegacyMenuManager


1 /*******************************************************************************
2  * Copyright (c) 2006 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
12 package org.eclipse.ui.internal.menus;
13
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.commands.Command;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.core.commands.NotEnabledException;
20 import org.eclipse.core.commands.NotHandledException;
21 import org.eclipse.core.commands.ParameterizedCommand;
22 import org.eclipse.core.commands.common.NotDefinedException;
23 import org.eclipse.jface.action.ActionContributionItem;
24 import org.eclipse.jface.action.IContributionItem;
25 import org.eclipse.jface.action.IContributionManager;
26 import org.eclipse.jface.action.LegacyActionTools;
27 import org.eclipse.jface.action.MenuManager;
28 import org.eclipse.jface.action.SubMenuManager;
29 import org.eclipse.jface.menus.IWidget;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.events.SelectionListener;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.CoolBar;
35 import org.eclipse.swt.widgets.Menu;
36 import org.eclipse.swt.widgets.MenuItem;
37 import org.eclipse.swt.widgets.ToolBar;
38 import org.eclipse.ui.commands.ICommandService;
39 import org.eclipse.ui.internal.ActionSetContributionItem;
40 import org.eclipse.ui.internal.WorkbenchWindow;
41 import org.eclipse.ui.internal.misc.Policy;
42
43 /**
44  * <p>
45  * A wrapper around the new command-based menu services that speaks in terms of
46  * the old menu manager class.
47  * </p>
48  * <p>
49  * This class is not intended to be used outside of the
50  * <code>org.eclipse.ui.workbench</code> plug-in.
51  * </p>
52  *
53  * @since 3.2
54  */

55 public class LegacyMenuManager extends MenuManager {
56
57     /**
58      * The window on which this menu manager exists; never <code>null</code>.
59      */

60     private final WorkbenchWindow fWindow;
61
62     private IMenuService fMenuService;
63
64     private SMenuLayout fLayout;
65
66     /**
67      * Constructs a new instance of <code>LegacyMenuManager</code>.
68      *
69      * @param window
70      * The window on which this menu manager exists; must not be
71      * <code>null</code>.
72      */

73     public LegacyMenuManager(final WorkbenchWindow window) {
74         if (window == null) {
75             throw new NullPointerException JavaDoc("The window cannot be null"); //$NON-NLS-1$
76
}
77         this.fWindow = window;
78         fMenuService = (IMenuService) fWindow.getService(IMenuService.class);
79     }
80
81     /*
82      * (non-Javadoc)
83      *
84      * @see org.eclipse.jface.action.MenuManager#update(boolean, boolean)
85      */

86     protected void update(boolean force, boolean recursive) {
87         if (isDirty()) {
88             generateMenus();
89             fLayout = fMenuService.getLayout();
90             Menu menu = getMenu();
91             if (menu.getItemCount() > 0) {
92                 MenuItem[] items = menu.getItems();
93                 for (int i = 0; i < items.length; i++) {
94                     items[i].dispose();
95                 }
96             }
97             SMenuBuilder builder = new SMenuBuilder(fLayout, menu, fWindow);
98             builder.build();
99             setDirty(false);
100         }
101     }
102
103     /**
104      *
105      */

106     private void generateMenus() {
107         IContributionItem[] items = getItems();
108         SLocation location = new SLocation(new SBar());
109         processChildren(items, location);
110     }
111
112     /**
113      * @param item
114      * @param location
115      */

116     private void createMenu(IContributionItem item, SLocation location) {
117         if (item == null) {
118             return;
119         }
120         if (item instanceof ActionContributionItem) {
121             addActionContribution((ActionContributionItem) item, location);
122         } else if (item instanceof ActionSetContributionItem) {
123             addActionSetContribution((ActionSetContributionItem) item, location);
124         } else if (item instanceof MenuManager) {
125             addMenu((MenuManager) item, location);
126         } else if (item instanceof SubMenuManager) {
127             addSubMenuManager((SubMenuManager) item, location);
128         } else if (item instanceof IContributionManager) {
129             addContributionManager((IContributionManager) item, location);
130         } else {
131             if (Policy.EXPERIMENTAL_MENU) {
132                 System.err.println("createMenu: unknown: " //$NON-NLS-1$
133
+ item.getClass().getName());
134             }
135             addWidget(item, location);
136         }
137     }
138
139     /**
140      * @param manager
141      * @param location
142      */

143     private void addSubMenuManager(SubMenuManager manager,
144             SLocation parentLocation) {
145         processChildren(manager.getItems(), parentLocation);
146     }
147
148     /**
149      * @param manager
150      * @param parentLocation
151      */

152     private void addContributionManager(IContributionManager manager,
153             SLocation parentLocation) {
154         if (Policy.EXPERIMENTAL_MENU) {
155             System.err.println("addContributionManager: unknown: " //$NON-NLS-1$
156
+ manager.getClass().getName());
157         }
158
159         processChildren(manager.getItems(), parentLocation);
160     }
161
162     /**
163      * @param manager
164      * @param location
165      */

166     private void addMenu(MenuManager menu, SLocation parentLocation) {
167         String JavaDoc id = normalizeId(menu);
168         SMenu smenu = fMenuService.getMenu(id);
169         if (smenu.isDefined()) {
170             smenu.addLocation(parentLocation);
171         } else {
172             smenu.define(LegacyActionTools.removeMnemonics(menu.getMenuText()),
173                     parentLocation);
174         }
175         final char mnemonic = LegacyActionTools.extractMnemonic(menu
176                 .getMenuText());
177         SLocation location = new SLocation(parentLocation, id, mnemonic);
178
179         processChildren(menu.getItems(), location);
180     }
181
182     /**
183      * @param items
184      * @param location
185      */

186     private void processChildren(IContributionItem[] items, SLocation location) {
187         for (int i = 0; i < items.length; i++) {
188             if (items[i].isGroupMarker()) {
189                 addGroup(items[i], location);
190             } else if (items[i].isSeparator()) {
191                 addWidget(items[i], location);
192             } else {
193                 createMenu(items[i], location);
194             }
195         }
196     }
197
198     /**
199      * @param item
200      * @param location
201      */

202     private void addWidget(final IContributionItem item, SLocation location) {
203         String JavaDoc id = normalizeId(item);
204
205         SWidget swidget = fMenuService.getWidget(id);
206         if (swidget.isDefined()) {
207             swidget.addLocation(location);
208         } else {
209             swidget.define(new IWidget() {
210
211                 public void dispose() {
212                     item.dispose();
213                 }
214
215                 public void fill(Composite parent) {
216                     item.fill(parent);
217                 }
218
219                 public void fill(Menu parent, int index) {
220                     item.fill(parent, index);
221                 }
222
223                 public void fill(ToolBar parent, int index) {
224                     item.fill(parent, index);
225                 }
226
227                 public void fill(CoolBar parent, int index) {
228                     item.fill(parent, index);
229                 }
230
231             }, location);
232         }
233     }
234
235     /**
236      * @param item
237      * @param id
238      * @return the ID, or a made up one.
239      */

240     private String JavaDoc normalizeId(final IContributionItem item) {
241         String JavaDoc id = item.getId();
242         if (id == null || id.length() < 1) {
243             id = item.getClass().getName() + "@" + item.hashCode(); //$NON-NLS-1$
244
}
245         return id;
246     }
247
248     /**
249      * @param marker
250      * @param location
251      * @return the group location for use as a parent location
252      */

253     private SLocation addGroup(IContributionItem marker, SLocation location) {
254         String JavaDoc id = normalizeId(marker);
255         return addGroup(id, marker.isSeparator(), location);
256     }
257
258     /**
259      * @param manager
260      * @param location
261      * @return the group location for use as a parent location
262      */

263     protected SLocation addGroup(IContributionManager manager,
264             SLocation location) {
265         String JavaDoc id = manager.getClass().getName() + "@" + manager.hashCode(); //$NON-NLS-1$
266
return addGroup(id, false, location);
267     }
268
269     /**
270      * @param id
271      * @param isSeparator
272      * @param location
273      * @return the group location for use as a parent location
274      */

275     private SLocation addGroup(String JavaDoc id, boolean isSeparator,
276             SLocation location) {
277         SGroup group = fMenuService.getGroup(id);
278         if (group.isDefined()) {
279             group.addLocation(location);
280         } else {
281             group.define(isSeparator, location);
282         }
283         return new SLocation(location, group.getId());
284     }
285
286     /**
287      * @param actionSetContribution
288      * @param parentLocation
289      */

290     private void addActionSetContribution(
291             ActionSetContributionItem actionSetContribution,
292             SLocation parentLocation) {
293         createMenu(actionSetContribution.getInnerItem(), parentLocation);
294     }
295
296     /**
297      * @param item
298      * @param location
299      */

300     private void addActionContribution(
301             ActionContributionItem actionContribution, SLocation parentLocation) {
302         String JavaDoc id = normalizeId(actionContribution);
303         String JavaDoc commandId = actionContribution.getAction()
304                 .getActionDefinitionId();
305         if (commandId == null && Policy.EXPERIMENTAL_MENU) {
306             System.err
307                     .println("addActionContribution: When is a command not a command! " //$NON-NLS-1$
308
+ actionContribution.getId());
309             return;
310         }
311         SItem sitem = fMenuService.getItem(id);
312         if (sitem.isDefined()) {
313             sitem.addLocation(parentLocation);
314         } else {
315             ICommandService commandService = (ICommandService) fWindow
316                     .getService(ICommandService.class);
317             Command c = commandService.getCommand(commandId);
318             if (c.isDefined()) {
319                 ParameterizedCommand pc = new ParameterizedCommand(c, null);
320                 sitem.define(pc, actionContribution.getAction().getText(),
321                         parentLocation);
322             } else if (Policy.EXPERIMENTAL_MENU) {
323                 System.err
324                         .println("addActionContribution: undefined command " + commandId); //$NON-NLS-1$
325
}
326         }
327     }
328
329     private static class SMenuBuilder {
330
331         private static class IndexManager {
332             /**
333              *
334              */

335             public int index = 0;
336         }
337
338         private SMenuLayout fLayout;
339
340         private Menu fRootMenu;
341
342         private WorkbenchWindow fWindow;
343
344         /**
345          * @param rootNode
346          * @param menu
347          * @param window
348          */

349         public SMenuBuilder(SMenuLayout rootNode, Menu menu,
350                 WorkbenchWindow window) {
351             fLayout = rootNode;
352             fRootMenu = menu;
353             fWindow = window;
354         }
355
356         /**
357          *
358          */

359         public void build() {
360             ILayoutNode root = fLayout.getMenuBar();
361
362             List JavaDoc children = root.getChildrenSorted();
363             IndexManager index = new IndexManager();
364             for (Iterator JavaDoc i = children.iterator(); i.hasNext();) {
365                 ILayoutNode child = (ILayoutNode) i.next();
366                 addNode(fRootMenu, child, index);
367             }
368         }
369
370         /**
371          * @param menu
372          * @param node
373          * @param index
374          */

375         public void addNode(Menu menu, ILayoutNode node, IndexManager index) {
376             MenuElement element = node.getMenuElement();
377             if (Policy.EXPERIMENTAL_MENU
378                     && node.getLocation().getPath().toString().indexOf(
379                             LeafLocationElement.BREAKPOINT_PATH) > -1) {
380                 System.err
381                         .println("addNode: tree: " + node.getLocation() + "\n\t" //$NON-NLS-1$ //$NON-NLS-2$
382
+ element);
383             }
384             // System.err.println("addNode: " + element); //$NON-NLS-1$
385
if (element instanceof SMenu) {
386                 SMenu smenu = (SMenu) element;
387                 if (!smenu.isVisible(fWindow)) {
388                     return;
389                 }
390                 MenuItem item = new MenuItem(menu, SWT.CASCADE, index.index++);
391                 item.setData(smenu);
392
393                 try {
394                     item.setText(smenu.getLabel());
395                 } catch (NotDefinedException e) {
396                     // TODO Auto-generated catch block
397
e.printStackTrace();
398                 }
399
400                 Menu itemMenu = new Menu(menu);
401                 item.setMenu(itemMenu);
402
403                 List JavaDoc children = node.getChildrenSorted();
404                 IndexManager childIndex = new IndexManager();
405                 for (Iterator JavaDoc i = children.iterator(); i.hasNext();) {
406                     ILayoutNode child = (ILayoutNode) i.next();
407                     addNode(itemMenu, child, childIndex);
408                 }
409
410             } else if (element instanceof SItem) {
411                 final SItem sitem = (SItem) element;
412                 if (!sitem.isVisible(fWindow)) {
413                     return;
414                 }
415                 final MenuItem item = new MenuItem(menu, SWT.PUSH,
416                         index.index++);
417                 item.setData(element);
418
419                 try {
420                     item.setText(sitem.getCommand().getName());
421                     item.addSelectionListener(new SelectionListener() {
422                         public void widgetSelected(SelectionEvent e) {
423                             try {
424                                 sitem.getCommand()
425                                         .executeWithChecks(null, null);
426                             } catch (ExecutionException e1) {
427                                 // TODO Auto-generated catch block
428
e1.printStackTrace();
429                             } catch (NotDefinedException e1) {
430                                 // TODO Auto-generated catch block
431
e1.printStackTrace();
432                             } catch (NotEnabledException e1) {
433                                 // TODO Auto-generated catch block
434
e1.printStackTrace();
435                             } catch (NotHandledException e1) {
436                                 // TODO Auto-generated catch block
437
e1.printStackTrace();
438                             }
439                         }
440
441                         public void widgetDefaultSelected(SelectionEvent e) {
442                         }
443                     });
444                 } catch (NotDefinedException e1) {
445                     // TODO Auto-generated catch block
446
e1.printStackTrace();
447                 }
448             } else if (element instanceof SWidget) {
449                 SWidget swidget = (SWidget) element;
450                 if (!swidget.isVisible(fWindow)) {
451                     return;
452                 }
453                 try {
454                     swidget.getWidget().fill(menu, index.index);
455                 } catch (NotDefinedException e) {
456                     // TODO Auto-generated catch block
457
e.printStackTrace();
458                 }
459             } else if (element instanceof SGroup) {
460                 SGroup sgroup = (SGroup) element;
461                 if (!sgroup.isVisible(fWindow)) {
462                     return;
463                 }
464                 try {
465                     if (sgroup.isSeparatorsVisible()) {
466                         MenuItem item = new MenuItem(menu, SWT.SEPARATOR,
467                                 index.index++);
468                         item.setData(sgroup);
469                     }
470
471                     List JavaDoc children = node.getChildrenSorted();
472                     for (Iterator JavaDoc i = children.iterator(); i.hasNext();) {
473                         ILayoutNode child = (ILayoutNode) i.next();
474                         addNode(menu, child, index);
475                     }
476                 } catch (NotDefinedException e) {
477                     // TODO Auto-generated catch block
478
e.printStackTrace();
479                 }
480             } else {
481                 if (Policy.EXPERIMENTAL_MENU) {
482                     System.err.println("addNode: no element for " //$NON-NLS-1$
483
+ node.getLocation());
484                 }
485                 if (node.isEmpty()) {
486                     return;
487                 }
488
489                 List JavaDoc children = node.getChildrenSorted();
490
491                 if (!children.isEmpty()) {
492
493                     MenuItem item = new MenuItem(menu, SWT.CASCADE,
494                             index.index++);
495                     item.setData(node);
496
497                     item
498                             .setText(node.getId() == null ? "node" + (index.index - 1) //$NON-NLS-1$
499
: node.getId());
500
501                     Menu itemMenu = new Menu(menu);
502                     item.setMenu(itemMenu);
503
504                     IndexManager childIndex = new IndexManager();
505                     for (Iterator JavaDoc i = children.iterator(); i.hasNext();) {
506                         ILayoutNode child = (ILayoutNode) i.next();
507                         addNode(itemMenu, child, childIndex);
508                     }
509                 }
510             }
511         }
512     }
513
514 }
515
Popular Tags