1 11 12 package org.eclipse.ui.internal.menus; 13 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Map ; 17 import java.util.Set ; 18 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.IConfigurationElement; 23 import org.eclipse.core.runtime.InvalidRegistryObjectException; 24 import org.eclipse.jface.action.GroupMarker; 25 import org.eclipse.jface.action.IContributionItem; 26 import org.eclipse.jface.action.MenuManager; 27 import org.eclipse.jface.action.Separator; 28 import org.eclipse.jface.action.ToolBarContributionItem; 29 import org.eclipse.jface.action.ToolBarManager; 30 import org.eclipse.jface.resource.ImageDescriptor; 31 import org.eclipse.ui.IWorkbenchWindow; 32 import org.eclipse.ui.actions.CompoundContributionItem; 33 import org.eclipse.ui.internal.WorkbenchWindow; 34 import org.eclipse.ui.internal.provisional.presentations.IActionBarPresentationFactory; 35 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 36 import org.eclipse.ui.internal.util.Util; 37 import org.eclipse.ui.menus.AbstractContributionFactory; 38 import org.eclipse.ui.menus.CommandContributionItem; 39 import org.eclipse.ui.menus.IContributionRoot; 40 import org.eclipse.ui.menus.IMenuService; 41 import org.eclipse.ui.menus.WorkbenchWindowControlContribution; 42 import org.eclipse.ui.plugin.AbstractUIPlugin; 43 import org.eclipse.ui.services.IServiceLocator; 44 45 49 public class MenuAdditionCacheEntry extends AbstractContributionFactory { 50 private IConfigurationElement additionElement; 51 52 54 57 Map iciToConfigElementMap = new HashMap (); 58 59 65 Set failedLoads = new HashSet (); 66 67 70 private HashMap visWhenMap = new HashMap (); 71 72 75 private IMenuService menuService; 76 77 public MenuAdditionCacheEntry(IMenuService menuService, 78 IConfigurationElement element, String location, String namespace) { 79 super(location, namespace); 80 this.menuService = menuService; 81 this.additionElement = element; 82 generateSubCaches(); 83 } 84 85 88 private void generateSubCaches() { 89 IConfigurationElement[] items = additionElement.getChildren(); 90 for (int i = 0; i < items.length; i++) { 91 String itemType = items[i].getName(); 92 if (IWorkbenchRegistryConstants.TAG_MENU.equals(itemType) 93 || IWorkbenchRegistryConstants.TAG_TOOLBAR.equals(itemType)) { 94 99 String location = new MenuLocationURI(getLocation()) 100 .getScheme() 101 + ":" + MenuAdditionCacheEntry.getId(items[i]); 103 MenuAdditionCacheEntry subMenuEntry = new MenuAdditionCacheEntry( 106 menuService, items[i], location, getNamespace()); 107 menuService.addContributionFactory(subMenuEntry); 108 } 109 } 110 } 111 112 private Expression getVisibleWhenForItem(IContributionItem item) { 113 IConfigurationElement configElement = (IConfigurationElement) iciToConfigElementMap 114 .get(item); 115 if (configElement == null) 116 return null; 117 118 if (!visWhenMap.containsKey(configElement)) { 119 try { 121 IConfigurationElement[] visibleConfig = configElement 122 .getChildren(IWorkbenchRegistryConstants.TAG_VISIBLE_WHEN); 123 if (visibleConfig.length > 0 && visibleConfig.length < 2) { 124 IConfigurationElement[] visibleChild = visibleConfig[0] 125 .getChildren(); 126 if (visibleChild.length > 0) { 127 Expression visWhen = ExpressionConverter.getDefault() 128 .perform(visibleChild[0]); 129 visWhenMap.put(configElement, visWhen); 130 } 131 } 132 } catch (InvalidRegistryObjectException e) { 133 visWhenMap.put(configElement, null); 134 e.printStackTrace(); 136 } catch (CoreException e) { 137 visWhenMap.put(configElement, null); 138 e.printStackTrace(); 140 } 141 } 142 143 return (Expression) visWhenMap.get(configElement); 144 } 145 146 152 public void createContributionItems(IServiceLocator serviceLocator, 153 IContributionRoot additions) { 154 IActionBarPresentationFactory actionBarPresentationFactory = null; 155 156 WorkbenchWindow window = (WorkbenchWindow) serviceLocator 157 .getService(IWorkbenchWindow.class); 158 if (window != null) { 159 actionBarPresentationFactory = window 160 .getActionBarPresentationFactory(); 161 } 162 163 IConfigurationElement[] items = additionElement.getChildren(); 164 for (int i = 0; i < items.length; i++) { 165 String itemType = items[i].getName(); 166 IContributionItem newItem = null; 167 168 if (IWorkbenchRegistryConstants.TAG_COMMAND.equals(itemType)) { 169 newItem = createCommandAdditionContribution(serviceLocator, 170 items[i]); 171 } else if (IWorkbenchRegistryConstants.TAG_DYNAMIC.equals(itemType)) { 172 newItem = createDynamicAdditionContribution(items[i]); 173 } else if (IWorkbenchRegistryConstants.TAG_CONTROL.equals(itemType)) { 174 newItem = createControlAdditionContribution(items[i]); 175 } else if (IWorkbenchRegistryConstants.TAG_SEPARATOR 176 .equals(itemType)) { 177 newItem = createSeparatorAdditionContribution(items[i]); 178 } else if (IWorkbenchRegistryConstants.TAG_MENU.equals(itemType)) { 179 newItem = createMenuAdditionContribution(items[i]); 180 } else if (IWorkbenchRegistryConstants.TAG_TOOLBAR.equals(itemType)) { 181 newItem = createToolBarAdditionContribution( 182 actionBarPresentationFactory, items[i]); 183 } 184 185 if (newItem != null) { 188 iciToConfigElementMap.put(newItem, items[i]); 189 additions.addContributionItem(newItem, 190 getVisibleWhenForItem(newItem)); 191 } 192 } 193 } 194 195 199 private IContributionItem createToolBarAdditionContribution( 200 IActionBarPresentationFactory actionBarPresentationFactory, 201 IConfigurationElement configurationElement) { 202 if (!getLocation().startsWith("toolbar")) { return null; 204 } 205 if (actionBarPresentationFactory != null) { 206 return actionBarPresentationFactory.createToolBarContributionItem( 207 actionBarPresentationFactory.createToolBarManager(), 208 getId(configurationElement)); 209 } 210 return new ToolBarContributionItem(new ToolBarManager(), 211 getId(configurationElement)); 212 } 213 214 218 private IContributionItem createMenuAdditionContribution( 219 final IConfigurationElement menuAddition) { 220 if (getLocation().startsWith("toolbar")) { return null; 225 } 226 227 String text = getLabel(menuAddition); 228 String mnemonic = getMnemonic(menuAddition); 229 if (text != null && mnemonic != null) { 230 int idx = text.indexOf(mnemonic); 231 if (idx != -1) { 232 text = text.substring(0, idx) + '&' + text.substring(idx); 233 } 234 } 235 return new MenuManager(text, getId(menuAddition)); 236 } 237 238 242 private IContributionItem createSeparatorAdditionContribution( 243 final IConfigurationElement sepAddition) { 244 if (isSeparatorVisible(sepAddition)) { 245 return new Separator(getName(sepAddition)); 246 } 247 return new GroupMarker(getName(sepAddition)); 248 } 249 250 253 private IContributionItem createDynamicAdditionContribution( 254 final IConfigurationElement dynamicAddition) { 255 if (failedLoads.contains(dynamicAddition)) 258 return null; 259 260 final CompoundContributionItem loadedDynamicContribution = (CompoundContributionItem) Util 262 .safeLoadExecutableExtension(dynamicAddition, 263 IWorkbenchRegistryConstants.ATT_CLASS, 264 CompoundContributionItem.class); 265 266 if (loadedDynamicContribution == null) { 268 failedLoads.add(loadedDynamicContribution); 269 return null; 270 } 271 272 return loadedDynamicContribution; 276 } 277 278 281 private IContributionItem createControlAdditionContribution( 282 final IConfigurationElement widgetAddition) { 283 if (!getLocation().startsWith("toolbar")) { return null; 285 } 286 if (failedLoads.contains(widgetAddition)) 289 return null; 290 291 final WorkbenchWindowControlContribution loadedWidget = (WorkbenchWindowControlContribution) Util 293 .safeLoadExecutableExtension(widgetAddition, 294 IWorkbenchRegistryConstants.ATT_CLASS, 295 WorkbenchWindowControlContribution.class); 296 297 if (loadedWidget == null) { 299 failedLoads.add(widgetAddition); 300 return null; 301 } 302 303 ((InternalControlContribution) loadedWidget) 305 .setId(getId(widgetAddition)); 306 307 return loadedWidget; 308 } 309 310 314 private IContributionItem createCommandAdditionContribution( 315 IServiceLocator locator, final IConfigurationElement commandAddition) { 316 return new CommandContributionItem(locator, getId(commandAddition), 317 getCommandId(commandAddition), getParameters(commandAddition), 318 getIconDescriptor(commandAddition), 319 getDisabledIconDescriptor(commandAddition), 320 getHoverIconDescriptor(commandAddition), 321 getLabel(commandAddition), getMnemonic(commandAddition), 322 getTooltip(commandAddition), getStyle(commandAddition)); 323 } 324 325 328 public static String getId(IConfigurationElement element) { 329 String id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID); 330 331 if (id == null || id.length() == 0) 335 id = element.toString(); 336 337 return id; 338 } 339 340 static String getName(IConfigurationElement element) { 341 return element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME); 342 } 343 344 static String getLabel(IConfigurationElement element) { 345 return element.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL); 346 } 347 348 static String getMnemonic(IConfigurationElement element) { 349 return element.getAttribute(IWorkbenchRegistryConstants.ATT_MNEMONIC); 350 } 351 352 static String getTooltip(IConfigurationElement element) { 353 return element.getAttribute(IWorkbenchRegistryConstants.ATT_TOOLTIP); 354 } 355 356 static String getIconPath(IConfigurationElement element) { 357 return element.getAttribute(IWorkbenchRegistryConstants.ATT_ICON); 358 } 359 360 static String getDisabledIconPath(IConfigurationElement element) { 361 return element 362 .getAttribute(IWorkbenchRegistryConstants.ATT_DISABLEDICON); 363 } 364 365 static String getHoverIconPath(IConfigurationElement element) { 366 return element.getAttribute(IWorkbenchRegistryConstants.ATT_HOVERICON); 367 } 368 369 static ImageDescriptor getIconDescriptor(IConfigurationElement element) { 370 String extendingPluginId = element.getDeclaringExtension() 371 .getContributor().getName(); 372 373 String iconPath = getIconPath(element); 374 if (iconPath != null) { 375 return AbstractUIPlugin.imageDescriptorFromPlugin( 376 extendingPluginId, iconPath); 377 } 378 return null; 379 } 380 381 static ImageDescriptor getDisabledIconDescriptor( 382 IConfigurationElement element) { 383 String extendingPluginId = element.getDeclaringExtension() 384 .getContributor().getName(); 385 386 String iconPath = getDisabledIconPath(element); 387 if (iconPath != null) { 388 return AbstractUIPlugin.imageDescriptorFromPlugin( 389 extendingPluginId, iconPath); 390 } 391 return null; 392 } 393 394 static ImageDescriptor getHoverIconDescriptor(IConfigurationElement element) { 395 String extendingPluginId = element.getDeclaringExtension() 396 .getContributor().getName(); 397 398 String iconPath = getHoverIconPath(element); 399 if (iconPath != null) { 400 return AbstractUIPlugin.imageDescriptorFromPlugin( 401 extendingPluginId, iconPath); 402 } 403 return null; 404 } 405 406 public static boolean isSeparatorVisible(IConfigurationElement element) { 407 String val = element 408 .getAttribute(IWorkbenchRegistryConstants.ATT_VISIBLE); 409 return Boolean.valueOf(val).booleanValue(); 410 } 411 412 public static String getClassSpec(IConfigurationElement element) { 413 return element.getAttribute(IWorkbenchRegistryConstants.ATT_CLASS); 414 } 415 416 public static String getCommandId(IConfigurationElement element) { 417 return element.getAttribute(IWorkbenchRegistryConstants.ATT_COMMAND_ID); 418 } 419 420 private int getStyle(IConfigurationElement element) { 421 String style = element 422 .getAttribute(IWorkbenchRegistryConstants.ATT_STYLE); 423 if (style == null || style.length() == 0) { 424 return CommandContributionItem.STYLE_PUSH; 425 } 426 if (IWorkbenchRegistryConstants.STYLE_TOGGLE.equals(style)) { 427 return CommandContributionItem.STYLE_CHECK; 428 } 429 if (IWorkbenchRegistryConstants.STYLE_RADIO.equals(style)) { 430 return CommandContributionItem.STYLE_RADIO; 431 } 432 if (IWorkbenchRegistryConstants.STYLE_PULLDOWN.equals(style)) { 433 return CommandContributionItem.STYLE_PULLDOWN; 434 } 435 return CommandContributionItem.STYLE_PUSH; 436 } 437 438 443 public static Map getParameters(IConfigurationElement element) { 444 HashMap map = new HashMap (); 445 IConfigurationElement[] parameters = element 446 .getChildren(IWorkbenchRegistryConstants.TAG_PARAMETER); 447 for (int i = 0; i < parameters.length; i++) { 448 String name = parameters[i] 449 .getAttribute(IWorkbenchRegistryConstants.ATT_NAME); 450 String value = parameters[i] 451 .getAttribute(IWorkbenchRegistryConstants.ATT_VALUE); 452 if (name != null && value != null) { 453 map.put(name, value); 454 } 455 } 456 return map; 457 } 458 } 459 | Popular Tags |