Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 16:11:35 GMT
  2. Package javajs.async

File Async.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
60% of files have more coverage

Code metrics

2
4
2
1
189
14
4
1
2
2
2

Classes

Class Line # Actions
Async 169 4 4
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package javajs.async;
2   
3    /**
4    * A package to manage asynchronous aspects of SwingJS
5    *
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.
10    *
11    * Included in this package are
12    *
13    * Async
14    *
15    * Provides few simple generic static methods.
16    *
17    * Async.isJS() -- true if we are running this in JavaScript
18    *
19    * Async.javaSleep() -- bypassing Thread.sleep() for JavaScript; allowing it for
20    * Java
21    *
22    *
23    * AsyncDialog
24    *
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).
32    *
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.
37    *
38    * Initially, the following methods are implemented:
39    *
40    * public void showConfirmDialog(Component frame, Object message, String title,
41    * ActionListener a)
42    *
43    * public void showConfirmDialog(Component frame, Object message, String title,
44    * int optionType, ActionListener a)
45    *
46    * public void showConfirmDialog(Component frame, Object message, String title,
47    * int optionType, int messageType, ActionListener a)
48    *
49    * public void showInputDialog(Component frame, Object message, ActionListener
50    * a)
51    *
52    * public void showInputDialog(Component frame, Object message, String title,
53    * int messageType, Icon icon, Object[] selectionValues, Object
54    * initialSelectionValue, ActionListener a)
55    *
56    * public void showMessageDialog(Component frame, Object message, ActionListener
57    * a)
58    *
59    * public void showOptionDialog(Component frame, Object message, String title,
60    * int optionType, int messageType, Icon icon, Object[] options, Object
61    * initialValue, ActionListener a)
62    *
63    *
64    * All nonstatic methods, requiring new AsyncDialog(), also require an
65    * ActionListener. This listener will get a call to actionPerformed(ActionEvent)
66    * where:
67    *
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().
71    *
72    * event.getID() is a reference to the standard JOptionPane int return code.
73    *
74    * event.getActionCommand() also holds a value, but it may or may not be of
75    * value.
76    *
77    *
78    * A few especially useful methods are static, allowing just one or two expected
79    * callbacks of interest:
80    *
81    * AsyncDialog.showOKAsync(Component parent, Object message, String title,
82    * Runnable ok)
83    *
84    * AsyncDialog.showYesAsync (Component parent, Object message, String title,
85    * Runnable yes)
86    *
87    * AsyncDialog.showYesNoAsync (Component parent, Object message, String title,
88    * Runnable yes, Runnable no)
89    *
90    * These methods provide a fast way to adjust JOptionPane calls to be
91    * asynchronous.
92    *
93    *
94    *
95    * AsyncFileChooser extends javax.swing.JFileChooser
96    *
97    * Accepted constructors include:
98    *
99    * public AsyncFileChooser()
100    *
101    * public AsyncFileChooser(File file)
102    *
103    * public AsyncFileChooser(File file, FileSystemView view)
104    *
105    * (Note, however, that FileSystemView has no equivalent in JavaScript.)
106    *
107    * It's three public methods include:
108    *
109    * public void showDialog(Component frame, String btnLabel, Runnable ok,
110    * Runnable cancel)
111    *
112    * public void showOpenDialog(Component frame, Runnable ok, Runnable cancel)
113    *
114    * public void showSaveDialog(Component frame, Runnable ok, Runnable cancel)
115    *
116    *
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().
120    *
121    * As a subclass of JFileChooser, it accepts all three public showXXXX methods
122    * of JFileChooser, namely:
123    *
124    * public void showDialog(Component frame, String btnLabel)
125    *
126    * public void showOpenDialog(Component frame)
127    *
128    * public void showSaveDialog(Component frame)
129    *
130    *
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
135    * saver in HTML5.
136    *
137    *
138    *
139    * AsyncColorChooser
140    *
141    *
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
145    * two methods:
146    *
147    * public void showDialog(Component component, String title, Color initialColor,
148    * ActionListener listener)
149    *
150    * public Color getSelectedColor()
151    *
152    *
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
157    *
158    * ((AsyncColorChooser)e.getSource()).getSelectedColor()
159    *
160    * As in Java, a null value for the selected color indicates that the
161    * JColorChooser was closed.
162    *
163    * Bob Hanson 2019.11.07
164    *
165    *
166    * @author Bob Hanson hansonr_at_stolaf.edu
167    *
168    */
 
169    public class Async {
170   
 
171  0 toggle public static boolean isJS() {
172  0 return (/** @j2sNative 1 ? true : */false);
173    }
174   
175    /**
176    * No sleep in JavaScript
177    * @param ms
178    */
 
179  0 toggle public static void javaSleep(int ms) {
180  0 if (!isJS()) {
181  0 try {
182  0 Thread.sleep(ms);
183    } catch (InterruptedException e) {
184    }
185    }
186   
187    }
188   
189    }