Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / awt / Dialog.java
blob8248df3c70dcd8e05d50a594a5937aefbcc0bd33
1 /* Dialog.java -- An AWT dialog box
2 Copyright (C) 1999, 2000, 2001, 2002, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt;
41 import java.awt.peer.DialogPeer;
43 import javax.accessibility.AccessibleContext;
44 import javax.accessibility.AccessibleRole;
45 import javax.accessibility.AccessibleState;
46 import javax.accessibility.AccessibleStateSet;
48 /**
49 * A dialog box widget class.
51 * @author Aaron M. Renn (arenn@urbanophile.com)
52 * @author Tom Tromey (tromey@redhat.com)
54 public class Dialog extends Window
58 * Static Variables
61 // Serialization constant
62 private static final long serialVersionUID = 5920926903803293709L;
64 /*************************************************************************/
67 * Instance Variables
70 /**
71 * @serial Indicates whether or not this dialog box is modal.
73 private boolean modal;
75 /**
76 * @serial Indicates whether or not this dialog box is resizable.
78 private boolean resizable = true;
80 /**
81 * @serial The title string for this dialog box, which can be
82 * <code>null</code>.
84 private String title;
86 /**
87 * This field indicates whether the dialog is undecorated or not.
89 private boolean undecorated = false;
91 /**
92 * Indicates that we are blocked for modality in show
94 private boolean blocked = false;
96 /**
97 * Secondary EventQueue to handle AWT events while
98 * we are blocked for modality in show
100 private EventQueue eq2 = null;
102 /*************************************************************************/
105 * Constructors
109 * Initializes a new instance of <code>Dialog</code> with the specified
110 * parent, that is resizable and not modal, and which has no title.
112 * @param parent The parent frame of this dialog box.
114 * @exception IllegalArgumentException If the owner's GraphicsConfiguration
115 * is not from a screen device, or if owner is null. This exception is always
116 * thrown when GraphicsEnvironment.isHeadless() returns true.
118 public
119 Dialog(Frame parent)
121 this(parent, "", false);
124 /*************************************************************************/
127 * Initializes a new instance of <code>Dialog</code> with the specified
128 * parent and modality, that is resizable and which has no title.
130 * @param parent The parent frame of this dialog box.
131 * @param modal <code>true</code> if this dialog box is modal,
132 * <code>false</code> otherwise.
134 * @exception IllegalArgumentException If the owner's GraphicsConfiguration
135 * is not from a screen device, or if owner is null. This exception is always
136 * thrown when GraphicsEnvironment.isHeadless() returns true.
138 public
139 Dialog(Frame parent, boolean modal)
141 this(parent, "", modal);
144 /*************************************************************************/
147 * Initializes a new instance of <code>Dialog</code> with the specified
148 * parent, that is resizable and not modal, and which has the specified
149 * title.
151 * @param parent The parent frame of this dialog box.
152 * @param title The title string for this dialog box.
154 * @exception IllegalArgumentException If the owner's GraphicsConfiguration
155 * is not from a screen device, or if owner is null. This exception is always
156 * thrown when GraphicsEnvironment.isHeadless() returns true.
158 public
159 Dialog(Frame parent, String title)
161 this(parent, title, false);
164 /*************************************************************************/
167 * Initializes a new instance of <code>Dialog</code> with the specified,
168 * parent, title, and modality, that is resizable.
170 * @param parent The parent frame of this dialog box.
171 * @param title The title string for this dialog box.
172 * @param modal <code>true</code> if this dialog box is modal,
173 * <code>false</code> otherwise.
175 * @exception IllegalArgumentException If owner is null or
176 * GraphicsEnvironment.isHeadless() returns true.
178 public
179 Dialog(Frame parent, String title, boolean modal)
181 this (parent, title, modal, parent.getGraphicsConfiguration ());
185 * Initializes a new instance of <code>Dialog</code> with the specified,
186 * parent, title, modality and <code>GraphicsConfiguration</code>,
187 * that is resizable.
189 * @param parent The parent frame of this dialog box.
190 * @param title The title string for this dialog box.
191 * @param modal <code>true</code> if this dialog box is modal,
192 * <code>false</code> otherwise.
193 * @param gc The <code>GraphicsConfiguration</code> object to use.
195 * @exception IllegalArgumentException If owner is null, the
196 * GraphicsConfiguration is not a screen device or
197 * GraphicsEnvironment.isHeadless() returns true.
199 * @since 1.4
201 public
202 Dialog (Frame parent, String title, boolean modal, GraphicsConfiguration gc)
204 super (parent, gc);
206 // A null title is equivalent to an empty title
207 this.title = (title != null) ? title : "";
208 this.modal = modal;
209 visible = false;
211 setLayout(new BorderLayout());
215 * Initializes a new instance of <code>Dialog</code> with the specified,
216 * parent, that is resizable.
218 * @exception IllegalArgumentException If parent is null. This exception is
219 * always thrown when GraphicsEnvironment.isHeadless() returns true.
221 * @since 1.2
223 public
224 Dialog (Dialog owner)
226 this (owner, "", false, owner.getGraphicsConfiguration ());
230 * Initializes a new instance of <code>Dialog</code> with the specified,
231 * parent and title, that is resizable.
233 * @exception IllegalArgumentException If parent is null. This exception is
234 * always thrown when GraphicsEnvironment.isHeadless() returns true.
236 * @since 1.2
238 public
239 Dialog (Dialog owner, String title)
241 this (owner, title, false, owner.getGraphicsConfiguration ());
245 * Initializes a new instance of <code>Dialog</code> with the specified,
246 * parent, title and modality, that is resizable.
248 * @exception IllegalArgumentException If parent is null. This exception is
249 * always thrown when GraphicsEnvironment.isHeadless() returns true.
251 * @since 1.2
253 public
254 Dialog (Dialog owner, String title, boolean modal)
256 this (owner, title, modal, owner.getGraphicsConfiguration ());
260 * Initializes a new instance of <code>Dialog</code> with the specified,
261 * parent, title, modality and <code>GraphicsConfiguration</code>,
262 * that is resizable.
264 * @exception IllegalArgumentException If parent is null, the
265 * GraphicsConfiguration is not a screen device or
266 * GraphicsEnvironment.isHeadless() returns true.
268 * @since 1.4
270 public
271 Dialog (Dialog parent, String title, boolean modal, GraphicsConfiguration gc)
273 super (parent, parent.getGraphicsConfiguration ());
275 // A null title is equivalent to an empty title
276 this.title = (title != null) ? title : "";
277 this.modal = modal;
278 visible = false;
280 setLayout (new BorderLayout ());
283 /*************************************************************************/
286 * Instance Variables
290 * Returns the title of this dialog box.
292 * @return The title of this dialog box.
294 public String
295 getTitle()
297 return(title);
300 /*************************************************************************/
303 * Sets the title of this dialog box to the specified string.
305 * @param title The new title.
307 public synchronized void
308 setTitle(String title)
310 // A null title is equivalent to an empty title
311 this.title = (title != null) ? title : "";
313 if (peer != null)
315 DialogPeer d = (DialogPeer) peer;
316 d.setTitle (title);
320 /*************************************************************************/
323 * Tests whether or not this dialog box is modal.
325 * @return <code>true</code> if this dialog box is modal,
326 * <code>false</code> otherwise.
328 public boolean
329 isModal()
331 return(modal);
334 /*************************************************************************/
337 * Changes the modality of this dialog box. This can only be done before
338 * the peer is created.
340 * @param modal <code>true</code> to make this dialog box modal,
341 * <code>false</code> to make it non-modal.
343 public void
344 setModal(boolean modal)
346 this.modal = modal;
349 /*************************************************************************/
352 * Tests whether or not this dialog box is resizable.
354 * @return <code>true</code> if this dialog is resizable, <code>false</code>,
355 * otherwise.
357 public boolean
358 isResizable()
360 return(resizable);
363 /*************************************************************************/
366 * Changes the resizability of this dialog box.
368 * @param resizable <code>true</code> to make this dialog resizable,
369 * <code>false</code> to make it non-resizable.
371 public synchronized void
372 setResizable(boolean resizable)
374 this.resizable = resizable;
375 if (peer != null)
377 DialogPeer d = (DialogPeer) peer;
378 d.setResizable (resizable);
382 /*************************************************************************/
385 * Creates this object's native peer.
387 public synchronized void
388 addNotify()
390 if (peer == null)
391 peer = getToolkit ().createDialog (this);
392 super.addNotify ();
395 /*************************************************************************/
398 * Makes this dialog visible and brings it to the front.
399 * If the dialog is modal and is not already visible, this call will not
400 * return until the dialog is hidden by someone calling hide or dispose.
401 * If this is the event dispatching thread we must ensure that another event
402 * thread runs while the one which invoked this method is blocked.
404 public synchronized void
405 show()
407 super.show();
409 if (isModal())
411 // If already shown (and blocked) just return
412 if (blocked)
413 return;
415 /* If show is called in the dispatch thread for a modal dialog it will
416 block so we must run another thread so the events keep being
417 dispatched.*/
418 if (EventQueue.isDispatchThread ())
420 EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
421 eq2 = new EventQueue ();
422 eq.push (eq2);
425 try
427 blocked = true;
428 wait ();
429 blocked = false;
431 catch (InterruptedException e)
433 blocked = false;
436 if (eq2 != null)
438 eq2.pop ();
439 eq2 = null;
444 /*************************************************************************/
447 * Hides the Dialog and then
448 * causes show() to return if it is currently blocked.
451 public synchronized void
452 hide ()
454 if (blocked)
456 notifyAll ();
459 super.hide();
462 /*************************************************************************/
465 * Disposes the Dialog and then causes show() to return
466 * if it is currently blocked.
469 public synchronized void
470 dispose ()
472 if (blocked)
474 notifyAll ();
477 super.dispose();
480 /*************************************************************************/
483 * Returns a debugging string for this component.
485 * @return A debugging string for this component.
487 protected String
488 paramString()
490 return ("title+" + title + ",modal=" + modal +
491 ",resizable=" + resizable + "," + super.paramString());
495 * Returns whether this frame is undecorated or not.
497 * @since 1.4
499 public boolean isUndecorated ()
501 return undecorated;
505 * Disables or enables decorations for this frame. This method can only be
506 * called while the frame is not displayable.
508 * @exception IllegalComponentStateException If this frame is displayable.
510 * @since 1.4
512 public void setUndecorated (boolean undecorated)
514 if (isDisplayable ())
515 throw new IllegalComponentStateException ();
517 this.undecorated = undecorated;
520 protected class AccessibleAWTDialog extends AccessibleAWTWindow
522 public AccessibleRole getAccessibleRole()
524 return AccessibleRole.DIALOG;
527 public AccessibleStateSet getAccessibleState()
529 AccessibleStateSet states = super.getAccessibleStateSet();
530 if (isResizable())
531 states.add(AccessibleState.RESIZABLE);
532 if (isModal())
533 states.add(AccessibleState.MODAL);
534 return states;
539 * Gets the AccessibleContext associated with this <code>Dialog</code>.
540 * The context is created, if necessary.
542 * @return the associated context
544 public AccessibleContext getAccessibleContext()
546 /* Create the context if this is the first request */
547 if (accessibleContext == null)
548 accessibleContext = new AccessibleAWTDialog();
549 return accessibleContext;
552 } // class Dialog