home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1999 February / CDW0299.iso / Demos / Cafe / Source.bin / DialogBox.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  2.9 KB  |  98 lines

  1. package symantec.itools.awt.util.dialog;
  2.  
  3. import java.awt.Button;
  4. import java.awt.Event;
  5. import java.awt.Frame;
  6. import java.awt.Dialog;
  7. import java.awt.Rectangle;
  8.  
  9. /**
  10.  * This implements a non-resizable dialog box.
  11.  * It has an available button and closes itself as needed.
  12.  */
  13. public class DialogBox extends Dialog {
  14.  
  15.     /**
  16.      * An available button.
  17.      */
  18.     protected Button okButton;
  19.  
  20.     /**
  21.      *
  22.      * Constructs a DialogBox with the given parent.
  23.      * @param f the frame of this dialogÆs parent
  24.      */
  25.     public DialogBox(Frame f) {
  26.         this(f, false);
  27.     }
  28.  
  29.     /**
  30.      * Constructs a DialogBox with the given parent and modality.
  31.      * @param f the frame of this dialogÆs parent
  32.      * @param modal true to construct a modal dialog, false to construct a
  33.      * non-modal dialog
  34.      */
  35.     public DialogBox(Frame f, boolean modal) {
  36.         this(f, "", modal);
  37.     }
  38.  
  39.     /**
  40.      * Constructs a DialogBox with the given parent, title, and modality.
  41.      * @param f the frame of this dialogÆs parent
  42.      * @param s the title for this dialog
  43.      * @param modal true to construct a modal dialog, false to construct a
  44.      * non-modal dialog
  45.      */
  46.     public DialogBox(Frame f, String s, boolean modal) {
  47.         super(f, s, modal);
  48.         setResizable(false);
  49.     }
  50.  
  51.     /**
  52.      * Makes this component visible.
  53.      * <p>
  54.      * This is a standard Java AWT method which gets called to show this
  55.      * component. If this component was invisible due to a previous hide()
  56.      * call it makes this component visible again.
  57.      */
  58.     public synchronized void show() {
  59.         Rectangle bounds = getParent().bounds();
  60.         Rectangle abounds = bounds();
  61.  
  62.         move(bounds.x + (bounds.width - abounds.width) / 2,
  63.              bounds.y + (bounds.height - abounds.height) /2);
  64.  
  65.         super.show();
  66.     }
  67.  
  68.     /**
  69.      * Hides and disposes of the dialog.
  70.      */
  71.     protected void closeDialog() {
  72.         hide();
  73.         dispose();
  74.     }
  75.  
  76.     /**
  77.      * Processes events for this component.
  78.      * <p>
  79.      * This is a standard Java AWT method which gets called by the AWT to
  80.      * handle this component's events. The default handler for components
  81.      * dispatches to one of the following methods as needed: action(),
  82.      * gotFocus(), lostFocus(), keyDown(), keyUp(), mouseEnter(), mouseExit(),
  83.      * mouseMove(), mouseDrag(), mouseDown(), or mouseUp().
  84.      *
  85.      * @param event the event to handle
  86.      * @return true if the event was handled and no further action is needed,
  87.      * false to pass the event to this component's parent
  88.      */
  89.     public boolean handleEvent(Event event) {
  90.         if ((event.target == okButton && event.id == Event.ACTION_EVENT) ||
  91.             (event.target == this && event.id == Event.WINDOW_DESTROY))
  92.             closeDialog();
  93.  
  94.         return super.handleEvent(event);
  95.     }
  96.  
  97. }
  98.