Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 12116 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jface/action/MenuManager.java (-33 / +109 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2006 IBM Corporation and others.
2
 * Copyright (c) 2000, 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Remy Chi Jian Suen <remy.suen@gmail.com> - Bug 12116 [Contributions] widgets: MenuManager.setImageDescriptor() method needed
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jface.action;
12
package org.eclipse.jface.action;
12
13
Lines 15-20 Link Here
15
import java.util.List;
16
import java.util.List;
16
17
17
import org.eclipse.core.runtime.ListenerList;
18
import org.eclipse.core.runtime.ListenerList;
19
import org.eclipse.jface.resource.ImageDescriptor;
20
import org.eclipse.jface.resource.JFaceResources;
21
import org.eclipse.jface.resource.LocalResourceManager;
18
import org.eclipse.swt.SWT;
22
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.events.MenuAdapter;
23
import org.eclipse.swt.events.MenuAdapter;
20
import org.eclipse.swt.events.MenuEvent;
24
import org.eclipse.swt.events.MenuEvent;
Lines 63-68 Link Here
63
     * The text for a sub-menu.
67
     * The text for a sub-menu.
64
     */
68
     */
65
    private String menuText;
69
    private String menuText;
70
    
71
    /**
72
     * The image for a sub-menu.
73
     */
74
    private ImageDescriptor image;
75
    
76
    /**
77
     * A resource manager to remember all of the images that have been used by this menu.
78
     */
79
    private LocalResourceManager imageManager;
66
80
67
    /**
81
    /**
68
     * The overrides for items of this manager
82
     * The overrides for items of this manager
Lines 92-98 Link Here
92
     * Typically used for creating a context menu, where it doesn't need to be referred to by id.
106
     * Typically used for creating a context menu, where it doesn't need to be referred to by id.
93
     */
107
     */
94
    public MenuManager() {
108
    public MenuManager() {
95
        this(null, null);
109
        this(null, null, null);
96
    }
110
    }
97
111
98
    /**
112
    /**
Lines 103-109 Link Here
103
     * @param text the text for the menu, or <code>null</code> if none
117
     * @param text the text for the menu, or <code>null</code> if none
104
     */
118
     */
105
    public MenuManager(String text) {
119
    public MenuManager(String text) {
106
        this(text, null);
120
        this(text, null, null);
121
    }
122
123
    /**
124
     * Creates a menu manager with the given text and image. The id of the menu
125
     * is <code>null</code>.
126
     * Typically used for creating a sub-menu, where it doesn't need to be referred to by id.
127
     *
128
     * @param text the text for the menu, or <code>null</code> if none
129
     * @param image the image for the menu, or <code>null</code> if none
130
     * @since 3.4
131
     */
132
    public MenuManager(String text, ImageDescriptor image) {
133
        this(text, image, null);
107
    }
134
    }
108
135
109
    /**
136
    /**
Lines 114-120 Link Here
114
     * @param id the menu id, or <code>null</code> if it is to have no id
141
     * @param id the menu id, or <code>null</code> if it is to have no id
115
     */
142
     */
116
    public MenuManager(String text, String id) {
143
    public MenuManager(String text, String id) {
144
        this(text, null, id);
145
    }
146
147
    /**
148
     * Creates a menu manager with the given text, image, and id.
149
     * Typically used for creating a sub-menu, where it needs to be referred to by id.
150
     * 
151
     * @param text the text for the menu, or <code>null</code> if none
152
     * @param image the image for the menu, or <code>null</code> if none
153
     * @param id the menu id, or <code>null</code> if it is to have no id
154
     * @since 3.4
155
     */
156
    public MenuManager(String text, ImageDescriptor image, String id) {
117
        this.menuText = text;
157
        this.menuText = text;
158
        this.image = image;
118
        this.id = id;
159
        this.id = id;
119
    }
160
    }
120
161
Lines 223-228 Link Here
223
264
224
            menuItem.setText(getMenuText());
265
            menuItem.setText(getMenuText());
225
266
267
			ImageDescriptor image = getImageDescriptor();
268
			LocalResourceManager localManager = new LocalResourceManager(JFaceResources
269
					.getResources());
270
			menuItem.setImage(image == null ? null : localManager.createImage(image));
271
			disposeOldImages();
272
			imageManager = localManager;
273
226
            if (!menuExist()) {
274
            if (!menuExist()) {
227
				menu = new Menu(parent);
275
				menu = new Menu(parent);
228
			}
276
			}
Lines 336-341 Link Here
336
    public String getMenuText() {
384
    public String getMenuText() {
337
        return menuText;
385
        return menuText;
338
    }
386
    }
387
    
388
    /**
389
     * Returns the image for this menu as an image descriptor.
390
     * 
391
     * @return the image, or <code>null</code> if this menu has no image
392
     * @since 3.4
393
     */
394
    public ImageDescriptor getImageDescriptor() {
395
    	return image;
396
    }
339
397
340
    /* (non-Javadoc)
398
    /* (non-Javadoc)
341
     * @see org.eclipse.jface.action.IContributionManager#getOverrides()
399
     * @see org.eclipse.jface.action.IContributionManager#getOverrides()
Lines 723-764 Link Here
723
        for (int i = 0; i < items.length; i++) {
781
        for (int i = 0; i < items.length; i++) {
724
			items[i].update(property);
782
			items[i].update(property);
725
		}
783
		}
726
784
        
727
        if (menu != null && !menu.isDisposed() && menu.getParentItem() != null
785
        if (menu != null && !menu.isDisposed() && menu.getParentItem() != null) {
728
                && IAction.TEXT.equals(property)) {
786
        	if (IAction.TEXT.equals(property)) {
729
            String text = getOverrides().getText(this);
787
                String text = getOverrides().getText(this);
730
788
731
            if (text == null) {
789
                if (text == null) {
732
				text = getMenuText();
790
    				text = getMenuText();
733
			}
791
    			}
734
792
735
            if (text != null) {
793
                if (text != null) {
736
                ExternalActionManager.ICallback callback = ExternalActionManager
794
                    ExternalActionManager.ICallback callback = ExternalActionManager
737
                        .getInstance().getCallback();
795
                            .getInstance().getCallback();
738
796
739
                if (callback != null) {
797
                    if (callback != null) {
740
                    int index = text.indexOf('&');
798
                        int index = text.indexOf('&');
741
799
742
                    if (index >= 0 && index < text.length() - 1) {
800
                        if (index >= 0 && index < text.length() - 1) {
743
                        char character = Character.toUpperCase(text
801
                            char character = Character.toUpperCase(text
744
                                .charAt(index + 1));
802
                                    .charAt(index + 1));
745
803
746
                        if (callback.isAcceleratorInUse(SWT.ALT | character)) {
804
                            if (callback.isAcceleratorInUse(SWT.ALT | character)) {
747
                            if (index == 0) {
805
                                if (index == 0) {
748
								text = text.substring(1);
806
    								text = text.substring(1);
749
							} else {
807
    							} else {
750
								text = text.substring(0, index)
808
    								text = text.substring(0, index)
751
                                        + text.substring(index + 1);
809
                                            + text.substring(index + 1);
752
							}
810
    							}
811
                            }
753
                        }
812
                        }
754
                    }
813
                    }
755
                }
756
814
757
                menu.getParentItem().setText(text);
815
                    menu.getParentItem().setText(text);
758
            }
816
                }
817
        	} else if (IAction.IMAGE.equals(property)) {
818
    			ImageDescriptor image = getImageDescriptor();
819
    			LocalResourceManager localManager = new LocalResourceManager(JFaceResources
820
    					.getResources());
821
    			menu.getParentItem().setImage(image == null ? null : localManager.createImage(image));
822
    			disposeOldImages();
823
    			imageManager = localManager;
824
        	}
759
        }
825
        }
760
    }
826
    }
761
827
828
	/**
829
	 * Dispose any images allocated for this menu
830
	 */
831
	private void disposeOldImages() {
832
		if (imageManager != null) {
833
			imageManager.dispose();
834
			imageManager = null;
835
		}
836
	}
837
762
    /* (non-Javadoc)
838
    /* (non-Javadoc)
763
     * @see org.eclipse.jface.action.IMenuManager#updateAll(boolean)
839
     * @see org.eclipse.jface.action.IMenuManager#updateAll(boolean)
764
     */
840
     */

Return to bug 12116