home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / Dialog.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  8.9 KB  |  314 lines

  1. /*
  2.  * @(#)Dialog.java    1.41 98/03/18
  3.  *
  4.  * Copyright 1995-1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.DialogPeer;
  17. import java.awt.event.*;
  18. import java.io.ObjectOutputStream;
  19. import java.io.ObjectInputStream;
  20. import java.io.IOException;
  21.  
  22.  
  23. /**
  24.  * A class that produces a dialog - a window that takes input from the user.
  25.  * The default layout for a dialog is BorderLayout.
  26.  * <p>
  27.  * Dialogs are capable of generating the following window events:
  28.  * WindowOpened, WindowClosing, WindowClosed, WindowActivated,
  29.  * WindowDeactivated.
  30.  *
  31.  * @see WindowEvent
  32.  * @see Window#addWindowListener
  33.  *
  34.  * @version     1.41, 03/18/98
  35.  * @author     Sami Shaio
  36.  * @author     Arthur van Hoff
  37.  * @since       JDK1.0
  38.  */
  39. public class Dialog extends Window {
  40.  
  41.     static {
  42.         /* ensure that the necessary native libraries are loaded */
  43.         Toolkit.getDefaultToolkit();
  44.         initIDs();
  45.     }
  46.  
  47.     boolean    resizable = true;
  48.  
  49.     /**
  50.      * Sets to true if the Dialog is modal.  A modal
  51.      * Dialog grabs all the input to the owner frame from the user.
  52.      */
  53.     boolean modal;
  54.  
  55.     /**
  56.      * The title of the Dialog.
  57.      */
  58.     String title;
  59.  
  60.     private static final String base = "dialog";
  61.     private static int nameCounter = 0;
  62.  
  63.     /*
  64.      * JDK 1.1 serialVersionUID 
  65.      */
  66.     private static final long serialVersionUID = 5920926903803293709L;
  67.  
  68.     /**
  69.      * Constructs an initially invisible Dialog with an empty title.
  70.      * @param owner the owner of the dialog
  71.      * @see Component#setSize
  72.      * @see Component#setVisible
  73.      */
  74.     public Dialog(Frame owner) {
  75.     this(owner, "", false);
  76.     }
  77.  
  78.     /**
  79.      * Constructs an initially invisible Dialog with an empty title.
  80.      * A modal Dialog grabs all the input to the owner frame from the user.
  81.      * @param owner the owner of the dialog
  82.      * @param modal if true, dialog blocks input to the parent window when shown
  83.      */
  84.     public Dialog(Frame owner, boolean modal) {
  85.     this(owner, "", modal);
  86.     }
  87.  
  88.     /**
  89.      * Constructs an initially invisible Dialog with a title. 
  90.      * @param owner the owner of the dialog
  91.      * @param title the title of the dialog
  92.      * @see Component#setSize
  93.      * @see Component#setVisible
  94.      */
  95.     public Dialog(Frame owner, String title) {
  96.     this(owner, title, false);
  97.     }
  98.  
  99.     /**
  100.      * Constructs an initially invisible Dialog with a title. 
  101.      * A modal Dialog grabs all the input to the owner frame from the user.
  102.      * @param owner the owner of the dialog
  103.      * @param title the title of the dialog
  104.      * @param modal if true, dialog blocks input to the parent window when shown
  105.      * @see Component#setSize
  106.      * @see Component#setVisible
  107.      */
  108.     public Dialog(Frame owner, String title, boolean modal) {
  109.     super(owner);
  110.     if (owner == null) {
  111.         throw new IllegalArgumentException("null owner frame");
  112.     }
  113.     this.name = base + nameCounter++;
  114.     this.title = title;
  115.     this.modal = modal;
  116.     }
  117.  
  118.  
  119.     /**
  120.      * Constructs an initially invisible Dialog with an empty title.
  121.      * @param owner the owner of the dialog
  122.      * @since JDK1.2
  123.      */
  124.     public Dialog(Dialog owner) {
  125.     this(owner, "", false);
  126.     }
  127.  
  128.     /**
  129.      * Constructs an initially invisible Dialog with a title. 
  130.      * @param owner the owner of the dialog
  131.      * @param title the title of the dialog
  132.      * @since JDK1.2
  133.      */
  134.     public Dialog(Dialog owner, String title) {
  135.     this(owner, title, false);
  136.     }
  137.  
  138.     /**
  139.      * Constructs an initially invisible Dialog with a title. 
  140.      * A modal Dialog grabs all the input to the owning window from the user.
  141.      * @param owner the owner of the dialog
  142.      * @param title the title of the dialog
  143.      * @param modal if true, dialog blocks input to the owning window when shown
  144.      * @since JDK1.2
  145.      */
  146.     public Dialog(Dialog owner, String title, boolean modal) {
  147.     super(owner);
  148.     if (owner == null) {
  149.         throw new IllegalArgumentException("null owner dialog");
  150.     }
  151.     this.name = base + nameCounter++;
  152.     this.title = title;
  153.     this.modal = modal;
  154.     }
  155.  
  156.     /**
  157.      * Creates the dialog's peer.  The peer allows us to change the appearance
  158.      * of the frame without changing its functionality.
  159.      */
  160.     public void addNotify() {
  161.     if (peer == null) {
  162.         peer = getToolkit().createDialog(this);
  163.     }
  164.     super.addNotify();
  165.     }
  166.  
  167.     /**
  168.      * Indicates whether the dialog is modal.  
  169.      * A modal dialog grabs all input from the user.
  170.      * @return    <code>true</code> if this dialog window is modal; 
  171.      *            <code>false</code> otherwise. 
  172.      * @see       java.awt.Dialog#setModal    
  173.      */
  174.     public boolean isModal() {
  175.     return modal;
  176.     }
  177.  
  178.     /**
  179.      * Specifies whether this dialog is modal.  A modal
  180.      * Dialog grabs all the input to the parent frame from the user.
  181.      * @see       java.awt.Dialog#isModal 
  182.      * @since     JDK1.1
  183.      */
  184.     public void setModal(boolean b) {
  185.     this.modal = b;
  186.     }
  187.  
  188.     /**
  189.      * Gets the title of the dialog.
  190.      * @return    the title of this dialog window.
  191.      * @see       java.awt.Dialog#setTitle
  192.      */
  193.     public String getTitle() {
  194.     return title;
  195.     }
  196.  
  197.     /**
  198.      * Sets the title of the Dialog.
  199.      * @param title the new title being given to the dialog
  200.      * @see #getTitle
  201.      */
  202.     public synchronized void setTitle(String title) {
  203.     this.title = title;
  204.     DialogPeer peer = (DialogPeer)this.peer;
  205.     if (peer != null) {
  206.         peer.setTitle(title);
  207.     }
  208.     }
  209.  
  210.    /**
  211.      * Shows the dialog. This will bring the dialog to the
  212.      * front if the dialog is already visible.  If the dialog is
  213.      * modal, this call will block input to the parent window
  214.      * until the dialog is taken down
  215.      * by calling hide or dispose. It is permissible to show modal
  216.      * dialogs from the event dispatching thread because the toolkit
  217.      * will ensure that another dispatching thread will run while
  218.      * the one which invoked show is blocked. 
  219.      * @see Component#hide
  220.      */
  221.     public void show() {
  222.     synchronized(Component.LOCK) {
  223.             if (parent != null && parent.getPeer() == null) {
  224.                 parent.addNotify();
  225.             }
  226.         if (peer == null) {
  227.         addNotify();
  228.         }
  229.         validate();        
  230.     }
  231.     if (visible) {
  232.         toFront();
  233.     } else {
  234.         synchronized(Component.LOCK) {
  235.             visible = true;
  236.         }
  237.         if (isModal()) {
  238.                 EventDispatchThread dt = null;
  239.                 if (Thread.currentThread() instanceof EventDispatchThread) {
  240.             try {
  241.             java.security.AccessController.beginPrivileged();
  242.             dt = new EventDispatchThread("AWT-Dispatch-Proxy", 
  243.                                                  Toolkit.getEventQueue());
  244.             } finally {
  245.             java.security.AccessController.endPrivileged();
  246.             }
  247.                     dt.start();
  248.                 }
  249.                 // For modal case, we most post this event before calling
  250.                 // show, since calling peer.show() will block; this is not
  251.                 // ideal, as the window isn't yet visible on the screen...
  252.                 if ((state & OPENED) == 0) {
  253.                     postWindowEvent(WindowEvent.WINDOW_OPENED);
  254.                     state |= OPENED;
  255.                 }
  256.         peer.show(); // blocks until dialog brought down
  257.                 if (dt != null) {
  258.                     dt.stopDispatching();
  259.                 }                
  260.         } else {
  261.         synchronized(Component.LOCK) {
  262.             peer.show();
  263.         }
  264.                 if ((state & OPENED) == 0) {
  265.                     postWindowEvent(WindowEvent.WINDOW_OPENED);
  266.                     state |= OPENED;
  267.                 }
  268.         }
  269.     }
  270.     }
  271.  
  272.     /**
  273.      * Indicates whether this dialog window is resizable.
  274.      * @return    <code>true</code> if the user can resize the dialog;
  275.      *            <code>false</code> otherwise.
  276.      * @see       java.awt.Dialog#setResizable
  277.      */
  278.     public boolean isResizable() {
  279.     return resizable;
  280.     }
  281.  
  282.     /**
  283.      * Sets the resizable flag.
  284.      * @param     resizable <code>true</code> if the user can 
  285.      *                 resize this dialog; <code>false</code> otherwise.
  286.      * @see       java.awt.Dialog#isResizable
  287.      */
  288.     public synchronized void setResizable(boolean resizable) {
  289.     this.resizable = resizable;
  290.     DialogPeer peer = (DialogPeer)this.peer;
  291.     if (peer != null) {
  292.         peer.setResizable(resizable);
  293.     }
  294.     }
  295.  
  296.     /**
  297.      * Returns the parameter string representing the state of this 
  298.      * dialog window. This string is useful for debugging. 
  299.      * @return    the parameter string of this dialog window.
  300.      */
  301.     protected String paramString() {
  302.     String str = super.paramString() + (modal ? ",modal" : ",modeless");
  303.     if (title != null) {
  304.         str += ",title=" + title;
  305.     }
  306.     return str;
  307.     }
  308.  
  309.     /**
  310.      * Initialize JNI field and method IDs
  311.      */
  312.     private static native void initIDs();
  313. }
  314.