Browse Source

Merge pull request #380 from greboid/master

Switch lots of things to use the new RunnableLoggingSwingWorker.
pull/382/head
Chris Smith 9 years ago
parent
commit
41471b6758

+ 6
- 26
ui_swing/src/com/dmdirc/addons/ui_swing/components/FontPicker.java View File

24
 
24
 
25
 import com.dmdirc.DMDircMBassador;
25
 import com.dmdirc.DMDircMBassador;
26
 import com.dmdirc.addons.ui_swing.components.renderers.FontListCellRenderer;
26
 import com.dmdirc.addons.ui_swing.components.renderers.FontListCellRenderer;
27
-import com.dmdirc.events.UserErrorEvent;
28
-import com.dmdirc.logger.ErrorLevel;
29
 
27
 
30
 import java.awt.Font;
28
 import java.awt.Font;
31
 import java.awt.GraphicsEnvironment;
29
 import java.awt.GraphicsEnvironment;
32
-import java.util.concurrent.ExecutionException;
33
 
30
 
34
 import javax.swing.DefaultComboBoxModel;
31
 import javax.swing.DefaultComboBoxModel;
35
 import javax.swing.JComboBox;
32
 import javax.swing.JComboBox;
36
-import javax.swing.SwingUtilities;
33
+import javax.swing.MutableComboBoxModel;
37
 
34
 
38
 /**
35
 /**
39
  * System font picking component.
36
  * System font picking component.
56
         this.fontFamily = fontFamily;
53
         this.fontFamily = fontFamily;
57
 
54
 
58
         setRenderer(new FontListCellRenderer(getRenderer()));
55
         setRenderer(new FontListCellRenderer(getRenderer()));
59
-        new LoggingSwingWorker<String[], String[]>(eventBus) {
60
-
61
-            @Override
62
-            protected String[] doInBackground() {
63
-                return GraphicsEnvironment.getLocalGraphicsEnvironment().
64
-                        getAvailableFontFamilyNames();
65
-            }
66
-
67
-            @Override
68
-            protected void done() {
69
-                try {
70
-                    loadFonts(get());
71
-                } catch (InterruptedException ex) {
72
-                    //Ignore
73
-                } catch (ExecutionException ex) {
74
-                    eventBus.publishAsync(new UserErrorEvent(ErrorLevel.MEDIUM, ex, ex.getMessage(), ""));
75
-                }
76
-            }
77
-        }.execute();
56
+        new RunnableLoggingSwingWorker<String[], String[]>(eventBus,
57
+                () -> GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(),
58
+                this::loadFonts).execute();
78
     }
59
     }
79
 
60
 
80
     /**
61
     /**
85
     private void loadFonts(final String... fonts) {
66
     private void loadFonts(final String... fonts) {
86
         final int size = getFont().getSize();
67
         final int size = getFont().getSize();
87
         for (final String font : fonts) {
68
         for (final String font : fonts) {
88
-            SwingUtilities.invokeLater(() -> ((DefaultComboBoxModel<Object>) getModel()).addElement(new Font(
89
-                    font, Font.PLAIN, size)));
69
+            ((MutableComboBoxModel<Object>) getModel()).addElement(new Font(font, Font.PLAIN, size));
90
         }
70
         }
91
-        SwingUtilities.invokeLater(() -> setSelectedItem(new Font(fontFamily, Font.PLAIN, size)));
71
+        setSelectedItem(new Font(fontFamily, Font.PLAIN, size));
92
     }
72
     }
93
 
73
 
94
 }
74
 }

+ 35
- 10
ui_swing/src/com/dmdirc/addons/ui_swing/components/RunnableLoggingSwingWorker.java View File

24
 
24
 
25
 import com.dmdirc.DMDircMBassador;
25
 import com.dmdirc.DMDircMBassador;
26
 
26
 
27
+import java.util.List;
28
+import java.util.function.Consumer;
29
+
27
 /**
30
 /**
28
  * {@link LoggingSwingWorker} that runs a {@link Runnable}.
31
  * {@link LoggingSwingWorker} that runs a {@link Runnable}.
29
  */
32
  */
30
-public class RunnableLoggingSwingWorker<T, V> extends LoggingSwingWorker<T, V> {
33
+public class RunnableLoggingSwingWorker<T, V> extends SupplierLoggingSwingWorker<T, V> {
31
 
34
 
32
-    private final Runnable runnable;
35
+    /**
36
+     * Creates a new logging swing worker.
37
+     *
38
+     * @param eventBus           Event bus to post errors to.
39
+     * @param backgroundRunnable The runnable to call as the background task, off the EDT.
40
+     */
41
+    public RunnableLoggingSwingWorker(final DMDircMBassador eventBus,
42
+            final Runnable backgroundRunnable) {
43
+        this(eventBus, backgroundRunnable, result -> {});
44
+    }
33
 
45
 
34
     /**
46
     /**
35
      * Creates a new logging swing worker.
47
      * Creates a new logging swing worker.
36
      *
48
      *
37
-     * @param eventBus Event bus to post errors to.
49
+     * @param eventBus           Event bus to post errors to.
50
+     * @param backgroundRunnable The runnable to call as the background task, off the EDT.
51
+     * @param doneConsumer       The consumer called when the background task is complete
38
      */
52
      */
39
-    public RunnableLoggingSwingWorker(final DMDircMBassador eventBus, final Runnable runnable) {
40
-        super(eventBus);
41
-        this.runnable = runnable;
53
+    public RunnableLoggingSwingWorker(final DMDircMBassador eventBus,
54
+            final Runnable backgroundRunnable,
55
+            final Consumer<T> doneConsumer) {
56
+        this(eventBus, backgroundRunnable, doneConsumer, chunks -> {});
42
     }
57
     }
43
 
58
 
44
-    @Override
45
-    protected T doInBackground() throws Exception {
46
-        runnable.run();
47
-        return null;
59
+    /**
60
+     * Creates a new logging swing worker.
61
+     *
62
+     * @param eventBus           Event bus to post errors to.
63
+     * @param backgroundRunnable The runnable to call as the background task, off the EDT.
64
+     * @param doneConsumer       The consumer called when the background task is complete
65
+     * @param processConsumer    The consumer called to process results of the background task
66
+     *                           as it progresses
67
+     */
68
+    public RunnableLoggingSwingWorker(final DMDircMBassador eventBus,
69
+            final Runnable backgroundRunnable,
70
+            final Consumer<T> doneConsumer,
71
+            final Consumer<List<V>> processConsumer) {
72
+        super(eventBus, () -> { backgroundRunnable.run(); return null; } , doneConsumer, processConsumer);
48
     }
73
     }
49
 }
74
 }

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

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

+ 12
- 24
ui_swing/src/com/dmdirc/addons/ui_swing/components/addonpanel/AddonPanel.java View File

23
 package com.dmdirc.addons.ui_swing.components.addonpanel;
23
 package com.dmdirc.addons.ui_swing.components.addonpanel;
24
 
24
 
25
 import com.dmdirc.DMDircMBassador;
25
 import com.dmdirc.DMDircMBassador;
26
-import com.dmdirc.addons.ui_swing.UIUtilities;
27
-import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
26
+import com.dmdirc.addons.ui_swing.components.RunnableLoggingSwingWorker;
28
 import com.dmdirc.addons.ui_swing.components.addonbrowser.BrowserWindow;
27
 import com.dmdirc.addons.ui_swing.components.addonbrowser.BrowserWindow;
29
 import com.dmdirc.addons.ui_swing.components.addonbrowser.DataLoaderWorkerFactory;
28
 import com.dmdirc.addons.ui_swing.components.addonbrowser.DataLoaderWorkerFactory;
30
 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
29
 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
37
 import javax.swing.JScrollPane;
36
 import javax.swing.JScrollPane;
38
 import javax.swing.JTable;
37
 import javax.swing.JTable;
39
 import javax.swing.ListSelectionModel;
38
 import javax.swing.ListSelectionModel;
39
+import javax.swing.ScrollPaneConstants;
40
 import javax.swing.event.HyperlinkEvent;
40
 import javax.swing.event.HyperlinkEvent;
41
 import javax.swing.event.HyperlinkEvent.EventType;
41
 import javax.swing.event.HyperlinkEvent.EventType;
42
 import javax.swing.event.HyperlinkListener;
42
 import javax.swing.event.HyperlinkListener;
111
 
111
 
112
         scrollPane = new JScrollPane(new JLabel("Loading " + getTypeName()
112
         scrollPane = new JScrollPane(new JLabel("Loading " + getTypeName()
113
                 + "..."));
113
                 + "..."));
114
-        scrollPane.setHorizontalScrollBarPolicy(
115
-                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
116
-        scrollPane.setVerticalScrollBarPolicy(
117
-                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
114
+        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
115
+        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
118
 
116
 
119
         blurbLabel = new TextLabel(getTypeName().substring(0, 1).toUpperCase()
117
         blurbLabel = new TextLabel(getTypeName().substring(0, 1).toUpperCase()
120
                 + getTypeName().substring(1) + " allow you to extend the "
118
                 + getTypeName().substring(1) + " allow you to extend the "
130
      * Populates the list in a background thread.
128
      * Populates the list in a background thread.
131
      */
129
      */
132
     protected void load() {
130
     protected void load() {
133
-        new LoggingSwingWorker<Object, Object>(eventBus) {
134
-            @Override
135
-            protected Object doInBackground() {
136
-                return populateList(addonList);
137
-            }
138
-
139
-            @Override
140
-            protected void done() {
141
-                super.done();
142
-                scrollPane.setViewportView(addonList);
143
-                UIUtilities.invokeLater(() -> {
144
-                    addonList.getSelectionModel()
145
-                            .addListSelectionListener(AddonPanel.this);
146
-                    addonList.getSelectionModel()
147
-                            .setSelectionInterval(0, 0);
148
-                });
149
-            }
150
-        }.execute();
131
+        new RunnableLoggingSwingWorker<>(eventBus,
132
+                () -> populateList(addonList),
133
+                value -> {
134
+                    scrollPane.setViewportView(addonList);
135
+                    addonList.getSelectionModel().addListSelectionListener(this);
136
+                    addonList.getSelectionModel().setSelectionInterval(0, 0);
137
+                }
138
+        ).execute();
151
     }
139
     }
152
 
140
 
153
     /** Lays out the dialog. */
141
     /** Lays out the dialog. */

+ 15
- 41
ui_swing/src/com/dmdirc/addons/ui_swing/components/inputfields/SwingInputHandler.java View File

25
 import com.dmdirc.DMDircMBassador;
25
 import com.dmdirc.DMDircMBassador;
26
 import com.dmdirc.addons.ui_swing.Apple;
26
 import com.dmdirc.addons.ui_swing.Apple;
27
 import com.dmdirc.addons.ui_swing.UIUtilities;
27
 import com.dmdirc.addons.ui_swing.UIUtilities;
28
-import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
28
+import com.dmdirc.addons.ui_swing.components.RunnableLoggingSwingWorker;
29
 import com.dmdirc.commandparser.parsers.CommandParser;
29
 import com.dmdirc.commandparser.parsers.CommandParser;
30
 import com.dmdirc.interfaces.CommandController;
30
 import com.dmdirc.interfaces.CommandController;
31
 import com.dmdirc.interfaces.WindowModel;
31
 import com.dmdirc.interfaces.WindowModel;
35
 import com.dmdirc.ui.input.TabCompleterUtils;
35
 import com.dmdirc.ui.input.TabCompleterUtils;
36
 
36
 
37
 import java.awt.event.ActionEvent;
37
 import java.awt.event.ActionEvent;
38
+import java.awt.event.InputEvent;
38
 import java.awt.event.KeyEvent;
39
 import java.awt.event.KeyEvent;
39
 import java.awt.event.KeyListener;
40
 import java.awt.event.KeyListener;
40
 
41
 
153
 
154
 
154
             @Override
155
             @Override
155
             public void actionPerformed(final ActionEvent e) {
156
             public void actionPerformed(final ActionEvent e) {
156
-                new LoggingSwingWorker<Object, Void>(eventBus) {
157
-
158
-                    @Override
159
-                    protected Object doInBackground() {
160
-                        localTarget.setEditable(false);
161
-                        doTabCompletion(false);
162
-                        return null;
163
-                    }
164
-
165
-                    @Override
166
-                    protected void done() {
167
-                        localTarget.setEditable(true);
168
-                        super.done();
169
-                    }
170
-                }.execute();
157
+                localTarget.setEditable(false);
158
+                new RunnableLoggingSwingWorker<>(eventBus,
159
+                        () -> doTabCompletion(false),
160
+                        value -> localTarget.setEditable(true)
161
+                ).execute();
171
             }
162
             }
172
         });
163
         });
173
         localTarget.getActionMap().put("insert-shift-tab",
164
         localTarget.getActionMap().put("insert-shift-tab",
177
 
168
 
178
                     @Override
169
                     @Override
179
                     public void actionPerformed(final ActionEvent e) {
170
                     public void actionPerformed(final ActionEvent e) {
180
-                        new LoggingSwingWorker<Object, Void>(eventBus) {
181
-
182
-                            @Override
183
-                            protected Object doInBackground() {
184
-                                localTarget.setEditable(false);
185
-                                doTabCompletion(true);
186
-                                return null;
187
-                            }
188
-
189
-                            @Override
190
-                            protected void done() {
191
-                                localTarget.setEditable(true);
192
-                                super.done();
193
-                            }
194
-                        }.execute();
171
+                        localTarget.setEditable(false);
172
+                        new RunnableLoggingSwingWorker<>(eventBus,
173
+                                () -> doTabCompletion(true),
174
+                                value -> localTarget.setEditable(true)
175
+                        ).execute();
195
                     }
176
                     }
196
                 });
177
                 });
197
         localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
178
         localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
198
                 put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "insert-tab");
179
                 put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "insert-tab");
199
         localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
180
         localTarget.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).
200
-                put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB,
201
-                                KeyEvent.SHIFT_MASK), "insert-shift-tab");
181
+                put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK),
182
+                        "insert-shift-tab");
202
     }
183
     }
203
 
184
 
204
     @Override
185
     @Override
232
                                         "Event is not from known source.");
213
                                         "Event is not from known source.");
233
                     }
214
                     }
234
                     if (source.isEditable()) {
215
                     if (source.isEditable()) {
235
-                        new LoggingSwingWorker<Object, Void>(eventBus) {
236
-
237
-                            @Override
238
-                            protected Object doInBackground() {
239
-                                enterPressed(line);
240
-                                return null;
241
-                            }
242
-                        }.execute();
216
+                        new RunnableLoggingSwingWorker<>(eventBus, () -> enterPressed(line)).execute();
243
                     }
217
                     }
244
                 });
218
                 });
245
             }
219
             }

+ 29
- 43
ui_swing/src/com/dmdirc/addons/ui_swing/dialogs/prefs/SwingPreferencesDialog.java View File

24
 
24
 
25
 import com.dmdirc.DMDircMBassador;
25
 import com.dmdirc.DMDircMBassador;
26
 import com.dmdirc.addons.ui_swing.UIUtilities;
26
 import com.dmdirc.addons.ui_swing.UIUtilities;
27
+import com.dmdirc.addons.ui_swing.components.IconManager;
27
 import com.dmdirc.addons.ui_swing.components.ListScroller;
28
 import com.dmdirc.addons.ui_swing.components.ListScroller;
28
-import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
29
+import com.dmdirc.addons.ui_swing.components.RunnableLoggingSwingWorker;
30
+import com.dmdirc.addons.ui_swing.components.SupplierLoggingSwingWorker;
29
 import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
31
 import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
30
 import com.dmdirc.addons.ui_swing.dialogs.updater.SwingRestartDialog;
32
 import com.dmdirc.addons.ui_swing.dialogs.updater.SwingRestartDialog;
31
 import com.dmdirc.addons.ui_swing.injection.DialogModule.ForSettings;
33
 import com.dmdirc.addons.ui_swing.injection.DialogModule.ForSettings;
35
 import com.dmdirc.config.prefs.PreferencesDialogModel;
37
 import com.dmdirc.config.prefs.PreferencesDialogModel;
36
 import com.dmdirc.events.UserErrorEvent;
38
 import com.dmdirc.events.UserErrorEvent;
37
 import com.dmdirc.logger.ErrorLevel;
39
 import com.dmdirc.logger.ErrorLevel;
38
-import com.dmdirc.addons.ui_swing.components.IconManager;
39
 
40
 
40
 import java.awt.Window;
41
 import java.awt.Window;
41
 import java.awt.event.ActionEvent;
42
 import java.awt.event.ActionEvent;
42
 import java.awt.event.ActionListener;
43
 import java.awt.event.ActionListener;
43
-import java.util.concurrent.ExecutionException;
44
 
44
 
45
 import javax.inject.Inject;
45
 import javax.inject.Inject;
46
 import javax.inject.Provider;
46
 import javax.inject.Provider;
51
 import javax.swing.JScrollPane;
51
 import javax.swing.JScrollPane;
52
 import javax.swing.ListSelectionModel;
52
 import javax.swing.ListSelectionModel;
53
 import javax.swing.ScrollPaneConstants;
53
 import javax.swing.ScrollPaneConstants;
54
+import javax.swing.SwingWorker;
54
 import javax.swing.WindowConstants;
55
 import javax.swing.WindowConstants;
55
 import javax.swing.event.ListSelectionEvent;
56
 import javax.swing.event.ListSelectionEvent;
56
 import javax.swing.event.ListSelectionListener;
57
 import javax.swing.event.ListSelectionListener;
74
     /** Preferences Manager. */
75
     /** Preferences Manager. */
75
     private PreferencesDialogModel manager;
76
     private PreferencesDialogModel manager;
76
     /** Manager loading swing worker. */
77
     /** Manager loading swing worker. */
77
-    private final LoggingSwingWorker<PreferencesDialogModel, Void> worker;
78
+    private final SwingWorker<PreferencesDialogModel, Void> worker;
78
     /** The provider to use for restart dialogs. */
79
     /** The provider to use for restart dialogs. */
79
     private final DialogProvider<SwingRestartDialog> restartDialogProvider;
80
     private final DialogProvider<SwingRestartDialog> restartDialogProvider;
80
     /** The provider to use to produce a category panel. */
81
     /** The provider to use to produce a category panel. */
111
 
112
 
112
         initComponents();
113
         initComponents();
113
 
114
 
114
-        worker = new LoggingSwingWorker<PreferencesDialogModel, Void>(eventBus) {
115
-
116
-            @Override
117
-            protected PreferencesDialogModel doInBackground() {
118
-                mainPanel.setWaiting(true);
119
-                PreferencesDialogModel prefsManager = null;
120
-                try {
121
-                    prefsManager = dialogModelProvider.get();
122
-                } catch (IllegalArgumentException ex) {
123
-                    mainPanel.setError(ex.getMessage());
124
-                    eventBus.publishAsync(new UserErrorEvent(ErrorLevel.HIGH, ex,
125
-                            "Unable to load the preferences dialog", ""));
126
-                }
127
-                return prefsManager;
128
-            }
129
-
130
-            @Override
131
-            protected void done() {
132
-                if (!isCancelled()) {
133
-                    try {
134
-                        final PreferencesDialogModel prefsManager = get();
135
-                        if (prefsManager != null) {
136
-                            setPrefsManager(prefsManager);
137
-                        }
138
-                    } catch (InterruptedException ex) {
139
-                        //Ignore
140
-                    } catch (ExecutionException ex) {
141
-                        eventBus.publishAsync(new UserErrorEvent(ErrorLevel.MEDIUM, ex, ex.getMessage(), ""));
115
+        worker = new SupplierLoggingSwingWorker<>(eventBus,
116
+                () -> getPrefsModel(dialogModelProvider),
117
+                value -> {
118
+                    if (value != null) {
119
+                        setPrefsManager(value);
142
                     }
120
                     }
143
-                }
144
-            }
145
-        };
121
+                });
146
         worker.execute();
122
         worker.execute();
147
     }
123
     }
148
 
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
+
149
     private void setPrefsManager(final PreferencesDialogModel manager) {
139
     private void setPrefsManager(final PreferencesDialogModel manager) {
150
         this.manager = manager;
140
         this.manager = manager;
151
 
141
 
242
             saveOptions();
232
             saveOptions();
243
         }
233
         }
244
 
234
 
245
-        new LoggingSwingWorker<Void, Void>(eventBus) {
246
-            @Override
247
-            protected Void doInBackground() {
248
-                if (manager != null) {
249
-                    manager.dismiss();
250
-                }
251
-                return null;
235
+        new RunnableLoggingSwingWorker<>(eventBus, () -> {
236
+            if (manager != null) {
237
+                manager.dismiss();
252
             }
238
             }
253
-        }.execute();
239
+        }).execute();
254
         dispose();
240
         dispose();
255
     }
241
     }
256
 
242
 

+ 5
- 11
ui_swing/src/com/dmdirc/addons/ui_swing/dialogs/updater/SwingUpdaterDialog.java View File

24
 
24
 
25
 import com.dmdirc.DMDircMBassador;
25
 import com.dmdirc.DMDircMBassador;
26
 import com.dmdirc.addons.ui_swing.UIUtilities;
26
 import com.dmdirc.addons.ui_swing.UIUtilities;
27
-import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
28
 import com.dmdirc.addons.ui_swing.components.PackingTable;
27
 import com.dmdirc.addons.ui_swing.components.PackingTable;
28
+import com.dmdirc.addons.ui_swing.components.RunnableLoggingSwingWorker;
29
 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
29
 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
30
 import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
30
 import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
31
 import com.dmdirc.addons.ui_swing.injection.DialogModule.ForUpdates;
31
 import com.dmdirc.addons.ui_swing.injection.DialogModule.ForUpdates;
182
 
182
 
183
             header.setText("DMDirc is updating the following components:");
183
             header.setText("DMDirc is updating the following components:");
184
 
184
 
185
-            new LoggingSwingWorker<Void, Void>(eventBus) {
186
-
187
-                @Override
188
-                protected Void doInBackground() {
189
-                    ((UpdateTableModel) table.getModel()).getUpdates().stream()
190
-                            .filter(((UpdateTableModel) table.getModel())::isEnabled)
191
-                            .forEach(updateManager::install);
192
-                    return null;
193
-                }
194
-            }.execute();
185
+            new RunnableLoggingSwingWorker<>(eventBus,
186
+                    () -> ((UpdateTableModel) table.getModel()).getUpdates().stream()
187
+                    .filter(((UpdateTableModel) table.getModel())::isEnabled)
188
+                    .forEach(updateManager::install)).execute();
195
 
189
 
196
             if (updateManager.getManagerStatus() == UpdateManagerStatus.IDLE_RESTART_NEEDED) {
190
             if (updateManager.getManagerStatus() == UpdateManagerStatus.IDLE_RESTART_NEEDED) {
197
                 restartDialogProvider.displayOrRequestFocus();
191
                 restartDialogProvider.displayOrRequestFocus();

Loading…
Cancel
Save