View Javadoc
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(dialogTitle);
18          progressBarDialog.addWindowListener(new java.awt.event.WindowAdapter() {
19              @Override
20              public void windowClosing(java.awt.event.WindowEvent windowEvent) {
21                  cancel();
22              }
23          });
24  
25          Timer timer = new Timer(1000, e -> {
26              if (isFinished || isCancelled) {
27                  ((Timer) e.getSource()).stop();
28                  progressBarDialog.dispose();
29              } else {
30                  progressBarDialog.setValue(progress);
31              }
32          });
33          timer.start();
34      }
35  
36      protected void cancel() {
37          isCancelled = true;
38      }
39  }