|
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 |
|
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 |
|
|
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; |
|
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 |
|
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 |
/** |
|
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); |
| 107 |
} |
121 |
} |
| 108 |
|
122 |
|
| 109 |
/** |
123 |
/** |
|
Link Here
|
| 114 |
* @param id the menu id, or <code>null</code> if it is to have no id |
128 |
* @param id the menu id, or <code>null</code> if it is to have no id |
| 115 |
*/ |
129 |
*/ |
| 116 |
public MenuManager(String text, String id) { |
130 |
public MenuManager(String text, String id) { |
|
|
131 |
this(text, null, id); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Creates a menu manager with the given text, image, and id. |
| 136 |
* Typically used for creating a sub-menu, where it needs to be referred to by id. |
| 137 |
* |
| 138 |
* @param text the text for the menu, or <code>null</code> if none |
| 139 |
* @param image the image for the menu, or <code>null</code> if none |
| 140 |
* @param id the menu id, or <code>null</code> if it is to have no id |
| 141 |
* @since 3.4 |
| 142 |
*/ |
| 143 |
public MenuManager(String text, ImageDescriptor image, String id) { |
| 117 |
this.menuText = text; |
144 |
this.menuText = text; |
|
|
145 |
this.image = image; |
| 118 |
this.id = id; |
146 |
this.id = id; |
| 119 |
} |
147 |
} |
| 120 |
|
148 |
|
|
Link Here
|
| 192 |
menuItem = null; |
220 |
menuItem = null; |
| 193 |
} |
221 |
} |
| 194 |
|
222 |
|
|
|
223 |
disposeOldImages(); |
| 224 |
|
| 195 |
IContributionItem[] items = getItems(); |
225 |
IContributionItem[] items = getItems(); |
| 196 |
for (int i = 0; i < items.length; i++) { |
226 |
for (int i = 0; i < items.length; i++) { |
| 197 |
items[i].dispose(); |
227 |
items[i].dispose(); |
|
Link Here
|
| 223 |
|
253 |
|
| 224 |
menuItem.setText(getMenuText()); |
254 |
menuItem.setText(getMenuText()); |
| 225 |
|
255 |
|
|
|
256 |
if (image != null) { |
| 257 |
LocalResourceManager localManager = new LocalResourceManager( |
| 258 |
JFaceResources.getResources()); |
| 259 |
menuItem.setImage(localManager.createImage(image)); |
| 260 |
disposeOldImages(); |
| 261 |
imageManager = localManager; |
| 262 |
} |
| 263 |
|
| 226 |
if (!menuExist()) { |
264 |
if (!menuExist()) { |
| 227 |
menu = new Menu(parent); |
265 |
menu = new Menu(parent); |
| 228 |
} |
266 |
} |
|
Link Here
|
| 336 |
public String getMenuText() { |
374 |
public String getMenuText() { |
| 337 |
return menuText; |
375 |
return menuText; |
| 338 |
} |
376 |
} |
|
|
377 |
|
| 378 |
/** |
| 379 |
* Returns the image for this menu as an image descriptor. |
| 380 |
* |
| 381 |
* @return the image, or <code>null</code> if this menu has no image |
| 382 |
* @since 3.4 |
| 383 |
*/ |
| 384 |
public ImageDescriptor getImageDescriptor() { |
| 385 |
return image; |
| 386 |
} |
| 339 |
|
387 |
|
| 340 |
/* (non-Javadoc) |
388 |
/* (non-Javadoc) |
| 341 |
* @see org.eclipse.jface.action.IContributionManager#getOverrides() |
389 |
* @see org.eclipse.jface.action.IContributionManager#getOverrides() |
|
Link Here
|
| 723 |
for (int i = 0; i < items.length; i++) { |
771 |
for (int i = 0; i < items.length; i++) { |
| 724 |
items[i].update(property); |
772 |
items[i].update(property); |
| 725 |
} |
773 |
} |
| 726 |
|
774 |
|
| 727 |
if (menu != null && !menu.isDisposed() && menu.getParentItem() != null |
775 |
if (menu != null && !menu.isDisposed() && menu.getParentItem() != null) { |
| 728 |
&& IAction.TEXT.equals(property)) { |
776 |
if (IAction.TEXT.equals(property)) { |
| 729 |
String text = getOverrides().getText(this); |
777 |
String text = getOverrides().getText(this); |
| 730 |
|
778 |
|
| 731 |
if (text == null) { |
779 |
if (text == null) { |
| 732 |
text = getMenuText(); |
780 |
text = getMenuText(); |
| 733 |
} |
781 |
} |
| 734 |
|
782 |
|
| 735 |
if (text != null) { |
783 |
if (text != null) { |
| 736 |
ExternalActionManager.ICallback callback = ExternalActionManager |
784 |
ExternalActionManager.ICallback callback = ExternalActionManager |
| 737 |
.getInstance().getCallback(); |
785 |
.getInstance().getCallback(); |
| 738 |
|
786 |
|
| 739 |
if (callback != null) { |
787 |
if (callback != null) { |
| 740 |
int index = text.indexOf('&'); |
788 |
int index = text.indexOf('&'); |
| 741 |
|
789 |
|
| 742 |
if (index >= 0 && index < text.length() - 1) { |
790 |
if (index >= 0 && index < text.length() - 1) { |
| 743 |
char character = Character.toUpperCase(text |
791 |
char character = Character.toUpperCase(text |
| 744 |
.charAt(index + 1)); |
792 |
.charAt(index + 1)); |
| 745 |
|
793 |
|
| 746 |
if (callback.isAcceleratorInUse(SWT.ALT | character)) { |
794 |
if (callback.isAcceleratorInUse(SWT.ALT | character)) { |
| 747 |
if (index == 0) { |
795 |
if (index == 0) { |
| 748 |
text = text.substring(1); |
796 |
text = text.substring(1); |
| 749 |
} else { |
797 |
} else { |
| 750 |
text = text.substring(0, index) |
798 |
text = text.substring(0, index) |
| 751 |
+ text.substring(index + 1); |
799 |
+ text.substring(index + 1); |
| 752 |
} |
800 |
} |
|
|
801 |
} |
| 753 |
} |
802 |
} |
| 754 |
} |
803 |
} |
| 755 |
} |
|
|
| 756 |
|
804 |
|
| 757 |
menu.getParentItem().setText(text); |
805 |
menu.getParentItem().setText(text); |
| 758 |
} |
806 |
} |
|
|
807 |
} else if (IAction.IMAGE.equals(property) && image != null) { |
| 808 |
LocalResourceManager localManager = new LocalResourceManager(JFaceResources |
| 809 |
.getResources()); |
| 810 |
menu.getParentItem().setImage(localManager.createImage(image)); |
| 811 |
disposeOldImages(); |
| 812 |
imageManager = localManager; |
| 813 |
} |
| 759 |
} |
814 |
} |
| 760 |
} |
815 |
} |
| 761 |
|
816 |
|
|
|
817 |
/** |
| 818 |
* Dispose any images allocated for this menu |
| 819 |
*/ |
| 820 |
private void disposeOldImages() { |
| 821 |
if (imageManager != null) { |
| 822 |
imageManager.dispose(); |
| 823 |
imageManager = null; |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 762 |
/* (non-Javadoc) |
827 |
/* (non-Javadoc) |
| 763 |
* @see org.eclipse.jface.action.IMenuManager#updateAll(boolean) |
828 |
* @see org.eclipse.jface.action.IMenuManager#updateAll(boolean) |
| 764 |
*/ |
829 |
*/ |