OSDN Git Service

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