100 likes | 434 Views
The SWT Browser Widget. Shawn Spiars Yazoo Consulting http://www.yazooconsulting.com. SWT Browser. Works with IE, Mozilla, and Safari New in Eclipse 3.6 Allows clients to include post data and add/override header values when initiating HTTP requests
E N D
The SWT Browser Widget Shawn Spiars Yazoo Consulting http://www.yazooconsulting.com
SWT Browser Works with IE, Mozilla, and Safari New in Eclipse 3.6 Allows clients to include post data and add/override header values when initiating HTTP requests Browser.setURL(String url, String postData, String[] headers); Attempts to dispose the receiver, but allows the dispose to be vetoed in response to an <code>onbeforeunload</code> listener in the browsers current page Browser.close();
new Browser(parent, SWT.NONE) //use default browser • new Browser(parent, SWT.MOZILLA) //explicit for Mozilla • Bundle bundle = Platform.getBundle("org.mozilla.xulrunner.win32.win32.x86"); • if (bundle != null) { • URL resourceUrl = bundle.getResource("xulrunner"); • try { • URL fileUrl = FileLocator.toFileURL(resourceUrl); • File file = new File(fileUrl.toURI()); • System.setProperty("org.eclipse.swt.browser.XULRunnerPath", file.getAbsolutePath()); • } catch (Exception e) { • logger.error(e); • } • } Default Browser or Mozilla
browser = new Browser(composite, SWT.NONE); • browser.setUrl(“http://www.google.com”); • Object result = browser.evaluate(String script); • Supported mappings: JavaScript null or undefined ==> null JavaScript number ==> java.lang.Double JavaScript string ==> java.lang.String JavaScript boolean ==> java.lang.Boolean JavaScript array whose elements are all of supported types ==> java.lang.Object[] Java to JavaScript
Extend Java BrowserFunction class • static class CustomFunction extends BrowserFunction { • CustomFunction (Browser browser, String name) { • super (browser, name); • } • public Object function (Object[] arguments) { • System.out.println ("theJavaFunction() called from javascriptargs:"); • ……. • return value; • } • } • new CustomFunction (browser, "theJavaFunction"); • Call Java BrowserFunction from JavaScript: • <script language="JavaScript”> • var result = theJavaFunction(12, false, null); • </script> JavaScript to Java
Renders a string containing HTML • Browser.setText(String html) • Listen for page load completion • Browser.addProgressListener • Listen for link selections • Browser.addLocationListener • Browser.addOpenWindowListener • Simple authentication • Browser.addAuthenticationListener • Cookies • Browser.getCookie • Browser.setCookie • Browser.clearSessions Additional API
Simple RCP application which demonstrates some functions of the SWT Browser widget (Eclipse 3.6.0) • SWT Browser embedded in Eclipse editor • Editor contributions create toolbar buttons for dialogs: • Evaluate JavaScript dialog • Show page source dialog • Set page source dialog • Activator class loads Xulrunner/Mozilla xpcom bundles • (includes bundle version 1.8.1.3) SWT Browser Example
References Download SWT Browser Example application (includes source) http://www.yazooconsulting.com/downloads/SWTBrowserExample.zip Embedding Web UI Components in Eclipse by Boris Bokowski http://www.slideshare.net/bokowski/tutorial-embedding-web-ui-components-in-eclipse SWT FAQ http://www.eclipse.org/swt/faq.php#whatisbrowser SWT Snippets http://www.eclipse.org/swt/snippets/
Thanks! • Shawn Spiars • sspiars@yazooconsulting.com