|
Link Here
|
| 12 |
|
12 |
|
| 13 |
package org.eclipse.ui.internal.menus; |
13 |
package org.eclipse.ui.internal.menus; |
| 14 |
|
14 |
|
| 15 |
import java.net.MalformedURLException; |
|
|
| 16 |
import java.net.URL; |
| 17 |
import java.util.HashMap; |
| 18 |
import java.util.Map; |
15 |
import java.util.Map; |
| 19 |
import org.eclipse.core.expressions.Expression; |
|
|
| 20 |
import org.eclipse.core.expressions.ExpressionConverter; |
| 21 |
import org.eclipse.core.runtime.CoreException; |
| 22 |
import org.eclipse.core.runtime.FileLocator; |
| 23 |
import org.eclipse.core.runtime.IConfigurationElement; |
16 |
import org.eclipse.core.runtime.IConfigurationElement; |
| 24 |
import org.eclipse.core.runtime.InvalidRegistryObjectException; |
|
|
| 25 |
import org.eclipse.e4.core.contexts.IEclipseContext; |
17 |
import org.eclipse.e4.core.contexts.IEclipseContext; |
| 26 |
import org.eclipse.e4.ui.model.application.MApplication; |
18 |
import org.eclipse.e4.ui.model.application.MApplication; |
| 27 |
import org.eclipse.e4.ui.model.application.commands.MCommand; |
|
|
| 28 |
import org.eclipse.e4.ui.model.application.commands.MParameter; |
19 |
import org.eclipse.e4.ui.model.application.commands.MParameter; |
| 29 |
import org.eclipse.e4.ui.model.application.commands.impl.CommandsFactoryImpl; |
20 |
import org.eclipse.e4.ui.model.application.commands.impl.CommandsFactoryImpl; |
| 30 |
import org.eclipse.e4.ui.model.application.ui.MCoreExpression; |
|
|
| 31 |
import org.eclipse.e4.ui.model.application.ui.MElementContainer; |
21 |
import org.eclipse.e4.ui.model.application.ui.MElementContainer; |
| 32 |
import org.eclipse.e4.ui.model.application.ui.MExpression; |
|
|
| 33 |
import org.eclipse.e4.ui.model.application.ui.impl.UiFactoryImpl; |
| 34 |
import org.eclipse.e4.ui.model.application.ui.menu.ItemType; |
| 35 |
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem; |
22 |
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem; |
| 36 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenu; |
23 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenu; |
| 37 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenuContribution; |
24 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenuContribution; |
| 38 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement; |
25 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement; |
| 39 |
import org.eclipse.e4.ui.model.application.ui.menu.impl.MenuFactoryImpl; |
26 |
import org.eclipse.e4.ui.model.application.ui.menu.impl.MenuFactoryImpl; |
| 40 |
import org.eclipse.e4.ui.workbench.swt.modeling.MenuServiceFilter; |
27 |
import org.eclipse.e4.ui.workbench.swt.modeling.MenuServiceFilter; |
| 41 |
import org.eclipse.jface.resource.ImageDescriptor; |
|
|
| 42 |
import org.eclipse.ui.internal.e4.compatibility.E4Util; |
28 |
import org.eclipse.ui.internal.e4.compatibility.E4Util; |
| 43 |
import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; |
29 |
import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; |
| 44 |
import org.eclipse.ui.internal.util.Util; |
|
|
| 45 |
import org.eclipse.ui.menus.CommandContributionItem; |
| 46 |
import org.eclipse.ui.plugin.AbstractUIPlugin; |
| 47 |
|
30 |
|
| 48 |
/** |
31 |
/** |
| 49 |
* @since 3.3 |
32 |
* @since 3.3 |
|
Link Here
|
| 116 |
return null; |
99 |
return null; |
| 117 |
} |
100 |
} |
| 118 |
|
101 |
|
| 119 |
MMenu element = MenuFactoryImpl.eINSTANCE.createMenu(); |
102 |
return MenuHelper.createMenuAddition(menuAddition); |
| 120 |
String id = getId(menuAddition); |
|
|
| 121 |
element.setElementId(id); |
| 122 |
String text = getLabel(menuAddition); |
| 123 |
String mnemonic = getMnemonic(menuAddition); |
| 124 |
if (text != null && mnemonic != null) { |
| 125 |
E4Util.unsupported("mnemonic processing in menus: " + id + ": " + text); //$NON-NLS-1$//$NON-NLS-2$ |
| 126 |
int idx = text.indexOf(mnemonic); |
| 127 |
if (idx != -1) { |
| 128 |
text = text.substring(0, idx) + '&' + text.substring(idx); |
| 129 |
} |
| 130 |
} |
| 131 |
element.setIconURI(getIconUrl(menuAddition, IWorkbenchRegistryConstants.ATT_ICON)); |
| 132 |
element.setLabel(Util.safeString(text)); |
| 133 |
|
| 134 |
return element; |
| 135 |
} |
103 |
} |
| 136 |
|
104 |
|
| 137 |
private MMenuElement createSeparatorAddition(final IConfigurationElement sepAddition) { |
105 |
private MMenuElement createSeparatorAddition(final IConfigurationElement sepAddition) { |
| 138 |
String name = getName(sepAddition); |
106 |
String name = MenuHelper.getName(sepAddition); |
| 139 |
MMenuElement element = MenuFactoryImpl.eINSTANCE.createMenuSeparator(); |
107 |
MMenuElement element = MenuFactoryImpl.eINSTANCE.createMenuSeparator(); |
| 140 |
element.setElementId(name); |
108 |
element.setElementId(name); |
| 141 |
if (!isSeparatorVisible(sepAddition)) { |
109 |
if (!MenuHelper.isSeparatorVisible(sepAddition)) { |
| 142 |
element.setVisible(false); |
110 |
element.setVisible(false); |
| 143 |
} |
111 |
} |
| 144 |
return element; |
112 |
return element; |
| 145 |
} |
113 |
} |
| 146 |
|
114 |
|
| 147 |
private MCommand findCommand(String id) { |
|
|
| 148 |
for (MCommand cmd : application.getCommands()) { |
| 149 |
if (id.equals(cmd.getElementId())) { |
| 150 |
return cmd; |
| 151 |
} |
| 152 |
} |
| 153 |
return null; |
| 154 |
} |
| 155 |
|
| 156 |
private MMenuElement createCommandAddition(final IConfigurationElement commandAddition) { |
115 |
private MMenuElement createCommandAddition(final IConfigurationElement commandAddition) { |
| 157 |
MHandledMenuItem item = MenuFactoryImpl.eINSTANCE.createHandledMenuItem(); |
116 |
MHandledMenuItem item = MenuFactoryImpl.eINSTANCE.createHandledMenuItem(); |
| 158 |
item.setElementId(getId(commandAddition)); |
117 |
item.setElementId(MenuHelper.getId(commandAddition)); |
| 159 |
item.setCommand(findCommand(getCommandId(commandAddition))); |
118 |
item.setCommand(MenuHelper.getCommandById(application, |
| 160 |
Map parms = getParameters(commandAddition); |
119 |
MenuHelper.getCommandId(commandAddition))); |
|
|
120 |
Map parms = MenuHelper.getParameters(commandAddition); |
| 161 |
for (Object obj : parms.entrySet()) { |
121 |
for (Object obj : parms.entrySet()) { |
| 162 |
Map.Entry e = (Map.Entry) obj; |
122 |
Map.Entry e = (Map.Entry) obj; |
| 163 |
MParameter parm = CommandsFactoryImpl.eINSTANCE.createParameter(); |
123 |
MParameter parm = CommandsFactoryImpl.eINSTANCE.createParameter(); |
|
Link Here
|
| 165 |
parm.setValue(e.getValue().toString()); |
125 |
parm.setValue(e.getValue().toString()); |
| 166 |
item.getParameters().add(parm); |
126 |
item.getParameters().add(parm); |
| 167 |
} |
127 |
} |
| 168 |
item.setIconURI(getIconUrl(commandAddition, IWorkbenchRegistryConstants.ATT_ICON)); |
128 |
item.setIconURI(MenuHelper |
| 169 |
item.setLabel(getLabel(commandAddition)); |
129 |
.getIconUrl(commandAddition, IWorkbenchRegistryConstants.ATT_ICON)); |
| 170 |
item.setMnemonics(getMnemonic(commandAddition)); |
130 |
item.setLabel(MenuHelper.getLabel(commandAddition)); |
| 171 |
item.setTooltip(getTooltip(commandAddition)); |
131 |
item.setMnemonics(MenuHelper.getMnemonic(commandAddition)); |
| 172 |
item.setType(getStyle(commandAddition)); |
132 |
item.setTooltip(MenuHelper.getTooltip(commandAddition)); |
| 173 |
item.setVisibleWhen(getVisibleWhen(commandAddition)); |
133 |
item.setType(MenuHelper.getStyle(commandAddition)); |
|
|
134 |
item.setVisibleWhen(MenuHelper.getVisibleWhen(commandAddition)); |
| 174 |
return item; |
135 |
return item; |
| 175 |
} |
136 |
} |
| 176 |
|
137 |
|
| 177 |
/** |
|
|
| 178 |
* @param commandAddition |
| 179 |
* @return |
| 180 |
*/ |
| 181 |
private MExpression getVisibleWhen(IConfigurationElement commandAddition) { |
| 182 |
try { |
| 183 |
IConfigurationElement[] visibleConfig = configElement |
| 184 |
.getChildren(IWorkbenchRegistryConstants.TAG_VISIBLE_WHEN); |
| 185 |
if (visibleConfig.length > 0 && visibleConfig.length < 2) { |
| 186 |
IConfigurationElement[] visibleChild = visibleConfig[0].getChildren(); |
| 187 |
if (visibleChild.length > 0) { |
| 188 |
Expression visWhen = ExpressionConverter.getDefault().perform(visibleChild[0]); |
| 189 |
MCoreExpression exp = UiFactoryImpl.eINSTANCE.createCoreExpression(); |
| 190 |
exp.setCoreExpressionId("programmatic.value"); //$NON-NLS-1$ |
| 191 |
exp.setCoreExpression(visWhen); |
| 192 |
return exp; |
| 193 |
// visWhenMap.put(configElement, visWhen); |
| 194 |
} |
| 195 |
} |
| 196 |
} catch (InvalidRegistryObjectException e) { |
| 197 |
// visWhenMap.put(configElement, null); |
| 198 |
// TODO Auto-generated catch block |
| 199 |
e.printStackTrace(); |
| 200 |
} catch (CoreException e) { |
| 201 |
// visWhenMap.put(configElement, null); |
| 202 |
// TODO Auto-generated catch block |
| 203 |
e.printStackTrace(); |
| 204 |
} |
| 205 |
return null; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* @param element |
| 210 |
* the configuration element |
| 211 |
* @return <code>true</code> if the checkEnabled is <code>true</code>. |
| 212 |
*/ |
| 213 |
static boolean getVisibleEnabled(IConfigurationElement element) { |
| 214 |
IConfigurationElement[] children = element |
| 215 |
.getChildren(IWorkbenchRegistryConstants.TAG_VISIBLE_WHEN); |
| 216 |
String checkEnabled = null; |
| 217 |
|
| 218 |
if (children.length > 0) { |
| 219 |
checkEnabled = children[0].getAttribute(IWorkbenchRegistryConstants.ATT_CHECK_ENABLED); |
| 220 |
} |
| 221 |
|
| 222 |
return checkEnabled != null && checkEnabled.equalsIgnoreCase("true"); //$NON-NLS-1$ |
| 223 |
} |
| 224 |
|
| 225 |
/* |
| 226 |
* Support Utilities |
| 227 |
*/ |
| 228 |
public static String getId(IConfigurationElement element) { |
| 229 |
String id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID); |
| 230 |
|
| 231 |
// For sub-menu management -all- items must be id'd so enforce this |
| 232 |
// here (we could optimize by checking the 'name' of the config |
| 233 |
// element == "menu" |
| 234 |
if (id == null || id.length() == 0) { |
| 235 |
id = getCommandId(element); |
| 236 |
} |
| 237 |
if (id == null || id.length() == 0) { |
| 238 |
id = element.toString(); |
| 239 |
} |
| 240 |
|
| 241 |
return id; |
| 242 |
} |
| 243 |
|
| 244 |
static String getName(IConfigurationElement element) { |
| 245 |
return element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME); |
| 246 |
} |
| 247 |
|
| 248 |
static int getMode(IConfigurationElement element) { |
| 249 |
if ("FORCE_TEXT".equals(element.getAttribute(IWorkbenchRegistryConstants.ATT_MODE))) { //$NON-NLS-1$ |
| 250 |
return CommandContributionItem.MODE_FORCE_TEXT; |
| 251 |
} |
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
static String getLabel(IConfigurationElement element) { |
| 256 |
return element.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL); |
| 257 |
} |
| 258 |
|
| 259 |
static String getMnemonic(IConfigurationElement element) { |
| 260 |
return element.getAttribute(IWorkbenchRegistryConstants.ATT_MNEMONIC); |
| 261 |
} |
| 262 |
|
| 263 |
static String getTooltip(IConfigurationElement element) { |
| 264 |
return element.getAttribute(IWorkbenchRegistryConstants.ATT_TOOLTIP); |
| 265 |
} |
| 266 |
|
| 267 |
static String getIconPath(IConfigurationElement element) { |
| 268 |
return element.getAttribute(IWorkbenchRegistryConstants.ATT_ICON); |
| 269 |
} |
| 270 |
|
| 271 |
static String getDisabledIconPath(IConfigurationElement element) { |
| 272 |
return element.getAttribute(IWorkbenchRegistryConstants.ATT_DISABLEDICON); |
| 273 |
} |
| 274 |
|
| 275 |
static String getHoverIconPath(IConfigurationElement element) { |
| 276 |
return element.getAttribute(IWorkbenchRegistryConstants.ATT_HOVERICON); |
| 277 |
} |
| 278 |
|
| 279 |
static String getIconUrl(IConfigurationElement element, String attr) { |
| 280 |
String extendingPluginId = element.getDeclaringExtension().getContributor().getName(); |
| 281 |
|
| 282 |
String iconPath = element.getAttribute(attr); |
| 283 |
if (iconPath == null) { |
| 284 |
return null; |
| 285 |
} |
| 286 |
if (!iconPath.startsWith("platform:")) { //$NON-NLS-1$ |
| 287 |
iconPath = "platform:/plugin/" + extendingPluginId + "/" + iconPath; //$NON-NLS-1$//$NON-NLS-2$ |
| 288 |
} |
| 289 |
URL url = null; |
| 290 |
try { |
| 291 |
url = FileLocator.find(new URL(iconPath)); |
| 292 |
} catch (MalformedURLException e) { |
| 293 |
// TODO Auto-generated catch block |
| 294 |
e.printStackTrace(); |
| 295 |
} |
| 296 |
return url == null ? null : url.toString(); |
| 297 |
} |
| 298 |
|
| 299 |
static ImageDescriptor getDisabledIconDescriptor(IConfigurationElement element) { |
| 300 |
String extendingPluginId = element.getDeclaringExtension().getContributor().getName(); |
| 301 |
|
| 302 |
String iconPath = getDisabledIconPath(element); |
| 303 |
if (iconPath != null) { |
| 304 |
return AbstractUIPlugin.imageDescriptorFromPlugin(extendingPluginId, iconPath); |
| 305 |
} |
| 306 |
return null; |
| 307 |
} |
| 308 |
|
| 309 |
static ImageDescriptor getHoverIconDescriptor(IConfigurationElement element) { |
| 310 |
String extendingPluginId = element.getDeclaringExtension().getContributor().getName(); |
| 311 |
|
| 312 |
String iconPath = getHoverIconPath(element); |
| 313 |
if (iconPath != null) { |
| 314 |
return AbstractUIPlugin.imageDescriptorFromPlugin(extendingPluginId, iconPath); |
| 315 |
} |
| 316 |
return null; |
| 317 |
} |
| 318 |
|
| 319 |
static String getHelpContextId(IConfigurationElement element) { |
| 320 |
return element.getAttribute(IWorkbenchRegistryConstants.ATT_HELP_CONTEXT_ID); |
| 321 |
} |
| 322 |
|
| 323 |
public static boolean isSeparatorVisible(IConfigurationElement element) { |
| 324 |
String val = element.getAttribute(IWorkbenchRegistryConstants.ATT_VISIBLE); |
| 325 |
return Boolean.valueOf(val).booleanValue(); |
| 326 |
} |
| 327 |
|
| 328 |
public static String getClassSpec(IConfigurationElement element) { |
| 329 |
return element.getAttribute(IWorkbenchRegistryConstants.ATT_CLASS); |
| 330 |
} |
| 331 |
|
| 332 |
public static String getCommandId(IConfigurationElement element) { |
| 333 |
return element.getAttribute(IWorkbenchRegistryConstants.ATT_COMMAND_ID); |
| 334 |
} |
| 335 |
|
| 336 |
private ItemType getStyle(IConfigurationElement element) { |
| 337 |
String style = element.getAttribute(IWorkbenchRegistryConstants.ATT_STYLE); |
| 338 |
if (style == null || style.length() == 0) { |
| 339 |
return ItemType.PUSH; |
| 340 |
} |
| 341 |
if (IWorkbenchRegistryConstants.STYLE_TOGGLE.equals(style)) { |
| 342 |
return ItemType.CHECK; |
| 343 |
} |
| 344 |
if (IWorkbenchRegistryConstants.STYLE_RADIO.equals(style)) { |
| 345 |
return ItemType.RADIO; |
| 346 |
} |
| 347 |
if (IWorkbenchRegistryConstants.STYLE_PULLDOWN.equals(style)) { |
| 348 |
E4Util.unsupported("Failed to get style for " + IWorkbenchRegistryConstants.STYLE_PULLDOWN); //$NON-NLS-1$ |
| 349 |
// return CommandContributionItem.STYLE_PULLDOWN; |
| 350 |
} |
| 351 |
return ItemType.PUSH; |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* @param element |
| 356 |
* @return A map of parameters names to parameter values. All Strings. The |
| 357 |
* map may be empty. |
| 358 |
*/ |
| 359 |
public static Map getParameters(IConfigurationElement element) { |
| 360 |
HashMap map = new HashMap(); |
| 361 |
IConfigurationElement[] parameters = element |
| 362 |
.getChildren(IWorkbenchRegistryConstants.TAG_PARAMETER); |
| 363 |
for (int i = 0; i < parameters.length; i++) { |
| 364 |
String name = parameters[i].getAttribute(IWorkbenchRegistryConstants.ATT_NAME); |
| 365 |
String value = parameters[i].getAttribute(IWorkbenchRegistryConstants.ATT_VALUE); |
| 366 |
if (name != null && value != null) { |
| 367 |
map.put(name, value); |
| 368 |
} |
| 369 |
} |
| 370 |
return map; |
| 371 |
} |
| 372 |
|
138 |
|
| 373 |
private void addChildren(final MElementContainer<MMenuElement> container, |
139 |
private void addChildren(final MElementContainer<MMenuElement> container, |
| 374 |
IConfigurationElement parent, String filter) { |
140 |
IConfigurationElement parent, String filter) { |
| 375 |
IConfigurationElement[] items = parent.getChildren(); |
141 |
IConfigurationElement[] items = parent.getChildren(); |
| 376 |
for (int i = 0; i < items.length; i++) { |
142 |
for (int i = 0; i < items.length; i++) { |
| 377 |
String itemType = items[i].getName(); |
143 |
String itemType = items[i].getName(); |
| 378 |
String id = getId(items[i]); |
144 |
String id = MenuHelper.getId(items[i]); |
| 379 |
|
145 |
|
| 380 |
if (IWorkbenchRegistryConstants.TAG_COMMAND.equals(itemType)) { |
146 |
if (IWorkbenchRegistryConstants.TAG_COMMAND.equals(itemType)) { |
| 381 |
MMenuElement element = createCommandAddition(items[i]); |
147 |
MMenuElement element = createCommandAddition(items[i]); |
|
Link Here
|
| 417 |
return; |
183 |
return; |
| 418 |
} |
184 |
} |
| 419 |
menuContribution = MenuFactoryImpl.eINSTANCE.createMenuContribution(); |
185 |
menuContribution = MenuFactoryImpl.eINSTANCE.createMenuContribution(); |
| 420 |
String idContrib = getId(configElement); |
186 |
String idContrib = MenuHelper.getId(configElement); |
| 421 |
if (idContrib != null && idContrib.length() > 0) { |
187 |
if (idContrib != null && idContrib.length() > 0) { |
| 422 |
menuContribution.setElementId(idContrib); |
188 |
menuContribution.setElementId(idContrib); |
| 423 |
} |
189 |
} |
|
Link Here
|
| 437 |
filter = MenuServiceFilter.MC_POPUP; |
203 |
filter = MenuServiceFilter.MC_POPUP; |
| 438 |
} |
204 |
} |
| 439 |
menuContribution.getTags().add(filter); |
205 |
menuContribution.getTags().add(filter); |
|
|
206 |
menuContribution.setVisibleWhen(MenuHelper.getVisibleWhen(configElement)); |
| 440 |
addChildren(menuContribution, configElement, filter); |
207 |
addChildren(menuContribution, configElement, filter); |
| 441 |
application.getMenuContributions().add(menuContribution); |
208 |
application.getMenuContributions().add(menuContribution); |
| 442 |
} |
209 |
} |