1111import javax .swing .JOptionPane ;
1212
1313/**
14- * A class to manage asynchronous input and confirmation dialogs.
14+ * A class to manage asynchronous input, option, and confirmation dialogs.
1515 *
1616 * @author Bob Hanson hansonr
1717 *
@@ -21,6 +21,7 @@ public class AsyncDialog extends Window implements PropertyChangeListener {
2121
2222 private ActionListener actionListener ;
2323 private Object choice ;
24+ private String [] options ;
2425
2526 public AsyncDialog (ActionListener a ) {
2627 super (null );
@@ -35,51 +36,72 @@ public AsyncDialog(ActionListener a) {
3536 public void propertyChange (PropertyChangeEvent evt ) {
3637 switch (evt .getPropertyName ()) {
3738 case "value" :
38- process (evt .getNewValue ());
39+ Object val = evt .getNewValue ();
40+ if (options != null ) {
41+ int i ;
42+ for (i = 0 ; i < options .length ; i ++) {
43+ if (options [i ] == val )
44+ break ;
45+ }
46+ val = Integer .valueOf (i < options .length ? i : JOptionPane .CLOSED_OPTION );
47+ }
48+ process (val );
3949 }
4050 }
4151
42- public void showInputDialog ( Component frame , String message , String title , int type ,
43- Icon icon , Integer [] choices , Integer initialChoice ) {
44- if ( frame != null ) {
45- setBounds ( frame . getBounds ());
46- }
52+ // These options can be supplemented as desired.
53+
54+ public void showInputDialog ( Component frame , String message , String title , int type , Icon icon , Integer [] choices ,
55+ Integer initialChoice ) {
56+ setFrame ( frame );
4757 process (JOptionPane .showInputDialog (this , message , title , type , icon , choices , initialChoice ));
4858 }
4959
60+ public void showOptionDialog (Component frame , String message , String title , int optionType , int messageType ,
61+ Icon icon , String [] options , String initialValue ) {
62+ setFrame (frame );
63+ this .options = options ;
64+ process (JOptionPane .showOptionDialog (this , message , title , optionType , messageType , icon , options ,
65+ initialValue ));
66+ }
67+
5068 public void showConfirmDialog (Component frame , String message , String title , int optionType ) {
5169 showConfirmDialog (frame , message , title , optionType , JOptionPane .QUESTION_MESSAGE );
5270 }
5371
5472 public void showConfirmDialog (Component frame , String message , String title , int optionType , int messageType ) {
73+ setFrame (frame );
74+ process (JOptionPane .showConfirmDialog (this , message , title , optionType , messageType ));
75+ }
76+
77+ private void setFrame (Component frame ) {
5578 if (frame != null ) {
5679 setBounds (frame .getBounds ());
5780 }
58- process (JOptionPane .showConfirmDialog (this , message , title , optionType , messageType ));
5981 }
6082
6183 private boolean processed ;
62-
84+
6385 /**
6486 * Return for confirm dialog.
6587 *
66- * @param ret may be JavaScript NaN, testable as ret != ret or ret != - -ret
88+ * @param ret may be JavaScript NaN, testable as ret != ret or ret != - -ret
6789 */
6890 private void process (int ret ) {
6991 if (ret != - -ret || processed )
7092 return ;
7193 process (new Integer (ret ));
7294 }
73-
95+
7496 private void process (Object ret ) {
7597 if (ret instanceof javax .swing .plaf .UIResource || processed )
7698 return ;
7799 processed = true ;
78- choice = ret ;
100+ choice = ret ;
79101 actionListener .actionPerformed (new ActionEvent (this , 0 , "SelectedOption" ));
80102 dispose ();
81103 }
82-
104+
83105 /**
84106 * retrieve selection from the ActionEvent, for which "this" is getSource()
85107 *
@@ -95,5 +117,5 @@ public int getOption() {
95117 }
96118 return ((Integer ) choice ).intValue ();
97119 }
98-
120+
99121}
0 commit comments