/
MainPanel.java
467 lines (437 loc) · 15.1 KB
/
MainPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
public final class MainPanel extends JPanel {
private static final String PAUSE = "pause";
private static final String RESUME = "resume";
private final JTextArea area = new JTextArea();
private final JPanel statusPanel = new JPanel(new BorderLayout());
private final JButton runButton = new JButton("run");
private final JButton cancelButton = new JButton("cancel");
private final JButton pauseButton = new JButton(PAUSE);
private final JProgressBar bar1 = new JProgressBar();
private final JProgressBar bar2 = new JProgressBar();
private transient BackgroundTask worker;
private MainPanel() {
super(new BorderLayout(5, 5));
area.setEditable(false);
runButton.addActionListener(e -> {
// System.out.println("actionPerformed() is EDT?: " + EventQueue.isDispatchThread());
runButton.setEnabled(false);
cancelButton.setEnabled(true);
pauseButton.setEnabled(true);
bar1.setValue(0);
bar2.setValue(0);
statusPanel.add(bar1, BorderLayout.NORTH);
statusPanel.add(bar2, BorderLayout.SOUTH);
statusPanel.revalidate();
// bar1.setIndeterminate(true);
worker = new ProgressTask();
worker.execute();
});
pauseButton.setEnabled(false);
pauseButton.addActionListener(e -> {
JButton b = (JButton) e.getSource();
// String pause = (String) getValue(Action.NAME);
if (Objects.nonNull(worker)) {
if (worker.isCancelled() || worker.isPaused) {
b.setText(PAUSE);
} else {
b.setText(RESUME);
}
worker.isPaused ^= true;
} else {
b.setText(PAUSE);
}
});
cancelButton.setEnabled(false);
cancelButton.addActionListener(e -> {
if (Objects.nonNull(worker) && !worker.isDone()) {
worker.cancel(true);
}
// worker = null;
pauseButton.setText(PAUSE);
pauseButton.setEnabled(false);
});
Component box = makeRightAlignBox(Arrays.asList(pauseButton, cancelButton, runButton), 80, 5);
add(new JScrollPane(area));
add(box, BorderLayout.NORTH);
add(statusPanel, BorderLayout.SOUTH);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setPreferredSize(new Dimension(320, 240));
}
private final class ProgressTask extends BackgroundTask {
@Override protected void process(List<Progress> chunks) {
// System.out.println("process() is EDT?: " + EventQueue.isDispatchThread());
if (isDisplayable() && !isCancelled()) {
chunks.forEach(MainPanel.this::updateProgress);
} else {
// System.out.println("process: DISPOSE_ON_CLOSE");
cancel(true);
}
}
@Override protected void done() {
if (!isDisplayable()) {
// System.out.println("done: DISPOSE_ON_CLOSE");
cancel(true);
return;
}
// System.out.println("done() is EDT?: " + EventQueue.isDispatchThread());
updateComponentDone();
String message;
try {
message = isCancelled() ? "Cancelled" : get();
} catch (InterruptedException ex) {
message = "Interrupted";
Thread.currentThread().interrupt();
} catch (ExecutionException ex) {
message = "ExecutionException";
}
appendLine(String.format("%n%s%n", message));
}
}
public void updateComponentDone() {
runButton.requestFocusInWindow();
runButton.setEnabled(true);
cancelButton.setEnabled(false);
pauseButton.setEnabled(false);
statusPanel.removeAll();
statusPanel.revalidate();
}
public void updateProgress(Progress s) {
switch (s.getComponent()) {
case TOTAL:
bar1.setValue((Integer) s.getValue());
break;
case FILE:
bar2.setValue((Integer) s.getValue());
break;
case LOG:
area.append(Objects.toString(s.getValue()));
break;
case PAUSE:
textProgress((Boolean) s.getValue());
break;
default:
throw new AssertionError("Unknown Progress");
}
}
public void textProgress(boolean append) {
if (append) {
area.append("*");
} else {
try {
Document doc = area.getDocument();
doc.remove(doc.getLength() - 1, 1);
} catch (BadLocationException ex) {
// should never happen
RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
wrap.initCause(ex);
throw wrap;
}
}
}
// @see https://ateraimemo.com/Swing/ButtonWidth.html
public static Component makeRightAlignBox(List<? extends Component> list, int width, int gap) {
SpringLayout layout = new SpringLayout();
JPanel p = new JPanel(layout) {
@Override public Dimension getPreferredSize() {
int maxHeight = list.stream()
.map(c -> c.getPreferredSize().height)
.max(Integer::compare)
.orElse(0);
return new Dimension(width * list.size() + gap + gap, maxHeight + gap + gap);
}
};
Spring x = layout.getConstraint(SpringLayout.WIDTH, p);
Spring y = Spring.constant(gap);
Spring g = Spring.minus(Spring.constant(gap));
Spring w = Spring.constant(width);
for (Component b : list) {
SpringLayout.Constraints constraints = layout.getConstraints(b);
x = Spring.sum(x, g);
constraints.setConstraint(SpringLayout.EAST, x);
constraints.setY(y);
constraints.setWidth(w);
p.add(b);
x = Spring.sum(x, Spring.minus(w));
}
return p;
}
public void appendLine(String str) {
area.append(str);
area.setCaretPosition(area.getDocument().getLength());
}
public static void main(String[] args) {
EventQueue.invokeLater(MainPanel::createAndShowGui);
}
private static void createAndShowGui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException ignored) {
Toolkit.getDefaultToolkit().beep();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
ex.printStackTrace();
return;
}
JFrame frame = new JFrame("@title@");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
// frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
enum ProgressType {
TOTAL, FILE, LOG, PAUSE
}
class Progress {
private final Object value;
private final ProgressType component;
protected Progress(ProgressType component, Object value) {
this.component = component;
this.value = value;
}
public Object getValue() {
return value;
}
public ProgressType getComponent() {
return component;
}
}
class BackgroundTask extends SwingWorker<String, Progress> {
protected boolean isPaused;
private final Random rnd = new Random();
@Override protected String doInBackground() throws InterruptedException {
// System.out.println("doInBackground() is EDT?: " + EventQueue.isDispatchThread());
int current = 0;
int lengthOfTask = 12; // fileList.size();
publish(new Progress(ProgressType.LOG, "Length Of Task: " + lengthOfTask));
publish(new Progress(ProgressType.LOG, "\n------------------------------\n"));
while (current < lengthOfTask && !isCancelled()) {
convertFileToSomething(100 * current / lengthOfTask);
current++;
}
publish(new Progress(ProgressType.LOG, "\n"));
return "Done";
}
protected void convertFileToSomething(int progress) throws InterruptedException {
boolean blinking = false;
int current = 0;
int lengthOfTask = 10 + rnd.nextInt(50); // long lengthOfTask = file.length();
publish(new Progress(ProgressType.TOTAL, progress));
publish(new Progress(ProgressType.LOG, "*"));
while (current <= lengthOfTask && !isCancelled()) {
if (isPaused) {
pause(blinking);
blinking ^= true;
continue;
}
doSomething(100 * current / lengthOfTask);
current++;
}
}
private void pause(boolean blinking) throws InterruptedException {
Thread.sleep(500);
publish(new Progress(ProgressType.PAUSE, blinking));
}
protected void doSomething(int progress) throws InterruptedException {
Thread.sleep(20);
publish(new Progress(ProgressType.FILE, progress + 1));
}
}
// public final class MainPanel extends JPanel {
// private final JTextArea area = new JTextArea();
// private final JPanel statusPanel = new JPanel(new BorderLayout());
// private final JButton runButton = new JButton(new RunAction());
// private final JButton cancelButton = new JButton(new CancelAction());
// private SwingWorker<String, String> worker;
//
// public MainPanel() {
// super(new BorderLayout(5, 5));
// area.setEditable(false);
// Box box = Box.createHorizontalBox();
// box.add(Box.createHorizontalGlue());
// box.add(runButton);
// box.add(Box.createHorizontalStrut(2));
// box.add(cancelButton);
// add(new JScrollPane(area));
// add(box, BorderLayout.NORTH);
// add(statusPanel, BorderLayout.SOUTH);
// setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// setPreferredSize(new Dimension(320, 240));
// }
//
// class RunAction extends AbstractAction {
// protected RunAction() {
// super("run");
// }
//
// @Override public void actionPerformed(ActionEvent e) {
// // System.out.println("actionPerformed() is EDT?: " + EventQueue.isDispatchThread());
// JProgressBar bar1 = new JProgressBar();
// JProgressBar bar2 = new JProgressBar();
// runButton.setEnabled(false);
// cancelButton.setEnabled(true);
// statusPanel.removeAll();
// statusPanel.add(bar1, BorderLayout.NORTH);
// statusPanel.add(bar2, BorderLayout.SOUTH);
// statusPanel.revalidate();
// // bar1.setIndeterminate(true);
//
// worker = new SwingWorker<String, String>() {
// private final Random r = new Random();
//
// @Override protected String doInBackground() {
// // System.out.println("doInBackground() is EDT?: " + EventQueue.isDispatchThread());
// int current = 0;
// int lengthOfTask = 12; // fileList.size();
// publish("Length Of Task: " + lengthOfTask);
// publish("\n------------------------------\n");
// setProgress(0);
// while (current < lengthOfTask && !isCancelled()) {
// if (!bar1.isDisplayable()) {
// return "Disposed";
// }
// try {
// convertFileToSomething();
// } catch (InterruptedException ex) {
// return "Interrupted";
// }
// publish("*");
// setProgress(100 * current / lengthOfTask);
// current++;
// }
// publish("\n");
// return "Done";
// }
//
// private void convertFileToSomething() throws InterruptedException {
// int current = 0;
// int lengthOfTask = 10 + rnd.nextInt(50); // long lengthOfTask = file.length();
// while (current <= lengthOfTask && !isCancelled()) {
// int iv = 100 * current / lengthOfTask;
// Thread.sleep(20);
// firePropertyChange("progress2", iv, iv + 1);
// current++;
// }
// }
//
// @Override protected void process(List<String> chunks) {
// // System.out.println("process() is EDT?: " + EventQueue.isDispatchThread());
// for (String message : chunks) {
// appendLine(message);
// }
// }
//
// @Override protected void done() {
// // System.out.println("done() is EDT?: " + EventQueue.isDispatchThread());
// runButton.setEnabled(true);
// cancelButton.setEnabled(false);
// statusPanel.remove(bar1);
// statusPanel.remove(bar2);
// statusPanel.revalidate();
// String text = null;
// if (isCancelled()) {
// text = "Cancelled";
// } else {
// try {
// text = get();
// } catch (Exception ex) {
// text = "Exception";
// }
// }
// // System.out.println(text);
// appendLine(text);
// }
// };
// worker.addPropertyChangeListener(new MainProgressListener(bar1));
// worker.addPropertyChangeListener(new SubProgressListener(bar2));
// worker.execute();
// }
// }
//
// class CancelAction extends AbstractAction {
// protected CancelAction() {
// super("cancel");
// }
//
// @Override public void actionPerformed(ActionEvent e) {
// if (Objects.nonNull(worker) && !worker.isDone()) {
// worker.cancel(true);
// }
// // worker = null;
// }
// }
//
// private void appendLine(String str) {
// area.append(str);
// area.setCaretPosition(area.getDocument().getLength());
// }
//
// public static void main(String[] args) {
// EventQueue.invokeLater(MainPanel::createAndShowGui);
// }
//
// private static void createAndShowGui() {
// try {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// } catch (UnsupportedLookAndFeelException ignored) {
// Toolkit.getDefaultToolkit().beep();
// } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
// ex.printStackTrace();
// return;
// }
// JFrame frame = new JFrame("@title@");
// // frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
// frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// frame.getContentPane().add(new MainPanel());
// frame.pack();
// frame.setLocationRelativeTo(null);
// frame.setVisible(true);
// }
// }
//
// class MainProgressListener implements PropertyChangeListener {
// protected final JProgressBar progressBar;
// protected MainProgressListener(JProgressBar progressBar) {
// this.progressBar = progressBar;
// this.progressBar.setValue(0);
// }
//
// @Override public void propertyChange(PropertyChangeEvent e) {
// String strPropertyName = e.getPropertyName();
// if ("progress".equals(strPropertyName)) {
// progressBar.setIndeterminate(false);
// int progress = (Integer) e.getNewValue();
// progressBar.setValue(progress);
// }
// }
// }
//
// class SubProgressListener implements PropertyChangeListener {
// private final JProgressBar progressBar;
// protected SubProgressListener(JProgressBar progressBar) {
// this.progressBar = progressBar;
// this.progressBar.setValue(0);
// }
//
// @Override public void propertyChange(PropertyChangeEvent e) {
// String strPropertyName = e.getPropertyName();
// if ("progress2".equals(strPropertyName)) {
// int progress = (Integer) e.getNewValue();
// progressBar.setValue(progress);
// }
// }
// }