Inside XML (Inside (New Riders))

     

10.9.1 Problem

You want to give users some online accessfor example, to access online help.

10.9.2 Solution

Use an SWT browser widget. Starting in Eclipse 3.0, SWT contains a browser widget built in.

10.9.3 Discussion

In Eclipse 2.x, you do not have much choice for displaying browsers in SWT windows . You really can do so only in Windows , using OLE. Here's how it works. You create an OleFrame object and connect the system's browser to it:

import org.eclipse.swt.ole.win32.*; . . . OleControlSite oleControlSite; OleFrame oleFrame = new OleFrame(shell, SWT.NONE); oleControlSite = new OleControlSite(oleFrame, SWT.NONE, "Shell.Explorer"); oleControlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE); shell.open( ); . . .

Then you create an OleAutomation object and navigate to the URL you want like so:

import org.eclipse.swt.ole.win32.*; . . . OleControlSite oleControlSite; OleFrame oleFrame = new OleFrame(shell, SWT.NONE); oleControlSite = new OleControlSite(oleFrame, SWT.NONE, "Shell.Explorer"); oleControlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE); shell.open( ); final OleAutomation browser = new OleAutomation(oleControlSite); int[] browserIDs = browser.getIDsOfNames(new String[]{"Navigate", "URL"}); Variant[] address = new Variant[] {new Variant("http://www.oreilly.com")}; browser.invoke(browserIDs[0], address, new int[]{browserIDs[1]});

10.9.3.1 Eclipse 3.0

In Eclipse 3.x, life is easier because SWT comes with a built-in browser widget. This widget currently is supported in Windows (using Internet Explorer 5+) and in Linux GTK (using Mozilla 1.4 GTK2). More operating systems will be supported in time. Here are some of the useful navigation methods of this widget:

boolean back( )

Moves back one page

boolean forward( )

Moves forward one page

boolean setUrl(String string)

Navigates to a URL

void stop( )

Stops the current operation

We'll create an example ( BrowserApp at this book's site) that shows how to use this widget. This example includes a toolbar with Go, Back, and Stop buttons , and a text widget into which you can enter URLs:

public class BrowserClass { public static void main(String[] args) { Display display = new Display( ); final Shell shell = new Shell(display); shell.setText("Browser Example"); shell.setSize(620, 500); ToolBar toolbar = new ToolBar(shell, SWT.NONE); toolbar.setBounds(5, 5, 200, 30); ToolItem goButton = new ToolItem(toolbar, SWT.PUSH); goButton.setText("Go"); ToolItem backButton = new ToolItem(toolbar, SWT.PUSH); backButton.setText("Back"); ToolItem stopButton = new ToolItem(toolbar, SWT.PUSH); stopButton.setText("Stop"); final Text text = new Text(shell, SWT.BORDER); text.setBounds(5, 35, 400, 25);

Creating the browser is easy enough in Eclipse 3.x:

final Browser browser = new Browser(shell, SWT.NONE); browser.setBounds(5, 75, 600, 400);

The listener we'll create will handle the Go, Back, and Stop buttons, calling the appropriate browser methods like so:

Listener listener = new Listener( ) { public void handleEvent(Event event) { ToolItem item = (ToolItem) event.widget; String string = item.getText( ); if (string.equals("Back")) browser.back( ); else if (string.equals("Stop")) browser.stop( ); else if (string.equals("Go")) browser.setUrl(text.getText( )); } };

All that remains is to add a listener for the text widget, which browses to the URL the user typed if she uses the Enter key, and to open the shell that displays the new browser. The code that does this appears in Example 10-4.

Example 10-4. Using an SWT browser widget

package org.cookbook.ch10; import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.browser.*; public class BrowserClass { public static void main(String[] args) { Display display = new Display( ); final Shell shell = new Shell(display); shell.setText("Browser Example"); shell.setSize(620, 500); ToolBar toolbar = new ToolBar(shell, SWT.NONE); toolbar.setBounds(5, 5, 200, 30); ToolItem goButton = new ToolItem(toolbar, SWT.PUSH); goButton.setText("Go"); ToolItem backButton = new ToolItem(toolbar, SWT.PUSH); backButton.setText("Back"); ToolItem stopButton = new ToolItem(toolbar, SWT.PUSH); stopButton.setText("Stop"); final Text text = new Text(shell, SWT.BORDER); text.setBounds(5, 35, 400, 25); final Browser browser = new Browser(shell, SWT.NONE); browser.setBounds(5, 75, 600, 400); Listener listener = new Listener( ) { public void handleEvent(Event event) { ToolItem item = (ToolItem) event.widget; String string = item.getText( ); if (string.equals("Back")) browser.back( ); else if (string.equals("Forward")) browser.forward( ); else if (string.equals("Stop")) browser.stop( ); else if (string.equals("Go")) browser.setUrl(text.getText( )); } }; backButton.addListener(SWT.Selection, listener); stopButton.addListener(SWT.Selection, listener); text.addListener(SWT.DefaultSelection, new Listener( ) { public void handleEvent(Event e) { browser.setUrl(text.getText( )); } }); shell.open( ); browser.setUrl("http://oreilly.com"); while (!shell.isDisposed( )) { if (!display.readAndDispatch( )) display.sleep( ); } display.dispose( ); } }

The new browser appears in Figure 10-7, where we're browsing to the O'Reilly web site. The user can enter URLs into the address text widget, and the buttons in the toolbar are active. That's the browser widget in Eclipse 3.0very cool.

Figure 10-7. An SWT browser widget

Категории