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 * Unlike JComponent derivatives, JDialog inherits from java.awt.Dialog. But
57 * also lets a look-and-feel component to its work.
59 * @author Ronald Veldema (rveldema_AT_cs.vu.nl)
61 public class JDialog extends Dialog implements Accessible, WindowConstants,
64 private static final long serialVersionUID = -864070866424508218L;
67 protected AccessibleContext accessibleContext;
69 /** The single RootPane in the Dialog. */
70 protected JRootPane rootPane;
73 * Whether checking is enabled on the RootPane.
75 * @specnote Should be false to comply with J2SE 5.0
77 protected boolean rootPaneCheckingEnabled = false;
79 /** The default action taken when closed. */
80 private int close_action = HIDE_ON_CLOSE;
82 /** Whether JDialogs are decorated by the Look and Feel. */
83 private static boolean decorated;
86 * Whether we're in the init stage or not.
87 * If so, adds and layouts are for top-level, otherwise they're for the
90 private boolean initStageDone = false;
92 /* Creates a new non-modal JDialog with no title
93 * using a shared Frame as the owner.
97 this(SwingUtilities.getOwnerFrame(), "", false, null);
101 * Creates a new non-modal JDialog with no title
102 * using the given owner.
104 * @param owner The owner of the JDialog.
106 public JDialog(Dialog owner)
108 this(owner, "", false, null);
112 * Creates a new JDialog with no title using the
113 * given modal setting and owner.
115 * @param owner The owner of the JDialog.
116 * @param modal Whether the JDialog is modal.
118 public JDialog(Dialog owner, boolean modal)
120 this(owner, "", modal, null);
124 * Creates a new non-modal JDialog using the
125 * given title and owner.
127 * @param owner The owner of the JDialog.
128 * @param title The title of the JDialog.
130 public JDialog(Dialog owner, String title)
132 this(owner, title, false, null);
136 * Creates a new JDialog using the given modal
137 * settings, title, and owner.
139 * @param owner The owner of the JDialog.
140 * @param title The title of the JDialog.
141 * @param modal Whether the JDialog is modal.
143 public JDialog(Dialog owner, String title, boolean modal)
145 this(owner, title, modal, null);
149 * Creates a new JDialog using the given modal
150 * settings, title, owner and graphics configuration.
152 * @param owner The owner of the JDialog.
153 * @param title The title of the JDialog.
154 * @param modal Whether the JDialog is modal.
155 * @param gc The Graphics Configuration to use.
157 public JDialog(Dialog owner, String title, boolean modal,
158 GraphicsConfiguration gc)
160 super(owner, title, modal, gc);
165 * Creates a new non-modal JDialog with no title
166 * using the given owner.
168 * @param owner The owner of the JDialog.
170 public JDialog(Frame owner)
172 this(owner, "", false, null);
176 * Creates a new JDialog with no title using the
177 * given modal setting and owner.
179 * @param owner The owner of the JDialog.
180 * @param modal Whether the JDialog is modal.
182 public JDialog(Frame owner, boolean modal)
184 this(owner, "", modal, null);
188 * Creates a new non-modal JDialog using the
189 * given title and owner.
191 * @param owner The owner of the JDialog.
192 * @param title The title of the JDialog.
194 public JDialog(Frame owner, String title)
196 this(owner, title, false, null);
200 * Creates a new JDialog using the given modal
201 * settings, title, and owner.
203 * @param owner The owner of the JDialog.
204 * @param title The title of the JDialog.
205 * @param modal Whether the JDialog is modal.
207 public JDialog(Frame owner, String title, boolean modal)
209 this(owner, title, modal, null);
213 * Creates a new JDialog using the given modal
214 * settings, title, owner and graphics configuration.
216 * @param owner The owner of the JDialog.
217 * @param title The title of the JDialog.
218 * @param modal Whether the JDialog is modal.
219 * @param gc The Graphics Configuration to use.
221 public JDialog(Frame owner, String title, boolean modal,
222 GraphicsConfiguration gc)
224 super((owner == null) ? SwingUtilities.getOwnerFrame() : owner,
230 * This method is called to initialize the
231 * JDialog. It sets the layout used, the locale,
232 * and creates the RootPane.
234 protected void dialogInit()
236 // FIXME: Do a check on GraphicsEnvironment.isHeadless()
237 setLocale(JComponent.getDefaultLocale());
238 getRootPane(); // Will do set/create.
240 // Now that initStageDone is true, adds and layouts apply to contentPane,
242 initStageDone = true;
246 * This method returns whether JDialogs will have their
247 * window decorations provided by the Look and Feel.
249 * @return Whether the window decorations are Look and Feel provided.
251 public static boolean isDefaultLookAndFeelDecorated()
257 * This method sets whether JDialogs will have their
258 * window decorations provided by the Look and Feel.
260 * @param defaultLookAndFeelDecorated Whether the window
261 * decorations are Look and Feel provided.
263 public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)
265 decorated = defaultLookAndFeelDecorated;
269 * This method returns the preferred size of
272 * @return The preferred size.
274 public Dimension getPreferredSize()
276 Dimension d = super.getPreferredSize();
281 * This method returns the JMenuBar used
284 * @return The JMenuBar in the JDialog.
286 public JMenuBar getJMenuBar()
288 return getRootPane().getJMenuBar();
292 * This method sets the JMenuBar used
295 * @param menubar The JMenuBar to use.
297 public void setJMenuBar(JMenuBar menubar)
299 getRootPane().setJMenuBar(menubar);
303 * This method sets the LayoutManager used in the JDialog.
304 * This method will throw an Error if rootPaneChecking is
307 * @param manager The LayoutManager to use.
309 public void setLayout(LayoutManager manager)
311 // Check if we're in initialization stage. If so, call super.setLayout
312 // otherwise, valid calls go to the content pane.
315 if (isRootPaneCheckingEnabled())
316 throw new Error("Cannot set top-level layout. Use"
317 + " getConentPane().setLayout instead.");
318 getContentPane().setLayout(manager);
321 super.setLayout(manager);
325 * This method sets the JLayeredPane used in the JDialog.
326 * If the given JLayeredPane is null, then this method
327 * will throw an Error.
329 * @param layeredPane The JLayeredPane to use.
331 public void setLayeredPane(JLayeredPane layeredPane)
333 if (layeredPane == null)
334 throw new IllegalComponentStateException("layeredPane cannot be null.");
335 getRootPane().setLayeredPane(layeredPane);
339 * This method returns the JLayeredPane used with this JDialog.
341 * @return The JLayeredPane used with this JDialog.
343 public JLayeredPane getLayeredPane()
345 return getRootPane().getLayeredPane();
349 * This method returns the JRootPane used with this JDialog.
351 * @return The JRootPane used with this JDialog.
353 public JRootPane getRootPane()
355 if (rootPane == null)
356 setRootPane(createRootPane());
361 * This method sets the JRootPane used with this JDialog.
363 * @param root The JRootPane to use.
365 protected void setRootPane(JRootPane root)
367 if (rootPane != null)
376 * This method creates a new JRootPane.
378 * @return A new JRootPane.
380 protected JRootPane createRootPane()
382 return new JRootPane();
386 * This method returns the ContentPane
389 * @return The ContentPane in the JRootPane.
391 public Container getContentPane()
393 return getRootPane().getContentPane();
397 * This method sets the ContentPane to use with this
398 * JDialog. If the ContentPane given is null, this method
399 * will throw an exception.
401 * @param contentPane The ContentPane to use with the JDialog.
403 public void setContentPane(Container contentPane)
405 if (contentPane == null)
406 throw new IllegalComponentStateException("contentPane cannot be null.");
407 getRootPane().setContentPane(contentPane);
411 * This method returns the GlassPane for this JDialog.
413 * @return The GlassPane for this JDialog.
415 public Component getGlassPane()
417 return getRootPane().getGlassPane();
421 * This method sets the GlassPane for this JDialog.
423 * @param glassPane The GlassPane for this JDialog.
425 public void setGlassPane(Component glassPane)
427 getRootPane().setGlassPane(glassPane);
431 * This method is called when a component is added to the
432 * the JDialog. Calling this method with rootPaneCheckingEnabled
433 * will cause an Error to be thrown.
435 * @param comp The component to add.
436 * @param constraints The constraints.
437 * @param index The position of the component.
439 protected void addImpl(Component comp, Object constraints, int index)
441 // If we're adding in the initialization stage use super.add.
442 // Otherwise pass the add onto the content pane.
444 super.addImpl(comp, constraints, index);
447 if (isRootPaneCheckingEnabled())
448 throw new Error("Do not add directly to JDialog."
449 + " Use getContentPane().add instead.");
450 getContentPane().add(comp, constraints, index);
455 * This method removes a component from the JDialog.
457 * @param comp The component to remove.
459 public void remove(Component comp)
461 // If we're removing the root pane, use super.remove. Otherwise
462 // pass it on to the content pane instead.
463 if (comp == rootPane)
464 super.remove(rootPane);
466 getContentPane().remove(comp);
470 * This method returns whether rootPane checking is enabled.
472 * @return Whether rootPane checking is enabled.
474 protected boolean isRootPaneCheckingEnabled()
476 return rootPaneCheckingEnabled;
480 * This method sets whether rootPane checking is enabled.
482 * @param enabled Whether rootPane checking is enabled.
484 protected void setRootPaneCheckingEnabled(boolean enabled)
486 rootPaneCheckingEnabled = enabled;
490 * This method simply calls paint and returns.
492 * @param g The Graphics object to paint with.
494 public void update(Graphics g)
501 * This method handles window events. This allows the JDialog
502 * to honour its default close operation.
504 * @param e The WindowEvent.
506 protected void processWindowEvent(WindowEvent e)
508 // System.out.println("PROCESS_WIN_EV-1: " + e);
509 super.processWindowEvent(e);
510 // System.out.println("PROCESS_WIN_EV-2: " + e);
513 case WindowEvent.WINDOW_CLOSING:
515 switch (getDefaultCloseOperation())
517 case DISPOSE_ON_CLOSE:
527 case DO_NOTHING_ON_CLOSE:
532 case WindowEvent.WINDOW_CLOSED:
533 case WindowEvent.WINDOW_OPENED:
534 case WindowEvent.WINDOW_ICONIFIED:
535 case WindowEvent.WINDOW_DEICONIFIED:
536 case WindowEvent.WINDOW_ACTIVATED:
537 case WindowEvent.WINDOW_DEACTIVATED:
543 * This method sets the action to take
544 * when the JDialog is closed.
546 * @param operation The action to take.
548 public void setDefaultCloseOperation(int operation)
550 /* Reference implementation allows invalid operations
551 to be specified. If so, getDefaultCloseOperation
552 must return the invalid code, and the behaviour
553 defaults to DO_NOTHING_ON_CLOSE. processWindowEvent
554 above handles this */
555 close_action = operation;
559 * This method returns the action taken when
560 * the JDialog is closed.
562 * @return The action to take.
564 public int getDefaultCloseOperation()
570 * This method returns a String describing the JDialog.
572 * @return A String describing the JDialog.
574 protected String paramString()
582 * @return DOCUMENT ME!
584 public AccessibleContext getAccessibleContext()