Building an Eclipse Web Search Plug-in

by Michael Pilone



Listing One



package org.mpilone.ddj;



import org.eclipse.swt.SWT;

import org.eclipse.swt.browser.*;

import org.eclipse.swt.layout.GridData;

import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.*;



/** A basic dialog with a large HTML search

 * area with some simple navigation.  */

public class SearchDialog extends Dialog {

    /** Constructs the dialog as a child of the given parent shell.

     * @param parent the parent of this dialog */

    public SearchDialog(Shell parent) {

        super(parent);

        setText("DDJ Reference Search");

    }

    /** Opens the dialog and displays it on the screen. The dialog will

     * be modal and remain open until closed by the user. */

    public void open() {

        // Create the shell with a dialog trim and title text.

        Shell parent = getParent();

        final Shell shell = new Shell(parent, SWT.RESIZE | 

                SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);

        shell.setText(getText());

        // Set the basic layout

        GridLayout layout = new GridLayout();

        layout.numColumns = 1;

        shell.setLayout(layout);

        // Create the browser and setup the layout information to

        // allow it to fill all available space in the dialog.

        final Browser browser = new Browser(shell, 0);

        GridData gridData = new GridData(700, 500);

        gridData.grabExcessHorizontalSpace = true;

        gridData.grabExcessVerticalSpace = true;

        gridData.horizontalAlignment = GridData.FILL;

        gridData.verticalAlignment = GridData.FILL;

        browser.setLayoutData(gridData);

        // Tell the browser to load the URL desired. It may

        // make more sense for this to be a property in a production release.

        browser.setUrl("http://www.ddj.com");

        // Pack the shell to resize everything.

        shell.pack();

        shell.open();

        Display display = parent.getDisplay();

        // Display the shell and process events

        // as long as the user hasn't closed the dialog.

        while (!shell.isDisposed()) {

            if (!display.readAndDispatch()) display.sleep();

        }

    }

}







Listing Two



    public void open() {

        // Create the shell with a dialog trim and title text.

        Shell parent = getParent();

        final Shell shell = new Shell(parent, SWT.RESIZE | 

                SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);

        shell.setText(getText());

        // Set the basic layout

        GridLayout layout = new GridLayout();

        layout.numColumns = 1;

        shell.setLayout(layout);

        // Create the tool bar with the basic buttons: back, forward,

        // close. The back and forward buttons default to close since

        // they will be updated as the browser loads pages.

        ToolBar navBar = new ToolBar(shell, SWT.NONE);

        navBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL 

               | GridData.HORIZONTAL_ALIGN_END));

        final ToolItem back = new ToolItem(navBar, SWT.PUSH);

        back.setText("Back");

        back.setEnabled(false);

        final ToolItem forward = new ToolItem(navBar, SWT.PUSH);

        forward.setText("Forward");

        forward.setEnabled(false);

        final ToolItem close = new ToolItem(navBar, SWT.PUSH);

        close.setText("Close");

        // Create the browser and setup the layout information to

        // allow it to fill all available space in the dialog.

        final Browser browser = new Browser(shell, 0);

        GridData gridData = new GridData(700, 500);

        gridData.grabExcessHorizontalSpace = true;

        gridData.grabExcessVerticalSpace = true;

        gridData.horizontalAlignment = GridData.FILL;

        gridData.verticalAlignment = GridData.FILL;

        browser.setLayoutData(gridData);

        // Tell the browser to load the URL desired. It may

        // make more sense for this to be a property in a production release.

        browser.setUrl("http://www.ddj.com");

        // Add listeners to respond to the button clicks.

        back.addListener(SWT.Selection, new Listener() {

            public void handleEvent(Event event) {

                browser.back();

            }

        });

        forward.addListener(SWT.Selection, new Listener() {

            public void handleEvent(Event event) {

                browser.forward();

            }

        });

        close.addListener(SWT.Selection, new Listener() {

            public void handleEvent(Event event) {

                shell.close();

            }

        });

        // Add a listener to update the state of the buttons

        // based on the state of the browser while the user is surfing.

        browser.addLocationListener(new LocationListener() {

            public void changed(LocationEvent event) {

                Browser browser = (Browser)event.widget;

                back.setEnabled(browser.isBackEnabled());

                forward.setEnabled(browser.isForwardEnabled());

            }

            public void changing(LocationEvent event) {

            }

        });

        // Pack the shell to resize everything.

        shell.pack();

        shell.open();

        Display display = parent.getDisplay();

        // Display the shell and process events

        // as long as the user hasn't closed the dialog.

        while (!shell.isDisposed()) {

            if (!display.readAndDispatch()) display.sleep();

        }

    }





3



