|
@@ -23,35 +23,14 @@
|
23
|
23
|
package com.dmdirc.addons.ui_swing.components;
|
24
|
24
|
|
25
|
25
|
import com.dmdirc.DMDircMBassador;
|
26
|
|
-import com.dmdirc.events.UserErrorEvent;
|
27
|
|
-import com.dmdirc.logger.ErrorLevel;
|
28
|
26
|
|
29
|
27
|
import java.util.List;
|
30
|
|
-import java.util.concurrent.ExecutionException;
|
31
|
28
|
import java.util.function.Consumer;
|
32
|
|
-import java.util.function.Supplier;
|
33
|
29
|
|
34
|
30
|
/**
|
35
|
31
|
* {@link LoggingSwingWorker} that runs a {@link Runnable}.
|
36
|
32
|
*/
|
37
|
|
-public class RunnableLoggingSwingWorker<T, V> extends LoggingSwingWorker<T, V> {
|
38
|
|
-
|
39
|
|
- private final DMDircMBassador eventBus;
|
40
|
|
- private final Consumer<T> doneConsumer;
|
41
|
|
- private final Consumer<List<V>> processConsumer;
|
42
|
|
- private Supplier<T> backgroundSupplier;
|
43
|
|
- private Runnable backgroundRunnable;
|
44
|
|
-
|
45
|
|
- /**
|
46
|
|
- * Creates a new logging swing worker.
|
47
|
|
- *
|
48
|
|
- * @param eventBus Event bus to post errors to.
|
49
|
|
- * @param backgroundSupplier The supplier to call as the background task, off the EDT.
|
50
|
|
- */
|
51
|
|
- public RunnableLoggingSwingWorker(final DMDircMBassador eventBus,
|
52
|
|
- final Supplier<T> backgroundSupplier) {
|
53
|
|
- this(eventBus, backgroundSupplier, result -> {});
|
54
|
|
- }
|
|
33
|
+public class RunnableLoggingSwingWorker<T, V> extends SupplierLoggingSwingWorker<T, V> {
|
55
|
34
|
|
56
|
35
|
/**
|
57
|
36
|
* Creates a new logging swing worker.
|
|
@@ -64,19 +43,6 @@ public class RunnableLoggingSwingWorker<T, V> extends LoggingSwingWorker<T, V> {
|
64
|
43
|
this(eventBus, backgroundRunnable, result -> {});
|
65
|
44
|
}
|
66
|
45
|
|
67
|
|
- /**
|
68
|
|
- * Creates a new logging swing worker.
|
69
|
|
- *
|
70
|
|
- * @param eventBus Event bus to post errors to.
|
71
|
|
- * @param backgroundSupplier The supplier to call as the background task, off the EDT.
|
72
|
|
- * @param doneConsumer The consumer called when the background task is complete
|
73
|
|
- */
|
74
|
|
- public RunnableLoggingSwingWorker(final DMDircMBassador eventBus,
|
75
|
|
- final Supplier<T> backgroundSupplier,
|
76
|
|
- final Consumer<T> doneConsumer) {
|
77
|
|
- this(eventBus, backgroundSupplier, doneConsumer, chunks -> {});
|
78
|
|
- }
|
79
|
|
-
|
80
|
46
|
/**
|
81
|
47
|
* Creates a new logging swing worker.
|
82
|
48
|
*
|
|
@@ -90,26 +56,6 @@ public class RunnableLoggingSwingWorker<T, V> extends LoggingSwingWorker<T, V> {
|
90
|
56
|
this(eventBus, backgroundRunnable, doneConsumer, chunks -> {});
|
91
|
57
|
}
|
92
|
58
|
|
93
|
|
- /**
|
94
|
|
- * Creates a new logging swing worker.
|
95
|
|
- *
|
96
|
|
- * @param eventBus Event bus to post errors to.
|
97
|
|
- * @param backgroundSupplier The supplier to call as the background task, off the EDT.
|
98
|
|
- * @param doneConsumer The consumer called when the background task is complete
|
99
|
|
- * @param processConsumer The consumer called to process results of the background task
|
100
|
|
- * as it progresses
|
101
|
|
- */
|
102
|
|
- public RunnableLoggingSwingWorker(final DMDircMBassador eventBus,
|
103
|
|
- final Supplier<T> backgroundSupplier,
|
104
|
|
- final Consumer<T> doneConsumer,
|
105
|
|
- final Consumer<List<V>> processConsumer) {
|
106
|
|
- super(eventBus);
|
107
|
|
- this.eventBus = eventBus;
|
108
|
|
- this.backgroundSupplier = backgroundSupplier;
|
109
|
|
- this.doneConsumer = doneConsumer;
|
110
|
|
- this.processConsumer = processConsumer;
|
111
|
|
- }
|
112
|
|
-
|
113
|
59
|
/**
|
114
|
60
|
* Creates a new logging swing worker.
|
115
|
61
|
*
|
|
@@ -123,40 +69,6 @@ public class RunnableLoggingSwingWorker<T, V> extends LoggingSwingWorker<T, V> {
|
123
|
69
|
final Runnable backgroundRunnable,
|
124
|
70
|
final Consumer<T> doneConsumer,
|
125
|
71
|
final Consumer<List<V>> processConsumer) {
|
126
|
|
- super(eventBus);
|
127
|
|
- this.eventBus = eventBus;
|
128
|
|
- this.backgroundRunnable = backgroundRunnable;
|
129
|
|
- this.doneConsumer = doneConsumer;
|
130
|
|
- this.processConsumer = processConsumer;
|
131
|
|
- }
|
132
|
|
-
|
133
|
|
- @Override
|
134
|
|
- protected T doInBackground() throws Exception {
|
135
|
|
- if (backgroundSupplier == null) {
|
136
|
|
- backgroundRunnable.run();
|
137
|
|
- return null;
|
138
|
|
- } else {
|
139
|
|
- return backgroundSupplier.get();
|
140
|
|
- }
|
141
|
|
- }
|
142
|
|
-
|
143
|
|
- @Override
|
144
|
|
- protected void process(final List<V> chunks) {
|
145
|
|
- processConsumer.accept(chunks);
|
146
|
|
- }
|
147
|
|
-
|
148
|
|
- @Override
|
149
|
|
- protected void done() {
|
150
|
|
- try {
|
151
|
|
- handleDone(get());
|
152
|
|
- } catch (InterruptedException ex) {
|
153
|
|
- //Ignore
|
154
|
|
- } catch (ExecutionException ex) {
|
155
|
|
- eventBus.publishAsync(new UserErrorEvent(ErrorLevel.MEDIUM, ex, ex.getMessage(), ""));
|
156
|
|
- }
|
157
|
|
- }
|
158
|
|
-
|
159
|
|
- public void handleDone(final T value) {
|
160
|
|
- doneConsumer.accept(value);
|
|
72
|
+ super(eventBus, () -> { backgroundRunnable.run(); return null; } , doneConsumer, processConsumer);
|
161
|
73
|
}
|
162
|
74
|
}
|