Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 527fd2bf85dc1c9a9103974ba9def99b9eb3bf41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
/*******************************************************************************
 * Copyright (c) 2000, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Remy Chi Jian Suen <remy.suen@gmail.com> - Bug 12116 [Contributions] widgets: MenuManager.setImageDescriptor() method needed
 *******************************************************************************/
package org.eclipse.jface.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.internal.MenuManagerEventHelper;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.resource.LocalResourceManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.Decorations;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;

/**
 * A menu manager is a contribution manager which realizes itself and its items
 * in a menu control; either as a menu bar, a sub-menu, or a context menu.
 * <p>
 * This class may be instantiated; it may also be subclassed.
 * </p>
 */
public class MenuManager extends ContributionManager implements IMenuManager {
	private static final String MANAGER_KEY = "org.eclipse.jface.action.MenuManager.managerKey"; //$NON-NLS-1$

    /**
     * The menu id.
     */
    private String id;

    /**
     * List of registered menu listeners (element type: <code>IMenuListener</code>).
     */
    private ListenerList listeners = new ListenerList();

    /**
     * The menu control; <code>null</code> before
     * creation and after disposal.
     */
    private Menu menu = null;

    /**
     * The menu item widget; <code>null</code> before
     * creation and after disposal. This field is used
     * when this menu manager is a sub-menu.
     */
    private MenuItem menuItem;

    /**
     * The text for a sub-menu.
     */
    private String menuText;
    
    /**
     * The image for a sub-menu.
     */
    private ImageDescriptor image;
    
    /**
     * A resource manager to remember all of the images that have been used by this menu.
     */
    private LocalResourceManager imageManager;

    /**
     * The overrides for items of this manager
     */
    private IContributionManagerOverrides overrides;

    /**
     * The parent contribution manager.
     */
    private IContributionManager parent;

    /**
     * Indicates whether <code>removeAll</code> should be
     * called just before the menu is displayed.
     */
    private boolean removeAllWhenShown = false;

    /**
     * Indicates this item is visible in its manager; <code>true</code> 
     * by default.
     * @since 3.3
     */
    protected boolean visible = true;

	/**
	 * allows a submenu to display a shortcut key. This is often used with the
	 * QuickMenu command or action which can pop up a menu using the shortcut.
	 */
	private String definitionId = null;

    /**
     * Creates a menu manager.  The text and id are <code>null</code>.
     * Typically used for creating a context menu, where it doesn't need to be referred to by id.
     */
    public MenuManager() {
        this(null, null, null);
    }

    /**
     * Creates a menu manager with the given text. The id of the menu
     * is <code>null</code>.
     * Typically used for creating a sub-menu, where it doesn't need to be referred to by id.
     *
     * @param text the text for the menu, or <code>null</code> if none
     */
    public MenuManager(String text) {
        this(text, null, null);
    }

    /**
     * Creates a menu manager with the given text and id.
     * Typically used for creating a sub-menu, where it needs to be referred to by id.
     *
     * @param text the text for the menu, or <code>null</code> if none
     * @param id the menu id, or <code>null</code> if it is to have no id
     */
    public MenuManager(String text, String id) {
        this(text, null, id);
    }

    /**
     * Creates a menu manager with the given text, image, and id.
     * Typically used for creating a sub-menu, where it needs to be referred to by id.
     * 
     * @param text the text for the menu, or <code>null</code> if none
     * @param image the image for the menu, or <code>null</code> if none
     * @param id the menu id, or <code>null</code> if it is to have no id
     * @since 3.4
     */
    public MenuManager(String text, ImageDescriptor image, String id) {
        this.menuText = text;
        this.image = image;
        this.id = id;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IMenuManager#addMenuListener(org.eclipse.jface.action.IMenuListener)
     */
    public void addMenuListener(IMenuListener listener) {
        listeners.add(listener);
    }

    /**
     * Creates and returns an SWT context menu control for this menu,
     * and installs all registered contributions.
     * Does not create a new control if one already exists.
     * <p>
     * Note that the menu is not expected to be dynamic.
     * </p>
     *
     * @param parent the parent control
     * @return the menu control
     */
    public Menu createContextMenu(Control parent) {
        if (!menuExist()) {
            menu = new Menu(parent);
            menu.setData(MANAGER_KEY, this);
            initializeMenu();
        }
        return menu;
    }

    /**
     * Creates and returns an SWT menu bar control for this menu,
     * for use in the given <code>Decorations</code>, and installs all registered
     * contributions. Does not create a new control if one already exists.
     *
     * @param parent the parent decorations
     * @return the menu control
     * @since 2.1
     */
    public Menu createMenuBar(Decorations parent) {
        if (!menuExist()) {
            menu = new Menu(parent, SWT.BAR);
            menu.setData(MANAGER_KEY, this);
            update(false);
        }
        return menu;
    }

    /**
     * Creates and returns an SWT menu bar control for this menu, for use in the
     * given <code>Shell</code>, and installs all registered contributions. Does not
     * create a new control if one already exists. This implementation simply calls
     * the <code>createMenuBar(Decorations)</code> method
     *
     * @param parent the parent decorations
     * @return the menu control
     * @deprecated use <code>createMenuBar(Decorations)</code> instead.
     */
    public Menu createMenuBar(Shell parent) {
        return createMenuBar((Decorations) parent);
    }

    /**
     * Disposes of this menu manager and frees all allocated SWT resources. Notifies all
     * contribution items of the dispose. Note that this method does not clean up references between
     * this menu manager and its associated contribution items. Use {@link #removeAll()} for that
     * purpose, but note that will not dispose the items.
     */
    public void dispose() {
        if (menuExist()) {
			menu.dispose();
		}
        menu = null;

        if (menuItem != null) {
            menuItem.dispose();
            menuItem = null;
        }

        disposeOldImages();
        
        IContributionItem[] items = getItems();
        for (int i = 0; i < items.length; i++) {
            items[i].dispose();
        }
        
        markDirty();
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IContributionItem#fill(org.eclipse.swt.widgets.Composite)
     */
    public void fill(Composite parent) {
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IContributionItem#fill(org.eclipse.swt.widgets.CoolBar, int)
     */
    public void fill(CoolBar parent, int index) {
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IContributionItem#fill(org.eclipse.swt.widgets.Menu, int)
     */
    public void fill(Menu parent, int index) {
        if (menuItem == null || menuItem.isDisposed()) {
            if (index >= 0) {
				menuItem = new MenuItem(parent, SWT.CASCADE, index);
			} else {
				menuItem = new MenuItem(parent, SWT.CASCADE);
			}

			String text = getMenuText();
			if (text != null) {
				menuItem.setText(text);
			}

            if (image != null) {
				LocalResourceManager localManager = new LocalResourceManager(
						JFaceResources.getResources());
				menuItem.setImage(localManager.createImage(image));
				disposeOldImages();
				imageManager = localManager;
			}

            if (!menuExist()) {
				menu = new Menu(parent);
				menu.setData(MANAGER_KEY, this);
			}

            menuItem.setMenu(menu);

            initializeMenu();

            setDirty(true);
        }
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IContributionItem#fill(org.eclipse.swt.widgets.ToolBar, int)
     */
    public void fill(ToolBar parent, int index) {
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IMenuManager#findMenuUsingPath(java.lang.String)
     */
    public IMenuManager findMenuUsingPath(String path) {
        IContributionItem item = findUsingPath(path);
        if (item instanceof IMenuManager) {
			return (IMenuManager) item;
		}
        return null;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IMenuManager#findUsingPath(java.lang.String)
     */
    public IContributionItem findUsingPath(String path) {
        String id = path;
        String rest = null;
        int separator = path.indexOf('/');
        if (separator != -1) {
            id = path.substring(0, separator);
            rest = path.substring(separator + 1);
        } else {
            return super.find(path);
        }

        IContributionItem item = super.find(id);
        if (item instanceof IMenuManager) {
            IMenuManager manager = (IMenuManager) item;
            return manager.findUsingPath(rest);
        }
        return null;
    }

    /**
     * Notifies any menu listeners that a menu is about to show.
     * Only listeners registered at the time this method is called are notified.
     *
     * @param manager the menu manager
     *
     * @see IMenuListener#menuAboutToShow
     */
    private void fireAboutToShow(IMenuManager manager) {
        Object[] listeners = this.listeners.getListeners();
        for (int i = 0; i < listeners.length; ++i) {
            ((IMenuListener) listeners[i]).menuAboutToShow(manager);
        }
    }

    /**
     * Notifies any menu listeners that a menu is about to hide.
     * Only listeners registered at the time this method is called are notified.
     *
     * @param manager the menu manager
     *
     */
    private void fireAboutToHide(IMenuManager manager) {
        final Object[] listeners = this.listeners.getListeners();
        for (int i = 0; i < listeners.length; ++i) {
        	final Object listener = listeners[i];
			if (listener instanceof IMenuListener2) {
				final IMenuListener2 listener2 = (IMenuListener2) listener;
				listener2.menuAboutToHide(manager);
			}
        }
    }

    /**
	 * Returns the menu id. The menu id is used when creating a contribution
	 * item for adding this menu as a sub menu of another.
	 * 
	 * @return the menu id
	 */
    public String getId() {
        return id;
    }

    /**
     * Returns the SWT menu control for this menu manager.
     *
     * @return the menu control
     */
    public Menu getMenu() {
        return menu;
    }

    /**
     * Returns the text shown in the menu, potentially with a shortcut
     * appended.
     *
     * @return the menu text
     */
    public String getMenuText() {
		if (definitionId == null) {
			return menuText;
		}
		ExternalActionManager.ICallback callback = ExternalActionManager
				.getInstance().getCallback();
		if (callback != null) {
			String shortCut = callback.getAcceleratorText(definitionId);
			if (shortCut == null) {
				return menuText;
			}
			return menuText + "\t" + shortCut; //$NON-NLS-1$
		}
		return menuText;
	}
    
    /**
	 * Returns the image for this menu as an image descriptor.
	 * 
	 * @return the image, or <code>null</code> if this menu has no image
	 * @since 3.4
	 */
    public ImageDescriptor getImageDescriptor() {
    	return image;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IContributionManager#getOverrides()
     */
    @Override
	public IContributionManagerOverrides getOverrides() {
        if (overrides == null) {
            if (parent == null) {
                overrides = new IContributionManagerOverrides() {
                    public Integer getAccelerator(IContributionItem item) {
                        return null;
                    }

                    public String getAcceleratorText(IContributionItem item) {
                        return null;
                    }

                    public Boolean getEnabled(IContributionItem item) {
                        return null;
                    }

                    public String getText(IContributionItem item) {
                        return null;
                    }
    				public Boolean getVisible(IContributionItem item) {
    					return null;
    				}
                };
            } else {
                overrides = parent.getOverrides();
            }
            super.setOverrides(overrides);
        }
        return overrides;
    }

    /**
     * Returns the parent contribution manager of this manger.
     * 
     * @return the parent contribution manager
     * @since 2.0
     */
    public IContributionManager getParent() {
        return parent;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IMenuManager#getRemoveAllWhenShown()
     */
    public boolean getRemoveAllWhenShown() {
        return removeAllWhenShown;
    }

    /**
     * Notifies all listeners that this menu is about to appear.
     */
    private void handleAboutToShow() {
        if (removeAllWhenShown) {
			removeAll();
		}
        MenuManagerEventHelper.showEventPreHelper(this);
        fireAboutToShow(this);
        MenuManagerEventHelper.showEventPostHelper(this);
        update(false, false);
    }

    /**
     * Notifies all listeners that this menu is about to disappear.
     */
    private void handleAboutToHide() {
    	MenuManagerEventHelper.hideEventPreHelper(this);
        fireAboutToHide(this);
        MenuManagerEventHelper.hideEventPostHelper(this);
    }

    /**
     * Initializes the menu control.
     */
    private void initializeMenu() {
        menu.addMenuListener(new MenuAdapter() {
            @Override
			public void menuHidden(MenuEvent e) {
                //			ApplicationWindow.resetDescription(e.widget);
            	handleAboutToHide();
            }

            @Override
			public void menuShown(MenuEvent e) {
                handleAboutToShow();
            }
        });
        // Don't do an update(true) here, in case menu is never opened.
        // Always do it lazily in handleAboutToShow().
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IContributionItem#isDynamic()
     */
    public boolean isDynamic() {
        return false;
    }

    /**
     * Returns whether this menu should be enabled or not.
     * Used to enable the menu item containing this menu when it is realized as a sub-menu.
     * <p>
     * The default implementation of this framework method
     * returns <code>true</code>. Subclasses may reimplement.
     * </p>
     *
     * @return <code>true</code> if enabled, and
     *   <code>false</code> if disabled
     */
    public boolean isEnabled() {
        return true;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IContributionItem#isGroupMarker()
     */
    public boolean isGroupMarker() {
        return false;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IContributionItem#isSeparator()
     */
    public boolean isSeparator() {
        return false;
    }

    /**
     * Check if the contribution is item is a subsitute for ourselves
     * 
     * @param item the contribution item
     * @return <code>true</code> if give item is a substitution for ourselves 
     * @deprecated this method is no longer a part of the 
     *   {@link org.eclipse.jface.action.IContributionItem} API.
     */
    public boolean isSubstituteFor(IContributionItem item) {
        return this.equals(item);
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IContributionItem#isVisible()
     */
    public boolean isVisible() {
        if (!visible) {
			return false; // short circuit calculations in this case
		}

        if (removeAllWhenShown) {
        	// we have no way of knowing if the menu has children
        	return true;
        }
        
        // menus aren't visible if all of its children are invisible (or only contains visible separators).
        IContributionItem[] childItems = getItems();
        boolean visibleChildren = false;
        for (int j = 0; j < childItems.length; j++) {
            if (isChildVisible(childItems[j]) && !childItems[j].isSeparator()) {
                visibleChildren = true;
                break;
            }
        }

        return visibleChildren;
    }

    
    /**
     * The <code>MenuManager</code> implementation of this <code>ContributionManager</code> method
     * also propagates the dirty flag up the parent chain.
     * 
     * @since 3.1
     */
    @Override
	public void markDirty() {
        super.markDirty();
        // Can't optimize by short-circuiting when the first dirty manager is encountered,
        // since non-visible children are not even processed.
        // That is, it's possible to have a dirty sub-menu under a non-dirty parent menu
        // even after the parent menu has been updated. 
        // If items are added/removed in the sub-menu, we still need to propagate the dirty flag up,
        // even if the sub-menu is already dirty, since the result of isVisible() may change
        // due to the added/removed items.
        IContributionManager parent = getParent();
        if (parent != null) {
            parent.markDirty();
        }
    }
    
    /**
     * Returns whether the menu control is created
     * and not disposed.
     * 
     * @return <code>true</code> if the control is created
     *	and not disposed, <code>false</code> otherwise
	 * @since 3.4 protected, was added in 3.1 as private method
     */
    protected boolean menuExist() {
        return menu != null && !menu.isDisposed();
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IMenuManager#removeMenuListener(org.eclipse.jface.action.IMenuListener)
     */
    public void removeMenuListener(IMenuListener listener) {
        listeners.remove(listener);
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IContributionItem#saveWidgetState()
     */
    public void saveWidgetState() {
    }

    /**
     * Sets the overrides for this contribution manager
     * 
     * @param newOverrides the overrides for the items of this manager
     * @since 2.0
     */
    @Override
	public void setOverrides(IContributionManagerOverrides newOverrides) {
        overrides = newOverrides;
        super.setOverrides(overrides);
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IContributionItem#setParent(org.eclipse.jface.action.IContributionManager)
     */
    public void setParent(IContributionManager manager) {
        parent = manager;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IMenuManager#setRemoveAllWhenShown(boolean)
     */
    public void setRemoveAllWhenShown(boolean removeAll) {
        this.removeAllWhenShown = removeAll;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IContributionItem#setVisible(boolean)
     */
    public void setVisible(boolean visible) {
        this.visible = visible;
    }
    
    /**
	 * Sets the action definition id of this action. This simply allows the menu
	 * item text to include a short cut if available.  It can be used to
	 * notify a user of a key combination that will open a quick menu.
	 * 
	 * @param definitionId
	 *            the command definition id
	 * @since 3.4
	 */
    public void setActionDefinitionId(String definitionId) {
    	this.definitionId = definitionId; 
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IContributionItem#update()
     */
    public void update() {
        updateMenuItem();
    }

    /**
     * The <code>MenuManager</code> implementation of this <code>IContributionManager</code>
     * updates this menu, but not any of its submenus.
     *
     * @see #updateAll
     */
    public void update(boolean force) {
        update(force, false);
    }

    /**
	 * Get all the items from the implementation's widget.
	 * 
	 * @return the menu items
	 * @since 3.4
	 */
    protected Item[] getMenuItems() {
    	if (menu != null) {
    		return menu.getItems();
    	}
    	return null;
    }

    /**
	 * Get an item from the implementation's widget.
	 * 
	 * @param index
	 *            of the item
	 * @return the menu item
	 * @since 3.4
	 */
    protected Item getMenuItem(int index) {
    	if (menu !=null) {
    		return menu.getItem(index);
    	}
    	return null;
    }

    /**
     * Get the menu item count for the implementation's widget.
     * 
     * @return the number of items
     * @since 3.4
     */
    protected int getMenuItemCount() {
    	if (menu != null) {
    		return menu.getItemCount();
    	}
    	return 0;
    }

    /**
	 * Call an <code>IContributionItem</code>'s fill method with the
	 * implementation's widget. The default is to use the <code>Menu</code>
	 * widget.<br>
	 * <code>fill(Menu menu, int index)</code>
	 * 
	 * @param ci
	 *            An <code>IContributionItem</code> whose <code>fill()</code>
	 *            method should be called.
	 * @param index
	 *            The position the <code>fill()</code> method should start
	 *            inserting at.
	 * @since 3.4
	 */
    protected void doItemFill(IContributionItem ci, int index) {
        ci.fill(menu, index);
    }

    /**
     * Incrementally builds the menu from the contribution items.
     * This method leaves out double separators and separators in the first 
     * or last position.
     *
     * @param force <code>true</code> means update even if not dirty,
     *   and <code>false</code> for normal incremental updating
     * @param recursive <code>true</code> means recursively update 
     *   all submenus, and <code>false</code> means just this menu
     */
    protected void update(boolean force, boolean recursive) {
        if (isDirty() || force) {
            if (menuExist()) {
                // clean contains all active items without double separators
                IContributionItem[] items = getItems();
                List<IContributionItem> clean = new ArrayList<IContributionItem>(items.length);
                IContributionItem separator = null;
                for (int i = 0; i < items.length; ++i) {
                    IContributionItem ci = items[i];
                    if (!isChildVisible(ci)) {
						continue;
					}
                    if (ci.isSeparator()) {
                        // delay creation until necessary 
                        // (handles both adjacent separators, and separator at end)
                        separator = ci;
                    } else {
                        if (separator != null) {
                            if (clean.size() > 0) {
								clean.add(separator);
							}
                            separator = null;
                        }
                        clean.add(ci);
                    }
                }

                // remove obsolete (removed or non active)
                Item[] mi = getMenuItems();

                for (int i = 0; i < mi.length; i++) {
                    Object data = mi[i].getData();

                    if (data == null || !clean.contains(data)) {
                        mi[i].dispose();
                    } else if (data instanceof IContributionItem
                            && ((IContributionItem) data).isDynamic()
                            && ((IContributionItem) data).isDirty()) {
                        mi[i].dispose();
                    }
                }

                // add new
                mi = getMenuItems();
                int srcIx = 0;
                int destIx = 0;

                for (Iterator<IContributionItem> e = clean.iterator(); e.hasNext();) {
                    IContributionItem src = e.next();
                    IContributionItem dest;

                    // get corresponding item in SWT widget
                    if (srcIx < mi.length) {
						dest = (IContributionItem) mi[srcIx].getData();
					} else {
						dest = null;
					}

                    if (dest != null && src.equals(dest)) {
                        srcIx++;
                        destIx++;
                    } else if (dest != null && dest.isSeparator()
                            && src.isSeparator()) {
                        mi[srcIx].setData(src);
                        srcIx++;
                        destIx++;
                    } else {
                        int start = getMenuItemCount();
                        doItemFill(src, destIx);
                        int newItems = getMenuItemCount() - start;
                        for (int i = 0; i < newItems; i++) {
                            Item item = getMenuItem(destIx++);
                            item.setData(src);
                        }
                    }

                    // May be we can optimize this call. If the menu has just
                    // been created via the call src.fill(fMenuBar, destIx) then
                    // the menu has already been updated with update(true) 
                    // (see MenuManager). So if force is true we do it again. But
                    // we can't set force to false since then information for the
                    // sub sub menus is lost.
                    if (recursive) {
                        IContributionItem item = src;
                        if (item instanceof SubContributionItem) {
							item = ((SubContributionItem) item).getInnerItem();
						}
                        if (item instanceof IMenuManager) {
							((IMenuManager) item).updateAll(force);
						}
                    }

                }

                // remove any old menu items not accounted for
                for (; srcIx < mi.length; srcIx++) {
					mi[srcIx].dispose();
				}

                setDirty(false);
            }
        } else {
            // I am not dirty. Check if I must recursivly walk down the hierarchy.
            if (recursive) {
                IContributionItem[] items = getItems();
                for (int i = 0; i < items.length; ++i) {
                    IContributionItem ci = items[i];
                    if (ci instanceof IMenuManager) {
                        IMenuManager mm = (IMenuManager) ci;
                        if (isChildVisible(mm)) {
                            mm.updateAll(force);
                        }
                    }
                }
            }
        }
        updateMenuItem();
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IContributionItem#update(java.lang.String)
     */
    public void update(String property) {
        IContributionItem items[] = getItems();

        for (int i = 0; i < items.length; i++) {
			items[i].update(property);
		}
        
        if (menu != null && !menu.isDisposed() && menu.getParentItem() != null) {
        	if (IAction.TEXT.equals(property)) {
                String text = getOverrides().getText(this);

                if (text == null) {
    				text = getMenuText();
    			}

                if (text != null) {
                    ExternalActionManager.ICallback callback = ExternalActionManager
                            .getInstance().getCallback();

                    if (callback != null) {
                        int index = text.indexOf('&');

                        if (index >= 0 && index < text.length() - 1) {
                            char character = Character.toUpperCase(text
                                    .charAt(index + 1));

                            if (callback.isAcceleratorInUse(SWT.ALT | character) && isTopLevelMenu()) {
                                if (index == 0) {
    								text = text.substring(1);
    							} else {
    								text = text.substring(0, index)
                                            + text.substring(index + 1);
    							}
                            }
                        }
                    }

                    menu.getParentItem().setText(text);
                }
        	} else if (IAction.IMAGE.equals(property) && image != null) {
    			LocalResourceManager localManager = new LocalResourceManager(JFaceResources
    					.getResources());
    			menu.getParentItem().setImage(localManager.createImage(image));
    			disposeOldImages();
    			imageManager = localManager;
        	}
        }
    }

	private boolean isTopLevelMenu() {
		if (menu != null && !menu.isDisposed() && menuItem != null
				&& !menuItem.isDisposed()) {
			Menu parentMenu = menuItem.getParent();
			return parentMenu != null
					&& ((parentMenu.getStyle() & SWT.BAR) == SWT.BAR);
		}
		return false;
	}

	/**
	 * Dispose any images allocated for this menu
	 */
	private void disposeOldImages() {
		if (imageManager != null) {
			imageManager.dispose();
			imageManager = null;
		}
	}

    /* (non-Javadoc)
     * @see org.eclipse.jface.action.IMenuManager#updateAll(boolean)
     */
    public void updateAll(boolean force) {
        update(force, true);
    }

    /**
     * Updates the menu item for this sub menu.
     * The menu item is disabled if this sub menu is empty.
     * Does nothing if this menu is not a submenu.
     */
    private void updateMenuItem() {
        /*
         * Commented out until proper solution to enablement of
         * menu item for a sub-menu is found. See bug 30833 for
         * more details.
         *  
         if (menuItem != null && !menuItem.isDisposed() && menuExist()) {
         IContributionItem items[] = getItems();
         boolean enabled = false;
         for (int i = 0; i < items.length; i++) {
         IContributionItem item = items[i];
         enabled = item.isEnabled();
         if(enabled) break;
         }
         // Workaround for 1GDDCN2: SWT:Linux - MenuItem.setEnabled() always causes a redraw
         if (menuItem.getEnabled() != enabled)
         menuItem.setEnabled(enabled);
         }
         */
        // Partial fix for bug #34969 - diable the menu item if no
        // items in sub-menu (for context menus).
        if (menuItem != null && !menuItem.isDisposed() && menuExist()) {
            boolean enabled = removeAllWhenShown || menu.getItemCount() > 0;
            // Workaround for 1GDDCN2: SWT:Linux - MenuItem.setEnabled() always causes a redraw
            if (menuItem.getEnabled() != enabled) {
                // We only do this for context menus (for bug #34969)
                Menu topMenu = menu;
                while (topMenu.getParentMenu() != null) {
					topMenu = topMenu.getParentMenu();
				}
                if ((topMenu.getStyle() & SWT.BAR) == 0) {
					menuItem.setEnabled(enabled);
				}
            }
        }
    }
    
	private boolean isChildVisible(IContributionItem item) {
		Boolean v = getOverrides().getVisible(item);
		if (v != null) {
			return v.booleanValue();
		}
		return item.isVisible();
	}
}

Back to the top