1 11 package org.eclipse.ui.internal.help; 12 13 import java.net.URL ; 14 15 import org.eclipse.core.runtime.Assert; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IConfigurationElement; 18 import org.eclipse.core.runtime.IExtension; 19 import org.eclipse.core.runtime.IExtensionPoint; 20 import org.eclipse.core.runtime.Platform; 21 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; 22 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 23 import org.eclipse.help.HelpSystem; 24 import org.eclipse.help.IContext; 25 import org.eclipse.help.IContext2; 26 import org.eclipse.help.IHelp; 27 import org.eclipse.help.IHelpResource; 28 import org.eclipse.help.IToc; 29 import org.eclipse.jface.action.IAction; 30 import org.eclipse.swt.custom.BusyIndicator; 31 import org.eclipse.swt.events.HelpEvent; 32 import org.eclipse.swt.events.HelpListener; 33 import org.eclipse.swt.graphics.Point; 34 import org.eclipse.swt.widgets.Control; 35 import org.eclipse.swt.widgets.Display; 36 import org.eclipse.swt.widgets.Menu; 37 import org.eclipse.swt.widgets.MenuItem; 38 import org.eclipse.ui.PlatformUI; 39 import org.eclipse.ui.commands.ICommand; 40 import org.eclipse.ui.help.AbstractHelpUI; 41 import org.eclipse.ui.help.IContextComputer; 42 import org.eclipse.ui.help.IWorkbenchHelpSystem; 43 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 44 import org.eclipse.ui.internal.WorkbenchPlugin; 45 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 46 47 53 public final class WorkbenchHelpSystem implements IWorkbenchHelpSystem { 54 55 60 public static final String HELP_KEY = "org.eclipse.ui.help"; 62 65 private static final String HELP_SYSTEM_EXTENSION_ID = PlatformUI.PLUGIN_ID + '.' + IWorkbenchRegistryConstants.PL_HELPSUPPORT; 66 67 70 private static final String HELP_SYSTEM_CLASS_ATTRIBUTE = "class"; 72 75 private static WorkbenchHelpSystem instance; 76 77 80 private static class WorkbenchHelpListener implements HelpListener { 81 public void helpRequested(HelpEvent event) { 82 83 if (getInstance().getHelpUI() == null) { 84 return; 85 } 86 87 Object object = event.widget.getData(HELP_KEY); 89 90 IContext context = null; 94 if (object instanceof String ) { 95 context = HelpSystem.getContext((String ) object); 97 } else if (object instanceof IContext) { 98 context = (IContext) object; 100 } else if (object instanceof IContextComputer) { 101 Object [] helpContexts = ((IContextComputer) object) 103 .computeContexts(event); 104 if (helpContexts != null && helpContexts.length > 0) { 106 Object primaryEntry = helpContexts[0]; 107 if (primaryEntry instanceof String ) { 108 context = HelpSystem.getContext((String ) primaryEntry); 109 } else if (primaryEntry instanceof IContext) { 110 context = (IContext) primaryEntry; 111 } 112 } 113 } else if (object instanceof Object []) { 114 Object [] helpContexts = (Object []) object; 117 if (helpContexts.length > 0) { 119 Object primaryEntry = helpContexts[0]; 120 if (primaryEntry instanceof String ) { 121 context = HelpSystem.getContext((String ) primaryEntry); 122 } else if (primaryEntry instanceof IContext) { 123 context = (IContext) primaryEntry; 124 } 125 } 126 } 127 128 131 if (context == null) { 132 context = HelpSystem.getContext(IWorkbenchHelpContextIds.MISSING); 133 } 134 135 if (context != null) { 136 Point point = computePopUpLocation(event.widget.getDisplay()); 139 getInstance().displayContext(context, point.x, point.y); 141 } 142 } 143 } 144 145 148 private boolean isInitialized; 149 150 153 private AbstractHelpUI pluggableHelpUI = null; 154 155 159 private String desiredHelpSystemId; 160 161 166 171 private IExtensionChangeHandler handler = new IExtensionChangeHandler() { 172 173 176 public void addExtension(IExtensionTracker tracker,IExtension extension) { 177 } 179 180 183 public void removeExtension(IExtension source, Object [] objects) { 184 for (int i = 0; i < objects.length; i++) { 185 if (objects[i] == pluggableHelpUI) { 186 isInitialized = false; 187 pluggableHelpUI = null; 188 helpCompatibilityWrapper = null; 189 PlatformUI.getWorkbench().getExtensionTracker() 192 .unregisterHandler(handler); 193 } 194 } 195 } 196 }; 197 198 202 private class CompatibilityIHelpImplementation implements IHelp { 203 204 205 public void displayHelp() { 206 AbstractHelpUI helpUI = getHelpUI(); 208 if (helpUI != null) { 209 helpUI.displayHelp(); 210 } 211 } 212 213 214 public void displayContext(IContext context, int x, int y) { 215 AbstractHelpUI helpUI = getHelpUI(); 217 if (helpUI != null) { 218 helpUI.displayContext(context, x, y); 219 } 220 } 221 222 223 public void displayContext(String contextId, int x, int y) { 224 IContext context = HelpSystem.getContext(contextId); 226 if (context != null) { 227 displayContext(context, x, y); 228 } 229 } 230 231 232 public void displayHelpResource(String href) { 233 AbstractHelpUI helpUI = getHelpUI(); 235 if (helpUI != null) { 236 helpUI.displayHelpResource(href); 237 } 238 } 239 240 241 public void displayHelpResource(IHelpResource helpResource) { 242 displayHelpResource(helpResource.getHref()); 244 } 245 246 247 public void displayHelp(String toc) { 248 displayHelpResource(toc); 250 } 251 252 253 public void displayHelp(String toc, String selectedTopic) { 254 displayHelpResource(selectedTopic); 256 } 257 258 259 public void displayHelp(String contextId, int x, int y) { 260 displayContext(contextId, x, y); 262 } 263 264 265 public void displayHelp(IContext context, int x, int y) { 266 displayContext(context, x, y); 268 } 269 270 271 public IContext getContext(String contextId) { 272 return HelpSystem.getContext(contextId); 274 } 275 276 277 public IToc[] getTocs() { 278 return HelpSystem.getTocs(); 280 } 281 282 283 public boolean isContextHelpDisplayed() { 284 return isContextHelpDisplayed(); 286 } 287 } 288 289 294 private static class ContextWithTitle implements IContext2 { 295 private IContext context; 296 private String title; 297 298 ContextWithTitle(IContext context, String title) { 299 this.context = context; 300 this.title = title; 301 } 302 303 public String getTitle() { 304 if (context instanceof IContext2) { 305 String ctitle = ((IContext2)context).getTitle(); 306 if (ctitle!=null) { 307 return ctitle; 308 } 309 } 310 return title; 311 } 312 313 public String getStyledText() { 314 if (context instanceof IContext2) { 315 return ((IContext2)context).getStyledText(); 316 } 317 return context.getText(); 318 } 319 320 public String getCategory(IHelpResource topic) { 321 if (context instanceof IContext2) { 322 return ((IContext2)context).getCategory(topic); 323 } 324 return null; 325 } 326 327 public IHelpResource[] getRelatedTopics() { 328 return context.getRelatedTopics(); 329 } 330 331 public String getText() { 332 return context.getText(); 333 } 334 } 335 336 340 private IHelp helpCompatibilityWrapper = null; 341 342 345 private static HelpListener helpListener; 346 347 352 public String getDesiredHelpSystemId() { 353 return desiredHelpSystemId; 354 } 355 356 361 public void setDesiredHelpSystemId(String desiredHelpSystemId) { 362 dispose(); this.desiredHelpSystemId = desiredHelpSystemId; 364 } 365 366 369 private WorkbenchHelpSystem() { 370 } 371 372 377 public static WorkbenchHelpSystem getInstance() { 378 if (instance == null) { 379 instance = new WorkbenchHelpSystem(); 380 } 381 382 return instance; 383 } 384 385 388 public static void disposeIfNecessary() { 389 if (instance != null) { 390 instance.dispose(); 391 instance = null; 392 } 393 } 394 395 398 public void dispose() { 399 pluggableHelpUI = null; 400 helpCompatibilityWrapper = null; 401 isInitialized = false; 402 PlatformUI.getWorkbench().getExtensionTracker() 403 .unregisterHandler(handler); 404 } 405 406 412 private AbstractHelpUI getHelpUI() { 413 if (!isInitialized) { 414 isInitialized = initializePluggableHelpUI(); 415 } 416 return pluggableHelpUI; 417 } 418 419 423 private boolean initializePluggableHelpUI() { 424 final boolean[] ret = new boolean[] { false }; 425 426 BusyIndicator.showWhile(Display.getCurrent(), new Runnable () { 427 428 433 public void run() { 434 IExtensionPoint point = Platform.getExtensionRegistry() 436 .getExtensionPoint(HELP_SYSTEM_EXTENSION_ID); 437 if (point == null) { 438 return; 441 } 442 IExtension[] extensions = point.getExtensions(); 443 if (extensions.length == 0) { 444 return; 446 } 447 448 IConfigurationElement elementToUse = null; 449 if (desiredHelpSystemId == null) { 450 elementToUse = getFirstElement(extensions); 451 } else { 452 elementToUse = findElement(desiredHelpSystemId, extensions); 453 } 454 455 if (elementToUse != null) { 456 ret[0] = initializePluggableHelpUI(elementToUse); 457 } 458 } 459 460 private IConfigurationElement findElement( 461 String desiredHelpSystemId, IExtension[] extensions) { 462 for (int i = 0; i < extensions.length; i++) { 463 IExtension extension = extensions[i]; 464 if (desiredHelpSystemId.equals(extension.getUniqueIdentifier())) { 465 IConfigurationElement[] elements = extensions[0] 466 .getConfigurationElements(); 467 if (elements.length == 0) { 468 return null; 472 } 473 return elements[0]; 474 } 475 476 } 477 return null; 478 } 479 480 private IConfigurationElement getFirstElement( 481 IExtension[] extensions) { 482 IConfigurationElement[] elements = extensions[0] 485 .getConfigurationElements(); 486 if (elements.length == 0) { 487 return null; 490 } 491 return elements[0]; 492 } 493 494 private boolean initializePluggableHelpUI( 495 IConfigurationElement element) { 496 try { 498 pluggableHelpUI = (AbstractHelpUI) WorkbenchPlugin 499 .createExtension(element, 500 HELP_SYSTEM_CLASS_ATTRIBUTE); 501 PlatformUI.getWorkbench().getExtensionTracker() 503 .registerHandler(handler, null); 504 PlatformUI 506 .getWorkbench() 507 .getExtensionTracker() 508 .registerObject(element.getDeclaringExtension(), 509 pluggableHelpUI, IExtensionTracker.REF_WEAK); 510 return true; 511 } catch (CoreException e) { 512 WorkbenchPlugin.log( 513 "Unable to instantiate help UI" + e.getStatus(), e); } 515 return false; 516 } 517 518 }); 519 return ret[0]; 520 } 521 522 529 private static Point computePopUpLocation(Display display) { 530 Point point = display.getCursorLocation(); 531 return new Point(point.x + 15, point.y); 532 } 533 534 539 private HelpListener getHelpListener() { 540 if (helpListener == null) { 541 helpListener = new WorkbenchHelpListener(); 542 } 543 return helpListener; 544 } 545 546 554 public IHelp getHelpSupport() { 555 AbstractHelpUI helpUI = getHelpUI(); 556 if (helpUI != null && helpCompatibilityWrapper == null) { 557 helpCompatibilityWrapper = new CompatibilityIHelpImplementation(); 559 } 560 return helpCompatibilityWrapper; 561 562 } 563 564 580 public void setHelp(IAction action, final Object [] contexts) { 581 for (int i = 0; i < contexts.length; i++) { 582 Assert.isTrue(contexts[i] instanceof String 583 || contexts[i] instanceof IContext); 584 } 585 action.setHelpListener(new HelpListener() { 586 public void helpRequested(HelpEvent event) { 587 if (contexts != null && contexts.length > 0 588 && getHelpUI() != null) { 589 IContext context = null; 591 if (contexts[0] instanceof String ) { 592 context = HelpSystem.getContext((String ) contexts[0]); 593 } else if (contexts[0] instanceof IContext) { 594 context = (IContext) contexts[0]; 595 } 596 if (context != null) { 597 Point point = computePopUpLocation(event.widget 598 .getDisplay()); 599 displayContext(context, point.x, point.y); 600 } 601 } 602 } 603 }); 604 } 605 606 622 public void setHelp(IAction action, final IContextComputer computer) { 623 action.setHelpListener(new HelpListener() { 624 public void helpRequested(HelpEvent event) { 625 Object [] helpContexts = computer.computeContexts(event); 626 if (helpContexts != null && helpContexts.length > 0 627 && getHelpUI() != null) { 628 IContext context = null; 630 if (helpContexts[0] instanceof String ) { 631 context = HelpSystem 632 .getContext((String ) helpContexts[0]); 633 } else if (helpContexts[0] instanceof IContext) { 634 context = (IContext) helpContexts[0]; 635 } 636 if (context != null) { 637 Point point = computePopUpLocation(event.widget 638 .getDisplay()); 639 displayContext(context, point.x, point.y); 640 } 641 } 642 } 643 }); 644 } 645 646 662 public void setHelp(Control control, Object [] contexts) { 663 for (int i = 0; i < contexts.length; i++) { 664 Assert.isTrue(contexts[i] instanceof String 665 || contexts[i] instanceof IContext); 666 } 667 668 control.setData(HELP_KEY, contexts); 669 control.removeHelpListener(getHelpListener()); 671 control.addHelpListener(getHelpListener()); 672 } 673 674 690 public void setHelp(Control control, IContextComputer computer) { 691 control.setData(HELP_KEY, computer); 692 control.removeHelpListener(getHelpListener()); 694 control.addHelpListener(getHelpListener()); 695 } 696 697 713 public void setHelp(Menu menu, Object [] contexts) { 714 for (int i = 0; i < contexts.length; i++) { 715 Assert.isTrue(contexts[i] instanceof String 716 || contexts[i] instanceof IContext); 717 } 718 menu.setData(HELP_KEY, contexts); 719 menu.removeHelpListener(getHelpListener()); 721 menu.addHelpListener(getHelpListener()); 722 } 723 724 740 public void setHelp(Menu menu, IContextComputer computer) { 741 menu.setData(HELP_KEY, computer); 742 menu.removeHelpListener(getHelpListener()); 744 menu.addHelpListener(getHelpListener()); 745 } 746 747 763 public void setHelp(MenuItem item, Object [] contexts) { 764 for (int i = 0; i < contexts.length; i++) { 765 Assert.isTrue(contexts[i] instanceof String 766 || contexts[i] instanceof IContext); 767 } 768 item.setData(HELP_KEY, contexts); 769 item.removeHelpListener(getHelpListener()); 771 item.addHelpListener(getHelpListener()); 772 } 773 774 790 public void setHelp(MenuItem item, IContextComputer computer) { 791 item.setData(HELP_KEY, computer); 792 item.removeHelpListener(getHelpListener()); 794 item.addHelpListener(getHelpListener()); 795 } 796 797 807 public HelpListener createHelpListener(ICommand command) { 808 final String contextId = ""; return new HelpListener() { 812 public void helpRequested(HelpEvent event) { 813 if (getHelpUI() != null) { 814 IContext context = HelpSystem.getContext(contextId); 815 if (context != null) { 816 Point point = computePopUpLocation(event.widget 817 .getDisplay()); 818 displayContext(context, point.x, point.y); 819 } 820 } 821 } 822 }; 823 } 824 825 830 public void displayHelp() { 831 AbstractHelpUI helpUI = getHelpUI(); 832 if (helpUI != null) { 833 helpUI.displayHelp(); 834 } 835 } 836 837 842 public void displaySearch() { 843 AbstractHelpUI helpUI = getHelpUI(); 844 if (helpUI != null) { 845 helpUI.displaySearch(); 846 } 847 } 848 849 854 public void displayDynamicHelp() { 855 AbstractHelpUI helpUI = getHelpUI(); 856 if (helpUI != null) { 857 helpUI.displayDynamicHelp(); 858 } 859 } 860 861 866 public void search(String expression) { 867 AbstractHelpUI helpUI = getHelpUI(); 868 if (helpUI != null) { 869 helpUI.search(expression); 870 } 871 } 872 873 876 public URL resolve(String href, boolean documentOnly) { 877 AbstractHelpUI helpUI = getHelpUI(); 878 if (helpUI != null) { 879 return helpUI.resolve(href, documentOnly); 880 } 881 return null; 882 } 883 884 890 public void displayContext(IContext context, int x, int y) { 891 if (context == null) { 892 throw new IllegalArgumentException (); 893 } 894 AbstractHelpUI helpUI = getHelpUI(); 895 if (helpUI != null) { 896 helpUI.displayContext(context, x, y); 897 } 898 } 899 900 905 public void displayHelpResource(String href) { 906 if (href == null) { 907 throw new IllegalArgumentException (); 908 } 909 AbstractHelpUI helpUI = getHelpUI(); 910 if (helpUI != null) { 911 helpUI.displayHelpResource(href); 912 } 913 } 914 915 920 public void displayHelp(String contextId) { 921 IContext context = HelpSystem.getContext(contextId); 922 if (context != null) { 923 Point point = computePopUpLocation(Display.getCurrent()); 924 displayContext(context, point.x, point.y); 925 } 926 } 927 928 933 public void displayHelp(IContext context) { 934 Point point = computePopUpLocation(Display.getCurrent()); 935 AbstractHelpUI helpUI = getHelpUI(); 936 if (helpUI != null) { 937 helpUI.displayContext(context, point.x, point.y); 938 } 939 } 940 941 946 public boolean isContextHelpDisplayed() { 947 if (!isInitialized) { 948 return false; 949 } 950 AbstractHelpUI helpUI = getHelpUI(); 951 return helpUI != null && helpUI.isContextHelpDisplayed(); 952 } 953 954 960 public void setHelp(final IAction action, final String contextId) { 961 action.setHelpListener(new HelpListener() { 962 public void helpRequested(HelpEvent event) { 963 if (getHelpUI() != null) { 964 IContext context = HelpSystem.getContext(contextId); 965 if (context != null) { 966 Point point = computePopUpLocation(event.widget 967 .getDisplay()); 968 displayContext(new ContextWithTitle(context, action.getText()), point.x, point.y); 969 } 970 } 971 } 972 }); 973 } 974 975 981 public void setHelp(Control control, String contextId) { 982 control.setData(HELP_KEY, contextId); 983 control.removeHelpListener(getHelpListener()); 985 control.addHelpListener(getHelpListener()); 986 } 987 988 994 public void setHelp(Menu menu, String contextId) { 995 menu.setData(HELP_KEY, contextId); 996 menu.removeHelpListener(getHelpListener()); 998 menu.addHelpListener(getHelpListener()); 999 } 1000 1001 1007 public void setHelp(MenuItem item, String contextId) { 1008 item.setData(HELP_KEY, contextId); 1009 item.removeHelpListener(getHelpListener()); 1011 item.addHelpListener(getHelpListener()); 1012 } 1013 1014 1017 public boolean hasHelpUI() { 1018 return getHelpUI() != null; 1019 } 1020} 1021 | Popular Tags |