| 1 |
|
package javajs.async; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
import java.awt.Component; |
| 5 |
|
import java.awt.event.ActionEvent; |
| 6 |
|
import java.awt.event.ActionListener; |
| 7 |
|
import java.beans.PropertyChangeEvent; |
| 8 |
|
import java.beans.PropertyChangeListener; |
| 9 |
|
import java.io.File; |
| 10 |
|
import java.util.function.Function; |
| 11 |
|
|
| 12 |
|
import javax.swing.JFileChooser; |
| 13 |
|
import javax.swing.JOptionPane; |
| 14 |
|
import javax.swing.filechooser.FileSystemView; |
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
@author |
| 23 |
|
|
| 24 |
|
|
| |
|
| 0% |
Uncovered Elements: 100 (100) |
Complexity: 35 |
Complexity Density: 0.59 |
|
| 25 |
|
public class AsyncFileChooser extends JFileChooser implements PropertyChangeListener { |
| 26 |
|
|
| 27 |
|
private int optionSelected; |
| 28 |
|
private Runnable ok, cancel; |
| 29 |
|
private boolean isAsyncSave = true; |
| 30 |
|
private static boolean notified; |
| 31 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 32 |
0 |
public AsyncFileChooser() {... |
| 33 |
0 |
super(); |
| 34 |
|
} |
| 35 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 36 |
0 |
public AsyncFileChooser(File file) {... |
| 37 |
0 |
super(file); |
| 38 |
|
} |
| 39 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 40 |
0 |
public AsyncFileChooser(File file, FileSystemView view) {... |
| 41 |
0 |
super(file, view); |
| 42 |
|
} |
| 43 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 44 |
0 |
@Deprecated... |
| 45 |
|
@Override |
| 46 |
|
public int showDialog(Component frame, String btnText) { |
| 47 |
|
|
| 48 |
0 |
return super.showDialog(frame, btnText); |
| 49 |
|
} |
| 50 |
|
|
| |
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 51 |
0 |
private int err() {... |
| 52 |
0 |
try { |
| 53 |
0 |
throw new java.lang.IllegalAccessException("Warning! AsyncFileChooser interface bypassed!"); |
| 54 |
|
} catch (IllegalAccessException e) { |
| 55 |
0 |
e.printStackTrace(); |
| 56 |
|
} |
| 57 |
0 |
return JFileChooser.ERROR_OPTION; |
| 58 |
|
} |
| 59 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 60 |
0 |
@Deprecated... |
| 61 |
|
@Override |
| 62 |
|
public int showOpenDialog(Component frame) { |
| 63 |
0 |
return err(); |
| 64 |
|
} |
| 65 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 66 |
0 |
@Override... |
| 67 |
|
public int showSaveDialog(Component frame) { |
| 68 |
0 |
isAsyncSave = false; |
| 69 |
0 |
return super.showSaveDialog(frame); |
| 70 |
|
} |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
@param |
| 75 |
|
@param |
| 76 |
|
@param |
| 77 |
|
@param |
| 78 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 79 |
0 |
public void showDialog(Component frame, String btnLabel, Runnable ok, Runnable cancel) {... |
| 80 |
0 |
this.ok = ok; |
| 81 |
0 |
if (getDialogType() != JFileChooser.SAVE_DIALOG && cancel != null) |
| 82 |
0 |
notifyCancel(); |
| 83 |
0 |
process(super.showDialog(frame, btnLabel)); |
| 84 |
|
} |
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
@param |
| 89 |
|
@param |
| 90 |
|
@param |
| 91 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 92 |
0 |
public void showOpenDialog(Component frame, Runnable ok, Runnable cancel) {... |
| 93 |
0 |
this.ok = ok; |
| 94 |
0 |
if (cancel != null) |
| 95 |
0 |
notifyCancel(); |
| 96 |
0 |
process(super.showOpenDialog(frame)); |
| 97 |
|
} |
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
@param |
| 105 |
|
@param |
| 106 |
|
@param |
| 107 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 108 |
0 |
public void showSaveDialog(Component frame, Runnable ok, Runnable cancel) {... |
| 109 |
0 |
this.ok = ok; |
| 110 |
0 |
this.cancel = cancel; |
| 111 |
0 |
process(super.showSaveDialog(frame)); |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
@param |
| 119 |
|
@param |
| 120 |
|
@param |
| 121 |
|
|
| |
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 122 |
0 |
public static void getFileAsync(Component parent, String title, int mode, Function<File, Void> processFile) {... |
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
0 |
AsyncFileChooser fc = new AsyncFileChooser(); |
| 127 |
0 |
fc.setDialogTitle(title); |
| 128 |
0 |
Runnable after = new Runnable() { |
| 129 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 130 |
0 |
@Override... |
| 131 |
|
public void run() { |
| 132 |
0 |
processFile.apply(fc.getSelectedFile()); |
| 133 |
|
} |
| 134 |
|
|
| 135 |
|
}; |
| 136 |
0 |
if (mode == JFileChooser.OPEN_DIALOG) { |
| 137 |
0 |
fc.showOpenDialog(parent, after, after); |
| 138 |
|
} else { |
| 139 |
0 |
fc.showSaveDialog(parent, after, after); |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
@param |
| 147 |
|
@param |
| 148 |
|
@param |
| 149 |
|
@param |
| 150 |
|
@param |
| 151 |
|
|
| |
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 152 |
0 |
public static void checkReplaceFileAsync(Component parent, File outfile, String title, Runnable yes, Runnable no) {... |
| 153 |
0 |
if (outfile.exists()) { |
| 154 |
0 |
AsyncDialog.showYesNoAsync(parent, |
| 155 |
|
outfile + " exists. Replace it?", null, new ActionListener() { |
| 156 |
|
|
| |
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
| 157 |
0 |
@Override... |
| 158 |
|
public void actionPerformed(ActionEvent e) { |
| 159 |
0 |
switch (e.getID()) { |
| 160 |
0 |
case JOptionPane.YES_OPTION: |
| 161 |
0 |
yes.run(); |
| 162 |
0 |
break; |
| 163 |
0 |
default: |
| 164 |
0 |
if (no != null) |
| 165 |
0 |
no.run(); |
| 166 |
0 |
break; |
| 167 |
|
} |
| 168 |
|
} |
| 169 |
|
|
| 170 |
|
}); |
| 171 |
|
|
| 172 |
|
} else { |
| 173 |
0 |
yes.run(); |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
} |
| 177 |
|
|
| |
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 178 |
0 |
private void notifyCancel() {... |
| 179 |
0 |
if (!notified) { |
| 180 |
0 |
System.err.println("developer note: JavaScript cannot fire a FileChooser CANCEL action"); |
| 181 |
|
} |
| 182 |
0 |
notified = true; |
| 183 |
|
} |
| 184 |
|
|
| |
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 4 |
Complexity Density: 0.8 |
|
| 185 |
0 |
@Override... |
| 186 |
|
public void propertyChange(PropertyChangeEvent evt) { |
| 187 |
0 |
switch (evt.getPropertyName()) { |
| 188 |
0 |
case "SelectedFile": |
| 189 |
0 |
case "SelectedFiles": |
| 190 |
0 |
process(optionSelected = (evt.getNewValue() == null ? CANCEL_OPTION : APPROVE_OPTION)); |
| 191 |
0 |
break; |
| 192 |
|
} |
| 193 |
|
} |
| 194 |
|
|
| |
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 5 |
Complexity Density: 0.56 |
|
| 195 |
0 |
private void process(int ret) {... |
| 196 |
0 |
if (ret != -(-ret)) |
| 197 |
0 |
return; |
| 198 |
0 |
optionSelected = ret; |
| 199 |
0 |
File f = getSelectedFile(); |
| 200 |
0 |
if (f == null) { |
| 201 |
0 |
if (cancel != null) |
| 202 |
0 |
cancel.run(); |
| 203 |
|
} else { |
| 204 |
0 |
if (ok != null) |
| 205 |
0 |
ok.run(); |
| 206 |
|
} |
| 207 |
|
} |
| 208 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 209 |
0 |
public int getSelectedOption() {... |
| 210 |
0 |
return optionSelected; |
| 211 |
|
} |
| 212 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 213 |
0 |
public static byte[] getFileBytes(File f) {... |
| 214 |
0 |
return null; |
| 215 |
|
} |
| 216 |
|
|
| 217 |
|
} |