1 11 package org.eclipse.ui.internal.commands.ws; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 19 import org.eclipse.core.runtime.Platform; 20 import org.eclipse.jface.action.ContributionItem; 21 import org.eclipse.jface.action.ExternalActionManager; 22 import org.eclipse.jface.action.IContributionManagerOverrides; 23 import org.eclipse.jface.resource.ImageDescriptor; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.events.HelpListener; 26 import org.eclipse.swt.graphics.Image; 27 import org.eclipse.swt.widgets.Button; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Display; 30 import org.eclipse.swt.widgets.Event; 31 import org.eclipse.swt.widgets.Listener; 32 import org.eclipse.swt.widgets.Menu; 33 import org.eclipse.swt.widgets.MenuItem; 34 import org.eclipse.swt.widgets.ToolBar; 35 import org.eclipse.swt.widgets.ToolItem; 36 import org.eclipse.swt.widgets.Widget; 37 import org.eclipse.ui.commands.CommandEvent; 38 import org.eclipse.ui.commands.ICommand; 39 import org.eclipse.ui.commands.ICommandListener; 40 import org.eclipse.ui.commands.NotDefinedException; 41 import org.eclipse.ui.commands.NotHandledException; 42 import org.eclipse.ui.help.WorkbenchHelp; 43 44 56 public class CommandContributionItem 57 extends ContributionItem 58 implements ICommandListener { 59 60 63 private static class ImageCache { 64 67 private class Entry { 68 69 Image grayImage; 70 71 Image image; 72 73 74 void dispose() { 75 if (image != null) { 76 image.dispose(); 77 image = null; 78 } 79 80 if (grayImage != null) { 81 grayImage.dispose(); 82 grayImage = null; 83 } 84 } 85 } 86 87 88 private Map entries = new HashMap (11); 89 90 private Image missingImage; 91 92 93 void dispose() { 94 for (Iterator i = entries.values().iterator(); i.hasNext();) { 95 ((Entry) i.next()).dispose(); 96 } 97 entries.clear(); 98 } 99 100 110 Entry getEntry(ImageDescriptor descriptor) { 111 Entry entry = (Entry) entries.get(descriptor); 112 113 if (entry == null) { 114 entry = new Entry(); 115 entries.put(descriptor, entry); 116 } 117 118 return entry; 119 } 120 121 133 Image getGrayImage(String imageURI) { 134 if (imageURI == null) { 135 return null; 136 } 137 138 try { 139 ImageDescriptor descriptor = 140 ImageDescriptor.createFromURL(new URL (imageURI)); 141 Entry entry = getEntry(descriptor); 142 if (entry.grayImage == null) { 143 Image image = getImage(imageURI); 144 if (image != null) { 145 entry.grayImage = 146 new Image(null, image, SWT.IMAGE_GRAY); 147 } 148 } 149 150 return entry.grayImage; 151 152 } catch (MalformedURLException e) { 153 return new Image(null, getMissingImage(), SWT.IMAGE_GRAY); 154 } 155 } 156 157 168 Image getImage(String imageURI) { 169 if (imageURI == null) { 170 return null; 171 } 172 173 try { 174 ImageDescriptor descriptor = 175 ImageDescriptor.createFromURL(new URL (imageURI)); 176 Entry entry = getEntry(descriptor); 177 if (entry.image == null) { 178 entry.image = descriptor.createImage(); 179 } 180 181 return entry.image; 182 183 } catch (MalformedURLException e) { 184 return getMissingImage(); 185 } 186 } 187 188 194 Image getMissingImage() { 195 if (missingImage == null) { 196 ImageDescriptor descriptor = 197 ImageDescriptor.getMissingImageDescriptor(); 198 Entry entry = getEntry(descriptor); 199 if (entry.image == null) { 200 entry.image = descriptor.createImage(); 201 } 202 missingImage = entry.image; 203 } 204 205 return missingImage; 206 } 207 } 208 209 210 private static ImageCache globalImageCache; 211 212 private static boolean useColourIcons = true; 213 214 218 227 233 public static boolean getUseColourIconsInToolbars() { 234 return useColourIcons; 235 } 236 237 246 public static String removeAcceleratorText(String text) { 247 int index = text.lastIndexOf('\t'); 248 if (index == -1) 249 index = text.lastIndexOf('@'); 250 if (index >= 0) 251 return text.substring(0, index); 252 return text; 253 } 254 255 262 public static void setUseColourIconsInToolbars(boolean newValue) { 263 useColourIcons = newValue; 264 } 265 266 267 private Listener buttonListener; 268 269 private ICommand command; 270 271 private HelpListener helpListener; 272 273 private Listener menuItemListener; 274 275 private Listener toolItemListener; 276 280 private Widget widget = null; 281 282 290 public CommandContributionItem(ICommand commandToUse) { 291 super(commandToUse.getId()); 292 command = commandToUse; 293 helpListener = WorkbenchHelp.createHelpListener(commandToUse); 294 } 295 296 303 public void commandChanged(final CommandEvent e) { 304 306 if (isVisible() && widget != null) { 307 Display display = widget.getDisplay(); 308 if (display.getThread() == Thread.currentThread()) { 309 update(e); 310 } else { 311 display.asyncExec(new Runnable () { 312 public void run() { 313 update(e); 314 } 315 }); 316 } 317 318 } 319 } 320 321 328 public boolean equals(Object o) { 329 if (!(o instanceof CommandContributionItem)) { 330 return false; 331 } 332 return command.equals(((CommandContributionItem) o).command); 333 } 334 335 345 public void fill(Composite parent) { 346 if (widget == null && parent != null) { 347 int flags = SWT.PUSH; 348 if (command != null) { 349 } 355 356 Button b = new Button(parent, flags); 357 b.setData(this); 358 b.addListener(SWT.Dispose, getButtonListener()); 359 b.addListener(SWT.Selection, getButtonListener()); 361 b.addHelpListener(helpListener); 362 widget = b; 363 364 update(); 365 366 command.addCommandListener(this); 367 } 368 } 369 370 386 public void fill(Menu parent, int index) { 387 if (widget == null && parent != null) { 388 Menu subMenu = null; 389 int flags = SWT.PUSH; 390 if (command != null) { 391 } 405 406 MenuItem mi = null; 407 if (index >= 0) 408 mi = new MenuItem(parent, flags, index); 409 else 410 mi = new MenuItem(parent, flags); 411 widget = mi; 412 413 mi.setData(this); 414 mi.addListener(SWT.Dispose, getMenuItemListener()); 415 mi.addListener(SWT.Selection, getMenuItemListener()); 416 mi.addHelpListener(helpListener); 417 418 if (subMenu != null) 419 mi.setMenu(subMenu); 420 421 update(); 422 423 command.addCommandListener(this); 424 } 425 } 426 427 443 public void fill(ToolBar parent, int index) { 444 if (widget == null && parent != null) { 445 int flags = SWT.PUSH; 446 if (command != null) { 447 } 456 457 ToolItem ti = null; 458 if (index >= 0) 459 ti = new ToolItem(parent, flags, index); 460 else 461 ti = new ToolItem(parent, flags); 462 ti.setData(this); 463 ti.addListener(SWT.Selection, getToolItemListener()); 464 ti.addListener(SWT.Dispose, getToolItemListener()); 465 466 widget = ti; 467 468 update(); 469 470 command.addCommandListener(this); 471 } 472 } 473 474 480 private Listener getButtonListener() { 481 if (buttonListener == null) { 482 buttonListener = new Listener() { 483 public void handleEvent(Event event) { 484 switch (event.type) { 485 case SWT.Dispose : 486 handleWidgetDispose(event); 487 break; 488 case SWT.Selection : 489 Widget ew = event.widget; 490 if (ew != null) { 491 handleWidgetSelection( 492 event, 493 ((Button) ew).getSelection()); 494 } 495 break; 496 } 497 } 498 }; 499 } 500 return buttonListener; 501 } 502 503 508 public ICommand getCommand() { 509 return command; 510 } 511 512 523 private ImageCache getImageCache() { 524 ImageCache cache = globalImageCache; 525 if (cache == null) { 526 globalImageCache = cache = new ImageCache(); 527 Display display = Display.getDefault(); 528 if (display != null) { 529 display.disposeExec(new Runnable () { 530 public void run() { 531 if (globalImageCache != null) { 532 globalImageCache.dispose(); 533 globalImageCache = null; 534 } 535 } 536 }); 537 } 538 } 539 return cache; 540 } 541 542 548 private Listener getMenuItemListener() { 549 if (menuItemListener == null) { 550 menuItemListener = new Listener() { 551 public void handleEvent(Event event) { 552 switch (event.type) { 553 case SWT.Dispose : 554 handleWidgetDispose(event); 555 break; 556 case SWT.Selection : 557 Widget ew = event.widget; 558 if (ew != null) { 559 handleWidgetSelection( 560 event, 561 ((MenuItem) ew).getSelection()); 562 } 563 break; 564 } 565 } 566 }; 567 } 568 return menuItemListener; 569 } 570 571 577 private Listener getToolItemListener() { 578 if (toolItemListener == null) { 579 toolItemListener = new Listener() { 580 public void handleEvent(Event event) { 581 switch (event.type) { 582 case SWT.Dispose : 583 handleWidgetDispose(event); 584 break; 585 case SWT.Selection : 586 Widget ew = event.widget; 587 if (ew != null) { 588 handleWidgetSelection( 589 event, 590 ((ToolItem) ew).getSelection()); 591 } 592 break; 593 } 594 } 595 }; 596 } 597 return toolItemListener; 598 } 599 600 608 private void handleWidgetDispose(Event e) { 609 if (e.widget == widget) { 610 command.removeCommandListener(this); 619 widget = null; 620 } 621 } 622 623 632 private void handleWidgetSelection(Event e, boolean selection) { 633 Widget item = e.widget; 634 if (item != null) { 635 676 680 if (isEnabled(command)) { 681 boolean trace = "true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.jface/trace/actions")); long ms = System.currentTimeMillis(); 683 try { 684 if (trace) 685 System.out.println("Running command: " + command.getName()); 687 690 if (trace) 691 System.out.println((System.currentTimeMillis() - ms) + " ms to run command: " + command.getName()); } catch (NotDefinedException nde) { 693 update(); } 696 } 697 } 698 } 699 700 703 public int hashCode() { 704 return command.hashCode(); 705 } 706 707 712 public boolean isDynamic() { 713 if (widget instanceof MenuItem) { 714 boolean itemIsCheck = (widget.getStyle() & SWT.CHECK) != 0; 718 boolean itemIsRadio = (widget.getStyle() & SWT.RADIO) != 0; 721 return false; 726 } 727 728 return false; 729 } 730 731 734 public boolean isEnabled() { 735 return isEnabled(command); 736 } 737 738 private static boolean isEnabled(ICommand command) { 739 try { 740 Map attributeValuesByName = command.getAttributeValuesByName(); 741 742 if (attributeValuesByName.containsKey("enabled") && !Boolean.TRUE.equals(attributeValuesByName.get("enabled"))) return false; 745 else 746 return true; 747 } catch (NotHandledException eNotHandled) { 748 return false; 749 } 750 } 751 752 759 protected boolean isEnabledAllowed() { 760 if (getParent() == null) 761 return true; 762 Boolean value = getParent().getOverrides().getEnabled(this); 763 return (value == null) ? true : value.booleanValue(); 764 } 765 766 772 public boolean isVisible() { 773 return true; } 775 776 780 public void update() { 781 update((CommandEvent) null); 782 } 783 784 792 public void update(CommandEvent event) { 793 if (widget != null) { 794 ICommand currentCommand = getCommand(); 795 796 boolean descriptionChanged = true; 798 boolean nameChanged = true; 799 boolean enabledChanged = true; 800 boolean checkedChanged = true; 801 if (event != null) { 802 descriptionChanged = event.hasDescriptionChanged(); 803 nameChanged = event.hasNameChanged(); 804 809 if ((event.hasDefinedChanged() && !currentCommand.isDefined()) 810 ) { 811 } 813 } 814 815 try { 816 if (widget instanceof ToolItem) { 818 ToolItem ti = (ToolItem) widget; 819 820 if (descriptionChanged) 821 ti.setToolTipText(currentCommand.getDescription()); 822 823 if (enabledChanged) { 824 boolean shouldBeEnabled = 825 isEnabled(currentCommand) && isEnabledAllowed(); 826 827 if (ti.getEnabled() != shouldBeEnabled) 828 ti.setEnabled(shouldBeEnabled); 829 } 830 831 if (checkedChanged) { 832 } 838 839 return; 840 } 841 842 if (widget instanceof MenuItem) { 844 MenuItem mi = (MenuItem) widget; 845 846 if (nameChanged) { 847 Integer accelerator = null; 848 String acceleratorText = null; 849 String name = null; 850 851 ExternalActionManager.ICallback callback = 852 ExternalActionManager.getInstance().getCallback(); 853 if (callback != null) { 854 String commandId = currentCommand.getId(); 855 if (commandId != null) { 856 accelerator = 857 callback.getAccelerator(commandId); 858 acceleratorText = 859 callback.getAcceleratorText(commandId); 860 } 861 } 862 863 IContributionManagerOverrides overrides = null; 864 865 if (getParent() != null) 866 overrides = getParent().getOverrides(); 867 868 if (overrides != null) 869 name = getParent().getOverrides().getText(this); 870 871 876 mi.setAccelerator(accelerator.intValue()); 877 878 if (name == null) 879 name = currentCommand.getName(); 880 881 if (name == null) 882 name = ""; else 884 name = removeAcceleratorText(name); 885 886 if (acceleratorText == null) 887 mi.setText(name); 888 else 889 mi.setText(name + '\t' + acceleratorText); 890 } 891 892 if (enabledChanged) { 893 boolean shouldBeEnabled = 894 isEnabled(currentCommand) && isEnabledAllowed(); 895 896 if (mi.getEnabled() != shouldBeEnabled) 897 mi.setEnabled(shouldBeEnabled); 898 } 899 900 if (checkedChanged) { 901 } 907 908 return; 909 } 910 911 if (widget instanceof Button) { 913 Button button = (Button) widget; 914 915 if (nameChanged) { 916 String name = currentCommand.getName(); 917 918 if (name != null) 919 button.setText(name); 920 } 921 922 if (descriptionChanged) 923 button.setToolTipText(currentCommand.getDescription()); 924 925 if (enabledChanged) { 926 boolean shouldBeEnabled = 927 isEnabled(currentCommand) && isEnabledAllowed(); 928 929 if (button.getEnabled() != shouldBeEnabled) 930 button.setEnabled(shouldBeEnabled); 931 } 932 933 if (checkedChanged) { 934 } 940 941 return; 942 } 943 } catch (NotDefinedException e) { 944 949 950 } 952 } 953 } 954 } 955 | Popular Tags |