1 10 package org.eclipse.help.ui.internal; 11 12 import java.net.URL ; 13 14 import org.eclipse.core.runtime.Platform; 15 import org.eclipse.core.runtime.Preferences; 16 import org.eclipse.help.IContext; 17 import org.eclipse.help.IHelpResource; 18 import org.eclipse.help.browser.IBrowser; 19 import org.eclipse.help.internal.base.BaseHelpSystem; 20 import org.eclipse.help.internal.base.HelpBasePlugin; 21 import org.eclipse.help.internal.base.IHelpBaseConstants; 22 import org.eclipse.help.ui.internal.util.ErrorUtil; 23 import org.eclipse.help.ui.internal.views.ContextHelpPart; 24 import org.eclipse.help.ui.internal.views.HelpTray; 25 import org.eclipse.help.ui.internal.views.HelpView; 26 import org.eclipse.help.ui.internal.views.ReusableHelpPart; 27 import org.eclipse.jface.dialogs.DialogTray; 28 import org.eclipse.jface.dialogs.MessageDialog; 29 import org.eclipse.jface.dialogs.TrayDialog; 30 import org.eclipse.osgi.service.environment.Constants; 31 import org.eclipse.swt.SWT; 32 import org.eclipse.swt.widgets.Control; 33 import org.eclipse.swt.widgets.Display; 34 import org.eclipse.swt.widgets.Shell; 35 import org.eclipse.ui.IViewPart; 36 import org.eclipse.ui.IWorkbenchPage; 37 import org.eclipse.ui.IWorkbenchPart; 38 import org.eclipse.ui.IWorkbenchWindow; 39 import org.eclipse.ui.PartInitException; 40 import org.eclipse.ui.PlatformUI; 41 import org.eclipse.ui.browser.IWebBrowser; 42 import org.eclipse.ui.browser.IWorkbenchBrowserSupport; 43 import org.eclipse.ui.help.AbstractHelpUI; 44 import org.eclipse.ui.intro.IIntroManager; 45 import org.eclipse.ui.intro.IIntroPart; 46 47 56 public class DefaultHelpUI extends AbstractHelpUI { 57 58 private ContextHelpDialog f1Dialog = null; 59 private static DefaultHelpUI instance; 60 61 private static final String HELP_VIEW_ID = "org.eclipse.help.ui.HelpView"; 63 class ExternalWorkbenchBrowser implements IBrowser { 64 65 public ExternalWorkbenchBrowser() { 66 } 67 68 private IWebBrowser getExternalBrowser() throws PartInitException { 69 IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport(); 70 return support.getExternalBrowser(); 71 } 72 73 public void close() { 74 } 75 76 public boolean isCloseSupported() { 77 return false; 78 } 79 80 public void displayURL(String url) throws Exception { 81 try { 82 IWebBrowser browser = getExternalBrowser(); 83 if (browser != null) { 84 browser.openURL(new URL (url)); 85 } 86 } catch (PartInitException pie) { 87 ErrorUtil.displayErrorDialog(pie.getLocalizedMessage()); 88 } 89 } 90 91 public boolean isSetLocationSupported() { 92 return false; 93 } 94 95 public boolean isSetSizeSupported() { 96 return false; 97 } 98 99 public void setLocation(int x, int y) { 100 } 101 102 public void setSize(int width, int height) { 103 } 104 } 105 106 109 public DefaultHelpUI() { 110 super(); 111 instance = this; 112 BaseHelpSystem.getInstance().setBrowserInstance(new ExternalWorkbenchBrowser()); 115 } 116 117 static DefaultHelpUI getInstance() { 118 return instance; 119 } 120 121 124 public void displayHelp() { 125 BaseHelpSystem.getHelpDisplay().displayHelp(useExternalBrowser(null)); 126 } 127 128 131 public void displaySearch() { 132 search(null); 133 } 134 135 138 public void displayDynamicHelp() { 139 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 140 Shell activeShell = getActiveShell(); 141 if (window != null && isActiveShell(activeShell, window)) { 142 IIntroManager introMng = PlatformUI.getWorkbench().getIntroManager(); 143 IIntroPart intro = introMng.getIntro(); 144 if (intro != null && !introMng.isIntroStandby(intro)) 145 introMng.setIntroStandby(intro, true); 146 147 IWorkbenchPage page = window.getActivePage(); 148 Control c = activeShell.getDisplay().getFocusControl(); 149 if (page != null) { 150 IWorkbenchPart activePart = page.getActivePart(); 151 try { 152 IViewPart part = page.showView(HELP_VIEW_ID, null, IWorkbenchPage.VIEW_VISIBLE); 153 if (part != null) { 154 HelpView view = (HelpView) part; 155 view.showDynamicHelp(activePart, c); 156 } 157 } catch (PartInitException e) { 158 } 159 } else { 160 if (activeShell != null) { 162 Object data = activeShell.getData(); 163 if (data instanceof TrayDialog) { 164 IContext context = ContextHelpPart.findHelpContext(c); 165 displayContextAsHelpTray(activeShell, context); 166 return; 167 } 168 } 169 warnNoOpenPerspective(window); 170 } 171 } 172 } 173 174 177 178 public void search(final String expression) { 179 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 180 Shell activeShell = getActiveShell(); 181 if (window != null && isActiveShell(activeShell, window)) { 182 IIntroManager introMng = PlatformUI.getWorkbench().getIntroManager(); 183 IIntroPart intro = introMng.getIntro(); 184 if (intro != null && !introMng.isIntroStandby(intro)) 185 introMng.setIntroStandby(intro, true); 186 187 IWorkbenchPage page = window.getActivePage(); 188 if (page != null) { 189 try { 190 IViewPart part = page.showView(HELP_VIEW_ID); 191 if (part != null) { 192 HelpView view = (HelpView) part; 193 view.startSearch(expression); 194 } 195 } catch (PartInitException e) { 196 } 197 } else { 198 if (activeShell != null) { 200 Object data = activeShell.getData(); 201 if (data instanceof TrayDialog) { 202 displayContextAsHelpTray(activeShell, null); 203 return; 204 } 205 else { 206 return; 209 } 210 } 211 warnNoOpenPerspective(window); 212 } 213 } 214 } 215 216 private void warnNoOpenPerspective(IWorkbenchWindow window) { 217 MessageDialog.openInformation(window.getShell(), Messages.DefaultHelpUI_wtitle, 218 Messages.DefaultHelpUI_noPerspMessage); 219 } 220 221 231 public void displayHelpResource(String href) { 232 BaseHelpSystem.getHelpDisplay().displayHelpResource(href, useExternalBrowser(href)); 233 } 234 235 245 public void displayContext(IContext context, int x, int y) { 246 displayContext(context, x, y, false); 247 } 248 249 void displayContext(IContext context, int x, int y, boolean noInfopop) { 250 if (context == null) 251 return; 252 Preferences pref = HelpBasePlugin.getDefault().getPluginPreferences(); 253 boolean winfopop = pref.getBoolean(IHelpBaseConstants.P_KEY_WINDOW_INFOPOP); 254 boolean dinfopop = pref.getBoolean(IHelpBaseConstants.P_KEY_DIALOG_INFOPOP); 255 256 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 257 Shell activeShell = getActiveShell(); 258 if (window != null && isActiveShell(activeShell, window)) { 259 IWorkbenchPage page = window.getActivePage(); 260 if (page != null) { 261 if (!noInfopop && winfopop) { 262 displayContextAsInfopop(context, x, y); 263 return; 264 } 265 try { 266 270 IHelpResource[] topics = context.getRelatedTopics(); 271 boolean openInEditor = pref.getBoolean(IHelpBaseConstants.P_KEY_OPEN_IN_EDITOR); 272 if (context.getText() == null && topics.length == 1 && openInEditor) { 273 showInWorkbenchBrowser(topics[0].getHref(), true); 274 } 275 else { 276 IWorkbenchPart activePart = page.getActivePart(); 277 Control c = window.getShell().getDisplay().getFocusControl(); 278 IViewPart part = page.showView(HELP_VIEW_ID, null, IWorkbenchPage.VIEW_VISIBLE); 279 if (part != null) { 280 HelpView view = (HelpView) part; 281 view.displayContext(context, activePart, c); 282 } 283 } 284 return; 285 } catch (PartInitException e) { 286 } 290 } 291 } 292 if (HelpTray.isAppropriateFor(activeShell) && (!dinfopop || noInfopop)) { 294 displayContextAsHelpTray(activeShell, context); 295 return; 296 } 297 displayContextAsInfopop(context, x, y); 299 } 300 301 306 public URL resolve(String href, boolean documentOnly) { 307 return BaseHelpSystem.resolve(href, documentOnly); 308 } 309 310 public String unresolve(URL url) { 311 return BaseHelpSystem.unresolve(url); 312 } 313 314 319 private static Shell getActiveShell() { 320 Display display = PlatformUI.getWorkbench().getDisplay(); 321 return display.getActiveShell(); 322 } 323 324 static boolean isActiveShell(Shell activeShell, IWorkbenchWindow window) { 325 return activeShell != null && activeShell.equals(window.getShell()); 327 } 328 329 private void displayContextAsInfopop(IContext context, int x, int y) { 330 if (f1Dialog != null) { 331 f1Dialog.close(); 332 } 333 if (context != null) { 334 338 IHelpResource[] topics = context.getRelatedTopics(); 339 if (context.getText() == null && topics.length == 1) { 340 try { 341 PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(topics[0].getHref()); 342 } 343 catch (Exception e) { 344 } 346 } 347 else { 348 f1Dialog = new ContextHelpDialog(context, x, y); 349 f1Dialog.open(); 350 } 351 } 352 } 353 354 private void displayContextAsHelpTray(Shell activeShell, IContext context) { 355 Control controlInFocus = activeShell.getDisplay().getFocusControl(); 356 TrayDialog dialog = (TrayDialog)activeShell.getData(); 357 358 DialogTray tray = dialog.getTray(); 359 if (tray == null) { 360 tray = new HelpTray(); 361 dialog.openTray(tray); 362 } 363 if (tray instanceof HelpTray) { 364 ReusableHelpPart helpPart = ((HelpTray)tray).getHelpPart(); 365 if (context != null) { 366 IHelpResource[] topics = context.getRelatedTopics(); 367 if (context.getText() == null && topics.length == 1) { 368 helpPart.showURL(topics[0].getHref()); 369 helpPart.update(context, null, controlInFocus); 370 } 371 else { 372 helpPart.showPage(IHelpUIConstants.HV_CONTEXT_HELP_PAGE); 373 helpPart.update(context, null, controlInFocus); 374 } 375 } 376 else { 377 helpPart.showPage(IHelpUIConstants.HV_FSEARCH_PAGE, true); 378 } 379 } 380 else { 381 } 383 } 384 385 389 public boolean isContextHelpDisplayed() { 390 if (f1Dialog == null) { 391 return false; 392 } 393 return f1Dialog.isShowing(); 394 } 395 396 private boolean useExternalBrowser(String url) { 397 if (!Constants.OS_WIN32.equalsIgnoreCase(Platform.getOS())) { 399 Display display = Display.getCurrent(); 400 if (display != null) { 401 if (insideModalParent(display)) 402 return true; 403 } 404 } 405 406 if (url != null) { 409 if (url.indexOf("?noframes=true") > 0 || url.indexOf("&noframes=true") > 0) { return true; 412 } 413 } 414 return false; 415 } 416 417 private boolean insideModalParent(Display display) { 418 return isDisplayModal(display.getActiveShell()); 419 } 420 421 public static boolean isDisplayModal(Shell activeShell) { 422 while (activeShell != null) { 423 if ((activeShell.getStyle() & (SWT.APPLICATION_MODAL | SWT.PRIMARY_MODAL | SWT.SYSTEM_MODAL)) > 0) 424 return true; 425 activeShell = (Shell) activeShell.getParent(); 426 } 427 return false; 428 } 429 430 public static boolean showInWorkbenchBrowser(String url, boolean onlyInternal) { 431 IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport(); 432 if (!onlyInternal || support.isInternalWebBrowserAvailable()) { 433 try { 434 IWebBrowser browser = support 435 .createBrowser( 436 IWorkbenchBrowserSupport.AS_EDITOR 437 | IWorkbenchBrowserSupport.NAVIGATION_BAR 438 | IWorkbenchBrowserSupport.STATUS, 439 "org.eclipse.help.ui", Messages.ReusableHelpPart_internalBrowserTitle, url); browser.openURL(BaseHelpSystem.resolve(url, "/help/nftopic")); return true; 442 } catch (PartInitException e) { 443 HelpUIPlugin.logError( 444 Messages.ReusableHelpPart_internalWebBrowserError, e); 445 } 446 } 447 return false; 448 } 449 } | Popular Tags |