1 11 package org.eclipse.swt.browser; 12 13 import org.eclipse.swt.*; 14 import org.eclipse.swt.widgets.*; 15 16 34 35 public class Browser extends Composite { 36 WebBrowser webBrowser; 37 38 static final String PACKAGE_PREFIX = "org.eclipse.swt.browser."; static final String NO_INPUT_METHOD = "org.eclipse.swt.internal.gtk.noInputMethod"; 41 71 public Browser (Composite parent, int style) { 72 super (checkParent (parent), checkStyle (style)); 73 String platform = SWT.getPlatform (); 74 Display display = parent.getDisplay (); 75 if ("gtk".equals (platform)) display.setData (NO_INPUT_METHOD, null); String className = null; 77 if ((style & SWT.MOZILLA) != 0) { 78 className = "org.eclipse.swt.browser.Mozilla"; } else { 80 if ("win32".equals (platform) || "wpf".equals (platform)) { className = "org.eclipse.swt.browser.IE"; } else if ("motif".equals (platform)) { className = "org.eclipse.swt.browser.Mozilla"; } else if ("gtk".equals (platform)) { className = "org.eclipse.swt.browser.Mozilla"; } else if ("carbon".equals (platform)) { className = "org.eclipse.swt.browser.Safari"; } else if ("photon".equals (platform)) { className = "org.eclipse.swt.browser.Voyager"; } else { 91 dispose (); 92 SWT.error (SWT.ERROR_NO_HANDLES); 93 } 94 } 95 96 try { 97 Class clazz = Class.forName (className); 98 webBrowser = (WebBrowser)clazz.newInstance (); 99 } catch (ClassNotFoundException e) { 100 } catch (IllegalAccessException e) { 101 } catch (InstantiationException e) { 102 } 103 if (webBrowser == null) { 104 dispose (); 105 SWT.error (SWT.ERROR_NO_HANDLES); 106 } 107 108 webBrowser.setBrowser (this); 109 webBrowser.create (parent, style); 110 } 111 112 static Composite checkParent (Composite parent) { 113 String platform = SWT.getPlatform (); 114 if (!"gtk".equals (platform)) return parent; 116 122 if (parent != null && !parent.isDisposed ()) { 123 Display display = parent.getDisplay (); 124 if (display != null) { 125 if (display.getThread () == Thread.currentThread ()) { 126 display.setData (NO_INPUT_METHOD, "true"); } 128 } 129 } 130 return parent; 131 } 132 133 static int checkStyle(int style) { 134 String platform = SWT.getPlatform (); 135 if ((style & SWT.MOZILLA) != 0) { 136 if ("carbon".equals (platform)) return style | SWT.EMBEDDED; if ("motif".equals (platform)) return style | SWT.EMBEDDED; return style; 139 } 140 141 if ("win32".equals (platform)) { return style & ~SWT.BORDER; 143 } else if ("motif".equals (platform)) { return style | SWT.EMBEDDED; 145 } 146 return style; 147 } 148 149 154 public static void clearSessions () { 155 WebBrowser.clearSessions (); 156 } 157 158 179 public void addCloseWindowListener (CloseWindowListener listener) { 180 checkWidget(); 181 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 182 webBrowser.addCloseWindowListener (listener); 183 } 184 185 207 public void addLocationListener (LocationListener listener) { 208 checkWidget(); 209 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 210 webBrowser.addLocationListener (listener); 211 } 212 213 234 public void addOpenWindowListener (OpenWindowListener listener) { 235 checkWidget(); 236 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 237 webBrowser.addOpenWindowListener (listener); 238 } 239 240 258 public void addProgressListener (ProgressListener listener) { 259 checkWidget(); 260 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 261 webBrowser.addProgressListener (listener); 262 } 263 264 285 public void addStatusTextListener (StatusTextListener listener) { 286 checkWidget(); 287 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 288 webBrowser.addStatusTextListener (listener); 289 } 290 291 309 public void addTitleListener (TitleListener listener) { 310 checkWidget(); 311 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 312 webBrowser.addTitleListener (listener); 313 } 314 315 333 public void addVisibilityWindowListener (VisibilityWindowListener listener) { 334 checkWidget(); 335 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 336 webBrowser.addVisibilityWindowListener (listener); 337 } 338 339 353 public boolean back () { 354 checkWidget(); 355 return webBrowser.back (); 356 } 357 358 protected void checkSubclass () { 359 String name = getClass ().getName (); 360 int index = name.lastIndexOf ('.'); 361 if (!name.substring (0, index + 1).equals (PACKAGE_PREFIX)) { 362 SWT.error (SWT.ERROR_INVALID_SUBCLASS); 363 } 364 } 365 366 387 public boolean execute (String script) { 388 checkWidget(); 389 if (script == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 390 return webBrowser.execute (script); 391 } 392 393 407 public boolean forward () { 408 checkWidget(); 409 return webBrowser.forward (); 410 } 411 412 426 public String getUrl () { 427 checkWidget(); 428 return webBrowser.getUrl (); 429 } 430 431 444 public Object getWebBrowser () { 445 checkWidget(); 446 return webBrowser.getWebBrowser (); 447 } 448 449 462 public boolean isBackEnabled () { 463 checkWidget(); 464 return webBrowser.isBackEnabled (); 465 } 466 467 public boolean isFocusControl () { 468 checkWidget(); 469 if (webBrowser.isFocusControl ()) return true; 470 return super.isFocusControl (); 471 } 472 473 486 public boolean isForwardEnabled () { 487 checkWidget(); 488 return webBrowser.isForwardEnabled (); 489 } 490 491 501 public void refresh () { 502 checkWidget(); 503 webBrowser.refresh (); 504 } 505 506 523 public void removeCloseWindowListener (CloseWindowListener listener) { 524 checkWidget(); 525 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 526 webBrowser.removeCloseWindowListener (listener); 527 } 528 529 546 public void removeLocationListener (LocationListener listener) { 547 checkWidget(); 548 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 549 webBrowser.removeLocationListener (listener); 550 } 551 552 569 public void removeOpenWindowListener (OpenWindowListener listener) { 570 checkWidget(); 571 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 572 webBrowser.removeOpenWindowListener (listener); 573 } 574 575 593 public void removeProgressListener (ProgressListener listener) { 594 checkWidget(); 595 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 596 webBrowser.removeProgressListener (listener); 597 } 598 599 616 public void removeStatusTextListener (StatusTextListener listener) { 617 checkWidget(); 618 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 619 webBrowser.removeStatusTextListener (listener); 620 } 621 622 640 public void removeTitleListener (TitleListener listener) { 641 checkWidget(); 642 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 643 webBrowser.removeTitleListener (listener); 644 } 645 646 664 public void removeVisibilityWindowListener (VisibilityWindowListener listener) { 665 checkWidget(); 666 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 667 webBrowser.removeVisibilityWindowListener (listener); 668 } 669 670 695 public boolean setText (String html) { 696 checkWidget(); 697 if (html == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 698 return webBrowser.setText (html); 699 } 700 701 721 public boolean setUrl (String url) { 722 checkWidget(); 723 if (url == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 724 return webBrowser.setUrl (url); 725 } 726 727 737 public void stop () { 738 checkWidget(); 739 webBrowser.stop (); 740 } 741 } 742 | Popular Tags |