home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Dialog.java < prev    next >
Text File  |  1997-10-01  |  9KB  |  276 lines

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