[Back]


DialogOnly Tutorial

/* This tutorial creates a simple plug-in that shows */
/* plug-in construction. You can compile and run it */
/* if you like. This plug-in includes only a minimum */
/* set of features; just enough to create an empty */
/* dialog box. Note the similarities to the AddLayer */
/* sample, on which it is based. */
/* To return to Chapter 8, click AWT Dialog steps. */

/* 1. Import the packages and classes to provide plug-in */
/*    functionality. The plug-in requires Java IO, */
/*    and AWT classes, as well as Composer classes. */
/*    Using * ensures that all needed classes are included. AWT Dialog steps */

/*    For information about this step, see "Importing Packages and Classes." */

import java.io.*;
import java.awt.*;
import netscape.plugin.composer.*;
import netscape.plugin.composer.io.*;


/* 2. Define the plug-in class. */

/*    For information about this step, see "Defining the Class." */

public class DialogOnly extends Plugin{
/* 3. Optionally, add a main method to your perform method */
/*    so that you can run the plug-in from the command line */
/*    and test it in the Composer Testbed. The plug-in does */
/*    not need this for normal operation. */

/*    For information about the run command, see "Preparing for Running in the Testbed." */

    static public void main(String[] args) {
        netscape.test.plugin.composer.Test.perform(args, new DialogOnly());
}

/* 4. Override basic information methods: */
/*    * getName (user interface selection name for Tool menu), */
/*    * getCategory (plug-in category for Tool menu), */
/*    * getHint (one-line description of what plug-in does) */

/*    For information about this step, see "Overriding Plug-in Information Methods." */

   public String getName(){
   return "Dialog Only";
   }

   public String getCategory(){
   return "HTML Tools";
   }

   public String getHint(){
   return "Create empty dialog box";
   }

/* 5. Override the perform method, which does the work */
/*    of the plug-in. The document parameter is the */
/*    document being worked on. */
/*    perform returns a boolean: True, plug-in succeeds. */
/*    The method must create and display a dialog box with */
/*    three buttons (OK, Apply, Cancel). AWT Dialog steps */

/*    For information about this step, see "Overriding the perform Method." */
   public boolean perform(Document document) throws IOException{
        MyDialog dialog = new MyDialog("My Dialog", document);

        /* Initialize the dialog box and set its size. AWT Dialog steps */
        dialog.reshape(50,50,300,300);
        /* Make the dialog box visible. AWT Dialog steps */
        dialog.show();
        /* Place dialog on top and give it focus. AWT Dialog steps */
        dialog.requestFocus();

        /* Wait for the user to exit the dialog. AWT Dialog steps */
        boolean result = dialog.waitForExit();

        /* Clean up the dialog's native OS window. AWT Dialog steps */
        dialog.dispose();
        return result;
    }
}

/* 6. Define the AWT dialog box class for */
/*    interacting with the user. */
/*    This is like the java.awt.Dialog class, except */
/*    that it doesn't require a parent Frame. */
/*    This method must    */
/*    - draw the dialog. */
/*    - handle the window close event. */
/*    - handle dialog actions, including exceptions. */
/*    - signal the plug-in when dialog is finished. AWT Dialog steps */


class MyDialog extends Frame {
    public MyDialog(String title, Document document) {
        super(title);
       /* Save document in an instance variable. AWT Dialog steps */
        this.document = document;
        /* Set dialog buttons as instance variables. AWT Dialog steps */
        Panel buttons = new Panel();
        buttons.add("East", ok = new Button("OK"));
        buttons.add("West", cancel = new Button("Cancel"));
        // add("Center", text = new TextArea());
        add("South", buttons);
     }
    /* Handle window close event. AWT Dialog steps */
    public boolean handleEvent(Event event) {
        if (event.id == Event.WINDOW_DESTROY) {
            hide();
            signalExit();
            return true;
        } else {
            return super.handleEvent(event);
        }
    }
    /* Handle dialog actions. AWT Dialog steps */
    public boolean action(Event evt, Object what){
        if ( evt.target == ok || evt.target == cancel) {
            success = evt.target == ok;
            hide();
            signalExit();
            return true;
        }
        return false;
    }
    /* Signal termination of the dialog thread. AWT Dialog steps */
    synchronized public boolean waitForExit() {
        while ( ! bExited ) {
            try {
                wait();
            } catch ( InterruptedException e){
            }
        }
        return success;
    }
    /* Signal that the dialog is finished. AWT Dialog steps */
    synchronized public void signalExit() {
        bExited = true;
        notifyAll();
    }
    /* Declare and initialize plug-in class variables. AWT Dialog steps */
    private Button ok;
    private Button cancel;
    private boolean bExited = false;
    private boolean success = false;
    private Document document;
}

/* You've constructed a plug-in class that has the minimum */
/* set of features needed to display a dialog box. */
/* The dialog box does not do anything; you can add */
/* functionality to this structure. You've performed */
/* these tasks: */
/*    * Importing the packages and classes your */
/*      plug-in requires */
/*    * Defining a plug-in class. */
/*    * Including a main method so that you can run it */
/*      in the Testbed. */
/*    * Overriding the Plugin class methods that provide basic */
/*      information and functionality and call other methods. */
/*    * Creating and displaying an AWT dialog box, which */
/*      includes drawing the dialog, handling the */
/*      window close event, handling dialog actions, and */
/*      signaling the plug-in that the dialog is finished. */

/* If you want to test this plug-in in Composer, */
/* at a minimum you'll need to compile, install, */
/* test-run and possibly debug. Packaging the plug-in */
/* is also an option, but you don't need to package */
/* it in order to test it. "Development Overview" */
/* covers each of these operations. */

[Top] [Back]



Copyright © 1997 Netscape Communications Corporation