| 1 |
|
package javajs.async; |
| 2 |
|
|
| 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; |
| 8 |
|
|
| 9 |
|
import javax.swing.Icon; |
| 10 |
|
import javax.swing.JOptionPane; |
| 11 |
|
import javax.swing.plaf.UIResource; |
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
@author |
| 17 |
|
|
| 18 |
|
|
| |
|
| 0% |
Uncovered Elements: 125 (125) |
Complexity: 46 |
Complexity Density: 0.66 |
|
| 19 |
|
public class AsyncDialog implements PropertyChangeListener { |
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 60 |
0 |
public AsyncDialog() {... |
| 61 |
|
} |
| 62 |
|
|
| 63 |
|
private ActionListener actionListener; |
| 64 |
|
private Object choice; |
| 65 |
|
private Object[] options; |
| 66 |
|
private Object value; |
| 67 |
|
private boolean wantsInput; |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
@param |
| 76 |
|
@param |
| 77 |
|
@return |
| 78 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 79 |
0 |
@Deprecated... |
| 80 |
|
public static String showInputDialog(Component frame, String msg) { |
| 81 |
0 |
return JOptionPane.showInputDialog(frame, msg); |
| 82 |
|
} |
| 83 |
|
|
| |
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 84 |
0 |
public void showInputDialog(Component frame, Object message, ActionListener a) {... |
| 85 |
0 |
setListener(a); |
| 86 |
0 |
wantsInput = true; |
| 87 |
0 |
process(JOptionPane.showInputDialog(frame, message)); |
| 88 |
0 |
unsetListener(); |
| 89 |
|
} |
| 90 |
|
|
| |
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 91 |
0 |
public void showInputDialog(Component frame, Object message, String title, int messageType, Icon icon,... |
| 92 |
|
Object[] selectionValues, Object initialSelectionValue, ActionListener a) { |
| 93 |
0 |
setListener(a); |
| 94 |
0 |
wantsInput = true; |
| 95 |
0 |
process(JOptionPane.showInputDialog(frame, message, title, messageType, icon, selectionValues, |
| 96 |
|
initialSelectionValue)); |
| 97 |
0 |
unsetListener(); |
| 98 |
|
} |
| 99 |
|
|
| |
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 100 |
0 |
public void showMessageDialog(Component frame, Object message, ActionListener a) {... |
| 101 |
0 |
setListener(a); |
| 102 |
0 |
JOptionPane.showMessageDialog(frame, message); |
| 103 |
0 |
unsetListener(); |
| 104 |
0 |
if (true) |
| 105 |
0 |
process("" + message); |
| 106 |
|
} |
| 107 |
|
|
| |
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 108 |
0 |
public void showOptionDialog(Component frame, Object message, String title, int optionType, int messageType,... |
| 109 |
|
Icon icon, Object[] options, Object initialValue, ActionListener a) { |
| 110 |
0 |
actionListener = a; |
| 111 |
0 |
this.options = options; |
| 112 |
0 |
setListener(a); |
| 113 |
0 |
process(JOptionPane.showOptionDialog(frame, message, title, optionType, messageType, icon, options, |
| 114 |
|
initialValue)); |
| 115 |
0 |
unsetListener(); |
| 116 |
|
} |
| 117 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 118 |
0 |
public void showConfirmDialog(Component frame, Object message, String title, ActionListener a) {... |
| 119 |
0 |
showConfirmDialog(frame, message, title, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, a); |
| 120 |
|
} |
| 121 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 122 |
0 |
public void showConfirmDialog(Component frame, Object message, String title, int optionType, ActionListener a) {... |
| 123 |
0 |
showConfirmDialog(frame, message, title, optionType, JOptionPane.QUESTION_MESSAGE, a); |
| 124 |
|
} |
| 125 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 126 |
0 |
public void showConfirmDialog(Component frame, Object message, String title, int optionType, int messageType,... |
| 127 |
|
ActionListener a) { |
| 128 |
0 |
setListener(a); |
| 129 |
0 |
process(JOptionPane.showConfirmDialog(frame, message, title, optionType, messageType)); |
| 130 |
0 |
unsetListener(); |
| 131 |
|
} |
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
|
@return |
| 137 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 138 |
0 |
public Object getChoice() {... |
| 139 |
0 |
return choice; |
| 140 |
|
} |
| 141 |
|
|
| |
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 142 |
0 |
public int getOption() {... |
| 143 |
0 |
if (!(choice instanceof Integer)) { |
| 144 |
0 |
throw new java.lang.IllegalArgumentException("AsyncDialog.getOption called for non-Integer choice"); |
| 145 |
|
} |
| 146 |
0 |
return ((Integer) choice).intValue(); |
| 147 |
|
} |
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
@param |
| 154 |
|
@param |
| 155 |
|
@param |
| 156 |
|
@param |
| 157 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
| 158 |
0 |
public static void showYesNoAsync(Component parent, Object message, String title, ActionListener listener) {... |
| 159 |
0 |
new AsyncDialog().showConfirmDialog(parent, message, (title == null ? "Question" : title), |
| 160 |
|
JOptionPane.YES_NO_OPTION, listener); |
| 161 |
|
} |
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
@param |
| 166 |
|
@param |
| 167 |
|
@param |
| 168 |
|
@param |
| 169 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 170 |
0 |
public static void showYesAsync(Component parent, Object message, String title, Runnable yes) {... |
| 171 |
0 |
AsyncDialog.showYesNoAsync(parent, message, title, new ActionListener() { |
| 172 |
|
|
| |
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 173 |
0 |
@Override... |
| 174 |
|
public void actionPerformed(ActionEvent e) { |
| 175 |
0 |
if (e.getID() == JOptionPane.YES_OPTION) { |
| 176 |
0 |
yes.run(); |
| 177 |
|
} |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
}); |
| 181 |
|
} |
| 182 |
|
|
| 183 |
|
|
| 184 |
|
|
| 185 |
|
@param |
| 186 |
|
@param |
| 187 |
|
@param |
| 188 |
|
@param |
| 189 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 190 |
0 |
public static void showOKAsync(Component parent, Object message, String title, Runnable ok) {... |
| 191 |
0 |
new AsyncDialog().showConfirmDialog(parent, message, title, JOptionPane.OK_CANCEL_OPTION, new ActionListener() { |
| 192 |
|
|
| |
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 193 |
0 |
@Override... |
| 194 |
|
public void actionPerformed(ActionEvent e) { |
| 195 |
0 |
if (e.getID() == JOptionPane.OK_OPTION) { |
| 196 |
0 |
ok.run(); |
| 197 |
|
} |
| 198 |
|
} |
| 199 |
|
|
| 200 |
|
}); |
| 201 |
|
} |
| 202 |
|
|
| 203 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 204 |
0 |
private void setListener(ActionListener a) {... |
| 205 |
0 |
actionListener = a; |
| 206 |
0 |
@SuppressWarnings("unused") |
| 207 |
|
Class c = JOptionPane.class; |
| 208 |
|
|
| 209 |
|
} |
| 210 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 211 |
0 |
private void unsetListener() {... |
| 212 |
|
|
| 213 |
|
} |
| 214 |
|
|
| 215 |
|
|
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| |
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 8 |
Complexity Density: 0.57 |
|
| 219 |
0 |
@Override... |
| 220 |
|
public void propertyChange(PropertyChangeEvent evt) { |
| 221 |
0 |
value = evt.getNewValue(); |
| 222 |
0 |
switch (evt.getPropertyName()) { |
| 223 |
0 |
case "inputValue": |
| 224 |
0 |
process(value); |
| 225 |
0 |
break; |
| 226 |
0 |
case "value": |
| 227 |
0 |
if (value != null && options == null && !(value instanceof Integer)) { |
| 228 |
0 |
process(getOptionIndex(((JOptionPane) evt.getSource()).getOptions(), value)); |
| 229 |
0 |
return; |
| 230 |
|
} |
| 231 |
0 |
if (options != null) { |
| 232 |
0 |
int i = getOptionIndex(options, value); |
| 233 |
0 |
value = Integer.valueOf(i >= 0 ? i : JOptionPane.CLOSED_OPTION); |
| 234 |
|
} |
| 235 |
0 |
process(value); |
| 236 |
0 |
break; |
| 237 |
|
} |
| 238 |
|
} |
| 239 |
|
|
| |
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 4 |
Complexity Density: 0.8 |
|
| 240 |
0 |
private int getOptionIndex(Object[] options, Object val) {... |
| 241 |
0 |
if (options != null) |
| 242 |
0 |
for (int i = 0; i < options.length; i++) { |
| 243 |
0 |
if (options[i] == val) |
| 244 |
0 |
return i; |
| 245 |
|
} |
| 246 |
0 |
return -1; |
| 247 |
|
} |
| 248 |
|
|
| |
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 4 |
Complexity Density: 1 |
|
| 249 |
0 |
public Object getValue() {... |
| 250 |
0 |
if (wantsInput || options == null) |
| 251 |
0 |
return value; |
| 252 |
0 |
int val = ((Integer) value).intValue(); |
| 253 |
0 |
return (val < 0 ? null : options[val]); |
| 254 |
|
} |
| 255 |
|
|
| 256 |
|
private boolean processed; |
| 257 |
|
|
| 258 |
|
|
| 259 |
|
|
| 260 |
|
|
| 261 |
|
@param |
| 262 |
|
|
| |
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 263 |
0 |
private void process(int ret) {... |
| 264 |
0 |
if (ret != -(-ret) || processed) |
| 265 |
0 |
return; |
| 266 |
0 |
processed = true; |
| 267 |
0 |
choice = ret; |
| 268 |
0 |
actionListener.actionPerformed(new ActionEvent(this, ret, "SelectedOption")); |
| 269 |
|
} |
| 270 |
|
|
| |
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 5 |
Complexity Density: 1 |
|
| 271 |
0 |
private void process(Object ret) {... |
| 272 |
0 |
if (ret instanceof UIResource || processed) |
| 273 |
0 |
return; |
| 274 |
0 |
processed = true; |
| 275 |
0 |
choice = ret; |
| 276 |
0 |
actionListener.actionPerformed(new ActionEvent(this, |
| 277 |
0 |
ret == null ? JOptionPane.CANCEL_OPTION : JOptionPane.OK_OPTION, |
| 278 |
0 |
(ret == null ? null : ret.toString()))); |
| 279 |
|
} |
| 280 |
|
|
| 281 |
|
|
| 282 |
|
} |