2 Copyright (C) 2002, 2004 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)
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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. */
41 import java.awt.Component;
42 import java.awt.Container;
43 import java.awt.Dialog;
44 import java.awt.Dimension;
45 import java.awt.Frame;
46 import java.awt.Graphics;
47 import java.awt.GraphicsConfiguration;
48 import java.awt.IllegalComponentStateException;
49 import java.awt.LayoutManager;
50 import java.awt.event.WindowEvent;
52 import javax.accessibility.Accessible;
53 import javax.accessibility.AccessibleContext;
56 * A dialog window. This is an extension of {@link java.awt.Dialog} that
57 * provides support for the Swing architecture. Most importantly it contains a
58 * {@link JRootPane} as it's only top-level child, that manages the content
59 * pane, the menu and a glass pane.
61 * Also, unlike <code>java.awt.Dialog</code>s, JDialogs support the
62 * Swing Pluggable Look & Feel architecture.
64 * @author Ronald Veldema (rveldema@cs.vu.nl)
66 public class JDialog extends Dialog implements Accessible, WindowConstants,
69 private static final long serialVersionUID = -864070866424508218L;
72 protected AccessibleContext accessibleContext;
74 /** The single RootPane in the Dialog. */
75 protected JRootPane rootPane;
78 * Whether checking is enabled on the RootPane.
80 * @specnote Should be false to comply with J2SE 5.0
82 protected boolean rootPaneCheckingEnabled = false;
84 /** The default action taken when closed. */
85 private int close_action = HIDE_ON_CLOSE;
87 /** Whether JDialogs are decorated by the Look and Feel. */
88 private static boolean decorated;
91 * Whether we're in the init stage or not.
92 * If so, adds and layouts are for top-level, otherwise they're for the
95 private boolean initStageDone = false;
97 /* Creates a new non-modal JDialog with no title
98 * using a shared Frame as the owner.
102 this(SwingUtilities.getOwnerFrame(), "", false, null);
106 * Creates a new non-modal JDialog with no title
107 * using the given owner.
109 * @param owner The owner of the JDialog.
111 public JDialog(Dialog owner)
113 this(owner, "", false, null);
117 * Creates a new JDialog with no title using the
118 * given modal setting and owner.
120 * @param owner The owner of the JDialog.
121 * @param modal Whether the JDialog is modal.
123 public JDialog(Dialog owner, boolean modal)
125 this(owner, "", modal, null);
129 * Creates a new non-modal JDialog using the
130 * given title and owner.
132 * @param owner The owner of the JDialog.
133 * @param title The title of the JDialog.
135 public JDialog(Dialog owner, String title)
137 this(owner, title, false, null);
141 * Creates a new JDialog using the given modal
142 * settings, title, and owner.
144 * @param owner The owner of the JDialog.
145 * @param title The title of the JDialog.
146 * @param modal Whether the JDialog is modal.
148 public JDialog(Dialog owner, String title, boolean modal)
150 this(owner, title, modal, null);
154 * Creates a new JDialog using the given modal
155 * settings, title, owner and graphics configuration.
157 * @param owner The owner of the JDialog.
158 * @param title The title of the JDialog.
159 * @param modal Whether the JDialog is modal.
160 * @param gc The Graphics Configuration to use.
162 public JDialog(Dialog owner, String title, boolean modal,
163 GraphicsConfiguration gc)
165 super(owner, title, modal, gc);
170 * Creates a new non-modal JDialog with no title
171 * using the given owner.
173 * @param owner The owner of the JDialog.
175 public JDialog(Frame owner)
177 this(owner, "", false, null);
181 * Creates a new JDialog with no title using the
182 * given modal setting and owner.
184 * @param owner The owner of the JDialog.
185 * @param modal Whether the JDialog is modal.
187 public JDialog(Frame owner, boolean modal)
189 this(owner, "", modal, null);
193 * Creates a new non-modal JDialog using the
194 * given title and owner.
196 * @param owner The owner of the JDialog.
197 * @param title The title of the JDialog.
199 public JDialog(Frame owner, String title)
201 this(owner, title, false, null);
205 * Creates a new JDialog using the given modal
206 * settings, title, and owner.
208 * @param owner The owner of the JDialog.
209 * @param title The title of the JDialog.
210 * @param modal Whether the JDialog is modal.
212 public JDialog(Frame owner, String title, boolean modal)
214 this(owner, title, modal, null);
218 * Creates a new JDialog using the given modal
219 * settings, title, owner and graphics configuration.
221 * @param owner The owner of the JDialog.
222 * @param title The title of the JDialog.
223 * @param modal Whether the JDialog is modal.
224 * @param gc The Graphics Configuration to use.
226 public JDialog(Frame owner, String title, boolean modal,
227 GraphicsConfiguration gc)
229 super((owner == null) ? SwingUtilities.getOwnerFrame() : owner,
235 * This method is called to initialize the
236 * JDialog. It sets the layout used, the locale,
237 * and creates the RootPane.
239 protected void dialogInit()
241 // FIXME: Do a check on GraphicsEnvironment.isHeadless()
242 setLocale(JComponent.getDefaultLocale());
243 getRootPane(); // Will do set/create.
245 // Now that initStageDone is true, adds and layouts apply to contentPane,
247 initStageDone = true;
251 * This method returns whether JDialogs will have their
252 * window decorations provided by the Look and Feel.
254 * @return Whether the window decorations are Look and Feel provided.
256 public static boolean isDefaultLookAndFeelDecorated()
262 * This method sets whether JDialogs will have their
263 * window decorations provided by the Look and Feel.
265 * @param defaultLookAndFeelDecorated Whether the window
266 * decorations are Look and Feel provided.
268 public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)
270 decorated = defaultLookAndFeelDecorated;
274 * This method returns the preferred size of
277 * @return The preferred size.
279 public Dimension getPreferredSize()
281 Dimension d = super.getPreferredSize();
286 * This method returns the JMenuBar used
289 * @return The JMenuBar in the JDialog.
291 public JMenuBar getJMenuBar()
293 return getRootPane().getJMenuBar();
297 * This method sets the JMenuBar used
300 * @param menubar The JMenuBar to use.
302 public void setJMenuBar(JMenuBar menubar)
304 getRootPane().setJMenuBar(menubar);
308 * This method sets the LayoutManager used in the JDialog.
309 * This method will throw an Error if rootPaneChecking is
312 * @param manager The LayoutManager to use.
314 public void setLayout(LayoutManager manager)
316 // Check if we're in initialization stage. If so, call super.setLayout
317 // otherwise, valid calls go to the content pane.
320 if (isRootPaneCheckingEnabled())
321 throw new Error("Cannot set top-level layout. Use"
322 + " getConentPane().setLayout instead.");
323 getContentPane().setLayout(manager);
326 super.setLayout(manager);
330 * This method sets the JLayeredPane used in the JDialog.
331 * If the given JLayeredPane is null, then this method
332 * will throw an Error.
334 * @param layeredPane The JLayeredPane to use.
336 public void setLayeredPane(JLayeredPane layeredPane)
338 if (layeredPane == null)
339 throw new IllegalComponentStateException("layeredPane cannot be null.");
340 getRootPane().setLayeredPane(layeredPane);
344 * This method returns the JLayeredPane used with this JDialog.
346 * @return The JLayeredPane used with this JDialog.
348 public JLayeredPane getLayeredPane()
350 return getRootPane().getLayeredPane();
354 * This method returns the JRootPane used with this JDialog.
356 * @return The JRootPane used with this JDialog.
358 public JRootPane getRootPane()
360 if (rootPane == null)
361 setRootPane(createRootPane());
366 * This method sets the JRootPane used with this JDialog.
368 * @param root The JRootPane to use.
370 protected void setRootPane(JRootPane root)
372 if (rootPane != null)
381 * This method creates a new JRootPane.
383 * @return A new JRootPane.
385 protected JRootPane createRootPane()
387 return new JRootPane();
391 * This method returns the ContentPane
394 * @return The ContentPane in the JRootPane.
396 public Container getContentPane()
398 return getRootPane().getContentPane();
402 * This method sets the ContentPane to use with this
403 * JDialog. If the ContentPane given is null, this method
404 * will throw an exception.
406 * @param contentPane The ContentPane to use with the JDialog.
408 public void setContentPane(Container contentPane)
410 if (contentPane == null)
411 throw new IllegalComponentStateException("contentPane cannot be null.");
412 getRootPane().setContentPane(contentPane);
416 * This method returns the GlassPane for this JDialog.
418 * @return The GlassPane for this JDialog.
420 public Component getGlassPane()
422 return getRootPane().getGlassPane();
426 * This method sets the GlassPane for this JDialog.
428 * @param glassPane The GlassPane for this JDialog.
430 public void setGlassPane(Component glassPane)
432 getRootPane().setGlassPane(glassPane);
436 * This method is called when a component is added to the
437 * the JDialog. Calling this method with rootPaneCheckingEnabled
438 * will cause an Error to be thrown.
440 * @param comp The component to add.
441 * @param constraints The constraints.
442 * @param index The position of the component.
444 protected void addImpl(Component comp, Object constraints, int index)
446 // If we're adding in the initialization stage use super.add.
447 // Otherwise pass the add onto the content pane.
449 super.addImpl(comp, constraints, index);
452 if (isRootPaneCheckingEnabled())
453 throw new Error("Do not add directly to JDialog."
454 + " Use getContentPane().add instead.");
455 getContentPane().add(comp, constraints, index);
460 * This method removes a component from the JDialog.
462 * @param comp The component to remove.
464 public void remove(Component comp)
466 // If we're removing the root pane, use super.remove. Otherwise
467 // pass it on to the content pane instead.
468 if (comp == rootPane)
469 super.remove(rootPane);
471 getContentPane().remove(comp);
475 * This method returns whether rootPane checking is enabled.
477 * @return Whether rootPane checking is enabled.
479 protected boolean isRootPaneCheckingEnabled()
481 return rootPaneCheckingEnabled;
485 * This method sets whether rootPane checking is enabled.
487 * @param enabled Whether rootPane checking is enabled.
489 protected void setRootPaneCheckingEnabled(boolean enabled)
491 rootPaneCheckingEnabled = enabled;
495 * This method simply calls paint and returns.
497 * @param g The Graphics object to paint with.
499 public void update(Graphics g)
506 * This method handles window events. This allows the JDialog
507 * to honour its default close operation.
509 * @param e The WindowEvent.
511 protected void processWindowEvent(WindowEvent e)
513 // System.out.println("PROCESS_WIN_EV-1: " + e);
514 super.processWindowEvent(e);
515 // System.out.println("PROCESS_WIN_EV-2: " + e);
518 case WindowEvent.WINDOW_CLOSING:
520 switch (getDefaultCloseOperation())
522 case DISPOSE_ON_CLOSE:
532 case DO_NOTHING_ON_CLOSE:
537 case WindowEvent.WINDOW_CLOSED:
538 case WindowEvent.WINDOW_OPENED:
539 case WindowEvent.WINDOW_ICONIFIED:
540 case WindowEvent.WINDOW_DEICONIFIED:
541 case WindowEvent.WINDOW_ACTIVATED:
542 case WindowEvent.WINDOW_DEACTIVATED:
548 * This method sets the action to take
549 * when the JDialog is closed.
551 * @param operation The action to take.
553 public void setDefaultCloseOperation(int operation)
555 /* Reference implementation allows invalid operations
556 to be specified. If so, getDefaultCloseOperation
557 must return the invalid code, and the behaviour
558 defaults to DO_NOTHING_ON_CLOSE. processWindowEvent
559 above handles this */
560 close_action = operation;
564 * This method returns the action taken when
565 * the JDialog is closed.
567 * @return The action to take.
569 public int getDefaultCloseOperation()
575 * This method returns a String describing the JDialog.
577 * @return A String describing the JDialog.
579 protected String paramString()
587 * @return DOCUMENT ME!
589 public AccessibleContext getAccessibleContext()