1 11 package org.eclipse.jface.action; 12 13 import org.eclipse.jface.action.ExternalActionManager.IBindingManagerCallback; 14 import org.eclipse.jface.bindings.Trigger; 15 import org.eclipse.jface.bindings.TriggerSequence; 16 import org.eclipse.jface.bindings.keys.IKeyLookup; 17 import org.eclipse.jface.bindings.keys.KeyLookupFactory; 18 import org.eclipse.jface.bindings.keys.KeyStroke; 19 import org.eclipse.jface.resource.ImageDescriptor; 20 import org.eclipse.jface.resource.JFaceResources; 21 import org.eclipse.jface.resource.LocalResourceManager; 22 import org.eclipse.jface.resource.ResourceManager; 23 import org.eclipse.jface.util.IPropertyChangeListener; 24 import org.eclipse.jface.util.Policy; 25 import org.eclipse.jface.util.PropertyChangeEvent; 26 import org.eclipse.swt.SWT; 27 import org.eclipse.swt.graphics.GC; 28 import org.eclipse.swt.graphics.Point; 29 import org.eclipse.swt.graphics.Rectangle; 30 import org.eclipse.swt.widgets.Button; 31 import org.eclipse.swt.widgets.Composite; 32 import org.eclipse.swt.widgets.Display; 33 import org.eclipse.swt.widgets.Event; 34 import org.eclipse.swt.widgets.Item; 35 import org.eclipse.swt.widgets.Listener; 36 import org.eclipse.swt.widgets.Menu; 37 import org.eclipse.swt.widgets.MenuItem; 38 import org.eclipse.swt.widgets.ToolBar; 39 import org.eclipse.swt.widgets.ToolItem; 40 import org.eclipse.swt.widgets.Widget; 41 42 48 public class ActionContributionItem extends ContributionItem { 49 50 57 public static int MODE_FORCE_TEXT = 1; 58 59 60 private static final String ellipsis = "..."; 62 private static boolean USE_COLOR_ICONS = true; 63 64 70 public static boolean getUseColorIconsInToolbars() { 71 return USE_COLOR_ICONS; 72 } 73 74 81 public static void setUseColorIconsInToolbars(boolean useColorIcons) { 82 USE_COLOR_ICONS = useColorIcons; 83 } 84 85 88 private int mode = 0; 89 90 93 private IAction action; 94 95 99 private final IPropertyChangeListener actionTextListener = new IPropertyChangeListener() { 100 101 104 public void propertyChange(PropertyChangeEvent event) { 105 update(event.getProperty()); 106 } 107 }; 108 109 112 private LocalResourceManager imageManager; 113 114 117 private Listener buttonListener; 118 119 122 private Listener menuItemListener; 123 124 127 private final IPropertyChangeListener propertyListener = new IPropertyChangeListener() { 128 public void propertyChange(PropertyChangeEvent event) { 129 actionPropertyChange(event); 130 } 131 }; 132 133 136 private Listener toolItemListener; 137 138 142 private Widget widget = null; 143 144 151 public ActionContributionItem(IAction action) { 152 super(action.getId()); 153 this.action = action; 154 } 155 156 160 private void actionPropertyChange(final PropertyChangeEvent e) { 161 163 if (isVisible() && widget != null) { 164 Display display = widget.getDisplay(); 165 if (display.getThread() == Thread.currentThread()) { 166 update(e.getProperty()); 167 } else { 168 display.asyncExec(new Runnable () { 169 public void run() { 170 update(e.getProperty()); 171 } 172 }); 173 } 174 175 } 176 } 177 178 182 public boolean equals(Object o) { 183 if (!(o instanceof ActionContributionItem)) { 184 return false; 185 } 186 return action.equals(((ActionContributionItem) o).action); 187 } 188 189 196 public void fill(Composite parent) { 197 if (widget == null && parent != null) { 198 int flags = SWT.PUSH; 199 if (action != null) { 200 if (action.getStyle() == IAction.AS_CHECK_BOX) { 201 flags = SWT.TOGGLE; 202 } 203 if (action.getStyle() == IAction.AS_RADIO_BUTTON) { 204 flags = SWT.RADIO; 205 } 206 } 207 208 Button b = new Button(parent, flags); 209 b.setData(this); 210 b.addListener(SWT.Dispose, getButtonListener()); 211 b.addListener(SWT.Selection, getButtonListener()); 213 if (action.getHelpListener() != null) { 214 b.addHelpListener(action.getHelpListener()); 215 } 216 widget = b; 217 218 update(null); 219 220 action.addPropertyChangeListener(propertyListener); 222 if (action != null) { 223 String commandId = action.getActionDefinitionId(); 224 ExternalActionManager.ICallback callback = ExternalActionManager 225 .getInstance().getCallback(); 226 227 if ((callback != null) && (commandId != null)) { 228 callback.addPropertyChangeListener(commandId, 229 actionTextListener); 230 } 231 } 232 } 233 } 234 235 243 public void fill(Menu parent, int index) { 244 if (widget == null && parent != null) { 245 Menu subMenu = null; 246 int flags = SWT.PUSH; 247 if (action != null) { 248 int style = action.getStyle(); 249 if (style == IAction.AS_CHECK_BOX) { 250 flags = SWT.CHECK; 251 } else if (style == IAction.AS_RADIO_BUTTON) { 252 flags = SWT.RADIO; 253 } else if (style == IAction.AS_DROP_DOWN_MENU) { 254 IMenuCreator mc = action.getMenuCreator(); 255 if (mc != null) { 256 subMenu = mc.getMenu(parent); 257 flags = SWT.CASCADE; 258 } 259 } 260 } 261 262 MenuItem mi = null; 263 if (index >= 0) { 264 mi = new MenuItem(parent, flags, index); 265 } else { 266 mi = new MenuItem(parent, flags); 267 } 268 widget = mi; 269 270 mi.setData(this); 271 mi.addListener(SWT.Dispose, getMenuItemListener()); 272 mi.addListener(SWT.Selection, getMenuItemListener()); 273 if (action.getHelpListener() != null) { 274 mi.addHelpListener(action.getHelpListener()); 275 } 276 277 if (subMenu != null) { 278 mi.setMenu(subMenu); 279 } 280 281 update(null); 282 283 action.addPropertyChangeListener(propertyListener); 285 if (action != null) { 286 String commandId = action.getActionDefinitionId(); 287 ExternalActionManager.ICallback callback = ExternalActionManager 288 .getInstance().getCallback(); 289 290 if ((callback != null) && (commandId != null)) { 291 callback.addPropertyChangeListener(commandId, 292 actionTextListener); 293 } 294 } 295 } 296 } 297 298 306 public void fill(ToolBar parent, int index) { 307 if (widget == null && parent != null) { 308 int flags = SWT.PUSH; 309 if (action != null) { 310 int style = action.getStyle(); 311 if (style == IAction.AS_CHECK_BOX) { 312 flags = SWT.CHECK; 313 } else if (style == IAction.AS_RADIO_BUTTON) { 314 flags = SWT.RADIO; 315 } else if (style == IAction.AS_DROP_DOWN_MENU) { 316 flags = SWT.DROP_DOWN; 317 } 318 } 319 320 ToolItem ti = null; 321 if (index >= 0) { 322 ti = new ToolItem(parent, flags, index); 323 } else { 324 ti = new ToolItem(parent, flags); 325 } 326 ti.setData(this); 327 ti.addListener(SWT.Selection, getToolItemListener()); 328 ti.addListener(SWT.Dispose, getToolItemListener()); 329 330 widget = ti; 331 332 update(null); 333 334 action.addPropertyChangeListener(propertyListener); 336 if (action != null) { 337 String commandId = action.getActionDefinitionId(); 338 ExternalActionManager.ICallback callback = ExternalActionManager 339 .getInstance().getCallback(); 340 341 if ((callback != null) && (commandId != null)) { 342 callback.addPropertyChangeListener(commandId, 343 actionTextListener); 344 } 345 } 346 } 347 } 348 349 354 public IAction getAction() { 355 return action; 356 } 357 358 363 private Listener getButtonListener() { 364 if (buttonListener == null) { 365 buttonListener = new Listener() { 366 public void handleEvent(Event event) { 367 switch (event.type) { 368 case SWT.Dispose: 369 handleWidgetDispose(event); 370 break; 371 case SWT.Selection: 372 Widget ew = event.widget; 373 if (ew != null) { 374 handleWidgetSelection(event, ((Button) ew) 375 .getSelection()); 376 } 377 break; 378 } 379 } 380 }; 381 } 382 return buttonListener; 383 } 384 385 390 private Listener getMenuItemListener() { 391 if (menuItemListener == null) { 392 menuItemListener = new Listener() { 393 public void handleEvent(Event event) { 394 switch (event.type) { 395 case SWT.Dispose: 396 handleWidgetDispose(event); 397 break; 398 case SWT.Selection: 399 Widget ew = event.widget; 400 if (ew != null) { 401 handleWidgetSelection(event, ((MenuItem) ew) 402 .getSelection()); 403 } 404 break; 405 } 406 } 407 }; 408 } 409 return menuItemListener; 410 } 411 412 422 public int getMode() { 423 return mode; 424 } 425 426 431 private Listener getToolItemListener() { 432 if (toolItemListener == null) { 433 toolItemListener = new Listener() { 434 public void handleEvent(Event event) { 435 switch (event.type) { 436 case SWT.Dispose: 437 handleWidgetDispose(event); 438 break; 439 case SWT.Selection: 440 Widget ew = event.widget; 441 if (ew != null) { 442 handleWidgetSelection(event, ((ToolItem) ew) 443 .getSelection()); 444 } 445 break; 446 } 447 } 448 }; 449 } 450 return toolItemListener; 451 } 452 453 456 private void handleWidgetDispose(Event e) { 457 if (e.widget == widget) { 459 if (action.getStyle() == IAction.AS_DROP_DOWN_MENU) { 461 IMenuCreator mc = action.getMenuCreator(); 462 if (mc != null) { 463 mc.dispose(); 464 } 465 } 466 467 action.removePropertyChangeListener(propertyListener); 469 if (action != null) { 470 String commandId = action.getActionDefinitionId(); 471 ExternalActionManager.ICallback callback = ExternalActionManager 472 .getInstance().getCallback(); 473 474 if ((callback != null) && (commandId != null)) { 475 callback.removePropertyChangeListener(commandId, 476 actionTextListener); 477 } 478 } 479 480 widget = null; 482 483 disposeOldImages(); 484 } 485 } 486 487 490 private void handleWidgetSelection(Event e, boolean selection) { 491 492 Widget item = e.widget; 493 if (item != null) { 494 int style = item.getStyle(); 495 496 if ((style & (SWT.TOGGLE | SWT.CHECK)) != 0) { 497 if (action.getStyle() == IAction.AS_CHECK_BOX) { 498 action.setChecked(selection); 499 } 500 } else if ((style & SWT.RADIO) != 0) { 501 if (action.getStyle() == IAction.AS_RADIO_BUTTON) { 502 action.setChecked(selection); 503 } 504 } else if ((style & SWT.DROP_DOWN) != 0) { 505 if (e.detail == 4) { if (action.getStyle() == IAction.AS_DROP_DOWN_MENU) { 507 IMenuCreator mc = action.getMenuCreator(); 508 ToolItem ti = (ToolItem) item; 509 if (mc != null) { 518 Menu m = mc.getMenu(ti.getParent()); 519 if (m != null) { 520 Rectangle b = ti.getBounds(); 522 Point p = ti.getParent().toDisplay( 523 new Point(b.x, b.y + b.height)); 524 m.setLocation(p.x, p.y); m.setVisible(true); 527 return; } 529 } 530 } 531 } 532 } 533 534 if (action.isEnabled()) { 538 boolean trace = Policy.TRACE_ACTIONS; 539 540 long ms = 0L; 541 if (trace) { 542 ms = System.currentTimeMillis(); 543 System.out.println("Running action: " + action.getText()); } 545 546 action.runWithEvent(e); 547 548 if (trace) { 549 System.out.println((System.currentTimeMillis() - ms) 550 + " ms to run action: " + action.getText()); } 552 } 553 } 554 } 555 556 559 public int hashCode() { 560 return action.hashCode(); 561 } 562 563 571 private boolean hasImages(IAction actionToCheck) { 572 return actionToCheck.getImageDescriptor() != null 573 || actionToCheck.getHoverImageDescriptor() != null 574 || actionToCheck.getDisabledImageDescriptor() != null; 575 } 576 577 580 private boolean isCommandActive() { 581 IAction actionToCheck = getAction(); 582 583 if (actionToCheck != null) { 584 String commandId = actionToCheck.getActionDefinitionId(); 585 ExternalActionManager.ICallback callback = ExternalActionManager 586 .getInstance().getCallback(); 587 588 if (callback != null) { 589 return callback.isActive(commandId); 590 } 591 } 592 return true; 593 } 594 595 600 public boolean isDynamic() { 601 if (widget instanceof MenuItem) { 602 boolean itemIsCheck = (widget.getStyle() & SWT.CHECK) != 0; 605 boolean actionIsCheck = getAction() != null 606 && getAction().getStyle() == IAction.AS_CHECK_BOX; 607 boolean itemIsRadio = (widget.getStyle() & SWT.RADIO) != 0; 608 boolean actionIsRadio = getAction() != null 609 && getAction().getStyle() == IAction.AS_RADIO_BUTTON; 610 return (itemIsCheck != actionIsCheck) 611 || (itemIsRadio != actionIsRadio); 612 } 613 return false; 614 } 615 616 619 public boolean isEnabled() { 620 return action != null && action.isEnabled(); 621 } 622 623 630 protected boolean isEnabledAllowed() { 631 if (getParent() == null) { 632 return true; 633 } 634 Boolean value = getParent().getOverrides().getEnabled(this); 635 return (value == null) ? true : value.booleanValue(); 636 } 637 638 644 public boolean isVisible() { 645 return super.isVisible() && isCommandActive(); 646 } 647 648 657 public void setMode(int mode) { 658 this.mode = mode; 659 update(); 660 } 661 662 666 public final void update() { 667 update(null); 668 } 669 670 677 public void update(String propertyName) { 678 if (widget != null) { 679 boolean textChanged = propertyName == null 681 || propertyName.equals(IAction.TEXT); 682 boolean imageChanged = propertyName == null 683 || propertyName.equals(IAction.IMAGE); 684 boolean tooltipTextChanged = propertyName == null 685 || propertyName.equals(IAction.TOOL_TIP_TEXT); 686 boolean enableStateChanged = propertyName == null 687 || propertyName.equals(IAction.ENABLED) 688 || propertyName 689 .equals(IContributionManagerOverrides.P_ENABLED); 690 boolean checkChanged = (action.getStyle() == IAction.AS_CHECK_BOX || action 691 .getStyle() == IAction.AS_RADIO_BUTTON) 692 && (propertyName == null || propertyName 693 .equals(IAction.CHECKED)); 694 695 if (widget instanceof ToolItem) { 696 ToolItem ti = (ToolItem) widget; 697 String text = action.getText(); 698 boolean showText = text != null 701 && ((getMode() & MODE_FORCE_TEXT) != 0 || !hasImages(action)); 702 703 if (showText && text != null) { 705 text = Action.removeAcceleratorText(text); 706 text = Action.removeMnemonics(text); 707 } 708 709 if (textChanged) { 710 String textToSet = showText ? text : ""; boolean rightStyle = (ti.getParent().getStyle() & SWT.RIGHT) != 0; 712 if (rightStyle || !ti.getText().equals(textToSet)) { 713 ti.setText(textToSet); 720 } 721 } 722 723 if (imageChanged) { 724 updateImages(!showText); 726 } 727 728 if (tooltipTextChanged || textChanged) { 729 String toolTip = action.getToolTipText(); 730 if ((toolTip == null) || (toolTip.length() == 0)) { 731 toolTip = text; 732 } 733 734 ExternalActionManager.ICallback callback = ExternalActionManager 735 .getInstance().getCallback(); 736 String commandId = action.getActionDefinitionId(); 737 if ((callback != null) && (commandId != null) && (toolTip != null)) { 738 String acceleratorText = callback.getAcceleratorText(commandId); 739 if (acceleratorText != null && acceleratorText.length() != 0) { 740 toolTip = JFaceResources.format( 741 "Toolbar_Tooltip_Accelerator", new Object [] { toolTip, acceleratorText }); 743 } 744 } 745 746 if (!showText || toolTip != null && !toolTip.equals(text)) { 749 ti.setToolTipText(toolTip); 750 } else { 751 ti.setToolTipText(null); 752 } 753 } 754 755 if (enableStateChanged) { 756 boolean shouldBeEnabled = action.isEnabled() 757 && isEnabledAllowed(); 758 759 if (ti.getEnabled() != shouldBeEnabled) { 760 ti.setEnabled(shouldBeEnabled); 761 } 762 } 763 764 if (checkChanged) { 765 boolean bv = action.isChecked(); 766 767 if (ti.getSelection() != bv) { 768 ti.setSelection(bv); 769 } 770 } 771 return; 772 } 773 774 if (widget instanceof MenuItem) { 775 MenuItem mi = (MenuItem) widget; 776 777 if (textChanged) { 778 int accelerator = 0; 779 String acceleratorText = null; 780 IAction updatedAction = getAction(); 781 String text = null; 782 accelerator = updatedAction.getAccelerator(); 783 ExternalActionManager.ICallback callback = ExternalActionManager 784 .getInstance().getCallback(); 785 786 if ((accelerator != 0) && (callback != null) 788 && (callback.isAcceleratorInUse(accelerator))) { 789 accelerator = 0; 790 } 791 792 798 final String commandId = updatedAction 799 .getActionDefinitionId(); 800 if (("gtk".equals(SWT.getPlatform())) && (callback instanceof IBindingManagerCallback) && (commandId != null)) { 802 final IBindingManagerCallback bindingManagerCallback = (IBindingManagerCallback) callback; 803 final IKeyLookup lookup = KeyLookupFactory.getDefault(); 804 final TriggerSequence[] triggerSequences = bindingManagerCallback 805 .getActiveBindingsFor(commandId); 806 for (int i = 0; i < triggerSequences.length; i++) { 807 final TriggerSequence triggerSequence = triggerSequences[i]; 808 final Trigger[] triggers = triggerSequence 809 .getTriggers(); 810 if (triggers.length == 1) { 811 final Trigger trigger = triggers[0]; 812 if (trigger instanceof KeyStroke) { 813 final KeyStroke currentKeyStroke = (KeyStroke) trigger; 814 final int currentNaturalKey = currentKeyStroke 815 .getNaturalKey(); 816 if ((currentKeyStroke.getModifierKeys() == (lookup 817 .getCtrl() | lookup.getShift())) 818 && ((currentNaturalKey >= '0' && currentNaturalKey <= '9') 819 || (currentNaturalKey >= 'A' && currentNaturalKey <= 'F') || (currentNaturalKey == 'U'))) { 820 accelerator = currentKeyStroke 821 .getModifierKeys() 822 | currentNaturalKey; 823 acceleratorText = triggerSequence 824 .format(); 825 break; 826 } 827 } 828 } 829 } 830 } 831 832 if (accelerator == 0) { 833 if ((callback != null) && (commandId != null)) { 834 acceleratorText = callback 835 .getAcceleratorText(commandId); 836 } 837 } 838 839 IContributionManagerOverrides overrides = null; 840 841 if (getParent() != null) { 842 overrides = getParent().getOverrides(); 843 } 844 845 if (overrides != null) { 846 text = getParent().getOverrides().getText(this); 847 } 848 849 mi.setAccelerator(accelerator); 850 851 if (text == null) { 852 text = updatedAction.getText(); 853 } 854 855 if (text != null && acceleratorText == null) { 856 acceleratorText = LegacyActionTools.extractAcceleratorText(text); 858 if (acceleratorText == null && accelerator != 0) { 859 acceleratorText= Action.convertAccelerator(accelerator); 860 } 861 } 862 863 if (text == null) { 864 text = ""; } else { 866 text = Action.removeAcceleratorText(text); 867 } 868 869 if (acceleratorText == null) { 870 mi.setText(text); 871 } else { 872 mi.setText(text + '\t' + acceleratorText); 873 } 874 } 875 876 if (imageChanged) { 877 updateImages(false); 878 } 879 880 if (enableStateChanged) { 881 boolean shouldBeEnabled = action.isEnabled() 882 && isEnabledAllowed(); 883 884 if (mi.getEnabled() != shouldBeEnabled) { 885 mi.setEnabled(shouldBeEnabled); 886 } 887 } 888 889 if (checkChanged) { 890 boolean bv = action.isChecked(); 891 892 if (mi.getSelection() != bv) { 893 mi.setSelection(bv); 894 } 895 } 896 897 return; 898 } 899 900 if (widget instanceof Button) { 901 Button button = (Button) widget; 902 903 if (imageChanged && updateImages(false)) { 904 textChanged = false; } 907 908 if (textChanged) { 909 String text = action.getText(); 910 if (text == null) { 911 text = ""; } else { 913 text = Action.removeAcceleratorText(text); 914 } 915 button.setText(text); 916 } 917 918 if (tooltipTextChanged) { 919 button.setToolTipText(action.getToolTipText()); 920 } 921 922 if (enableStateChanged) { 923 boolean shouldBeEnabled = action.isEnabled() 924 && isEnabledAllowed(); 925 926 if (button.getEnabled() != shouldBeEnabled) { 927 button.setEnabled(shouldBeEnabled); 928 } 929 } 930 931 if (checkChanged) { 932 boolean bv = action.isChecked(); 933 934 if (button.getSelection() != bv) { 935 button.setSelection(bv); 936 } 937 } 938 return; 939 } 940 } 941 } 942 943 953 private boolean updateImages(boolean forceImage) { 954 955 ResourceManager parentResourceManager = JFaceResources.getResources(); 956 957 if (widget instanceof ToolItem) { 958 if (USE_COLOR_ICONS) { 959 ImageDescriptor image = action.getHoverImageDescriptor(); 960 if (image == null) { 961 image = action.getImageDescriptor(); 962 } 963 ImageDescriptor disabledImage = action 964 .getDisabledImageDescriptor(); 965 966 if (image == null && forceImage) { 968 image = ImageDescriptor.getMissingImageDescriptor(); 969 } 970 971 LocalResourceManager localManager = new LocalResourceManager( 972 parentResourceManager); 973 974 ((ToolItem) widget) 977 .setDisabledImage(disabledImage == null ? null 978 : localManager 979 .createImageWithDefault(disabledImage)); 980 ((ToolItem) widget).setImage(image == null ? null 981 : localManager.createImageWithDefault(image)); 982 983 disposeOldImages(); 984 imageManager = localManager; 985 986 return image != null; 987 } 988 ImageDescriptor image = action.getImageDescriptor(); 989 ImageDescriptor hoverImage = action.getHoverImageDescriptor(); 990 ImageDescriptor disabledImage = action.getDisabledImageDescriptor(); 991 992 if (image == null && hoverImage != null) { 995 image = ImageDescriptor.createWithFlags(action 996 .getHoverImageDescriptor(), SWT.IMAGE_GRAY); 997 } else { 998 if (hoverImage == null && image != null) { 1002 hoverImage = image; 1003 image = ImageDescriptor.createWithFlags(action 1004 .getImageDescriptor(), SWT.IMAGE_GRAY); 1005 } 1006 } 1007 1008 if (hoverImage == null && image == null && forceImage) { 1010 image = ImageDescriptor.getMissingImageDescriptor(); 1011 } 1012 1013 LocalResourceManager localManager = new LocalResourceManager( 1016 parentResourceManager); 1017 1018 ((ToolItem) widget).setDisabledImage(disabledImage == null ? null 1021 : localManager.createImageWithDefault(disabledImage)); 1022 ((ToolItem) widget).setHotImage(hoverImage == null ? null 1023 : localManager.createImageWithDefault(hoverImage)); 1024 ((ToolItem) widget).setImage(image == null ? null : localManager 1025 .createImageWithDefault(image)); 1026 1027 disposeOldImages(); 1030 imageManager = localManager; 1031 1032 return image != null; 1033 } else if (widget instanceof Item || widget instanceof Button) { 1034 1035 ImageDescriptor image = action.getHoverImageDescriptor(); 1037 if (image == null) { 1038 image = action.getImageDescriptor(); 1039 } 1040 if (image == null && forceImage) { 1042 image = ImageDescriptor.getMissingImageDescriptor(); 1043 } 1044 1045 LocalResourceManager localManager = new LocalResourceManager( 1048 parentResourceManager); 1049 1050 if (widget instanceof Item) { 1051 ((Item) widget).setImage(image == null ? null : localManager 1052 .createImageWithDefault(image)); 1053 } else if (widget instanceof Button) { 1054 ((Button) widget).setImage(image == null ? null : localManager 1055 .createImageWithDefault(image)); 1056 } 1057 1058 disposeOldImages(); 1061 imageManager = localManager; 1062 1063 return image != null; 1064 } 1065 return false; 1066 } 1067 1068 1071 private void disposeOldImages() { 1072 if (imageManager != null) { 1073 imageManager.dispose(); 1074 imageManager = null; 1075 } 1076 } 1077 1078 1091 protected String shortenText(String textValue, ToolItem item) { 1092 if (textValue == null) { 1093 return null; 1094 } 1095 1096 GC gc = new GC(item.getParent()); 1097 1098 int maxWidth = item.getImage().getBounds().width * 4; 1099 1100 if (gc.textExtent(textValue).x < maxWidth) { 1101 gc.dispose(); 1102 return textValue; 1103 } 1104 1105 for (int i = textValue.length(); i > 0; i--) { 1106 String test = textValue.substring(0, i); 1107 test = test + ellipsis; 1108 if (gc.textExtent(test).x < maxWidth) { 1109 gc.dispose(); 1110 return test; 1111 } 1112 1113 } 1114 gc.dispose(); 1115 return textValue; 1117 } 1118} 1119 | Popular Tags |