Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / javax / swing / JDialog.java
blobb3f7c011f6859914e44e33bbe03b901c47e563b6
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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 * 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 &amp; Feel architecture.
64 * @author Ronald Veldema (rveldema@cs.vu.nl)
66 public class JDialog extends Dialog implements Accessible, WindowConstants,
67 RootPaneContainer
69 /**
70 * Provides accessibility support for <code>JDialog</code>s.
72 protected class AccessibleJDialog extends Dialog.AccessibleAWTDialog
74 /**
75 * Creates a new instance of <code>AccessibleJDialog</code>.
77 public AccessibleJDialog()
79 super();
80 // Nothing to do here.
84 private static final long serialVersionUID = -864070866424508218L;
86 /** DOCUMENT ME! */
87 protected AccessibleContext accessibleContext;
89 /** The single RootPane in the Dialog. */
90 protected JRootPane rootPane;
92 /**
93 * Whether checking is enabled on the RootPane.
95 * @specnote Should be false to comply with J2SE 5.0
96 */
97 protected boolean rootPaneCheckingEnabled = false;
99 /** The default action taken when closed. */
100 private int close_action = HIDE_ON_CLOSE;
102 /** Whether JDialogs are decorated by the Look and Feel. */
103 private static boolean decorated;
105 /* Creates a new non-modal JDialog with no title
106 * using a shared Frame as the owner.
108 public JDialog()
110 this(SwingUtilities.getOwnerFrame(), "", false, null);
114 * Creates a new non-modal JDialog with no title
115 * using the given owner.
117 * @param owner The owner of the JDialog.
119 public JDialog(Dialog owner)
121 this(owner, "", false, null);
125 * Creates a new JDialog with no title using the
126 * given modal setting and owner.
128 * @param owner The owner of the JDialog.
129 * @param modal Whether the JDialog is modal.
131 public JDialog(Dialog owner, boolean modal)
133 this(owner, "", modal, null);
137 * Creates a new non-modal JDialog using the
138 * given title and owner.
140 * @param owner The owner of the JDialog.
141 * @param title The title of the JDialog.
143 public JDialog(Dialog owner, String title)
145 this(owner, title, false, null);
149 * Creates a new JDialog using the given modal
150 * settings, title, and owner.
152 * @param owner The owner of the JDialog.
153 * @param title The title of the JDialog.
154 * @param modal Whether the JDialog is modal.
156 public JDialog(Dialog owner, String title, boolean modal)
158 this(owner, title, modal, null);
162 * Creates a new JDialog using the given modal
163 * settings, title, owner and graphics configuration.
165 * @param owner The owner of the JDialog.
166 * @param title The title of the JDialog.
167 * @param modal Whether the JDialog is modal.
168 * @param gc The Graphics Configuration to use.
170 public JDialog(Dialog owner, String title, boolean modal,
171 GraphicsConfiguration gc)
173 super(owner, title, modal, gc);
174 dialogInit();
178 * Creates a new non-modal JDialog with no title
179 * using the given owner.
181 * @param owner The owner of the JDialog.
183 public JDialog(Frame owner)
185 this(owner, "", false, null);
189 * Creates a new JDialog with no title using the
190 * given modal setting and owner.
192 * @param owner The owner of the JDialog.
193 * @param modal Whether the JDialog is modal.
195 public JDialog(Frame owner, boolean modal)
197 this(owner, "", modal, null);
201 * Creates a new non-modal JDialog using the
202 * given title and owner.
204 * @param owner The owner of the JDialog.
205 * @param title The title of the JDialog.
207 public JDialog(Frame owner, String title)
209 this(owner, title, false, null);
213 * Creates a new JDialog using the given modal
214 * settings, title, and owner.
216 * @param owner The owner of the JDialog.
217 * @param title The title of the JDialog.
218 * @param modal Whether the JDialog is modal.
220 public JDialog(Frame owner, String title, boolean modal)
222 this(owner, title, modal, null);
226 * Creates a new JDialog using the given modal
227 * settings, title, owner and graphics configuration.
229 * @param owner The owner of the JDialog.
230 * @param title The title of the JDialog.
231 * @param modal Whether the JDialog is modal.
232 * @param gc The Graphics Configuration to use.
234 public JDialog(Frame owner, String title, boolean modal,
235 GraphicsConfiguration gc)
237 super((owner == null) ? SwingUtilities.getOwnerFrame() : owner,
238 title, modal, gc);
239 dialogInit();
243 * This method is called to initialize the
244 * JDialog. It sets the layout used, the locale,
245 * and creates the RootPane.
247 protected void dialogInit()
249 // FIXME: Do a check on GraphicsEnvironment.isHeadless()
250 setLocale(JComponent.getDefaultLocale());
251 getRootPane(); // Will do set/create.
252 invalidate();
253 // Now that initStageDone is true, adds and layouts apply to contentPane,
254 // not top-level.
255 setRootPaneCheckingEnabled(true);
259 * This method returns whether JDialogs will have their
260 * window decorations provided by the Look and Feel.
262 * @return Whether the window decorations are Look and Feel provided.
264 public static boolean isDefaultLookAndFeelDecorated()
266 return decorated;
270 * This method sets whether JDialogs will have their
271 * window decorations provided by the Look and Feel.
273 * @param defaultLookAndFeelDecorated Whether the window
274 * decorations are Look and Feel provided.
276 public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)
278 decorated = defaultLookAndFeelDecorated;
282 * This method returns the preferred size of
283 * the JDialog.
285 * @return The preferred size.
287 public Dimension getPreferredSize()
289 Dimension d = super.getPreferredSize();
290 return d;
294 * This method returns the JMenuBar used
295 * in this JDialog.
297 * @return The JMenuBar in the JDialog.
299 public JMenuBar getJMenuBar()
301 return getRootPane().getJMenuBar();
305 * This method sets the JMenuBar used
306 * in this JDialog.
308 * @param menubar The JMenuBar to use.
310 public void setJMenuBar(JMenuBar menubar)
312 getRootPane().setJMenuBar(menubar);
316 * This method sets the LayoutManager used in the JDialog.
317 * This method will throw an Error if rootPaneChecking is
318 * enabled.
320 * @param manager The LayoutManager to use.
322 public void setLayout(LayoutManager manager)
324 // Check if we're in initialization stage. If so, call super.setLayout
325 // otherwise, valid calls go to the content pane.
326 if (isRootPaneCheckingEnabled())
327 getContentPane().setLayout(manager);
328 else
329 super.setLayout(manager);
333 * This method sets the JLayeredPane used in the JDialog.
334 * If the given JLayeredPane is null, then this method
335 * will throw an Error.
337 * @param layeredPane The JLayeredPane to use.
339 public void setLayeredPane(JLayeredPane layeredPane)
341 if (layeredPane == null)
342 throw new IllegalComponentStateException("layeredPane cannot be null.");
343 getRootPane().setLayeredPane(layeredPane);
347 * This method returns the JLayeredPane used with this JDialog.
349 * @return The JLayeredPane used with this JDialog.
351 public JLayeredPane getLayeredPane()
353 return getRootPane().getLayeredPane();
357 * This method returns the JRootPane used with this JDialog.
359 * @return The JRootPane used with this JDialog.
361 public JRootPane getRootPane()
363 if (rootPane == null)
364 setRootPane(createRootPane());
365 return rootPane;
369 * This method sets the JRootPane used with this JDialog.
371 * @param root The JRootPane to use.
373 protected void setRootPane(JRootPane root)
375 if (rootPane != null)
376 remove(rootPane);
378 rootPane = root;
379 rootPane.show();
380 add(rootPane);
384 * This method creates a new JRootPane.
386 * @return A new JRootPane.
388 protected JRootPane createRootPane()
390 return new JRootPane();
394 * This method returns the ContentPane
395 * in the JRootPane.
397 * @return The ContentPane in the JRootPane.
399 public Container getContentPane()
401 return getRootPane().getContentPane();
405 * This method sets the ContentPane to use with this
406 * JDialog. If the ContentPane given is null, this method
407 * will throw an exception.
409 * @param contentPane The ContentPane to use with the JDialog.
411 public void setContentPane(Container contentPane)
413 if (contentPane == null)
414 throw new IllegalComponentStateException("contentPane cannot be null.");
415 getRootPane().setContentPane(contentPane);
419 * This method returns the GlassPane for this JDialog.
421 * @return The GlassPane for this JDialog.
423 public Component getGlassPane()
425 return getRootPane().getGlassPane();
429 * This method sets the GlassPane for this JDialog.
431 * @param glassPane The GlassPane for this JDialog.
433 public void setGlassPane(Component glassPane)
435 getRootPane().setGlassPane(glassPane);
439 * This method is called when a component is added to the
440 * the JDialog. Calling this method with rootPaneCheckingEnabled
441 * will cause an Error to be thrown.
443 * @param comp The component to add.
444 * @param constraints The constraints.
445 * @param index The position of the component.
447 protected void addImpl(Component comp, Object constraints, int index)
449 // If we're adding in the initialization stage use super.add.
450 // Otherwise pass the add onto the content pane.
451 if (isRootPaneCheckingEnabled())
452 getContentPane().add(comp, constraints, index);
453 else
454 super.addImpl(comp, constraints, index);
458 * This method removes a component from the JDialog.
460 * @param comp The component to remove.
462 public void remove(Component comp)
464 // If we're removing the root pane, use super.remove. Otherwise
465 // pass it on to the content pane instead.
466 if (comp == rootPane)
467 super.remove(rootPane);
468 else
469 getContentPane().remove(comp);
473 * This method returns whether rootPane checking is enabled.
475 * @return Whether rootPane checking is enabled.
477 protected boolean isRootPaneCheckingEnabled()
479 return rootPaneCheckingEnabled;
483 * This method sets whether rootPane checking is enabled.
485 * @param enabled Whether rootPane checking is enabled.
487 protected void setRootPaneCheckingEnabled(boolean enabled)
489 rootPaneCheckingEnabled = enabled;
493 * This method simply calls paint and returns.
495 * @param g The Graphics object to paint with.
497 public void update(Graphics g)
499 paint(g);
504 * This method handles window events. This allows the JDialog
505 * to honour its default close operation.
507 * @param e The WindowEvent.
509 protected void processWindowEvent(WindowEvent e)
511 // System.out.println("PROCESS_WIN_EV-1: " + e);
512 super.processWindowEvent(e);
513 // System.out.println("PROCESS_WIN_EV-2: " + e);
514 switch (e.getID())
516 case WindowEvent.WINDOW_CLOSING:
518 switch (getDefaultCloseOperation())
520 case DISPOSE_ON_CLOSE:
522 dispose();
523 break;
525 case HIDE_ON_CLOSE:
527 setVisible(false);
528 break;
530 case DO_NOTHING_ON_CLOSE:
531 break;
533 break;
535 case WindowEvent.WINDOW_CLOSED:
536 case WindowEvent.WINDOW_OPENED:
537 case WindowEvent.WINDOW_ICONIFIED:
538 case WindowEvent.WINDOW_DEICONIFIED:
539 case WindowEvent.WINDOW_ACTIVATED:
540 case WindowEvent.WINDOW_DEACTIVATED:
541 break;
546 * This method sets the action to take
547 * when the JDialog is closed.
549 * @param operation The action to take.
551 public void setDefaultCloseOperation(int operation)
553 /* Reference implementation allows invalid operations
554 to be specified. If so, getDefaultCloseOperation
555 must return the invalid code, and the behaviour
556 defaults to DO_NOTHING_ON_CLOSE. processWindowEvent
557 above handles this */
558 close_action = operation;
562 * This method returns the action taken when
563 * the JDialog is closed.
565 * @return The action to take.
567 public int getDefaultCloseOperation()
569 return close_action;
573 * This method returns a String describing the JDialog.
575 * @return A String describing the JDialog.
577 protected String paramString()
579 return "JDialog";
583 * DOCUMENT ME!
585 * @return DOCUMENT ME!
587 public AccessibleContext getAccessibleContext()
589 if (accessibleContext == null)
590 accessibleContext = new AccessibleJDialog();
591 return accessibleContext;