|
| 1 | +package javajs.async; |
| 2 | + |
| 3 | +import java.awt.Color; |
| 4 | +import java.awt.Component; |
| 5 | +import java.awt.event.ActionListener; |
| 6 | + |
| 7 | +/** |
| 8 | + * A class to manage asynchronous aspects of SwingJS |
| 9 | + * |
| 10 | + * The javajs.async package simplifies the production of methods that can be |
| 11 | + * used equally well in Java and in JavaScript for handling "pseudo-modal" |
| 12 | + * blocking in JavaScript, meaning the user is locked out of other interactions, |
| 13 | + * as in Java, but the code is not actually blocking the thread. |
| 14 | + * |
| 15 | + * Included in this package are |
| 16 | + * |
| 17 | + * Async |
| 18 | + * |
| 19 | + * Provides few simple generic static methods. |
| 20 | + * |
| 21 | + * Async.isJS() -- true if we are running this in JavaScript |
| 22 | + * |
| 23 | + * Async.javaSleep() -- bypassing Thread.sleep() for JavaScript; allowing it for |
| 24 | + * Java |
| 25 | + * |
| 26 | + * |
| 27 | + * AsyncDialog |
| 28 | + * |
| 29 | + * Provides several very useful methods that act as a replacement for direct |
| 30 | + * JOptionPane or JDialog calls, including both a synchronous callback |
| 31 | + * (manifested in Java) and an asynchronous callback (JavaScript), both |
| 32 | + * resulting in the same effect, except for the fact that the JavaScript code |
| 33 | + * has returned immediately from the call with an "ignore me" reference, while |
| 34 | + * Java is waiting at that call to return a value from JOptionPane (which is |
| 35 | + * saved by AsyncDialog and not delivered as a return value). |
| 36 | + * |
| 37 | + * AsyncDialog does not extend JOptionPane, but it mirrors that classes public |
| 38 | + * methods. There are a LOT of public methods in JOPtionPane. I suppose we |
| 39 | + * should implement them all. In practice, AsyncDialog calls standard |
| 40 | + * JOptionPane static classes for the dialogs. |
| 41 | + * |
| 42 | + * Initially, the following methods are implemented: |
| 43 | + * |
| 44 | + * public void showConfirmDialog(Component frame, Object message, String title, |
| 45 | + * ActionListener a) |
| 46 | + * |
| 47 | + * public void showConfirmDialog(Component frame, Object message, String title, |
| 48 | + * int optionType, ActionListener a) |
| 49 | + * |
| 50 | + * public void showConfirmDialog(Component frame, Object message, String title, |
| 51 | + * int optionType, int messageType, ActionListener a) |
| 52 | + * |
| 53 | + * public void showInputDialog(Component frame, Object message, ActionListener |
| 54 | + * a) |
| 55 | + * |
| 56 | + * public void showInputDialog(Component frame, Object message, String title, |
| 57 | + * int messageType, Icon icon, Object[] selectionValues, Object |
| 58 | + * initialSelectionValue, ActionListener a) |
| 59 | + * |
| 60 | + * public void showMessageDialog(Component frame, Object message, ActionListener |
| 61 | + * a) |
| 62 | + * |
| 63 | + * public void showOptionDialog(Component frame, Object message, String title, |
| 64 | + * int optionType, int messageType, Icon icon, Object[] options, Object |
| 65 | + * initialValue, ActionListener a) |
| 66 | + * |
| 67 | + * |
| 68 | + * All nonstatic methods, requiring new AsyncDialog(), also require an |
| 69 | + * ActionListener. This listener will get a call to actionPerformed(ActionEvent) |
| 70 | + * where: |
| 71 | + * |
| 72 | + * event.getSource() is a reference to the originating AsyncDialog (super |
| 73 | + * JOptionPane) for all information that a standard JOptionPane can provide, |
| 74 | + * along with the two methods int getOption() and Object getChoice(). |
| 75 | + * |
| 76 | + * event.getID() is a reference to the standard JOptionPane int return code. |
| 77 | + * |
| 78 | + * event.getActionCommand() also holds a value, but it may or may not be of |
| 79 | + * value. |
| 80 | + * |
| 81 | + * |
| 82 | + * A few especially useful methods are static, allowing just one or two expected |
| 83 | + * callbacks of interest: |
| 84 | + * |
| 85 | + * AsyncDialog.showOKAsync(Component parent, Object message, String title, |
| 86 | + * Runnable ok) |
| 87 | + * |
| 88 | + * AsyncDialog.showYesAsync (Component parent, Object message, String title, |
| 89 | + * Runnable yes) |
| 90 | + * |
| 91 | + * AsyncDialog.showYesNoAsync (Component parent, Object message, String title, |
| 92 | + * Runnable yes, Runnable no) |
| 93 | + * |
| 94 | + * These methods provide a fast way to adjust JOptionPane calls to be |
| 95 | + * asynchronous. |
| 96 | + * |
| 97 | + * |
| 98 | + * |
| 99 | + * AsyncFileChooser extends javax.swing.JFileChooser |
| 100 | + * |
| 101 | + * Accepted constructors include: |
| 102 | + * |
| 103 | + * public AsyncFileChooser() |
| 104 | + * |
| 105 | + * public AsyncFileChooser(File file) |
| 106 | + * |
| 107 | + * public AsyncFileChooser(File file, FileSystemView view) |
| 108 | + * |
| 109 | + * (Note, however, that FileSystemView has no equivalent in JavaScript.) |
| 110 | + * |
| 111 | + * It's three public methods include: |
| 112 | + * |
| 113 | + * public void showDialog(Component frame, String btnLabel, Runnable ok, |
| 114 | + * Runnable cancel) |
| 115 | + * |
| 116 | + * public void showOpenDialog(Component frame, Runnable ok, Runnable cancel) |
| 117 | + * |
| 118 | + * public void showSaveDialog(Component frame, Runnable ok, Runnable cancel) |
| 119 | + * |
| 120 | + * |
| 121 | + * ActionListener is not needed here, as the instance of new AsyncFileChooser() |
| 122 | + * already has direct access to all the JFileChooser public methods such as |
| 123 | + * getSelectedFile() and getSelectedFiles(). |
| 124 | + * |
| 125 | + * As a subclass of JFileChooser, it accepts all three public showXXXX methods |
| 126 | + * of JFileChooser, namely: |
| 127 | + * |
| 128 | + * public void showDialog(Component frame, String btnLabel) |
| 129 | + * |
| 130 | + * public void showOpenDialog(Component frame) |
| 131 | + * |
| 132 | + * public void showSaveDialog(Component frame) |
| 133 | + * |
| 134 | + * |
| 135 | + * None of these are recommended. AsyncFileChooser will indicate errors if the |
| 136 | + * first of these two are called. (showSaveDialog is fine, as it is modal even |
| 137 | + * in JavaScript. However it is not recommended that showSaveDialog(Component |
| 138 | + * frame) be used, as in the future browsers may implement some sort of file |
| 139 | + * saver in HTML5. |
| 140 | + * |
| 141 | + * |
| 142 | + * |
| 143 | + * AsyncColorChooser |
| 144 | + * |
| 145 | + * |
| 146 | + * AsyncColorChooser accesses JColorChooser asynchronously, using a private |
| 147 | + * SwingJS setting that tells JColorChooser to report back to it with property |
| 148 | + * changes. It is constructed using new AsyncColorChooser() and implements just |
| 149 | + * two methods: |
| 150 | + * |
| 151 | + * public void showDialog(Component component, String title, Color initialColor, |
| 152 | + * ActionListener listener) |
| 153 | + * |
| 154 | + * public Color getSelectedColor() |
| 155 | + * |
| 156 | + * |
| 157 | + * The listener will get an actionPerformed(ActionEvent) callback with |
| 158 | + * event.getID() equal to the color value or 0 if canceled. The |
| 159 | + * getSelectedColor() method may also be called from this callback to retrieve |
| 160 | + * the associated java.awt.Color object, using |
| 161 | + * |
| 162 | + * ((AsyncColorChooser)e.getSource()).getSelectedColor() |
| 163 | + * |
| 164 | + * As in Java, a null value for the selected color indicates that the |
| 165 | + * JColorChooser was closed. |
| 166 | + * |
| 167 | + * Bob Hanson 2019.11.07 |
| 168 | + * |
| 169 | + * |
| 170 | + * @author Bob Hanson hansonr_at_stolaf.edu |
| 171 | + * |
| 172 | + */ |
| 173 | +public class Async { |
| 174 | + |
| 175 | + public static boolean isJS() { |
| 176 | + return (/** @j2sNative 1 ? true : */false); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * No sleep in JavaScript |
| 181 | + * @param ms |
| 182 | + */ |
| 183 | + public static void javaSleep(int ms) { |
| 184 | + if (!isJS()) { |
| 185 | + try { |
| 186 | + Thread.sleep(ms); |
| 187 | + } catch (InterruptedException e) { |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + } |
| 192 | + |
| 193 | +} |
0 commit comments