OSDN Git Service

2005-04-19 Roman Kennke <roman@kennke.org>
[pf3gnuchains/gcc-fork.git] / libjava / javax / swing / JDialog.java
1 /* JDialog.java --
2    Copyright (C) 2002, 2004 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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.
20
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.
25
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. */
37
38
39 package javax.swing;
40
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;
51
52 import javax.accessibility.Accessible;
53 import javax.accessibility.AccessibleContext;
54
55 /**
56  * Unlike JComponent derivatives, JDialog inherits from java.awt.Dialog. But
57  * also lets a look-and-feel component to its work.
58  *
59  * @author Ronald Veldema (rveldema_AT_cs.vu.nl)
60  */
61 public class JDialog extends Dialog implements Accessible, WindowConstants,
62                                                RootPaneContainer
63 {
64   private static final long serialVersionUID = -864070866424508218L;
65
66   /** DOCUMENT ME! */
67   protected AccessibleContext accessibleContext;
68
69   /** The single RootPane in the Dialog. */
70   protected JRootPane rootPane;
71
72   /** Whether checking is enabled on the RootPane */
73   protected boolean rootPaneCheckingEnabled = true;
74
75   /** The default action taken when closed. */
76   private int close_action = HIDE_ON_CLOSE;
77   
78   /** Whether JDialogs are decorated by the Look and Feel. */
79   private static boolean decorated;
80
81   /**
82    * Creates a new non-modal JDialog with no title 
83    * using a shared Frame as the owner.
84    */
85   public JDialog()
86   {
87     this(SwingUtilities.getOwnerFrame(), "", false, null);
88   }
89
90   /**
91    * Creates a new non-modal JDialog with no title
92    * using the given owner.
93    *
94    * @param owner The owner of the JDialog.
95    */
96   public JDialog(Dialog owner)
97   {
98     this(owner, "", false, null);
99   }
100
101   /**
102    * Creates a new JDialog with no title using the
103    * given modal setting and owner.
104    *
105    * @param owner The owner of the JDialog.
106    * @param modal Whether the JDialog is modal.
107    */
108   public JDialog(Dialog owner, boolean modal)
109   {
110     this(owner, "", modal, null);
111   }
112
113   /**
114    * Creates a new non-modal JDialog using the 
115    * given title and owner.
116    *
117    * @param owner The owner of the JDialog.
118    * @param title The title of the JDialog.
119    */
120   public JDialog(Dialog owner, String title)
121   {
122     this(owner, title, false, null);
123   }
124
125   /**
126    * Creates a new JDialog using the given modal 
127    * settings, title, and owner.
128    *
129    * @param owner The owner of the JDialog.
130    * @param title The title of the JDialog.
131    * @param modal Whether the JDialog is modal.
132    */
133   public JDialog(Dialog owner, String title, boolean modal)
134   {
135     this(owner, title, modal, null);
136   }
137
138   /**
139    * Creates a new JDialog using the given modal 
140    * settings, title, owner and graphics configuration.
141    *
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.
146    */
147   public JDialog(Dialog owner, String title, boolean modal,
148                  GraphicsConfiguration gc)
149   {
150     super(owner, title, modal, gc);
151     dialogInit();
152   }
153
154   /**
155    * Creates a new non-modal JDialog with no title
156    * using the given owner.
157    *
158    * @param owner The owner of the JDialog.
159    */
160   public JDialog(Frame owner)
161   {
162     this(owner, "", false, null);
163   }
164
165   /**
166    * Creates a new JDialog with no title using the
167    * given modal setting and owner.
168    *
169    * @param owner The owner of the JDialog.
170    * @param modal Whether the JDialog is modal.
171    */
172   public JDialog(Frame owner, boolean modal)
173   {
174     this(owner, "", modal, null);
175   }
176
177   /**
178    * Creates a new non-modal JDialog using the 
179    * given title and owner.
180    *
181    * @param owner The owner of the JDialog.
182    * @param title The title of the JDialog.
183    */
184   public JDialog(Frame owner, String title)
185   {
186     this(owner, title, false, null);
187   }
188
189   /**
190    * Creates a new JDialog using the given modal 
191    * settings, title, and owner.
192    *
193    * @param owner The owner of the JDialog.
194    * @param title The title of the JDialog.
195    * @param modal Whether the JDialog is modal.
196    */
197   public JDialog(Frame owner, String title, boolean modal)
198   {
199     this(owner, title, modal, null);
200   }
201
202   /**
203    * Creates a new JDialog using the given modal 
204    * settings, title, owner and graphics configuration.
205    *
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.
210    */
211   public JDialog(Frame owner, String title, boolean modal,
212                  GraphicsConfiguration gc)
213   {
214     super((owner == null) ? SwingUtilities.getOwnerFrame() : owner, 
215           title, modal, gc);
216     dialogInit();
217   }
218
219   /**
220    * This method is called to initialize the 
221    * JDialog. It sets the layout used, the locale, 
222    * and creates the RootPane.
223    */
224   protected void dialogInit()
225   {
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();
232
233   }
234
235   /**
236    * This method returns whether JDialogs will have their
237    * window decorations provided by the Look and Feel.
238    *
239    * @return Whether the window decorations are Look and Feel provided.
240    */
241   public static boolean isDefaultLookAndFeelDecorated()
242   {
243     return decorated;
244   }
245
246   /**
247    * This method sets whether JDialogs will have their
248    * window decorations provided by the Look and Feel.
249    *
250    * @param defaultLookAndFeelDecorated Whether the window
251    * decorations are Look and Feel provided.
252    */
253   public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated)
254   {
255     decorated = defaultLookAndFeelDecorated;
256   }
257
258   /**
259    * This method returns the preferred size of 
260    * the JDialog.
261    *
262    * @return The preferred size.
263    */
264   public Dimension getPreferredSize()
265   {
266     Dimension d = super.getPreferredSize();
267     return d;
268   }
269
270   /**
271    * This method returns the JMenuBar used
272    * in this JDialog.
273    *
274    * @return The JMenuBar in the JDialog.
275    */
276   public JMenuBar getJMenuBar()
277   {
278     return getRootPane().getJMenuBar();
279   }
280
281   /**
282    * This method sets the JMenuBar used 
283    * in this JDialog.
284    *
285    * @param menubar The JMenuBar to use.
286    */
287   public void setJMenuBar(JMenuBar menubar)
288   {
289     getRootPane().setJMenuBar(menubar);
290   }
291
292   /**
293    * This method sets the LayoutManager used in the JDialog.
294    * This method will throw an Error if rootPaneChecking is 
295    * enabled.
296    *
297    * @param manager The LayoutManager to use.
298    */
299   public void setLayout(LayoutManager manager)
300   {
301     if (isRootPaneCheckingEnabled())
302       throw new Error("rootPaneChecking is enabled - cannot set layout.");
303     super.setLayout(manager);
304   }
305
306   /**
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.
310    *
311    * @param layeredPane The JLayeredPane to use.
312    */
313   public void setLayeredPane(JLayeredPane layeredPane)
314   {
315     if (layeredPane == null)
316       throw new IllegalComponentStateException("layeredPane cannot be null.");
317     getRootPane().setLayeredPane(layeredPane);
318   }
319
320   /**
321    * This method returns the JLayeredPane used with this JDialog.
322    *
323    * @return The JLayeredPane used with this JDialog.
324    */
325   public JLayeredPane getLayeredPane()
326   {
327     return getRootPane().getLayeredPane();
328   }
329
330   /**
331    * This method returns the JRootPane used with this JDialog.
332    *
333    * @return The JRootPane used with this JDialog.
334    */
335   public JRootPane getRootPane()
336   {
337     if (rootPane == null)
338       setRootPane(createRootPane());
339     return rootPane;
340   }
341
342   /**
343    * This method sets the JRootPane used with this JDialog.
344    *
345    * @param root The JRootPane to use.
346    */
347   protected void setRootPane(JRootPane root)
348   {
349     if (rootPane != null)
350       remove(rootPane);
351
352     rootPane = root;
353     rootPane.show();
354     add(rootPane);
355   }
356
357   /**
358    * This method creates a new JRootPane.
359    *
360    * @return A new JRootPane.
361    */
362   protected JRootPane createRootPane()
363   {
364     return new JRootPane();
365   }
366
367   /**
368    * This method returns the ContentPane
369    * in the JRootPane.
370    *
371    * @return The ContentPane in the JRootPane.
372    */
373   public Container getContentPane()
374   {
375     return getRootPane().getContentPane();
376   }
377
378   /**
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.
382    *
383    * @param contentPane The ContentPane to use with the JDialog.
384    */
385   public void setContentPane(Container contentPane)
386   {
387     if (contentPane == null)
388       throw new IllegalComponentStateException("contentPane cannot be null.");
389     getRootPane().setContentPane(contentPane);
390   }
391
392   /**
393    * This method returns the GlassPane for this JDialog.
394    *
395    * @return The GlassPane for this JDialog.
396    */
397   public Component getGlassPane()
398   {
399     return getRootPane().getGlassPane();
400   }
401
402   /**
403    * This method sets the GlassPane for this JDialog.
404    *
405    * @param glassPane The GlassPane for this JDialog.
406    */
407   public void setGlassPane(Component glassPane)
408   {
409     getRootPane().setGlassPane(glassPane);
410   }
411
412   /**
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.
416    *
417    * @param comp The component to add.
418    * @param constraints The constraints.
419    * @param index The position of the component.
420    */
421   protected void addImpl(Component comp, Object constraints, int index)
422   {
423     if (isRootPaneCheckingEnabled())
424       throw new Error("rootPaneChecking is enabled - adding components disallowed.");
425     super.addImpl(comp, constraints, index);
426   }
427
428   /**
429    * This method removes a component from the JDialog.
430    *
431    * @param comp The component to remove.
432    */
433   public void remove(Component comp)
434   {
435     // The path changes if the component == root.
436     if (comp == rootPane)
437       super.remove(rootPane);
438     else 
439       getContentPane().remove(comp);
440   }
441
442   /**
443    * This method returns whether rootPane checking is enabled.
444    *
445    * @return Whether rootPane checking is enabled.
446    */
447   protected boolean isRootPaneCheckingEnabled()
448   {
449     return rootPaneCheckingEnabled;
450   }
451
452   /**
453    * This method sets whether rootPane checking is enabled.
454    *
455    * @param enabled Whether rootPane checking is enabled.
456    */
457   protected void setRootPaneCheckingEnabled(boolean enabled)
458   {
459     rootPaneCheckingEnabled = enabled;
460   }
461
462   /**
463    * This method simply calls paint and returns.
464    *
465    * @param g The Graphics object to paint with.
466    */
467   public void update(Graphics g)
468   {
469     paint(g);
470   }
471   
472   
473   /**
474    * This method handles window events. This allows the JDialog
475    * to honour its default close operation.
476    *
477    * @param e The WindowEvent.
478    */
479   protected void processWindowEvent(WindowEvent e)
480   {
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())
485       {
486       case WindowEvent.WINDOW_CLOSING:
487         {
488           switch (getDefaultCloseOperation())
489             {
490             case DISPOSE_ON_CLOSE:
491               {
492                 dispose();
493                 break;
494               }
495             case HIDE_ON_CLOSE:
496               {
497                 setVisible(false);
498                 break;
499               }
500             case DO_NOTHING_ON_CLOSE:
501               break;
502             }
503           break;
504         }
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;
512       }
513   }
514
515   /**
516    * This method sets the action to take
517    * when the JDialog is closed.
518    *
519    * @param operation The action to take.
520    */
521   public void setDefaultCloseOperation(int operation)
522   {
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");
529   }
530
531   /**
532    * This method returns the action taken when
533    * the JDialog is closed.
534    *
535    * @return The action to take.
536    */
537   public int getDefaultCloseOperation()
538   {
539     return close_action;
540   }
541
542   /**
543    * This method returns a String describing the JDialog.
544    *
545    * @return A String describing the JDialog.
546    */
547   protected String paramString()
548   {
549     return "JDialog";
550   }
551
552   /**
553    * DOCUMENT ME!
554    *
555    * @return DOCUMENT ME!
556    */
557   public AccessibleContext getAccessibleContext()
558   {
559     return null;
560   }
561 }