Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / JDialog.java
blobace284bd5da9dda1ec8a9d21319ed57a98f1fa27
1 /* JDialog.java --
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)
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 javax.swing;
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;
55 /**
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,
62 RootPaneContainer
64 private static final long serialVersionUID = -864070866424508218L;
66 /** DOCUMENT ME! */
67 protected AccessibleContext accessibleContext;
69 /** The single RootPane in the Dialog. */
70 protected JRootPane rootPane;
72 /** Whether checking is enabled on the RootPane */
73 protected boolean rootPaneCheckingEnabled = true;
75 /** The default action taken when closed. */
76 private int close_action = HIDE_ON_CLOSE;
78 /** Whether JDialogs are decorated by the Look and Feel. */
79 private static boolean decorated;
81 /**
82 * Creates a new non-modal JDialog with no title
83 * using a shared Frame as the owner.
85 public JDialog()
87 this(SwingUtilities.getOwnerFrame(), "", false, null);
90 /**
91 * Creates a new non-modal JDialog with no title
92 * using the given owner.
94 * @param owner The owner of the JDialog.
96 public JDialog(Dialog owner)
98 this(owner, "", false, null);
102 * Creates a new JDialog with no title using the
103 * given modal setting and owner.
105 * @param owner The owner of the JDialog.
106 * @param modal Whether the JDialog is modal.
108 public JDialog(Dialog owner, boolean modal)
110 this(owner, "", modal, null);
114 * Creates a new non-modal JDialog using the
115 * given title and owner.
117 * @param owner The owner of the JDialog.
118 * @param title The title of the JDialog.
120 public JDialog(Dialog owner, String title)
122 this(owner, title, false, null);
126 * Creates a new JDialog using the given modal
127 * settings, title, and owner.
129 * @param owner The owner of the JDialog.
130 * @param title The title of the JDialog.
131 * @param modal Whether the JDialog is modal.
133 public JDialog(Dialog owner, String title, boolean modal)
135 this(owner, title, modal, null);
139 * Creates a new JDialog using the given modal
140 * settings, title, owner and graphics configuration.
142 * @param owner The owner of the JDialog.
143 * @param title The title of the JDialog.
144 * @param modal Whether the JDialog is modal.
145 * @param gc The Graphics Configuration to use.
147 public JDialog(Dialog owner, String title, boolean modal,
148 GraphicsConfiguration gc)
150 super(owner, title, modal, gc);
151 dialogInit();
155 * Creates a new non-modal JDialog with no title
156 * using the given owner.
158 * @param owner The owner of the JDialog.
160 public JDialog(Frame owner)
162 this(owner, "", false, null);
166 * Creates a new JDialog with no title using the
167 * given modal setting and owner.
169 * @param owner The owner of the JDialog.
170 * @param modal Whether the JDialog is modal.
172 public JDialog(Frame owner, boolean modal)
174 this(owner, "", modal, null);
178 * Creates a new non-modal JDialog using the
179 * given title and owner.
181 * @param owner The owner of the JDialog.
182 * @param title The title of the JDialog.
184 public JDialog(Frame owner, String title)
186 this(owner, title, false, null);
190 * Creates a new JDialog using the given modal
191 * settings, title, and owner.
193 * @param owner The owner of the JDialog.
194 * @param title The title of the JDialog.
195 * @param modal Whether the JDialog is modal.
197 public JDialog(Frame owner, String title, boolean modal)
199 this(owner, title, modal, null);
203 * Creates a new JDialog using the given modal
204 * settings, title, owner and graphics configuration.
206 * @param owner The owner of the JDialog.
207 * @param title The title of the JDialog.
208 * @param modal Whether the JDialog is modal.
209 * @param gc The Graphics Configuration to use.
211 public JDialog(Frame owner, String title, boolean modal,
212 GraphicsConfiguration gc)
214 super((owner == null) ? SwingUtilities.getOwnerFrame() : owner,
215 title, modal, gc);
216 dialogInit();
220 * This method is called to initialize the
221 * JDialog. It sets the layout used, the locale,
222 * and creates the RootPane.
224 protected void dialogInit()
226 // FIXME: Do a check on GraphicsEnvironment.isHeadless()
227 setRootPaneCheckingEnabled(false);
228 setLocale(JComponent.getDefaultLocale());
229 getRootPane(); // will do set/create
230 setRootPaneCheckingEnabled(true);
231 invalidate();
236 * This method returns whether JDialogs will have their
237 * window decorations provided by the Look and Feel.
239 * @return Whether the window decorations are Look and Feel provided.
241 public static boolean isDefaultLookAndFeelDecorated()
243 return decorated;
247 * This method sets whether JDialogs will have their
248 * window decorations provided by the Look and Feel.
250 * @param defaultLookAndFeelDecorated Whether the window
251 * decorations are Look and Feel provided.
253 public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)
255 decorated = defaultLookAndFeelDecorated;
259 * This method returns the preferred size of
260 * the JDialog.
262 * @return The preferred size.
264 public Dimension getPreferredSize()
266 Dimension d = super.getPreferredSize();
267 return d;
271 * This method returns the JMenuBar used
272 * in this JDialog.
274 * @return The JMenuBar in the JDialog.
276 public JMenuBar getJMenuBar()
278 return getRootPane().getJMenuBar();
282 * This method sets the JMenuBar used
283 * in this JDialog.
285 * @param menubar The JMenuBar to use.
287 public void setJMenuBar(JMenuBar menubar)
289 getRootPane().setJMenuBar(menubar);
293 * This method sets the LayoutManager used in the JDialog.
294 * This method will throw an Error if rootPaneChecking is
295 * enabled.
297 * @param manager The LayoutManager to use.
299 public void setLayout(LayoutManager manager)
301 if (isRootPaneCheckingEnabled())
302 throw new Error("rootPaneChecking is enabled - cannot set layout.");
303 super.setLayout(manager);
307 * This method sets the JLayeredPane used in the JDialog.
308 * If the given JLayeredPane is null, then this method
309 * will throw an Error.
311 * @param layeredPane The JLayeredPane to use.
313 public void setLayeredPane(JLayeredPane layeredPane)
315 if (layeredPane == null)
316 throw new IllegalComponentStateException("layeredPane cannot be null.");
317 getRootPane().setLayeredPane(layeredPane);
321 * This method returns the JLayeredPane used with this JDialog.
323 * @return The JLayeredPane used with this JDialog.
325 public JLayeredPane getLayeredPane()
327 return getRootPane().getLayeredPane();
331 * This method returns the JRootPane used with this JDialog.
333 * @return The JRootPane used with this JDialog.
335 public JRootPane getRootPane()
337 if (rootPane == null)
338 setRootPane(createRootPane());
339 return rootPane;
343 * This method sets the JRootPane used with this JDialog.
345 * @param root The JRootPane to use.
347 protected void setRootPane(JRootPane root)
349 if (rootPane != null)
350 remove(rootPane);
352 rootPane = root;
353 rootPane.show();
354 add(rootPane);
358 * This method creates a new JRootPane.
360 * @return A new JRootPane.
362 protected JRootPane createRootPane()
364 return new JRootPane();
368 * This method returns the ContentPane
369 * in the JRootPane.
371 * @return The ContentPane in the JRootPane.
373 public Container getContentPane()
375 return getRootPane().getContentPane();
379 * This method sets the ContentPane to use with this
380 * JDialog. If the ContentPane given is null, this method
381 * will throw an exception.
383 * @param contentPane The ContentPane to use with the JDialog.
385 public void setContentPane(Container contentPane)
387 if (contentPane == null)
388 throw new IllegalComponentStateException("contentPane cannot be null.");
389 getRootPane().setContentPane(contentPane);
393 * This method returns the GlassPane for this JDialog.
395 * @return The GlassPane for this JDialog.
397 public Component getGlassPane()
399 return getRootPane().getGlassPane();
403 * This method sets the GlassPane for this JDialog.
405 * @param glassPane The GlassPane for this JDialog.
407 public void setGlassPane(Component glassPane)
409 getRootPane().setGlassPane(glassPane);
413 * This method is called when a component is added to the
414 * the JDialog. Calling this method with rootPaneCheckingEnabled
415 * will cause an Error to be thrown.
417 * @param comp The component to add.
418 * @param constraints The constraints.
419 * @param index The position of the component.
421 protected void addImpl(Component comp, Object constraints, int index)
423 if (isRootPaneCheckingEnabled())
424 throw new Error("rootPaneChecking is enabled - adding components disallowed.");
425 super.addImpl(comp, constraints, index);
429 * This method removes a component from the JDialog.
431 * @param comp The component to remove.
433 public void remove(Component comp)
435 // The path changes if the component == root.
436 if (comp == rootPane)
437 super.remove(rootPane);
438 else
439 getContentPane().remove(comp);
443 * This method returns whether rootPane checking is enabled.
445 * @return Whether rootPane checking is enabled.
447 protected boolean isRootPaneCheckingEnabled()
449 return rootPaneCheckingEnabled;
453 * This method sets whether rootPane checking is enabled.
455 * @param enabled Whether rootPane checking is enabled.
457 protected void setRootPaneCheckingEnabled(boolean enabled)
459 rootPaneCheckingEnabled = enabled;
463 * This method simply calls paint and returns.
465 * @param g The Graphics object to paint with.
467 public void update(Graphics g)
469 paint(g);
474 * This method handles window events. This allows the JDialog
475 * to honour its default close operation.
477 * @param e The WindowEvent.
479 protected void processWindowEvent(WindowEvent e)
481 // System.out.println("PROCESS_WIN_EV-1: " + e);
482 super.processWindowEvent(e);
483 // System.out.println("PROCESS_WIN_EV-2: " + e);
484 switch (e.getID())
486 case WindowEvent.WINDOW_CLOSING:
488 switch (getDefaultCloseOperation())
490 case DISPOSE_ON_CLOSE:
492 dispose();
493 break;
495 case HIDE_ON_CLOSE:
497 setVisible(false);
498 break;
500 case DO_NOTHING_ON_CLOSE:
501 break;
503 break;
505 case WindowEvent.WINDOW_CLOSED:
506 case WindowEvent.WINDOW_OPENED:
507 case WindowEvent.WINDOW_ICONIFIED:
508 case WindowEvent.WINDOW_DEICONIFIED:
509 case WindowEvent.WINDOW_ACTIVATED:
510 case WindowEvent.WINDOW_DEACTIVATED:
511 break;
516 * This method sets the action to take
517 * when the JDialog is closed.
519 * @param operation The action to take.
521 public void setDefaultCloseOperation(int operation)
523 if (operation == DO_NOTHING_ON_CLOSE ||
524 operation == HIDE_ON_CLOSE ||
525 operation == DISPOSE_ON_CLOSE)
526 close_action = operation;
527 else
528 throw new IllegalArgumentException("Default close operation must be one of DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, or DISPOSE_ON_CLOSE");
532 * This method returns the action taken when
533 * the JDialog is closed.
535 * @return The action to take.
537 public int getDefaultCloseOperation()
539 return close_action;
543 * This method returns a String describing the JDialog.
545 * @return A String describing the JDialog.
547 protected String paramString()
549 return "JDialog";
553 * DOCUMENT ME!
555 * @return DOCUMENT ME!
557 public AccessibleContext getAccessibleContext()
559 return null;