summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Marco Rebhan2021-04-06 14:40:54 +0200
committerGravatar Marco Rebhan2021-04-06 14:40:54 +0200
commitf05e8f3d02a6f5c3cce37bfe26d3e94e14ff266d (patch)
tree2951d35c251dcd4b806ea641ce0c35ed288439f8
parentBump version (diff)
downloadenigma-f05e8f3d02a6f5c3cce37bfe26d3e94e14ff266d.tar.gz
enigma-f05e8f3d02a6f5c3cce37bfe26d3e94e14ff266d.tar.xz
enigma-f05e8f3d02a6f5c3cce37bfe26d3e94e14ff266d.zip
Make progress dialog thread-safe.
Closes #377.
-rw-r--r--enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java54
1 files changed, 28 insertions, 26 deletions
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java
index f97c1c28..3773b8a0 100644
--- a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java
+++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java
@@ -71,46 +71,48 @@ public class ProgressDialog implements ProgressListener, AutoCloseable {
71 } 71 }
72 72
73 public static CompletableFuture<Void> runOffThread(final JFrame parent, final ProgressRunnable runnable) { 73 public static CompletableFuture<Void> runOffThread(final JFrame parent, final ProgressRunnable runnable) {
74 CompletableFuture<Void> future = new CompletableFuture<>(); 74 return CompletableFuture.supplyAsync(() -> {
75 new Thread(() -> 75 ProgressDialog progress = new ProgressDialog(parent);
76 { 76 progress.dialog.setVisible(true);
77 try (ProgressDialog progress = new ProgressDialog(parent)) { 77 return progress;
78 }, SwingUtilities::invokeLater).thenAcceptAsync(progress -> {
79 // TODO use "try (progress)" with Java 9
80 try {
78 runnable.run(progress); 81 runnable.run(progress);
79 future.complete(null); 82 } catch (Exception e) {
80 } catch (Exception ex) { 83 throw new RuntimeException(e);
81 future.completeExceptionally(ex); 84 } finally {
82 throw new Error(ex); 85 progress.close();
83 } 86 }
84 }).start(); 87 });
85 return future;
86 } 88 }
87 89
88 @Override 90 @Override
89 public void close() { 91 public void close() {
90 this.dialog.dispose(); 92 SwingUtilities.invokeLater(this.dialog::dispose);
91 } 93 }
92 94
93 @Override 95 @Override
94 public void init(int totalWork, String title) { 96 public void init(int totalWork, String title) {
95 this.labelTitle.setText(title); 97 SwingUtilities.invokeLater(() -> {
96 this.progress.setMinimum(0); 98 this.labelTitle.setText(title);
97 this.progress.setMaximum(totalWork); 99 this.progress.setMinimum(0);
98 this.progress.setValue(0); 100 this.progress.setMaximum(totalWork);
101 this.progress.setValue(0);
102 });
99 } 103 }
100 104
101 @Override 105 @Override
102 public void step(int numDone, String message) { 106 public void step(int numDone, String message) {
103 this.labelText.setText(message); 107 SwingUtilities.invokeLater(() -> {
104 if (numDone != -1) { 108 this.labelText.setText(message);
105 this.progress.setValue(numDone); 109 if (numDone != -1) {
106 this.progress.setIndeterminate(false); 110 this.progress.setValue(numDone);
107 } else { 111 this.progress.setIndeterminate(false);
108 this.progress.setIndeterminate(true); 112 } else {
109 } 113 this.progress.setIndeterminate(true);
110 114 }
111 // update the frame 115 });
112 this.dialog.validate();
113 this.dialog.repaint();
114 } 116 }
115 117
116 public interface ProgressRunnable { 118 public interface ProgressRunnable {