Browse Source

Split RunnableLoggingSwingWorker into two.

pull/380/head
Greg Holmes 9 years ago
parent
commit
97e8cd619a

+ 2
- 90
ui_swing/src/com/dmdirc/addons/ui_swing/components/RunnableLoggingSwingWorker.java View File

@@ -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
 }

+ 112
- 0
ui_swing/src/com/dmdirc/addons/ui_swing/components/SupplierLoggingSwingWorker.java View File

@@ -0,0 +1,112 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.ui_swing.components;
24
+
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.events.UserErrorEvent;
27
+import com.dmdirc.logger.ErrorLevel;
28
+
29
+import java.util.List;
30
+import java.util.concurrent.ExecutionException;
31
+import java.util.function.Consumer;
32
+import java.util.function.Supplier;
33
+
34
+/**
35
+ * {@link LoggingSwingWorker} that runs a {@link Supplier}.
36
+ */
37
+public class SupplierLoggingSwingWorker<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 final Supplier<T> backgroundSupplier;
43
+
44
+    /**
45
+     * Creates a new logging swing worker.
46
+     *
47
+     * @param eventBus           Event bus to post errors to.
48
+     * @param backgroundSupplier The supplier to call as the background task, off the EDT.
49
+     */
50
+    public SupplierLoggingSwingWorker(final DMDircMBassador eventBus,
51
+            final Supplier<T> backgroundSupplier) {
52
+        this(eventBus, backgroundSupplier, result -> {});
53
+    }
54
+
55
+    /**
56
+     * Creates a new logging swing worker.
57
+     *
58
+     * @param eventBus           Event bus to post errors to.
59
+     * @param backgroundSupplier The supplier to call as the background task, off the EDT.
60
+     * @param doneConsumer       The consumer called when the background task is complete
61
+     */
62
+    public SupplierLoggingSwingWorker(final DMDircMBassador eventBus,
63
+            final Supplier<T> backgroundSupplier,
64
+            final Consumer<T> doneConsumer) {
65
+        this(eventBus, backgroundSupplier, doneConsumer, chunks -> {});
66
+    }
67
+
68
+    /**
69
+     * Creates a new logging swing worker.
70
+     *
71
+     * @param eventBus           Event bus to post errors to.
72
+     * @param backgroundSupplier The supplier to call as the background task, off the EDT.
73
+     * @param doneConsumer       The consumer called when the background task is complete
74
+     * @param processConsumer    The consumer called to process results of the background task
75
+     *                           as it progresses
76
+     */
77
+    public SupplierLoggingSwingWorker(final DMDircMBassador eventBus,
78
+            final Supplier<T> backgroundSupplier,
79
+            final Consumer<T> doneConsumer,
80
+            final Consumer<List<V>> processConsumer) {
81
+        super(eventBus);
82
+        this.eventBus = eventBus;
83
+        this.backgroundSupplier = backgroundSupplier;
84
+        this.doneConsumer = doneConsumer;
85
+        this.processConsumer = processConsumer;
86
+    }
87
+
88
+    @Override
89
+    protected T doInBackground() throws Exception {
90
+        return backgroundSupplier.get();
91
+    }
92
+
93
+    @Override
94
+    protected void process(final List<V> chunks) {
95
+        processConsumer.accept(chunks);
96
+    }
97
+
98
+    @Override
99
+    protected void done() {
100
+        try {
101
+            handleDone(get());
102
+        } catch (InterruptedException ex) {
103
+            //Ignore
104
+        } catch (ExecutionException ex) {
105
+            eventBus.publishAsync(new UserErrorEvent(ErrorLevel.MEDIUM, ex, ex.getMessage(), ""));
106
+        }
107
+    }
108
+
109
+    public void handleDone(final T value) {
110
+        doneConsumer.accept(value);
111
+    }
112
+}

+ 18
- 13
ui_swing/src/com/dmdirc/addons/ui_swing/dialogs/prefs/SwingPreferencesDialog.java View File

@@ -27,6 +27,7 @@ import com.dmdirc.addons.ui_swing.UIUtilities;
27 27
 import com.dmdirc.addons.ui_swing.components.IconManager;
28 28
 import com.dmdirc.addons.ui_swing.components.ListScroller;
29 29
 import com.dmdirc.addons.ui_swing.components.RunnableLoggingSwingWorker;
30
+import com.dmdirc.addons.ui_swing.components.SupplierLoggingSwingWorker;
30 31
 import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
31 32
 import com.dmdirc.addons.ui_swing.dialogs.updater.SwingRestartDialog;
32 33
 import com.dmdirc.addons.ui_swing.injection.DialogModule.ForSettings;
@@ -111,26 +112,30 @@ public final class SwingPreferencesDialog extends StandardDialog implements
111 112
 
112 113
         initComponents();
113 114
 
114
-        worker = new RunnableLoggingSwingWorker<>(eventBus, () -> {
115
-            mainPanel.setWaiting(true);
116
-            PreferencesDialogModel prefsManager = null;
117
-            try {
118
-                prefsManager = dialogModelProvider.get();
119
-            } catch (IllegalArgumentException ex) {
120
-                mainPanel.setError(ex.getMessage());
121
-                eventBus.publishAsync(new UserErrorEvent(ErrorLevel.HIGH, ex,
122
-                        "Unable to load the preferences dialog", ""));
123
-            }
124
-            return prefsManager;
125
-        },
115
+        worker = new SupplierLoggingSwingWorker<>(eventBus,
116
+                () -> getPrefsModel(dialogModelProvider),
126 117
                 value -> {
127 118
                     if (value != null) {
128 119
                         setPrefsManager(value);
129 120
                     }
130
-        });
121
+                });
131 122
         worker.execute();
132 123
     }
133 124
 
125
+    private PreferencesDialogModel getPrefsModel(
126
+            final Provider<PreferencesDialogModel> dialogModelProvider) {
127
+        mainPanel.setWaiting(true);
128
+        PreferencesDialogModel prefsManager = null;
129
+        try {
130
+            prefsManager = dialogModelProvider.get();
131
+        } catch (IllegalArgumentException ex) {
132
+            mainPanel.setError(ex.getMessage());
133
+            eventBus.publishAsync(new UserErrorEvent(ErrorLevel.HIGH, ex,
134
+                    "Unable to load the preferences dialog", ""));
135
+        }
136
+        return prefsManager;
137
+    }
138
+
134 139
     private void setPrefsManager(final PreferencesDialogModel manager) {
135 140
         this.manager = manager;
136 141
 

Loading…
Cancel
Save