OSDN Git Service

Imported GNU Classpath 0.90
[pf3gnuchains/gcc-fork.git] / libjava / classpath / javax / swing / JOptionPane.java
1 /* JOptionPane.java
2    Copyright (C) 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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.Frame;
43
44 import javax.accessibility.Accessible;
45 import javax.accessibility.AccessibleContext;
46 import javax.accessibility.AccessibleRole;
47 import javax.swing.event.InternalFrameAdapter;
48 import javax.swing.event.InternalFrameEvent;
49 import javax.swing.plaf.OptionPaneUI;
50
51 /**
52  * This class creates different types of JDialogs and JInternalFrames that can
53  * ask users for input or pass on information. JOptionPane can be used by
54  * calling one of the show static methods or  by creating an instance of
55  * JOptionPane and calling createDialog or createInternalFrame.
56  */
57 public class JOptionPane extends JComponent implements Accessible
58 {
59   /**
60    * DOCUMENT ME!
61    */
62   // FIXME: This inner class is a complete stub and needs to be implemented
63   // properly.
64   protected class AccessibleJOptionPane extends JComponent.AccessibleJComponent
65   {
66     /** DOCUMENT ME! */
67     private static final long serialVersionUID = 686071432213084821L;
68     
69     /**
70      * Creates a new AccessibleJOptionPane object.
71      */
72     protected AccessibleJOptionPane()
73     {
74       // Nothing to do here.
75     }
76
77     /**
78      * Returns the accessible role of this object, which is always
79      * {@link AccessibleRole#OPTION_PANE}.
80      *
81      * @return the accessible role of this object
82      */
83     public AccessibleRole getAccessibleRole()
84     {
85       return AccessibleRole.OPTION_PANE;
86     }
87   }
88
89   /** DOCUMENT ME! */
90   private static final long serialVersionUID = 5231143276678566796L;
91
92   /** The value returned when cancel option is selected. */
93   public static final int CANCEL_OPTION = 2;
94
95   /** The value returned when the dialog is closed without a selection. */
96   public static final int CLOSED_OPTION = -1;
97
98   /** An option used in confirmation dialog methods. */
99   public static final int DEFAULT_OPTION = -1;
100
101   /** The value returned when the no option is selected. */
102   public static final int NO_OPTION = 1;
103
104   /** An option used in confirmation dialog methods. */
105   public static final int OK_CANCEL_OPTION = 2;
106
107   /** The value returned when the ok option is selected. */
108   public static final int OK_OPTION = 0;
109
110   /** An option used in confirmation dialog methods. */
111   public static final int YES_NO_CANCEL_OPTION = 1;
112
113   /** An option used in confirmation dialog methods. */
114   public static final int YES_NO_OPTION = 0;
115
116   /** The value returned when the yes option is selected. */
117   public static final int YES_OPTION = 0;
118
119   /** Identifier for the error message type. */
120   public static final int ERROR_MESSAGE = 0;
121
122   /** Identifier for the information message type. */
123   public static final int INFORMATION_MESSAGE = 1;
124
125   /** Identifier for the plain message type. */
126   public static final int PLAIN_MESSAGE = -1;
127
128   /** Identifier for the question message type. */
129   public static final int QUESTION_MESSAGE = 3;
130
131   /** Identifier for the warning message type. */
132   public static final int WARNING_MESSAGE = 2;
133
134   /**
135    * The identifier for the propertyChangeEvent when the icon property
136    * changes.
137    */
138   public static final String ICON_PROPERTY = "icon";
139
140   /**
141    * The identifier for the propertyChangeEvent when the initialSelectionValue
142    * property changes.
143    */
144   public static final String INITIAL_SELECTION_VALUE_PROPERTY = "initialSelectionValue";
145
146   /**
147    * The identifier for the propertyChangeEvent when the initialValue property
148    * changes.
149    */
150   public static final String INITIAL_VALUE_PROPERTY = "initialValue";
151
152   /**
153    * The identifier for the propertyChangeEvent when the inputValue property
154    * changes.
155    */
156   public static final String INPUT_VALUE_PROPERTY = "inputValue";
157
158   /**
159    * The identifier for the propertyChangeEvent when the message property
160    * changes.
161    */
162   public static final String MESSAGE_PROPERTY = "message";
163
164   /**
165    * The identifier for the propertyChangeEvent when the messageType property
166    * changes.
167    */
168   public static final String MESSAGE_TYPE_PROPERTY = "messageType";
169
170   /**
171    * The identifier for the propertyChangeEvent when the optionType property
172    * changes.
173    */
174   public static final String OPTION_TYPE_PROPERTY = "optionType";
175
176   /**
177    * The identifier for the propertyChangeEvent when the options property
178    * changes.
179    */
180   public static final String OPTIONS_PROPERTY = "options";
181
182   /**
183    * The identifier for the propertyChangeEvent when the selectionValues
184    * property changes.
185    */
186   public static final String SELECTION_VALUES_PROPERTY = "selectionValues";
187
188   /**
189    * The identifier for the propertyChangeEvent when the value property
190    * changes.
191    */
192   public static final String VALUE_PROPERTY = "value";
193
194   /**
195    * The identifier for the propertyChangeEvent when the wantsInput property
196    * changes.
197    */
198   public static final String WANTS_INPUT_PROPERTY = "wantsInput";
199
200   /** The value returned when the inputValue is uninitialized. */
201   public static final Object UNINITIALIZED_VALUE = "uninitializedValue";
202
203   /** The icon displayed in the dialog/internal frame. */
204   protected Icon icon;
205
206   /** The initial selected value in the input component. */
207   protected Object initialSelectionValue;
208
209   /** The object that is initially selected for options. */
210   protected Object initialValue;
211
212   /** The value the user inputs. */
213   protected Object inputValue = UNINITIALIZED_VALUE;
214
215   /** The message displayed in the dialog/internal frame. */
216   protected Object message;
217
218   /** The type of message displayed. */
219   protected int messageType = PLAIN_MESSAGE;
220
221   /**
222    * The options (usually buttons) aligned at the bottom for the user to
223    * select.
224    */
225   protected Object[] options;
226
227   /** The type of options to display. */
228   protected int optionType = DEFAULT_OPTION;
229
230   /** The input values the user can select. */
231   protected Object[] selectionValues;
232
233   /** The value returned by selecting an option. */
234   protected Object value = UNINITIALIZED_VALUE;
235
236   /** Whether the Dialog/InternalFrame needs input. */
237   protected boolean wantsInput;
238
239   /** The common frame used when no parent is provided. */
240   private static Frame privFrame = (Frame) SwingUtilities.getOwnerFrame(null);
241
242   /**
243    * Creates a new JOptionPane object using a message of "JOptionPane
244    * message", using the PLAIN_MESSAGE type and DEFAULT_OPTION.
245    */
246   public JOptionPane()
247   {
248     this("JOptionPane message", PLAIN_MESSAGE, DEFAULT_OPTION, null, null, null);
249   }
250
251   /**
252    * Creates a new JOptionPane object using the given message using the
253    * PLAIN_MESSAGE type and DEFAULT_OPTION.
254    *
255    * @param message The message to display.
256    */
257   public JOptionPane(Object message)
258   {
259     this(message, PLAIN_MESSAGE, DEFAULT_OPTION, null, null, null);
260   }
261
262   /**
263    * Creates a new JOptionPane object using the given message and messageType
264    * and DEFAULT_OPTION.
265    *
266    * @param message The message to display.
267    * @param messageType The type of message.
268    */
269   public JOptionPane(Object message, int messageType)
270   {
271     this(message, messageType, DEFAULT_OPTION, null, null, null);
272   }
273
274   /**
275    * Creates a new JOptionPane object using the given message, messageType and
276    * optionType.
277    *
278    * @param message The message to display.
279    * @param messageType The type of message.
280    * @param optionType The type of options.
281    */
282   public JOptionPane(Object message, int messageType, int optionType)
283   {
284     this(message, messageType, optionType, null, null, null);
285   }
286
287   /**
288    * Creates a new JOptionPane object using the given message, messageType,
289    * optionType and icon.
290    *
291    * @param message The message to display.
292    * @param messageType The type of message.
293    * @param optionType The type of options.
294    * @param icon The icon to display.
295    */
296   public JOptionPane(Object message, int messageType, int optionType, Icon icon)
297   {
298     this(message, messageType, optionType, icon, null, null);
299   }
300
301   /**
302    * Creates a new JOptionPane object using the given message, messageType,
303    * optionType, icon and options.
304    *
305    * @param message The message to display.
306    * @param messageType The type of message.
307    * @param optionType The type of options.
308    * @param icon The icon to display.
309    * @param options The options given.
310    */
311   public JOptionPane(Object message, int messageType, int optionType,
312                      Icon icon, Object[] options)
313   {
314     this(message, messageType, optionType, icon, options, null);
315   }
316
317   /**
318    * Creates a new JOptionPane object using the given message, messageType,
319    * optionType, icon, options and initialValue. The initialValue will be
320    * focused initially.
321    *
322    * @param message The message to display.
323    * @param messageType The type of message.
324    * @param optionType The type of options.
325    * @param icon The icon to display.
326    * @param options The options given.
327    * @param initialValue The component to focus on initially.
328    *
329    * @throws IllegalArgumentException If the messageType or optionType are not
330    *         legal values.
331    */
332   public JOptionPane(Object message, int messageType, int optionType,
333                      Icon icon, Object[] options, Object initialValue)
334   {
335     this.message = message;
336     if (! validMessageType(messageType))
337       throw new IllegalArgumentException("Message Type not legal value.");
338     this.messageType = messageType;
339     if (! validOptionType(optionType))
340       throw new IllegalArgumentException("Option Type not legal value.");
341     this.optionType = optionType;
342     this.icon = icon;
343     this.options = options;
344     this.initialValue = initialValue;
345
346     setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
347
348     updateUI();
349   }
350
351   /**
352    * This method creates a new JDialog that is either centered around the
353    * parent's frame or centered on the screen (if the parent is null). The
354    * JDialog will not be resizable and will be modal. Once the JDialog is
355    * disposed, the inputValue and value properties will  be set by the
356    * optionPane.
357    *
358    * @param parentComponent The parent of the Dialog.
359    * @param title The title in the bar of the JDialog.
360    *
361    * @return A new JDialog based on the JOptionPane configuration.
362    */
363   public JDialog createDialog(Component parentComponent, String title)
364   {
365     Frame toUse = getFrameForComponent(parentComponent);
366     if (toUse == null)
367       toUse = getRootFrame();
368
369     JDialog dialog = new JDialog(toUse, title);
370     inputValue = UNINITIALIZED_VALUE;
371     value = UNINITIALIZED_VALUE;
372
373     dialog.getContentPane().add(this);
374     dialog.setModal(true);
375     dialog.setResizable(false);
376     dialog.pack();
377     dialog.setLocationRelativeTo(parentComponent);
378     
379     return dialog;
380   }
381
382   /**
383    * This method creates a new JInternalFrame that is in the JLayeredPane
384    * which contains the parentComponent given. If no suitable JLayeredPane
385    * can be found from the parentComponent given, a RuntimeException will be
386    * thrown.
387    *
388    * @param parentComponent The parent to find a JDesktopPane from.
389    * @param title The title of the JInternalFrame.
390    *
391    * @return A new JInternalFrame based on the JOptionPane configuration.
392    *
393    * @throws RuntimeException If no suitable JDesktopPane is found.
394    *
395    * @specnote The specification says that the internal frame is placed
396    *           in the nearest <code>JDesktopPane</code> that is found in
397    *           <code>parent</code>'s ancestors. The behaviour of the JDK
398    *           is that it actually looks up the nearest
399    *           <code>JLayeredPane</code> in <code>parent</code>'s ancestors.
400    *           So do we.
401    */
402   public JInternalFrame createInternalFrame(Component parentComponent,
403                                             String title)
404                                      throws RuntimeException
405   {
406     // Try to find a JDesktopPane.
407     JLayeredPane toUse = getDesktopPaneForComponent(parentComponent);
408     // If we don't have a JDesktopPane, we try to find a JLayeredPane.
409     if (toUse == null)
410       toUse = JLayeredPane.getLayeredPaneAbove(parentComponent);
411     // If this still fails, we throw a RuntimeException.
412     if (toUse == null)
413       throw new RuntimeException
414         ("parentComponent does not have a valid parent");
415
416     JInternalFrame frame = new JInternalFrame(title);
417
418     inputValue = UNINITIALIZED_VALUE;
419     value = UNINITIALIZED_VALUE;
420
421     frame.setContentPane(this);
422     frame.setClosable(true);
423
424     toUse.add(frame);
425     frame.setLayer(JLayeredPane.MODAL_LAYER);
426
427     frame.pack();
428     frame.setVisible(true);
429
430     return frame;
431   }
432
433   /**
434    * DOCUMENT ME!
435    *
436    * @return DOCUMENT ME!
437    */
438   public AccessibleContext getAccessibleContext()
439   {
440     if (accessibleContext == null)
441       accessibleContext = new AccessibleJOptionPane();
442     return accessibleContext;
443   }
444
445   /**
446    * This method returns the JDesktopPane for the given parentComponent or
447    * null if none can be found.
448    *
449    * @param parentComponent The component to look in.
450    *
451    * @return The JDesktopPane for the given component or null if none can be
452    *         found.
453    */
454   public static JDesktopPane getDesktopPaneForComponent(Component parentComponent)
455   {
456     return (JDesktopPane) SwingUtilities.getAncestorOfClass(JDesktopPane.class,
457                                                             parentComponent);
458   }
459
460   /**
461    * This method returns the Frame for the given parentComponent or null if
462    * none can be found.
463    *
464    * @param parentComponent The component to look in.
465    *
466    * @return The Frame for the given component or null if none can be found.
467    */
468   public static Frame getFrameForComponent(Component parentComponent)
469   {
470     return (Frame) SwingUtilities.getAncestorOfClass(Frame.class,
471                                                      parentComponent);
472   }
473
474   /**
475    * This method returns the icon displayed.
476    *
477    * @return The icon displayed.
478    */
479   public Icon getIcon()
480   {
481     return icon;
482   }
483
484   /**
485    * This method returns the value initially selected from the list of values
486    * the user can input.
487    *
488    * @return The initial selection value.
489    */
490   public Object getInitialSelectionValue()
491   {
492     return initialSelectionValue;
493   }
494
495   /**
496    * This method returns the value that is focused from the list of options.
497    *
498    * @return The initial value from options.
499    */
500   public Object getInitialValue()
501   {
502     return initialValue;
503   }
504
505   /**
506    * This method returns the value that the user input.
507    *
508    * @return The user's input value.
509    */
510   public Object getInputValue()
511   {
512     if (getValue().equals(new Integer(CANCEL_OPTION)))
513       setInputValue(null);
514     return inputValue;
515   }
516
517   /**
518    * This method returns the maximum characters per line. By default, this is
519    * Integer.MAX_VALUE.
520    *
521    * @return The maximum characters per line.
522    */
523   public int getMaxCharactersPerLineCount()
524   {
525     return Integer.MAX_VALUE;
526   }
527
528   /**
529    * This method returns the message displayed.
530    *
531    * @return The message displayed.
532    */
533   public Object getMessage()
534   {
535     return message;
536   }
537
538   /**
539    * This method returns the message type.
540    *
541    * @return The message type.
542    */
543   public int getMessageType()
544   {
545     return messageType;
546   }
547
548   /**
549    * This method returns the options.
550    *
551    * @return The options.
552    */
553   public Object[] getOptions()
554   {
555     return options;
556   }
557
558   /**
559    * This method returns the option type.
560    *
561    * @return The option type.
562    */
563   public int getOptionType()
564   {
565     return optionType;
566   }
567
568   /**
569    * This method returns the Frame used by JOptionPane dialog's that have no
570    * parent.
571    *
572    * @return The Frame used by dialogs that have no parent.
573    */
574   public static Frame getRootFrame()
575   {
576     return privFrame;
577   }
578
579   /**
580    * This method returns the selection values.
581    *
582    * @return The selection values.
583    */
584   public Object[] getSelectionValues()
585   {
586     return selectionValues;
587   }
588
589   /**
590    * This method returns the UI used by the JOptionPane.
591    *
592    * @return The UI used by the JOptionPane.
593    */
594   public OptionPaneUI getUI()
595   {
596     return (OptionPaneUI) ui;
597   }
598
599   /**
600    * This method returns an identifier to determine which UI class will act as
601    * the UI.
602    *
603    * @return The UI identifier.
604    */
605   public String getUIClassID()
606   {
607     return "OptionPaneUI";
608   }
609
610   /**
611    * This method returns the value that the user selected out of options.
612    *
613    * @return The value that the user selected out of options.
614    */
615   public Object getValue()
616   {
617     return value;
618   }
619
620   /**
621    * This method returns whether this JOptionPane wants input.
622    *
623    * @return Whether this JOptionPane wants input.
624    */
625   public boolean getWantsInput()
626   {
627     return wantsInput;
628   }
629
630   /**
631    * This method returns a String that describes this JOptionPane.
632    *
633    * @return A String that describes this JOptionPane.
634    */
635   protected String paramString()
636   {
637     return "JOptionPane";
638   }
639
640   /**
641    * This method requests focus for the initial value.
642    */
643   public void selectInitialValue()
644   {
645     if (ui != null)
646       ((OptionPaneUI) ui).selectInitialValue(this);
647   }
648
649   /**
650    * This method changes the icon property.
651    *
652    * @param newIcon The new icon to use.
653    */
654   public void setIcon(Icon newIcon)
655   {
656     if (icon != newIcon)
657       {
658         Icon old = icon;
659         icon = newIcon;
660         firePropertyChange(ICON_PROPERTY, old, icon);
661       }
662   }
663
664   /**
665    * This method changes the initial selection property.
666    *
667    * @param newValue The new initial selection.
668    */
669   public void setInitialSelectionValue(Object newValue)
670   {
671     if (initialSelectionValue != newValue)
672       {
673         Object old = initialSelectionValue;
674         initialSelectionValue = newValue;
675         firePropertyChange(INITIAL_SELECTION_VALUE_PROPERTY, old,
676                            initialSelectionValue);
677       }
678   }
679
680   /**
681    * This method changes the initial value property.
682    *
683    * @param newValue The new initial value.
684    */
685   public void setInitialValue(Object newValue)
686   {
687     if (initialValue != newValue)
688       {
689         Object old = initialValue;
690         initialValue = newValue;
691         firePropertyChange(INITIAL_VALUE_PROPERTY, old, initialValue);
692       }
693   }
694
695   /**
696    * This method changes the inputValue property.
697    *
698    * @param newValue The new inputValue.
699    */
700   public void setInputValue(Object newValue)
701   {
702     if (inputValue != newValue)
703       {
704         Object old = inputValue;
705         inputValue = newValue;
706         firePropertyChange(INPUT_VALUE_PROPERTY, old, inputValue);
707       }
708   }
709
710   /**
711    * This method changes the message property.
712    *
713    * @param newMessage The new message.
714    */
715   public void setMessage(Object newMessage)
716   {
717     if (message != newMessage)
718       {
719         Object old = message;
720         message = newMessage;
721         firePropertyChange(MESSAGE_PROPERTY, old, message);
722       }
723   }
724
725   /**
726    * This method changes the messageType property.
727    *
728    * @param newType The new messageType.
729    *
730    * @throws IllegalArgumentException If the messageType is not valid.
731    */
732   public void setMessageType(int newType)
733   {
734     if (! validMessageType(newType))
735       throw new IllegalArgumentException("Message Type not legal value.");
736     if (newType != messageType)
737       {
738         int old = messageType;
739         messageType = newType;
740         firePropertyChange(MESSAGE_TYPE_PROPERTY, old, messageType);
741       }
742   }
743
744   /**
745    * This method changes the options property.
746    *
747    * @param newOptions The new options.
748    */
749   public void setOptions(Object[] newOptions)
750   {
751     if (options != newOptions)
752       {
753         Object[] old = options;
754         options = newOptions;
755         firePropertyChange(OPTIONS_PROPERTY, old, options);
756       }
757   }
758
759   /**
760    * This method changes the optionType property.
761    *
762    * @param newType The new optionType.
763    *
764    * @throws IllegalArgumentException If the optionType is not valid.
765    */
766   public void setOptionType(int newType)
767   {
768     if (! validOptionType(newType))
769       throw new IllegalArgumentException("Option Type not legal value.");
770     if (newType != optionType)
771       {
772         int old = optionType;
773         optionType = newType;
774         firePropertyChange(OPTION_TYPE_PROPERTY, old, optionType);
775       }
776   }
777
778   /**
779    * This method changes the Frame used for JOptionPane dialogs that have no
780    * parent.
781    *
782    * @param newRootFrame The Frame to use for dialogs that have no parent.
783    */
784   public static void setRootFrame(Frame newRootFrame)
785   {
786     privFrame = newRootFrame;
787   }
788
789   /**
790    * This method changes the selectionValues property.
791    *
792    * @param newValues The new selectionValues.
793    */
794   public void setSelectionValues(Object[] newValues)
795   {
796     if (newValues != selectionValues)
797       {
798         if (newValues != null)
799           wantsInput = true;
800         Object[] old = selectionValues;
801         selectionValues = newValues;
802         firePropertyChange(SELECTION_VALUES_PROPERTY, old, selectionValues);
803       }
804   }
805
806   /**
807    * This method sets the UI used with the JOptionPane.
808    *
809    * @param ui The UI used with the JOptionPane.
810    */
811   public void setUI(OptionPaneUI ui)
812   {
813     super.setUI(ui);
814   }
815
816   /**
817    * This method sets the value has been selected out of options.
818    *
819    * @param newValue The value that has been selected out of options.
820    */
821   public void setValue(Object newValue)
822   {
823     if (value != newValue)
824       {
825         Object old = value;
826         value = newValue;
827         firePropertyChange(VALUE_PROPERTY, old, value);
828       }
829   }
830
831   /**
832    * This method changes the wantsInput property.
833    *
834    * @param newValue Whether this JOptionPane requires input.
835    */
836   public void setWantsInput(boolean newValue)
837   {
838     if (wantsInput != newValue)
839       {
840         boolean old = wantsInput;
841         wantsInput = newValue;
842         firePropertyChange(WANTS_INPUT_PROPERTY, old, wantsInput);
843       }
844   }
845
846   /**
847    * This method shows a confirmation dialog with the title "Select an Option"
848    * and displays the given message. The parent frame will be the same as the
849    * parent frame of the given parentComponent. This method returns the
850    * option chosen by the user.
851    *
852    * @param parentComponent The parentComponent to find a frame in.
853    * @param message The message to display.
854    *
855    * @return The option that was selected.
856    */
857   public static int showConfirmDialog(Component parentComponent, Object message)
858   {
859     JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
860     JDialog dialog = pane.createDialog(parentComponent, "Select an Option");
861     dialog.show();
862     
863     if (pane.getValue() instanceof Integer)
864       return ((Integer) pane.getValue()).intValue();
865     return -1;
866   }
867
868   /**
869    * This method shows a confirmation dialog with the given message,
870    * optionType and title. The frame that owns the dialog will be the same
871    * frame that holds the given parentComponent. This method returns the
872    * option that was chosen.
873    *
874    * @param parentComponent The component to find a frame in.
875    * @param message The message displayed.
876    * @param title The title of the dialog.
877    * @param optionType The optionType.
878    *
879    * @return The option that was chosen.
880    */
881   public static int showConfirmDialog(Component parentComponent,
882                                       Object message, String title,
883                                       int optionType)
884   {
885     JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType);
886     JDialog dialog = pane.createDialog(parentComponent, title);
887     dialog.show();
888
889     if (pane.getValue() instanceof Integer)
890       return ((Integer) pane.getValue()).intValue();
891     return -1;
892   }
893
894   /**
895    * This method shows a confirmation dialog with the given message, title,
896    * messageType and optionType. The frame owner will be the same frame as
897    * the one that holds the given parentComponent. This method returns the
898    * option selected by the user.
899    *
900    * @param parentComponent The component to find a frame in.
901    * @param message The message displayed.
902    * @param title The title of the dialog.
903    * @param optionType The optionType.
904    * @param messageType The messageType.
905    *
906    * @return The selected option.
907    */
908   public static int showConfirmDialog(Component parentComponent,
909                                       Object message, String title,
910                                       int optionType, int messageType)
911   {
912     JOptionPane pane = new JOptionPane(message, messageType, optionType);
913     JDialog dialog = pane.createDialog(parentComponent, title);
914     dialog.show();
915
916     if (pane.getValue() instanceof Integer)
917       return ((Integer) pane.getValue()).intValue();
918     return -1;
919   }
920
921   /**
922    * This method shows a confirmation dialog with the given message, title,
923    * optionType, messageType and icon. The frame owner will be the same as
924    * the one that holds the given parentComponent. This method returns the
925    * option selected by the user.
926    *
927    * @param parentComponent The component to find a frame in.
928    * @param message The message displayed.
929    * @param title The title of the dialog.
930    * @param optionType The optionType.
931    * @param messageType The messsageType.
932    * @param icon The icon displayed.
933    *
934    * @return The selected option.
935    */
936   public static int showConfirmDialog(Component parentComponent,
937                                       Object message, String title,
938                                       int optionType, int messageType,
939                                       Icon icon)
940   {
941     JOptionPane pane = new JOptionPane(message, messageType, optionType, icon);
942     JDialog dialog = pane.createDialog(parentComponent, title);
943     dialog.show();
944
945     if (pane.getValue() instanceof Integer)
946       return ((Integer) pane.getValue()).intValue();
947     return -1;
948   }
949
950   /**
951    * This method will show a QUESTION_MESSAGE input dialog with the given
952    * message. No selectionValues is set so the Look and Feel will usually
953    * give the user a TextField to fill out. The frame owner will be the same
954    * frame that holds the given parentComponent. This method will return the
955    * value entered by the user.
956    *
957    * @param parentComponent The component to find a frame in.
958    * @param message The message displayed.
959    *
960    * @return The value entered by the user.
961    */
962   public static String showInputDialog(Component parentComponent,
963                                        Object message)
964   {
965     JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
966     pane.setWantsInput(true);
967     JDialog dialog = pane.createDialog(parentComponent, null);
968     dialog.show();
969     
970     return (String) pane.getInputValue();
971   }
972
973   /**
974    * This method will show a QUESTION_MESSAGE type input dialog with the given
975    * message and initialSelectionValue. Since there is no selectionValues
976    * set, the Look and Feel will usually give a TextField to fill out. The
977    * frame owner will be the same as the one that holds the given
978    * parentComponent. This method will return the value entered by the user.
979    *
980    * @param parentComponent The component to find a frame in.
981    * @param message The message to display.
982    * @param initialSelectionValue The initially selected value.
983    *
984    * @return The value the user input.
985    */
986   public static String showInputDialog(Component parentComponent,
987                                        Object message,
988                                        Object initialSelectionValue)
989   {
990     JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
991     pane.setInitialSelectionValue(initialSelectionValue);
992     pane.setWantsInput(true);
993     JDialog dialog = pane.createDialog(parentComponent, null);
994     dialog.show();
995     
996     return (String) pane.getInputValue();
997   }
998
999   /**
1000    * This method displays a new input dialog with the given message, title and
1001    * messageType. Since no selectionValues value is given, the Look and Feel
1002    * will usually give the user a TextField to input data to. This method
1003    * returns the value the user inputs.
1004    *
1005    * @param parentComponent The component to find a frame in.
1006    * @param message The message to display.
1007    * @param title The title of the dialog.
1008    * @param messageType The messageType.
1009    *
1010    * @return The value the user input.
1011    */
1012   public static String showInputDialog(Component parentComponent,
1013                                        Object message, String title,
1014                                        int messageType)
1015   {
1016     JOptionPane pane = new JOptionPane(message, messageType);
1017     pane.setWantsInput(true);
1018     JDialog dialog = pane.createDialog(parentComponent, title);
1019     dialog.show();
1020     
1021     return (String) pane.getInputValue();
1022   }
1023
1024   /**
1025    * This method shows an input dialog with the given message, title,
1026    * messageType, icon, selectionValues, and initialSelectionValue. This
1027    * method returns the value that the user selects.
1028    *
1029    * @param parentComponent The component to find a frame in.
1030    * @param message The message displayed.
1031    * @param title The title of the dialog.
1032    * @param messageType The messageType.
1033    * @param icon The icon displayed.
1034    * @param selectionValues The list of values to select from.
1035    * @param initialSelectionValue The initially selected value.
1036    *
1037    * @return The user selected value.
1038    */
1039   public static Object showInputDialog(Component parentComponent,
1040                                        Object message, String title,
1041                                        int messageType, Icon icon,
1042                                        Object[] selectionValues,
1043                                        Object initialSelectionValue)
1044   {
1045     JOptionPane pane = new JOptionPane(message, messageType);
1046     pane.setWantsInput(true);
1047     pane.setIcon(icon);
1048     pane.setSelectionValues(selectionValues);
1049     pane.setInitialSelectionValue(initialSelectionValue);
1050     JDialog dialog = pane.createDialog(parentComponent, title);
1051     dialog.show();
1052     
1053     return pane.getInputValue();
1054   }
1055
1056   /**
1057    * This method shows a QUESTION_MESSAGE type input dialog. Since no
1058    * selectionValues is set, the Look and Feel will usually give the user a
1059    * TextField to input data to. This method returns the value the user
1060    * inputs.
1061    *
1062    * @param message The message to display.
1063    *
1064    * @return The user selected value.
1065    */
1066   public static String showInputDialog(Object message)
1067   {
1068     JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
1069     pane.setWantsInput(true);
1070     JDialog dialog = pane.createDialog(null, null);
1071     dialog.show();
1072     
1073     return (String) pane.getInputValue();
1074   }
1075
1076   /**
1077    * This method shows a QUESTION_MESSAGE type input dialog. Since no
1078    * selectionValues is set, the Look and Feel will usually give the user a
1079    * TextField to input data to. The input component will be initialized with
1080    * the initialSelectionValue. This method returns the value the user
1081    * inputs.
1082    *
1083    * @param message The message to display.
1084    * @param initialSelectionValue The initialSelectionValue.
1085    *
1086    * @return The user selected value.
1087    */
1088   public static String showInputDialog(Object message,
1089                                        Object initialSelectionValue)
1090   {
1091     JOptionPane pane = new JOptionPane(message, QUESTION_MESSAGE);
1092     pane.setWantsInput(true);
1093     pane.setInitialSelectionValue(initialSelectionValue);
1094     JDialog dialog = pane.createDialog(null, null);
1095     dialog.show();
1096     
1097     return (String) pane.getInputValue();
1098   }
1099
1100   /**
1101    * This method shows an internal confirmation dialog with the given message.
1102    * The internal frame dialog will be placed in the first JDesktopPane
1103    * ancestor of the given parentComponent. This method will return the value
1104    * selected.
1105    *
1106    * @param parentComponent The parent to find a JDesktopPane in.
1107    * @param message The message to display.
1108    *
1109    * @return The value selected.
1110    */
1111   public static int showInternalConfirmDialog(Component parentComponent,
1112                                               Object message)
1113   {
1114     JOptionPane pane = new JOptionPane(message);
1115     JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1116
1117     startModal(frame);
1118     
1119     if (pane.getValue() instanceof Integer)
1120       return ((Integer) pane.getValue()).intValue();
1121     return -1;
1122   }
1123
1124   /**
1125    * This method shows an internal confirmation dialog with the given message,
1126    * optionType and title. The internal frame dialog will be placed in the
1127    * first JDesktopPane ancestor of the given parentComponent.  This method
1128    * will return the selected value.
1129    *
1130    * @param parentComponent The parent to find a JDesktopPane in.
1131    * @param message The message to display.
1132    * @param title The title to display.
1133    * @param optionType The option type.
1134    *
1135    * @return The selected value.
1136    */
1137   public static int showInternalConfirmDialog(Component parentComponent,
1138                                               Object message, String title,
1139                                               int optionType)
1140   {
1141     JOptionPane pane = new JOptionPane(message, PLAIN_MESSAGE, optionType);
1142     JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1143
1144     startModal(frame);
1145
1146     if (pane.getValue() instanceof Integer)
1147       return ((Integer) pane.getValue()).intValue();
1148     return -1;
1149   }
1150
1151   /**
1152    * This method shows an internal confirmation dialog with the given message,
1153    * title, optionTypes and icon for the given message type. The internal
1154    * confirmation dialog will be placed in the first  instance of
1155    * JDesktopPane ancestor of the given parentComponent.
1156    *
1157    * @param parentComponent The component to find a JDesktopPane in.
1158    * @param message The message to display.
1159    * @param title The title of the dialog.
1160    * @param optionType The option type.
1161    * @param messageType The message type.
1162    *
1163    * @return The selected value.
1164    */
1165   public static int showInternalConfirmDialog(Component parentComponent,
1166                                               Object message, String title,
1167                                               int optionType, int messageType)
1168   {
1169     JOptionPane pane = new JOptionPane(message, messageType, optionType);
1170     JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1171
1172     startModal(frame);
1173
1174     if (pane.getValue() instanceof Integer)
1175       return ((Integer) pane.getValue()).intValue();
1176     return -1;
1177   }
1178
1179   /**
1180    * This method shows an internal confirmation dialog with the given message,
1181    * title, option type, message type, and icon. The internal frame dialog
1182    * will be placed in the first JDesktopPane ancestor  that is found in the
1183    * given parentComponent. This method returns  the selected value.
1184    *
1185    * @param parentComponent The parent to find a JDesktopPane in.
1186    * @param message The message to display.
1187    * @param title The title to display.
1188    * @param optionType The option type.
1189    * @param messageType The message type.
1190    * @param icon The icon to display.
1191    *
1192    * @return The selected value.
1193    */
1194   public static int showInternalConfirmDialog(Component parentComponent,
1195                                               Object message, String title,
1196                                               int optionType, int messageType,
1197                                               Icon icon)
1198   {
1199     JOptionPane pane = new JOptionPane(message, messageType, optionType, icon);
1200     JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1201
1202     startModal(frame);
1203
1204     if (pane.getValue() instanceof Integer)
1205       return ((Integer) pane.getValue()).intValue();
1206     return -1;
1207   }
1208
1209   /**
1210    * This method shows an internal input dialog with the given message. The
1211    * internal frame dialog will be placed in the first JDesktopPane ancestor
1212    * of the given parent component. This method returns the value input by
1213    * the user.
1214    *
1215    * @param parentComponent The parent to find a JDesktopPane in.
1216    * @param message The message to display.
1217    *
1218    * @return The user selected value.
1219    */
1220   public static String showInternalInputDialog(Component parentComponent,
1221                                                Object message)
1222   {
1223     JOptionPane pane = new JOptionPane(message);
1224     pane.setWantsInput(true);
1225     JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1226
1227     startModal(frame);
1228     
1229     return (String) pane.getInputValue();
1230   }
1231
1232   /**
1233    * This method shows an internal input dialog with the given message,  title
1234    * and message type. The internal input dialog will be placed in the first
1235    * JDesktopPane ancestor found in the given parent component. This method
1236    * will return the input value given by the user.
1237    *
1238    * @param parentComponent The component to find a JDesktopPane in.
1239    * @param message The message to display.
1240    * @param title The title to display.
1241    * @param messageType The message type.
1242    *
1243    * @return The user input value.
1244    */
1245   public static String showInternalInputDialog(Component parentComponent,
1246                                                Object message, String title,
1247                                                int messageType)
1248   {
1249     JOptionPane pane = new JOptionPane(message, messageType);
1250     pane.setWantsInput(true);
1251     JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1252
1253     startModal(frame);
1254     
1255     return (String) pane.getInputValue();
1256   }
1257
1258   /**
1259    * This method shows an internal input dialog with the given message, title
1260    * message type, icon, selection value list and initial selection value.
1261    * The internal frame dialog will be placed in the first JDesktopPane
1262    * ancestor found in the given parent component. This method returns the
1263    * input value from the user.
1264    *
1265    * @param parentComponent The parent to find a JDesktopPane in.
1266    * @param message The message to display.
1267    * @param title The title to display.
1268    * @param messageType The message type.
1269    * @param icon The icon to display.
1270    * @param selectionValues The selection value list.
1271    * @param initialSelectionValue The initial selection value.
1272    *
1273    * @return The user input value.
1274    */
1275   public static Object showInternalInputDialog(Component parentComponent,
1276                                                Object message, String title,
1277                                                int messageType, Icon icon,
1278                                                Object[] selectionValues,
1279                                                Object initialSelectionValue)
1280   {
1281     JOptionPane pane = new JOptionPane(message, messageType);
1282     pane.setWantsInput(true);
1283     pane.setIcon(icon);
1284     pane.setSelectionValues(selectionValues);
1285     pane.setInitialSelectionValue(initialSelectionValue);
1286     JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1287
1288     startModal(frame);
1289     
1290     return pane.getInputValue();
1291   }
1292
1293   /**
1294    * This method shows an internal message dialog with the given message. The
1295    * internal frame dialog will be placed in the first JDesktopPane ancestor
1296    * found in the given parent component.
1297    *
1298    * @param parentComponent The component to find a JDesktopPane in.
1299    * @param message The message to display.
1300    */
1301   public static void showInternalMessageDialog(Component parentComponent,
1302                                                Object message)
1303   {
1304     JOptionPane pane = new JOptionPane(message);
1305     JInternalFrame frame = pane.createInternalFrame(parentComponent, null);
1306
1307     startModal(frame);
1308   }
1309
1310   /**
1311    * This method shows an internal message dialog with the given message,
1312    * title and message type. The internal message dialog is placed in the
1313    * first JDesktopPane ancestor found in the given parent component.
1314    *
1315    * @param parentComponent The parent component to find a JDesktopPane in.
1316    * @param message The message to display.
1317    * @param title The title to display.
1318    * @param messageType The message type.
1319    */
1320   public static void showInternalMessageDialog(Component parentComponent,
1321                                                Object message, String title,
1322                                                int messageType)
1323   {
1324     JOptionPane pane = new JOptionPane(message, messageType);
1325     JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1326
1327     startModal(frame);
1328   }
1329
1330   /**
1331    * This method shows an internal message dialog with the given message,
1332    * title, message type and icon. The internal message dialog is placed in
1333    * the first JDesktopPane ancestor found in the given parent component.
1334    *
1335    * @param parentComponent The component to find a JDesktopPane in.
1336    * @param message The message to display.
1337    * @param title The title to display.
1338    * @param messageType The message type.
1339    * @param icon The icon to display.
1340    */
1341   public static void showInternalMessageDialog(Component parentComponent,
1342                                                Object message, String title,
1343                                                int messageType, Icon icon)
1344   {
1345     JOptionPane pane = new JOptionPane(message, messageType);
1346     pane.setIcon(icon);
1347     JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1348
1349     startModal(frame);
1350   }
1351
1352   /**
1353    * This method displays an internal option dialog with the given message,
1354    * title, option type, message type, icon, option list, and initial option
1355    * value. The internal option dialog is placed in the first JDesktopPane
1356    * ancestor found in the parent component. This method returns the option
1357    * selected.
1358    *
1359    * @param parentComponent The parent to find a JDesktopPane in.
1360    * @param message The message displayed.
1361    * @param title The title displayed.
1362    * @param optionType The option type.
1363    * @param messageType The message type.
1364    * @param icon The icon to display.
1365    * @param options The array of options.
1366    * @param initialValue The initial value selected.
1367    *
1368    * @return The option that was selected.
1369    */
1370   public static int showInternalOptionDialog(Component parentComponent,
1371                                              Object message, String title,
1372                                              int optionType, int messageType,
1373                                              Icon icon, Object[] options,
1374                                              Object initialValue)
1375   {
1376     JOptionPane pane = new JOptionPane(message, messageType, optionType, icon,
1377                                        options, initialValue);
1378
1379     JInternalFrame frame = pane.createInternalFrame(parentComponent, title);
1380
1381     startModal(frame);
1382  
1383     if (pane.getValue() instanceof Integer)
1384       return ((Integer) pane.getValue()).intValue();
1385     return -1;
1386   }
1387
1388   /**
1389    * This method shows an INFORMATION_MESSAGE type message dialog.
1390    *
1391    * @param parentComponent The component to find a frame in.
1392    * @param message The message displayed.
1393    */
1394   public static void showMessageDialog(Component parentComponent,
1395                                        Object message)
1396   {
1397     JOptionPane pane = new JOptionPane(message, INFORMATION_MESSAGE);
1398     JDialog dialog = pane.createDialog(parentComponent, null);
1399     dialog.show();   
1400   }
1401
1402   /**
1403    * This method shows a message dialog with the given message, title and
1404    * messageType.
1405    *
1406    * @param parentComponent The component to find a frame in.
1407    * @param message The message displayed.
1408    * @param title The title of the dialog.
1409    * @param messageType The messageType.
1410    */
1411   public static void showMessageDialog(Component parentComponent,
1412                                        Object message, String title,
1413                                        int messageType)
1414   {
1415     JOptionPane pane = new JOptionPane(message, messageType);
1416     JDialog dialog = pane.createDialog(parentComponent, title);
1417     dialog.show();
1418   }
1419
1420   /**
1421    * This method shows a message dialog with the given message, title,
1422    * messageType and icon.
1423    *
1424    * @param parentComponent The component to find a frame in.
1425    * @param message The message displayed.
1426    * @param title The title of the dialog.
1427    * @param messageType The messageType.
1428    * @param icon The icon displayed.
1429    */
1430   public static void showMessageDialog(Component parentComponent,
1431                                        Object message, String title,
1432                                        int messageType, Icon icon)
1433   {
1434     JOptionPane pane = new JOptionPane(message, messageType);
1435     pane.setIcon(icon);
1436     JDialog dialog = pane.createDialog(parentComponent, title);
1437     dialog.show();
1438   }
1439
1440   /**
1441    * This method shows an option dialog with the given message, title,
1442    * optionType, messageType, icon, options and initialValue. This method
1443    * returns the option that was selected.
1444    *
1445    * @param parentComponent The component to find a frame in.
1446    * @param message The message displayed.
1447    * @param title The title of the dialog.
1448    * @param optionType The optionType.
1449    * @param messageType The messageType.
1450    * @param icon The icon displayed.
1451    * @param options The options to choose from.
1452    * @param initialValue The initial value.
1453    *
1454    * @return The selected option.
1455    */
1456   public static int showOptionDialog(Component parentComponent,
1457                                      Object message, String title,
1458                                      int optionType, int messageType,
1459                                      Icon icon, Object[] options,
1460                                      Object initialValue)
1461   {
1462     JOptionPane pane = new JOptionPane(message, messageType, optionType, icon,
1463                                        options, initialValue);
1464
1465     JDialog dialog = pane.createDialog(parentComponent, title);
1466     dialog.show();
1467
1468     if (pane.getValue() instanceof Integer)
1469       return ((Integer) pane.getValue()).intValue();
1470     return -1;
1471   }
1472
1473   /**
1474    * This method resets the UI to the Look and Feel default.
1475    */
1476   public void updateUI()
1477   {
1478     setUI((OptionPaneUI) UIManager.getUI(this));
1479   }
1480
1481   /**
1482    * This method returns true if the key is a valid messageType.
1483    *
1484    * @param key The key to check.
1485    *
1486    * @return True if key is valid.
1487    */
1488   private boolean validMessageType(int key)
1489   {
1490     switch (key)
1491       {
1492       case ERROR_MESSAGE:
1493       case INFORMATION_MESSAGE:
1494       case PLAIN_MESSAGE:
1495       case QUESTION_MESSAGE:
1496       case WARNING_MESSAGE:
1497         return true;
1498       }
1499     return false;
1500   }
1501
1502   /**
1503    * This method returns true if the key is a valid optionType.
1504    *
1505    * @param key The key to check.
1506    *
1507    * @return True if key is valid.
1508    */
1509   private boolean validOptionType(int key)
1510   {
1511     switch (key)
1512       {
1513       case DEFAULT_OPTION:
1514       case OK_CANCEL_OPTION:
1515       case YES_NO_CANCEL_OPTION:
1516       case YES_NO_OPTION:
1517         return true;
1518       }
1519     return false;
1520   }
1521
1522   /**
1523    * This helper method makes the JInternalFrame wait until it is notified by
1524    * an InternalFrameClosing event. This method also adds the given
1525    * JOptionPane to the JInternalFrame and sizes it according to the
1526    * JInternalFrame's preferred size.
1527    *
1528    * @param f The JInternalFrame to make modal.
1529    */
1530   private static void startModal(JInternalFrame f)
1531   {
1532     synchronized (f)
1533     {
1534       final JInternalFrame tmp = f;
1535       tmp.toFront();
1536
1537       f.addInternalFrameListener(new InternalFrameAdapter()
1538                                  {
1539                                    public void internalFrameClosed(InternalFrameEvent e)
1540                                    {
1541                                      synchronized (tmp)
1542                                      {
1543                                        tmp.removeInternalFrameListener(this);
1544                                        tmp.notifyAll();
1545                                      }
1546                                    }
1547                                  });
1548       try
1549         {
1550           while (! f.isClosed())
1551             f.wait();
1552         }
1553       catch (InterruptedException ignored)
1554         {
1555           // Ignore this Exception.
1556         }
1557     }
1558   }
1559 }