home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 8.9 KB | 314 lines |
- /*
- * @(#)Dialog.java 1.41 98/03/18
- *
- * Copyright 1995-1997 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
- package java.awt;
-
- import java.awt.peer.DialogPeer;
- import java.awt.event.*;
- import java.io.ObjectOutputStream;
- import java.io.ObjectInputStream;
- import java.io.IOException;
-
-
- /**
- * A class that produces a dialog - a window that takes input from the user.
- * The default layout for a dialog is BorderLayout.
- * <p>
- * Dialogs are capable of generating the following window events:
- * WindowOpened, WindowClosing, WindowClosed, WindowActivated,
- * WindowDeactivated.
- *
- * @see WindowEvent
- * @see Window#addWindowListener
- *
- * @version 1.41, 03/18/98
- * @author Sami Shaio
- * @author Arthur van Hoff
- * @since JDK1.0
- */
- public class Dialog extends Window {
-
- static {
- /* ensure that the necessary native libraries are loaded */
- Toolkit.getDefaultToolkit();
- initIDs();
- }
-
- boolean resizable = true;
-
- /**
- * Sets to true if the Dialog is modal. A modal
- * Dialog grabs all the input to the owner frame from the user.
- */
- boolean modal;
-
- /**
- * The title of the Dialog.
- */
- String title;
-
- private static final String base = "dialog";
- private static int nameCounter = 0;
-
- /*
- * JDK 1.1 serialVersionUID
- */
- private static final long serialVersionUID = 5920926903803293709L;
-
- /**
- * Constructs an initially invisible Dialog with an empty title.
- * @param owner the owner of the dialog
- * @see Component#setSize
- * @see Component#setVisible
- */
- public Dialog(Frame owner) {
- this(owner, "", false);
- }
-
- /**
- * Constructs an initially invisible Dialog with an empty title.
- * A modal Dialog grabs all the input to the owner frame from the user.
- * @param owner the owner of the dialog
- * @param modal if true, dialog blocks input to the parent window when shown
- */
- public Dialog(Frame owner, boolean modal) {
- this(owner, "", modal);
- }
-
- /**
- * Constructs an initially invisible Dialog with a title.
- * @param owner the owner of the dialog
- * @param title the title of the dialog
- * @see Component#setSize
- * @see Component#setVisible
- */
- public Dialog(Frame owner, String title) {
- this(owner, title, false);
- }
-
- /**
- * Constructs an initially invisible Dialog with a title.
- * A modal Dialog grabs all the input to the owner frame from the user.
- * @param owner the owner of the dialog
- * @param title the title of the dialog
- * @param modal if true, dialog blocks input to the parent window when shown
- * @see Component#setSize
- * @see Component#setVisible
- */
- public Dialog(Frame owner, String title, boolean modal) {
- super(owner);
- if (owner == null) {
- throw new IllegalArgumentException("null owner frame");
- }
- this.name = base + nameCounter++;
- this.title = title;
- this.modal = modal;
- }
-
-
- /**
- * Constructs an initially invisible Dialog with an empty title.
- * @param owner the owner of the dialog
- * @since JDK1.2
- */
- public Dialog(Dialog owner) {
- this(owner, "", false);
- }
-
- /**
- * Constructs an initially invisible Dialog with a title.
- * @param owner the owner of the dialog
- * @param title the title of the dialog
- * @since JDK1.2
- */
- public Dialog(Dialog owner, String title) {
- this(owner, title, false);
- }
-
- /**
- * Constructs an initially invisible Dialog with a title.
- * A modal Dialog grabs all the input to the owning window from the user.
- * @param owner the owner of the dialog
- * @param title the title of the dialog
- * @param modal if true, dialog blocks input to the owning window when shown
- * @since JDK1.2
- */
- public Dialog(Dialog owner, String title, boolean modal) {
- super(owner);
- if (owner == null) {
- throw new IllegalArgumentException("null owner dialog");
- }
- this.name = base + nameCounter++;
- this.title = title;
- this.modal = modal;
- }
-
- /**
- * Creates the dialog's peer. The peer allows us to change the appearance
- * of the frame without changing its functionality.
- */
- public void addNotify() {
- if (peer == null) {
- peer = getToolkit().createDialog(this);
- }
- super.addNotify();
- }
-
- /**
- * Indicates whether the dialog is modal.
- * A modal dialog grabs all input from the user.
- * @return <code>true</code> if this dialog window is modal;
- * <code>false</code> otherwise.
- * @see java.awt.Dialog#setModal
- */
- public boolean isModal() {
- return modal;
- }
-
- /**
- * Specifies whether this dialog is modal. A modal
- * Dialog grabs all the input to the parent frame from the user.
- * @see java.awt.Dialog#isModal
- * @since JDK1.1
- */
- public void setModal(boolean b) {
- this.modal = b;
- }
-
- /**
- * Gets the title of the dialog.
- * @return the title of this dialog window.
- * @see java.awt.Dialog#setTitle
- */
- public String getTitle() {
- return title;
- }
-
- /**
- * Sets the title of the Dialog.
- * @param title the new title being given to the dialog
- * @see #getTitle
- */
- public synchronized void setTitle(String title) {
- this.title = title;
- DialogPeer peer = (DialogPeer)this.peer;
- if (peer != null) {
- peer.setTitle(title);
- }
- }
-
- /**
- * Shows the dialog. This will bring the dialog to the
- * front if the dialog is already visible. If the dialog is
- * modal, this call will block input to the parent window
- * until the dialog is taken down
- * by calling hide or dispose. It is permissible to show modal
- * dialogs from the event dispatching thread because the toolkit
- * will ensure that another dispatching thread will run while
- * the one which invoked show is blocked.
- * @see Component#hide
- */
- public void show() {
- synchronized(Component.LOCK) {
- if (parent != null && parent.getPeer() == null) {
- parent.addNotify();
- }
- if (peer == null) {
- addNotify();
- }
- validate();
- }
- if (visible) {
- toFront();
- } else {
- synchronized(Component.LOCK) {
- visible = true;
- }
- if (isModal()) {
- EventDispatchThread dt = null;
- if (Thread.currentThread() instanceof EventDispatchThread) {
- try {
- java.security.AccessController.beginPrivileged();
- dt = new EventDispatchThread("AWT-Dispatch-Proxy",
- Toolkit.getEventQueue());
- } finally {
- java.security.AccessController.endPrivileged();
- }
- dt.start();
- }
- // For modal case, we most post this event before calling
- // show, since calling peer.show() will block; this is not
- // ideal, as the window isn't yet visible on the screen...
- if ((state & OPENED) == 0) {
- postWindowEvent(WindowEvent.WINDOW_OPENED);
- state |= OPENED;
- }
- peer.show(); // blocks until dialog brought down
- if (dt != null) {
- dt.stopDispatching();
- }
- } else {
- synchronized(Component.LOCK) {
- peer.show();
- }
- if ((state & OPENED) == 0) {
- postWindowEvent(WindowEvent.WINDOW_OPENED);
- state |= OPENED;
- }
- }
- }
- }
-
- /**
- * Indicates whether this dialog window is resizable.
- * @return <code>true</code> if the user can resize the dialog;
- * <code>false</code> otherwise.
- * @see java.awt.Dialog#setResizable
- */
- public boolean isResizable() {
- return resizable;
- }
-
- /**
- * Sets the resizable flag.
- * @param resizable <code>true</code> if the user can
- * resize this dialog; <code>false</code> otherwise.
- * @see java.awt.Dialog#isResizable
- */
- public synchronized void setResizable(boolean resizable) {
- this.resizable = resizable;
- DialogPeer peer = (DialogPeer)this.peer;
- if (peer != null) {
- peer.setResizable(resizable);
- }
- }
-
- /**
- * Returns the parameter string representing the state of this
- * dialog window. This string is useful for debugging.
- * @return the parameter string of this dialog window.
- */
- protected String paramString() {
- String str = super.paramString() + (modal ? ",modal" : ",modeless");
- if (title != null) {
- str += ",title=" + title;
- }
- return str;
- }
-
- /**
- * Initialize JNI field and method IDs
- */
- private static native void initIDs();
- }
-