1 package edu.jiangxin.apktoolbox.swing.extend.plugin.runnable;
2
3 import edu.jiangxin.apktoolbox.swing.extend.plugin.IPreparePluginCallback;
4 import edu.jiangxin.apktoolbox.swing.extend.plugin.ProgressBarDialog;
5
6 import javax.swing.*;
7
8 public abstract class AbstractRunnable implements Runnable {
9 protected final IPreparePluginCallback callback;
10 protected final ProgressBarDialog progressBarDialog;
11 protected int progress = 0;
12 protected boolean isCancelled = false;
13 protected boolean isFinished = false;
14
15 protected AbstractRunnable(String dialogTitle, IPreparePluginCallback callback) {
16 this.callback = callback;
17 this.progressBarDialog = new ProgressBarDialog();
18 this.progressBarDialog.initialize(dialogTitle);
19 progressBarDialog.addWindowListener(new java.awt.event.WindowAdapter() {
20 @Override
21 public void windowClosing(java.awt.event.WindowEvent windowEvent) {
22 cancel();
23 }
24 });
25
26 Timer timer = new Timer(1000, e -> {
27 if (isFinished || isCancelled) {
28 ((Timer) e.getSource()).stop();
29 progressBarDialog.dispose();
30 } else {
31 progressBarDialog.setValue(progress);
32 }
33 });
34 timer.start();
35 }
36
37 protected void cancel() {
38 isCancelled = true;
39 }
40 }