-
Notifications
You must be signed in to change notification settings - Fork 155
/
MainPanel.java
443 lines (412 loc) · 14 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
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import javax.swing.*;
public final class MainPanel extends JPanel {
private final JTextArea area = new JTextArea();
private final JProgressBar bar = new JProgressBar();
private final JPanel statusPanel = new JPanel(new BorderLayout());
private final JButton runButton = new JButton("run");
private final JButton cancelButton = new JButton("cancel");
private final LoadingLabel loadingLabel = new LoadingLabel();
private transient BackgroundTask worker;
private MainPanel() {
super(new BorderLayout());
area.setEditable(false);
area.setLineWrap(true);
runButton.addActionListener(e -> executeWorker());
cancelButton.addActionListener(e -> Optional.ofNullable(worker)
.filter(w -> !w.isDone())
.ifPresent(w -> w.cancel(true)));
Box box = Box.createHorizontalBox();
box.add(loadingLabel);
box.add(Box.createHorizontalGlue());
box.add(runButton);
box.add(cancelButton);
add(box, BorderLayout.NORTH);
add(statusPanel, BorderLayout.SOUTH);
add(new JScrollPane(area));
setPreferredSize(new Dimension(320, 240));
}
public void executeWorker() {
runButton.setEnabled(false);
cancelButton.setEnabled(true);
loadingLabel.startAnimation();
statusPanel.removeAll();
statusPanel.add(bar);
statusPanel.revalidate();
bar.setIndeterminate(true);
worker = new BackgroundTask() {
@Override protected void process(List<String> chunks) {
// System.out.println("process() is EDT?: " + EventQueue.isDispatchThread());
if (isDisplayable() && !isCancelled()) {
chunks.forEach(MainPanel.this::appendLine);
} else {
cancel(true);
}
}
@Override protected void done() {
// System.out.println("done() is EDT?: " + EventQueue.isDispatchThread());
if (!isDisplayable()) {
cancel(true);
return;
}
loadingLabel.stopAnimation();
runButton.setEnabled(true);
cancelButton.setEnabled(false);
statusPanel.remove(bar);
statusPanel.revalidate();
appendLine("\n");
try {
appendLine(isCancelled() ? "Cancelled" : get());
} catch (InterruptedException ex) {
appendLine("Interrupted");
Thread.currentThread().interrupt();
} catch (ExecutionException ex) {
appendLine("Error");
}
appendLine("\n\n");
}
};
worker.addPropertyChangeListener(new ProgressListener(bar));
worker.execute();
}
public void appendLine(String str) {
area.append(str);
area.setCaretPosition(area.getDocument().getLength());
}
// private boolean isCancelled() {
// return Objects.isNull(worker) && worker.isCancelled();
// }
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 BackgroundTask extends SwingWorker<String, String> {
@Override protected String doInBackground() throws InterruptedException {
// System.out.println("doInBackground() is EDT?: " + EventQueue.isDispatchThread());
Thread.sleep(1000);
int current = 0;
int lengthOfTask = 120; // list.size();
publish("Length Of Task: " + lengthOfTask);
publish("\n------------------------------\n");
while (current < lengthOfTask && !isCancelled()) {
publish(".");
setProgress(100 * current / lengthOfTask);
doSomething();
current++;
}
return "Done";
}
protected void doSomething() throws InterruptedException {
Thread.sleep(50);
}
}
class ProgressListener implements PropertyChangeListener {
private final JProgressBar progressBar;
protected ProgressListener(JProgressBar progressBar) {
this.progressBar = progressBar;
this.progressBar.setValue(0);
}
@Override public void propertyChange(PropertyChangeEvent e) {
boolean isProgress = "progress".equals(e.getPropertyName());
if (isProgress) {
progressBar.setIndeterminate(false);
int progress = (Integer) e.getNewValue();
progressBar.setValue(progress);
}
}
}
class LoadingLabel extends JLabel {
private transient HierarchyListener listener;
private final Timer animator = new Timer(100, null);
protected LoadingLabel() {
super(new AnimeIcon());
animator.addActionListener(e -> {
Icon icon = getIcon();
if (icon instanceof AnimeIcon) {
((AnimeIcon) icon).next();
}
repaint();
});
}
@Override public void updateUI() {
removeHierarchyListener(listener);
super.updateUI();
listener = e -> {
boolean b = (e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0;
if (b && !e.getComponent().isDisplayable()) {
animator.stop();
}
};
addHierarchyListener(listener);
}
public void startAnimation() {
Icon icon = getIcon();
if (icon instanceof AnimeIcon) {
((AnimeIcon) icon).setRunning(true);
}
animator.start();
repaint();
}
public void stopAnimation() {
Icon icon = getIcon();
if (icon instanceof AnimeIcon) {
((AnimeIcon) icon).setRunning(false);
}
animator.stop();
repaint();
}
}
// // TEST: 1
// class AnimeIcon implements Icon {
// private static final Color ELLIPSE_COLOR = new Color(0x80_80_80);
// private static final double R = 2d;
// private static final double SX = 1d;
// private static final double SY = 1d;
// private static final int WIDTH = (int) (R * 8 + SX * 2);
// private static final int HEIGHT = (int) (R * 8 + SY * 2);
// private final List<Shape> list = new ArrayList<>(Arrays.asList(
// new Ellipse2D.Double(SX + 3 * R, SY + 0 * R, 2 * R, 2 * R),
// new Ellipse2D.Double(SX + 5 * R, SY + 1 * R, 2 * R, 2 * R),
// new Ellipse2D.Double(SX + 6 * R, SY + 3 * R, 2 * R, 2 * R),
// new Ellipse2D.Double(SX + 5 * R, SY + 5 * R, 2 * R, 2 * R),
// new Ellipse2D.Double(SX + 3 * R, SY + 6 * R, 2 * R, 2 * R),
// new Ellipse2D.Double(SX + 1 * R, SY + 5 * R, 2 * R, 2 * R),
// new Ellipse2D.Double(SX + 0 * R, SY + 3 * R, 2 * R, 2 * R),
// new Ellipse2D.Double(SX + 1 * R, SY + 1 * R, 2 * R, 2 * R)));
//
// private boolean running;
// public void next() {
// if (running) {
// // list.add(list.remove(0));
// Collections.rotate(list, 1);
// }
// }
//
// public void setRunning(boolean running) {
// this.running = running;
// }
//
// @Override public void paintIcon(Component c, Graphics g, int x, int y) {
// Graphics2D g2 = (Graphics2D) g.create();
// g2.setPaint(Optional.ofNullable(c).map(Component::getBackground).orElse(Color.WHITE));
// g2.fillRect(x, y, getIconWidth(), getIconHeight());
// g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// g2.setPaint(ELLIPSE_COLOR);
// g2.translate(x, y);
// int size = list.size();
// for (int i = 0; i < size; i++) {
// float alpha = running ? (i + 1) / (float) size : .5f;
// g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
// g2.fill(list.get(i));
// }
// g2.dispose();
// }
//
// @Override public int getIconWidth() {
// return WIDTH;
// }
//
// @Override public int getIconHeight() {
// return HEIGHT;
// }
// }
// // TEST: 2
// class AnimeIcon implements Icon {
// private static final Color ELLIPSE_COLOR = new Color(0xE6_B3_B3);
// private final List<Shape> list = new ArrayList<>();
// private final Dimension dim;
// private boolean running;
// protected AnimeIcon() {
// super();
// int r = 4;
// Shape s = new Ellipse2D.Double(0, 0, 2 * r, 2 * r);
// for (int i = 0; i < 8; i++) {
// AffineTransform at = AffineTransform.getRotateInstance(i * 2 * Math.PI / 8);
// at.concatenate(AffineTransform.getTranslateInstance(r, r));
// list.add(at.createTransformedShape(s));
// }
// // int d = (int) (r * 2 * (1 + 2 * Math.sqrt(2)));
// int d = (int) r * 2 * (1 + 3); // 2 * Math.sqrt(2) is nearly equal to 3.
// dim = new Dimension(d, d);
// }
//
// @Override public int getIconWidth() {
// return dim.width;
// }
//
// @Override public int getIconHeight() {
// return dim.height;
// }
//
// @Override public void paintIcon(Component c, Graphics g, int x, int y) {
// Graphics2D g2 = (Graphics2D) g.create();
// g2.setPaint(Optional.ofNullable(c).map(Component::getBackground).orElse(Color.WHITE));
// g2.fillRect(x, y, getIconWidth(), getIconHeight());
// g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// g2.setPaint(ELLIPSE_COLOR);
// int xx = x + dim.width / 2;
// int yy = y + dim.height / 2;
// g2.translate(xx, yy);
// int size = list.size();
// for (int i = 0; i < size; i++) {
// float alpha = running ? (i + 1) / (float) size : .5f;
// g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
// g2.fill(list.get(i));
// }
// g2.dispose();
// }
//
// public void next() {
// if (running) {
// // list.add(list.remove(0));
// Collections.rotate(list, 1);
// }
// }
//
// public void setRunning(boolean running) {
// this.running = running;
// }
// }
// TEST: 3
class AnimeIcon implements Icon {
private static final Color ELLIPSE_COLOR = new Color(0xE6_B3_B3);
private final List<Shape> list = new ArrayList<>();
private final Dimension dim;
private boolean running;
private int rotate = 45;
protected AnimeIcon() {
super();
int r = 4;
Shape s = new Ellipse2D.Double(0d, 0d, 2d * r, 2d * r);
for (int i = 0; i < 8; i++) {
AffineTransform at = AffineTransform.getRotateInstance(i * 2 * Math.PI / 8);
at.concatenate(AffineTransform.getTranslateInstance(r, r));
list.add(at.createTransformedShape(s));
}
int d = r * 2 * (1 + 3);
dim = new Dimension(d, d);
}
@Override public int getIconWidth() {
return dim.width;
}
@Override public int getIconHeight() {
return dim.height;
}
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setPaint(Optional.ofNullable(c).map(Component::getBackground).orElse(Color.WHITE));
g2.fillRect(x, y, getIconWidth(), getIconHeight());
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(ELLIPSE_COLOR);
int xx = x + dim.width / 2;
int yy = y + dim.height / 2;
AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(rotate), xx, yy);
at.concatenate(AffineTransform.getTranslateInstance(xx, yy));
int size = list.size();
for (int i = 0; i < size; i++) {
float alpha = running ? (i + 1) / (float) size : .5f;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
g2.fill(at.createTransformedShape(list.get(i)));
}
g2.dispose();
}
public void next() {
if (running) {
rotate = (rotate + 45) % 360; // 45 = 360 / 8
}
}
public void setRunning(boolean running) {
this.running = running;
}
}
// // TEST: 4
// class AnimeIcon implements Icon {
// private static final int R = 4;
// private static final Color ELLIPSE_COLOR = new Color(0xE6_B3_B3);
// private final Dimension dim;
// private boolean running;
// private final List<Shape> list = new ArrayList<>();
// protected AnimeIcon() {
// super();
// int d = (int) R * 2 * (1 + 3);
// dim = new Dimension(d, d);
//
// Ellipse2D circle = new Ellipse2D.Double(R, R, d - 2 * R, d - 2 * R);
// PathIterator i = new FlatteningPathIterator(circle.getPathIterator(null), R);
// double[] coords = new double[6];
// int idx = 0;
// while (!i.isDone()) {
// i.currentSegment(coords);
// if (idx < 8) { // XXX
// list.add(new Ellipse2D.Double(coords[0] - R, coords[1] - R, 2 * R, 2 * R));
// idx++;
// }
// i.next();
// }
// }
//
// @Override public int getIconWidth() {
// return dim.width;
// }
//
// @Override public int getIconHeight() {
// return dim.height;
// }
//
// @Override public void paintIcon(Component c, Graphics g, int x, int y) {
// Graphics2D g2 = (Graphics2D) g.create();
// g2.setPaint(Optional.ofNullable(c).map(Component::getBackground).orElse(Color.WHITE));
// g2.fillRect(x, y, getIconWidth(), getIconHeight());
// g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// g2.setPaint(ELLIPSE_COLOR);
// int size = list.size();
// for (int i = 0; i < size; i++) {
// float alpha = running ? (i + 1) / (float) size : .5f;
// g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
// g2.fill(list.get(i));
// }
// g2.dispose();
// }
//
// public void next() {
// if (running) {
// // list.add(list.remove(0));
// Collections.rotate(list, 1);
// }
// }
//
// public void setRunning(boolean running) {
// this.running = running;
// }
// }