3 import java.awt.Component;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.beans.PropertyChangeEvent;
7 import java.beans.PropertyChangeListener;
9 import javax.swing.Icon;
10 import javax.swing.JOptionPane;
11 import javax.swing.plaf.UIResource;
14 * A class to manage asynchronous input, option, and confirmation dialogs.
16 * @author Bob Hanson hansonr_at_stolaf.edu
19 public class AsyncDialog implements PropertyChangeListener {
21 // see discussion in net.sf.j2s.core/doc/Differences.txt
23 // Confirmation dialog example. Note moving the parent component into the constructor.
26 // private void promptQuit() {
27 // int sel = JOptionPane.showConfirmDialog(null, PROMPT_EXIT, NAME, JOptionPane.YES_NO_OPTION);
29 // case JOptionPane.YES_OPTION:
30 // resultsTab.clean();
41 // private void promptQuitAsync() {
42 // new AsyncDialog().showConfirmDialog(null, PROMPT_EXIT, NAME, JOptionPane.YES_NO_OPTION, new ActionListener() {
45 // public void actionPerformed(ActionEvent e) {
46 // int sel = ((AsyncDialog)e.getSource()).getOption();
48 // case JOptionPane.YES_OPTION:
49 // resultsTab.clean();
60 public AsyncDialog() {
63 private ActionListener actionListener;
64 private Object choice;
65 private Object[] options;
67 private boolean wantsInput;
69 // These options can be supplemented as desired.
73 * Synchronous call; OK in JavaScript as long as we are using a JavaScript prompt() call
80 public static String showInputDialog(Component frame, String msg) {
81 return JOptionPane.showInputDialog(frame, msg);
84 public void showInputDialog(Component frame, Object message, ActionListener a) {
87 process(JOptionPane.showInputDialog(frame, message));
91 public void showInputDialog(Component frame, Object message, String title, int messageType, Icon icon,
92 Object[] selectionValues, Object initialSelectionValue, ActionListener a) {
95 process(JOptionPane.showInputDialog(frame, message, title, messageType, icon, selectionValues,
96 initialSelectionValue));
100 public void showMessageDialog(Component frame, Object message, ActionListener a) {
102 JOptionPane.showMessageDialog(frame, message);
104 if (/** @j2sNative false || */true)
105 process("" + message);
108 public void showOptionDialog(Component frame, Object message, String title, int optionType, int messageType,
109 Icon icon, Object[] options, Object initialValue, ActionListener a) {
111 this.options = options;
113 process(JOptionPane.showOptionDialog(frame, message, title, optionType, messageType, icon, options,
118 public void showConfirmDialog(Component frame, Object message, String title, ActionListener a) {
119 showConfirmDialog(frame, message, title, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, a);
122 public void showConfirmDialog(Component frame, Object message, String title, int optionType, ActionListener a) {
123 showConfirmDialog(frame, message, title, optionType, JOptionPane.QUESTION_MESSAGE, a);
126 public void showConfirmDialog(Component frame, Object message, String title, int optionType, int messageType,
129 process(JOptionPane.showConfirmDialog(frame, message, title, optionType, messageType));
134 * retrieve selection from the ActionEvent, for which "this" is getSource()
138 public Object getChoice() {
142 public int getOption() {
143 if (!(choice instanceof Integer)) {
144 throw new java.lang.IllegalArgumentException("AsyncDialog.getOption called for non-Integer choice");
146 return ((Integer) choice).intValue();
150 * A dialog option that allows for YES, NO, and CLOSE options via
151 * ActionListener. ActionEvent.getID() contains the reply.
153 * @param parent The parent component for the dialog
154 * @param message The text of the message to display
155 * @param title Optional title defaults to "Question"
156 * @param listener Handle options based on an ActionEvent
158 public static void showYesNoAsync(Component parent, Object message, String title, ActionListener listener) {
159 new AsyncDialog().showConfirmDialog(parent, message, (title == null ? "Question" : title),
160 JOptionPane.YES_NO_OPTION, listener);
164 * A dialog option that involves just a YES follower.
170 public static void showYesAsync(Component parent, Object message, String title, Runnable yes) {
171 AsyncDialog.showYesNoAsync(parent, message, title, new ActionListener() {
174 public void actionPerformed(ActionEvent e) {
175 if (e.getID() == JOptionPane.YES_OPTION) {
184 * A dialog option that involves just an OK follower.
190 public static void showOKAsync(Component parent, Object message, String title, Runnable ok) {
191 new AsyncDialog().showConfirmDialog(parent, message, title, JOptionPane.OK_CANCEL_OPTION, new ActionListener() {
194 public void actionPerformed(ActionEvent e) {
195 if (e.getID() == JOptionPane.OK_OPTION) {
204 private void setListener(ActionListener a) {
206 @SuppressWarnings("unused")
207 Class c = JOptionPane.class; // loads the class
208 /** @j2sNative c.$clazz$.listener = this */
211 private void unsetListener() {
212 /** @j2sNative javax.swing.JOptionPane.listener = null */
216 * Switch from property change to action.
220 public void propertyChange(PropertyChangeEvent evt) {
221 value = evt.getNewValue();
222 switch (evt.getPropertyName()) {
227 if (value != null && options == null && !(value instanceof Integer)) {
228 process(getOptionIndex(((JOptionPane) evt.getSource()).getOptions(), value));
231 if (options != null) {
232 int i = getOptionIndex(options, value);
233 value = Integer.valueOf(i >= 0 ? i : JOptionPane.CLOSED_OPTION);
240 private int getOptionIndex(Object[] options, Object val) {
242 for (int i = 0; i < options.length; i++) {
243 if (options[i] == val)
249 public Object getValue() {
250 if (wantsInput || options == null)
252 int val = ((Integer) value).intValue();
253 return (val < 0 ? null : options[val]);
256 private boolean processed;
259 * Return for confirm dialog.
261 * @param ret may be JavaScript NaN, testable as ret != ret or ret != - -ret
263 private void process(int ret) {
264 if (ret != -(-ret) || processed)
268 actionListener.actionPerformed(new ActionEvent(this, ret, "SelectedOption"));
271 private void process(Object ret) {
272 if (ret instanceof UIResource || processed)
276 actionListener.actionPerformed(new ActionEvent(this,
277 ret == null ? JOptionPane.CANCEL_OPTION : JOptionPane.OK_OPTION,
278 (ret == null ? null : ret.toString())));