1 11 12 package org.eclipse.ui.internal.menus; 13 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.commands.Command; 18 import org.eclipse.core.commands.ExecutionException; 19 import org.eclipse.core.commands.NotEnabledException; 20 import org.eclipse.core.commands.NotHandledException; 21 import org.eclipse.core.commands.ParameterizedCommand; 22 import org.eclipse.core.commands.common.NotDefinedException; 23 import org.eclipse.jface.action.ActionContributionItem; 24 import org.eclipse.jface.action.IContributionItem; 25 import org.eclipse.jface.action.IContributionManager; 26 import org.eclipse.jface.action.LegacyActionTools; 27 import org.eclipse.jface.action.MenuManager; 28 import org.eclipse.jface.action.SubMenuManager; 29 import org.eclipse.jface.menus.IWidget; 30 import org.eclipse.swt.SWT; 31 import org.eclipse.swt.events.SelectionEvent; 32 import org.eclipse.swt.events.SelectionListener; 33 import org.eclipse.swt.widgets.Composite; 34 import org.eclipse.swt.widgets.CoolBar; 35 import org.eclipse.swt.widgets.Menu; 36 import org.eclipse.swt.widgets.MenuItem; 37 import org.eclipse.swt.widgets.ToolBar; 38 import org.eclipse.ui.commands.ICommandService; 39 import org.eclipse.ui.internal.ActionSetContributionItem; 40 import org.eclipse.ui.internal.WorkbenchWindow; 41 import org.eclipse.ui.internal.misc.Policy; 42 43 55 public class LegacyMenuManager extends MenuManager { 56 57 60 private final WorkbenchWindow fWindow; 61 62 private IMenuService fMenuService; 63 64 private SMenuLayout fLayout; 65 66 73 public LegacyMenuManager(final WorkbenchWindow window) { 74 if (window == null) { 75 throw new NullPointerException ("The window cannot be null"); } 77 this.fWindow = window; 78 fMenuService = (IMenuService) fWindow.getService(IMenuService.class); 79 } 80 81 86 protected void update(boolean force, boolean recursive) { 87 if (isDirty()) { 88 generateMenus(); 89 fLayout = fMenuService.getLayout(); 90 Menu menu = getMenu(); 91 if (menu.getItemCount() > 0) { 92 MenuItem[] items = menu.getItems(); 93 for (int i = 0; i < items.length; i++) { 94 items[i].dispose(); 95 } 96 } 97 SMenuBuilder builder = new SMenuBuilder(fLayout, menu, fWindow); 98 builder.build(); 99 setDirty(false); 100 } 101 } 102 103 106 private void generateMenus() { 107 IContributionItem[] items = getItems(); 108 SLocation location = new SLocation(new SBar()); 109 processChildren(items, location); 110 } 111 112 116 private void createMenu(IContributionItem item, SLocation location) { 117 if (item == null) { 118 return; 119 } 120 if (item instanceof ActionContributionItem) { 121 addActionContribution((ActionContributionItem) item, location); 122 } else if (item instanceof ActionSetContributionItem) { 123 addActionSetContribution((ActionSetContributionItem) item, location); 124 } else if (item instanceof MenuManager) { 125 addMenu((MenuManager) item, location); 126 } else if (item instanceof SubMenuManager) { 127 addSubMenuManager((SubMenuManager) item, location); 128 } else if (item instanceof IContributionManager) { 129 addContributionManager((IContributionManager) item, location); 130 } else { 131 if (Policy.EXPERIMENTAL_MENU) { 132 System.err.println("createMenu: unknown: " + item.getClass().getName()); 134 } 135 addWidget(item, location); 136 } 137 } 138 139 143 private void addSubMenuManager(SubMenuManager manager, 144 SLocation parentLocation) { 145 processChildren(manager.getItems(), parentLocation); 146 } 147 148 152 private void addContributionManager(IContributionManager manager, 153 SLocation parentLocation) { 154 if (Policy.EXPERIMENTAL_MENU) { 155 System.err.println("addContributionManager: unknown: " + manager.getClass().getName()); 157 } 158 159 processChildren(manager.getItems(), parentLocation); 160 } 161 162 166 private void addMenu(MenuManager menu, SLocation parentLocation) { 167 String id = normalizeId(menu); 168 SMenu smenu = fMenuService.getMenu(id); 169 if (smenu.isDefined()) { 170 smenu.addLocation(parentLocation); 171 } else { 172 smenu.define(LegacyActionTools.removeMnemonics(menu.getMenuText()), 173 parentLocation); 174 } 175 final char mnemonic = LegacyActionTools.extractMnemonic(menu 176 .getMenuText()); 177 SLocation location = new SLocation(parentLocation, id, mnemonic); 178 179 processChildren(menu.getItems(), location); 180 } 181 182 186 private void processChildren(IContributionItem[] items, SLocation location) { 187 for (int i = 0; i < items.length; i++) { 188 if (items[i].isGroupMarker()) { 189 addGroup(items[i], location); 190 } else if (items[i].isSeparator()) { 191 addWidget(items[i], location); 192 } else { 193 createMenu(items[i], location); 194 } 195 } 196 } 197 198 202 private void addWidget(final IContributionItem item, SLocation location) { 203 String id = normalizeId(item); 204 205 SWidget swidget = fMenuService.getWidget(id); 206 if (swidget.isDefined()) { 207 swidget.addLocation(location); 208 } else { 209 swidget.define(new IWidget() { 210 211 public void dispose() { 212 item.dispose(); 213 } 214 215 public void fill(Composite parent) { 216 item.fill(parent); 217 } 218 219 public void fill(Menu parent, int index) { 220 item.fill(parent, index); 221 } 222 223 public void fill(ToolBar parent, int index) { 224 item.fill(parent, index); 225 } 226 227 public void fill(CoolBar parent, int index) { 228 item.fill(parent, index); 229 } 230 231 }, location); 232 } 233 } 234 235 240 private String normalizeId(final IContributionItem item) { 241 String id = item.getId(); 242 if (id == null || id.length() < 1) { 243 id = item.getClass().getName() + "@" + item.hashCode(); } 245 return id; 246 } 247 248 253 private SLocation addGroup(IContributionItem marker, SLocation location) { 254 String id = normalizeId(marker); 255 return addGroup(id, marker.isSeparator(), location); 256 } 257 258 263 protected SLocation addGroup(IContributionManager manager, 264 SLocation location) { 265 String id = manager.getClass().getName() + "@" + manager.hashCode(); return addGroup(id, false, location); 267 } 268 269 275 private SLocation addGroup(String id, boolean isSeparator, 276 SLocation location) { 277 SGroup group = fMenuService.getGroup(id); 278 if (group.isDefined()) { 279 group.addLocation(location); 280 } else { 281 group.define(isSeparator, location); 282 } 283 return new SLocation(location, group.getId()); 284 } 285 286 290 private void addActionSetContribution( 291 ActionSetContributionItem actionSetContribution, 292 SLocation parentLocation) { 293 createMenu(actionSetContribution.getInnerItem(), parentLocation); 294 } 295 296 300 private void addActionContribution( 301 ActionContributionItem actionContribution, SLocation parentLocation) { 302 String id = normalizeId(actionContribution); 303 String commandId = actionContribution.getAction() 304 .getActionDefinitionId(); 305 if (commandId == null && Policy.EXPERIMENTAL_MENU) { 306 System.err 307 .println("addActionContribution: When is a command not a command! " + actionContribution.getId()); 309 return; 310 } 311 SItem sitem = fMenuService.getItem(id); 312 if (sitem.isDefined()) { 313 sitem.addLocation(parentLocation); 314 } else { 315 ICommandService commandService = (ICommandService) fWindow 316 .getService(ICommandService.class); 317 Command c = commandService.getCommand(commandId); 318 if (c.isDefined()) { 319 ParameterizedCommand pc = new ParameterizedCommand(c, null); 320 sitem.define(pc, actionContribution.getAction().getText(), 321 parentLocation); 322 } else if (Policy.EXPERIMENTAL_MENU) { 323 System.err 324 .println("addActionContribution: undefined command " + commandId); } 326 } 327 } 328 329 private static class SMenuBuilder { 330 331 private static class IndexManager { 332 335 public int index = 0; 336 } 337 338 private SMenuLayout fLayout; 339 340 private Menu fRootMenu; 341 342 private WorkbenchWindow fWindow; 343 344 349 public SMenuBuilder(SMenuLayout rootNode, Menu menu, 350 WorkbenchWindow window) { 351 fLayout = rootNode; 352 fRootMenu = menu; 353 fWindow = window; 354 } 355 356 359 public void build() { 360 ILayoutNode root = fLayout.getMenuBar(); 361 362 List children = root.getChildrenSorted(); 363 IndexManager index = new IndexManager(); 364 for (Iterator i = children.iterator(); i.hasNext();) { 365 ILayoutNode child = (ILayoutNode) i.next(); 366 addNode(fRootMenu, child, index); 367 } 368 } 369 370 375 public void addNode(Menu menu, ILayoutNode node, IndexManager index) { 376 MenuElement element = node.getMenuElement(); 377 if (Policy.EXPERIMENTAL_MENU 378 && node.getLocation().getPath().toString().indexOf( 379 LeafLocationElement.BREAKPOINT_PATH) > -1) { 380 System.err 381 .println("addNode: tree: " + node.getLocation() + "\n\t" + element); 383 } 384 if (element instanceof SMenu) { 386 SMenu smenu = (SMenu) element; 387 if (!smenu.isVisible(fWindow)) { 388 return; 389 } 390 MenuItem item = new MenuItem(menu, SWT.CASCADE, index.index++); 391 item.setData(smenu); 392 393 try { 394 item.setText(smenu.getLabel()); 395 } catch (NotDefinedException e) { 396 e.printStackTrace(); 398 } 399 400 Menu itemMenu = new Menu(menu); 401 item.setMenu(itemMenu); 402 403 List children = node.getChildrenSorted(); 404 IndexManager childIndex = new IndexManager(); 405 for (Iterator i = children.iterator(); i.hasNext();) { 406 ILayoutNode child = (ILayoutNode) i.next(); 407 addNode(itemMenu, child, childIndex); 408 } 409 410 } else if (element instanceof SItem) { 411 final SItem sitem = (SItem) element; 412 if (!sitem.isVisible(fWindow)) { 413 return; 414 } 415 final MenuItem item = new MenuItem(menu, SWT.PUSH, 416 index.index++); 417 item.setData(element); 418 419 try { 420 item.setText(sitem.getCommand().getName()); 421 item.addSelectionListener(new SelectionListener() { 422 public void widgetSelected(SelectionEvent e) { 423 try { 424 sitem.getCommand() 425 .executeWithChecks(null, null); 426 } catch (ExecutionException e1) { 427 e1.printStackTrace(); 429 } catch (NotDefinedException e1) { 430 e1.printStackTrace(); 432 } catch (NotEnabledException e1) { 433 e1.printStackTrace(); 435 } catch (NotHandledException e1) { 436 e1.printStackTrace(); 438 } 439 } 440 441 public void widgetDefaultSelected(SelectionEvent e) { 442 } 443 }); 444 } catch (NotDefinedException e1) { 445 e1.printStackTrace(); 447 } 448 } else if (element instanceof SWidget) { 449 SWidget swidget = (SWidget) element; 450 if (!swidget.isVisible(fWindow)) { 451 return; 452 } 453 try { 454 swidget.getWidget().fill(menu, index.index); 455 } catch (NotDefinedException e) { 456 e.printStackTrace(); 458 } 459 } else if (element instanceof SGroup) { 460 SGroup sgroup = (SGroup) element; 461 if (!sgroup.isVisible(fWindow)) { 462 return; 463 } 464 try { 465 if (sgroup.isSeparatorsVisible()) { 466 MenuItem item = new MenuItem(menu, SWT.SEPARATOR, 467 index.index++); 468 item.setData(sgroup); 469 } 470 471 List children = node.getChildrenSorted(); 472 for (Iterator i = children.iterator(); i.hasNext();) { 473 ILayoutNode child = (ILayoutNode) i.next(); 474 addNode(menu, child, index); 475 } 476 } catch (NotDefinedException e) { 477 e.printStackTrace(); 479 } 480 } else { 481 if (Policy.EXPERIMENTAL_MENU) { 482 System.err.println("addNode: no element for " + node.getLocation()); 484 } 485 if (node.isEmpty()) { 486 return; 487 } 488 489 List children = node.getChildrenSorted(); 490 491 if (!children.isEmpty()) { 492 493 MenuItem item = new MenuItem(menu, SWT.CASCADE, 494 index.index++); 495 item.setData(node); 496 497 item 498 .setText(node.getId() == null ? "node" + (index.index - 1) : node.getId()); 500 501 Menu itemMenu = new Menu(menu); 502 item.setMenu(itemMenu); 503 504 IndexManager childIndex = new IndexManager(); 505 for (Iterator i = children.iterator(); i.hasNext();) { 506 ILayoutNode child = (ILayoutNode) i.next(); 507 addNode(itemMenu, child, childIndex); 508 } 509 } 510 } 511 } 512 } 513 514 } 515 | Popular Tags |