4 * A package to manage asynchronous aspects of SwingJS
6 * The javajs.async package simplifies the production of methods that can be
7 * used equally well in Java and in JavaScript for handling "pseudo-modal"
8 * blocking in JavaScript, meaning the user is locked out of other interactions,
9 * as in Java, but the code is not actually blocking the thread.
11 * Included in this package are
15 * Provides few simple generic static methods.
17 * Async.isJS() -- true if we are running this in JavaScript
19 * Async.javaSleep() -- bypassing Thread.sleep() for JavaScript; allowing it for
25 * Provides several very useful methods that act as a replacement for direct
26 * JOptionPane or JDialog calls, including both a synchronous callback
27 * (manifested in Java) and an asynchronous callback (JavaScript), both
28 * resulting in the same effect, except for the fact that the JavaScript code
29 * has returned immediately from the call with an "ignore me" reference, while
30 * Java is waiting at that call to return a value from JOptionPane (which is
31 * saved by AsyncDialog and not delivered as a return value).
33 * AsyncDialog does not extend JOptionPane, but it mirrors that classes public
34 * methods. There are a LOT of public methods in JOPtionPane. I suppose we
35 * should implement them all. In practice, AsyncDialog calls standard
36 * JOptionPane static classes for the dialogs.
38 * Initially, the following methods are implemented:
40 * public void showConfirmDialog(Component frame, Object message, String title,
43 * public void showConfirmDialog(Component frame, Object message, String title,
44 * int optionType, ActionListener a)
46 * public void showConfirmDialog(Component frame, Object message, String title,
47 * int optionType, int messageType, ActionListener a)
49 * public void showInputDialog(Component frame, Object message, ActionListener
52 * public void showInputDialog(Component frame, Object message, String title,
53 * int messageType, Icon icon, Object[] selectionValues, Object
54 * initialSelectionValue, ActionListener a)
56 * public void showMessageDialog(Component frame, Object message, ActionListener
59 * public void showOptionDialog(Component frame, Object message, String title,
60 * int optionType, int messageType, Icon icon, Object[] options, Object
61 * initialValue, ActionListener a)
64 * All nonstatic methods, requiring new AsyncDialog(), also require an
65 * ActionListener. This listener will get a call to actionPerformed(ActionEvent)
68 * event.getSource() is a reference to the originating AsyncDialog (super
69 * JOptionPane) for all information that a standard JOptionPane can provide,
70 * along with the two methods int getOption() and Object getChoice().
72 * event.getID() is a reference to the standard JOptionPane int return code.
74 * event.getActionCommand() also holds a value, but it may or may not be of
78 * A few especially useful methods are static, allowing just one or two expected
79 * callbacks of interest:
81 * AsyncDialog.showOKAsync(Component parent, Object message, String title,
84 * AsyncDialog.showYesAsync (Component parent, Object message, String title,
87 * AsyncDialog.showYesNoAsync (Component parent, Object message, String title,
88 * Runnable yes, Runnable no)
90 * These methods provide a fast way to adjust JOptionPane calls to be
95 * AsyncFileChooser extends javax.swing.JFileChooser
97 * Accepted constructors include:
99 * public AsyncFileChooser()
101 * public AsyncFileChooser(File file)
103 * public AsyncFileChooser(File file, FileSystemView view)
105 * (Note, however, that FileSystemView has no equivalent in JavaScript.)
107 * It's three public methods include:
109 * public void showDialog(Component frame, String btnLabel, Runnable ok,
112 * public void showOpenDialog(Component frame, Runnable ok, Runnable cancel)
114 * public void showSaveDialog(Component frame, Runnable ok, Runnable cancel)
117 * ActionListener is not needed here, as the instance of new AsyncFileChooser()
118 * already has direct access to all the JFileChooser public methods such as
119 * getSelectedFile() and getSelectedFiles().
121 * As a subclass of JFileChooser, it accepts all three public showXXXX methods
122 * of JFileChooser, namely:
124 * public void showDialog(Component frame, String btnLabel)
126 * public void showOpenDialog(Component frame)
128 * public void showSaveDialog(Component frame)
131 * None of these are recommended. AsyncFileChooser will indicate errors if the
132 * first of these two are called. (showSaveDialog is fine, as it is modal even
133 * in JavaScript. However it is not recommended that showSaveDialog(Component
134 * frame) be used, as in the future browsers may implement some sort of file
142 * AsyncColorChooser accesses JColorChooser asynchronously, using a private
143 * SwingJS setting that tells JColorChooser to report back to it with property
144 * changes. It is constructed using new AsyncColorChooser() and implements just
147 * public void showDialog(Component component, String title, Color initialColor,
148 * ActionListener listener)
150 * public Color getSelectedColor()
153 * The listener will get an actionPerformed(ActionEvent) callback with
154 * event.getID() equal to the color value or 0 if canceled. The
155 * getSelectedColor() method may also be called from this callback to retrieve
156 * the associated java.awt.Color object, using
158 * ((AsyncColorChooser)e.getSource()).getSelectedColor()
160 * As in Java, a null value for the selected color indicates that the
161 * JColorChooser was closed.
163 * Bob Hanson 2019.11.07
166 * @author Bob Hanson hansonr_at_stolaf.edu
171 public static boolean isJS() {
172 return (/** @j2sNative 1 ? true : */false);
176 * No sleep in JavaScript
179 public static void javaSleep(int ms) {
183 } catch (InterruptedException e) {