1 11 package org.eclipse.jface.action; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.ListenerList; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.events.MenuAdapter; 20 import org.eclipse.swt.events.MenuEvent; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.swt.widgets.CoolBar; 24 import org.eclipse.swt.widgets.Decorations; 25 import org.eclipse.swt.widgets.Menu; 26 import org.eclipse.swt.widgets.MenuItem; 27 import org.eclipse.swt.widgets.Shell; 28 import org.eclipse.swt.widgets.ToolBar; 29 30 37 public class MenuManager extends ContributionManager implements IMenuManager { 38 39 42 private String id; 43 44 47 private ListenerList listeners = new ListenerList(); 48 49 53 private Menu menu = null; 54 55 60 private MenuItem menuItem; 61 62 65 private String menuText; 66 67 70 private IContributionManagerOverrides overrides; 71 72 75 private IContributionManager parent; 76 77 81 private boolean removeAllWhenShown = false; 82 83 88 protected boolean visible = true; 89 90 94 public MenuManager() { 95 this(null, null); 96 } 97 98 105 public MenuManager(String text) { 106 this(text, null); 107 } 108 109 116 public MenuManager(String text, String id) { 117 this.menuText = text; 118 this.id = id; 119 } 120 121 124 public void addMenuListener(IMenuListener listener) { 125 listeners.add(listener); 126 } 127 128 139 public Menu createContextMenu(Control parent) { 140 if (!menuExist()) { 141 menu = new Menu(parent); 142 initializeMenu(); 143 } 144 return menu; 145 } 146 147 156 public Menu createMenuBar(Decorations parent) { 157 if (!menuExist()) { 158 menu = new Menu(parent, SWT.BAR); 159 update(false); 160 } 161 return menu; 162 } 163 164 174 public Menu createMenuBar(Shell parent) { 175 return createMenuBar((Decorations) parent); 176 } 177 178 184 public void dispose() { 185 if (menuExist()) { 186 menu.dispose(); 187 } 188 menu = null; 189 190 if (menuItem != null) { 191 menuItem.dispose(); 192 menuItem = null; 193 } 194 195 IContributionItem[] items = getItems(); 196 for (int i = 0; i < items.length; i++) { 197 items[i].dispose(); 198 } 199 } 200 201 204 public void fill(Composite parent) { 205 } 206 207 210 public void fill(CoolBar parent, int index) { 211 } 212 213 216 public void fill(Menu parent, int index) { 217 if (menuItem == null || menuItem.isDisposed()) { 218 if (index >= 0) { 219 menuItem = new MenuItem(parent, SWT.CASCADE, index); 220 } else { 221 menuItem = new MenuItem(parent, SWT.CASCADE); 222 } 223 224 menuItem.setText(getMenuText()); 225 226 if (!menuExist()) { 227 menu = new Menu(parent); 228 } 229 230 menuItem.setMenu(menu); 231 232 initializeMenu(); 233 234 update(true); 237 } 238 } 239 240 243 public void fill(ToolBar parent, int index) { 244 } 245 246 249 public IMenuManager findMenuUsingPath(String path) { 250 IContributionItem item = findUsingPath(path); 251 if (item instanceof IMenuManager) { 252 return (IMenuManager) item; 253 } 254 return null; 255 } 256 257 260 public IContributionItem findUsingPath(String path) { 261 String id = path; 262 String rest = null; 263 int separator = path.indexOf('/'); 264 if (separator != -1) { 265 id = path.substring(0, separator); 266 rest = path.substring(separator + 1); 267 } else { 268 return super.find(path); 269 } 270 271 IContributionItem item = super.find(id); 272 if (item instanceof IMenuManager) { 273 IMenuManager manager = (IMenuManager) item; 274 return manager.findUsingPath(rest); 275 } 276 return null; 277 } 278 279 287 private void fireAboutToShow(IMenuManager manager) { 288 Object [] listeners = this.listeners.getListeners(); 289 for (int i = 0; i < listeners.length; ++i) { 290 ((IMenuListener) listeners[i]).menuAboutToShow(manager); 291 } 292 } 293 294 301 private void fireAboutToHide(IMenuManager manager) { 302 final Object [] listeners = this.listeners.getListeners(); 303 for (int i = 0; i < listeners.length; ++i) { 304 final Object listener = listeners[i]; 305 if (listener instanceof IMenuListener2) { 306 final IMenuListener2 listener2 = (IMenuListener2) listener; 307 listener2.menuAboutToHide(manager); 308 } 309 } 310 } 311 312 318 public String getId() { 319 return id; 320 } 321 322 327 public Menu getMenu() { 328 return menu; 329 } 330 331 336 public String getMenuText() { 337 return menuText; 338 } 339 340 343 public IContributionManagerOverrides getOverrides() { 344 if (overrides == null) { 345 if (parent == null) { 346 overrides = new IContributionManagerOverrides() { 347 public Integer getAccelerator(IContributionItem item) { 348 return null; 349 } 350 351 public String getAcceleratorText(IContributionItem item) { 352 return null; 353 } 354 355 public Boolean getEnabled(IContributionItem item) { 356 return null; 357 } 358 359 public String getText(IContributionItem item) { 360 return null; 361 } 362 }; 363 } else { 364 overrides = parent.getOverrides(); 365 } 366 super.setOverrides(overrides); 367 } 368 return overrides; 369 } 370 371 377 public IContributionManager getParent() { 378 return parent; 379 } 380 381 384 public boolean getRemoveAllWhenShown() { 385 return removeAllWhenShown; 386 } 387 388 391 private void handleAboutToShow() { 392 if (removeAllWhenShown) { 393 removeAll(); 394 } 395 fireAboutToShow(this); 396 update(false, true); 397 } 398 399 402 private void handleAboutToHide() { 403 fireAboutToHide(this); 404 } 405 406 409 private void initializeMenu() { 410 menu.addMenuListener(new MenuAdapter() { 411 public void menuHidden(MenuEvent e) { 412 handleAboutToHide(); 414 } 415 416 public void menuShown(MenuEvent e) { 417 handleAboutToShow(); 418 } 419 }); 420 } 423 424 427 public boolean isDynamic() { 428 return false; 429 } 430 431 442 public boolean isEnabled() { 443 return true; 444 } 445 446 449 public boolean isGroupMarker() { 450 return false; 451 } 452 453 456 public boolean isSeparator() { 457 return false; 458 } 459 460 468 public boolean isSubstituteFor(IContributionItem item) { 469 return this.equals(item); 470 } 471 472 475 public boolean isVisible() { 476 if (!visible) { 477 return false; } 479 480 IContributionItem[] childItems = getItems(); 482 boolean visibleChildren = false; 483 for (int j = 0; j < childItems.length; j++) { 484 if (childItems[j].isVisible() && !childItems[j].isSeparator()) { 485 visibleChildren = true; 486 break; 487 } 488 } 489 490 return visibleChildren; 491 } 492 493 494 500 public void markDirty() { 501 super.markDirty(); 502 IContributionManager parent = getParent(); 510 if (parent != null) { 511 parent.markDirty(); 512 } 513 } 514 515 522 private boolean menuExist() { 523 return menu != null && !menu.isDisposed(); 524 } 525 526 529 public void removeMenuListener(IMenuListener listener) { 530 listeners.remove(listener); 531 } 532 533 536 public void saveWidgetState() { 537 } 538 539 545 public void setOverrides(IContributionManagerOverrides newOverrides) { 546 overrides = newOverrides; 547 super.setOverrides(overrides); 548 } 549 550 553 public void setParent(IContributionManager manager) { 554 parent = manager; 555 } 556 557 560 public void setRemoveAllWhenShown(boolean removeAll) { 561 this.removeAllWhenShown = removeAll; 562 } 563 564 567 public void setVisible(boolean visible) { 568 this.visible = visible; 569 } 570 571 574 public void update() { 575 updateMenuItem(); 576 } 577 578 584 public void update(boolean force) { 585 update(force, false); 586 } 587 588 598 protected void update(boolean force, boolean recursive) { 599 if (isDirty() || force) { 600 if (menuExist()) { 601 IContributionItem[] items = getItems(); 603 List clean = new ArrayList (items.length); 604 IContributionItem separator = null; 605 for (int i = 0; i < items.length; ++i) { 606 IContributionItem ci = items[i]; 607 if (!ci.isVisible()) { 608 continue; 609 } 610 if (ci.isSeparator()) { 611 separator = ci; 614 } else { 615 if (separator != null) { 616 if (clean.size() > 0) { 617 clean.add(separator); 618 } 619 separator = null; 620 } 621 clean.add(ci); 622 } 623 } 624 625 MenuItem[] mi = menu.getItems(); 627 628 for (int i = 0; i < mi.length; i++) { 629 Object data = mi[i].getData(); 630 631 if (data == null || !clean.contains(data)) { 632 mi[i].dispose(); 633 } else if (data instanceof IContributionItem 634 && ((IContributionItem) data).isDynamic() 635 && ((IContributionItem) data).isDirty()) { 636 mi[i].dispose(); 637 } 638 } 639 640 mi = menu.getItems(); 642 int srcIx = 0; 643 int destIx = 0; 644 645 for (Iterator e = clean.iterator(); e.hasNext();) { 646 IContributionItem src = (IContributionItem) e.next(); 647 IContributionItem dest; 648 649 if (srcIx < mi.length) { 651 dest = (IContributionItem) mi[srcIx].getData(); 652 } else { 653 dest = null; 654 } 655 656 if (dest != null && src.equals(dest)) { 657 srcIx++; 658 destIx++; 659 } else if (dest != null && dest.isSeparator() 660 && src.isSeparator()) { 661 mi[srcIx].setData(src); 662 srcIx++; 663 destIx++; 664 } else { 665 int start = menu.getItemCount(); 666 src.fill(menu, destIx); 667 int newItems = menu.getItemCount() - start; 668 for (int i = 0; i < newItems; i++) { 669 MenuItem item = menu.getItem(destIx++); 670 item.setData(src); 671 } 672 } 673 674 if (recursive) { 681 IContributionItem item = src; 682 if (item instanceof SubContributionItem) { 683 item = ((SubContributionItem) item).getInnerItem(); 684 } 685 if (item instanceof IMenuManager) { 686 ((IMenuManager) item).updateAll(force); 687 } 688 } 689 690 } 691 692 for (; srcIx < mi.length; srcIx++) { 694 mi[srcIx].dispose(); 695 } 696 697 setDirty(false); 698 } 699 } else { 700 if (recursive) { 702 IContributionItem[] items = getItems(); 703 for (int i = 0; i < items.length; ++i) { 704 IContributionItem ci = items[i]; 705 if (ci instanceof IMenuManager) { 706 IMenuManager mm = (IMenuManager) ci; 707 if (mm.isVisible()) { 708 mm.updateAll(force); 709 } 710 } 711 } 712 } 713 } 714 updateMenuItem(); 715 } 716 717 720 public void update(String property) { 721 IContributionItem items[] = getItems(); 722 723 for (int i = 0; i < items.length; i++) { 724 items[i].update(property); 725 } 726 727 if (menu != null && !menu.isDisposed() && menu.getParentItem() != null 728 && IAction.TEXT.equals(property)) { 729 String text = getOverrides().getText(this); 730 731 if (text == null) { 732 text = getMenuText(); 733 } 734 735 if (text != null) { 736 ExternalActionManager.ICallback callback = ExternalActionManager 737 .getInstance().getCallback(); 738 739 if (callback != null) { 740 int index = text.indexOf('&'); 741 742 if (index >= 0 && index < text.length() - 1) { 743 char character = Character.toUpperCase(text 744 .charAt(index + 1)); 745 746 if (callback.isAcceleratorInUse(SWT.ALT | character)) { 747 if (index == 0) { 748 text = text.substring(1); 749 } else { 750 text = text.substring(0, index) 751 + text.substring(index + 1); 752 } 753 } 754 } 755 } 756 757 menu.getParentItem().setText(text); 758 } 759 } 760 } 761 762 765 public void updateAll(boolean force) { 766 update(force, true); 767 } 768 769 774 private void updateMenuItem() { 775 793 if (menuItem != null && !menuItem.isDisposed() && menuExist()) { 796 boolean enabled = menu.getItemCount() > 0; 797 if (menuItem.getEnabled() != enabled) { 799 Menu topMenu = menu; 801 while (topMenu.getParentMenu() != null) { 802 topMenu = topMenu.getParentMenu(); 803 } 804 if ((topMenu.getStyle() & SWT.BAR) == 0) { 805 menuItem.setEnabled(enabled); 806 } 807 } 808 } 809 } 810 } 811 | Popular Tags |