Browse Source

Introduce a DialogProvider.

This fixes the issues with injection of dialogs and the behaviour of @Singleton.

Move the swing injection related classes into a new package (a new DialogModule
will follow shortly to make use of the DialogProvider).

Also introduce a SwingPreconditions class for asserting method calls are made
on/off the EDT.

Change-Id: I92010f61f2289579603a239ab46b0a304799dedc
Reviewed-on: http://gerrit.dmdirc.com/3044
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.8
Chris Smith 10 years ago
parent
commit
c33feee949

+ 1
- 1
src/com/dmdirc/addons/activewindow/ActiveWindowModule.java View File

@@ -22,7 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.activewindow;
24 24
 
25
-import com.dmdirc.addons.ui_swing.SwingModule;
25
+import com.dmdirc.addons.ui_swing.injection.SwingModule;
26 26
 
27 27
 import dagger.Module;
28 28
 

+ 1
- 1
src/com/dmdirc/addons/dcc/DCCPluginModule.java View File

@@ -22,7 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.dcc;
24 24
 
25
-import com.dmdirc.addons.ui_swing.SwingModule;
25
+import com.dmdirc.addons.ui_swing.injection.SwingModule;
26 26
 import com.dmdirc.plugins.PluginInfo;
27 27
 
28 28
 import dagger.Module;

+ 1
- 1
src/com/dmdirc/addons/lagdisplay/LagDisplayModule.java View File

@@ -22,7 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.lagdisplay;
24 24
 
25
-import com.dmdirc.addons.ui_swing.SwingModule;
25
+import com.dmdirc.addons.ui_swing.injection.SwingModule;
26 26
 
27 27
 import dagger.Module;
28 28
 import dagger.Provides;

+ 1
- 1
src/com/dmdirc/addons/serverlistdialog/ServerListModule.java View File

@@ -22,7 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.serverlistdialog;
24 24
 
25
-import com.dmdirc.addons.ui_swing.SwingModule;
25
+import com.dmdirc.addons.ui_swing.injection.SwingModule;
26 26
 
27 27
 import dagger.Module;
28 28
 

+ 1
- 0
src/com/dmdirc/addons/ui_swing/SwingController.java View File

@@ -46,6 +46,7 @@ import com.dmdirc.addons.ui_swing.dialogs.error.ErrorListDialog;
46 46
 import com.dmdirc.addons.ui_swing.dialogs.prefs.SwingPreferencesDialog;
47 47
 import com.dmdirc.addons.ui_swing.dialogs.serversetting.ServerSettingsDialog;
48 48
 import com.dmdirc.addons.ui_swing.dialogs.url.URLDialog;
49
+import com.dmdirc.addons.ui_swing.injection.SwingModule;
49 50
 import com.dmdirc.config.prefs.PluginPreferencesCategory;
50 51
 import com.dmdirc.config.prefs.PreferencesCategory;
51 52
 import com.dmdirc.config.prefs.PreferencesDialogModel;

+ 58
- 0
src/com/dmdirc/addons/ui_swing/SwingPreconditions.java View File

@@ -0,0 +1,58 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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;
24
+
25
+import com.google.common.base.Preconditions;
26
+
27
+import javax.swing.SwingUtilities;
28
+
29
+/**
30
+ * Swing-specific preconditions.
31
+ */
32
+public final class SwingPreconditions {
33
+
34
+    private SwingPreconditions() {
35
+        // Shouldn't be instansiated.
36
+    }
37
+
38
+    /**
39
+     * Checks that the method is called on the Swing EDT.
40
+     *
41
+     * @throw IllegalStateException if the method is called from another thread.
42
+     */
43
+    public static void checkOnEDT() {
44
+        Preconditions.checkState(SwingUtilities.isEventDispatchThread(),
45
+                "Must be called on the event despatch thread");
46
+    }
47
+
48
+    /**
49
+     * Checks that the method is NOT called on the Swing EDT.
50
+     *
51
+     * @throw IllegalStateException if the method is called from the EDT.
52
+     */
53
+    public static void checkNotOnEDT() {
54
+        Preconditions.checkState(!SwingUtilities.isEventDispatchThread(),
55
+                "Must not be called ont he event despatch thread");
56
+    }
57
+
58
+}

+ 104
- 0
src/com/dmdirc/addons/ui_swing/injection/DialogProvider.java View File

@@ -0,0 +1,104 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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.injection;
24
+
25
+import static com.dmdirc.addons.ui_swing.SwingPreconditions.checkOnEDT;
26
+import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
27
+
28
+import java.awt.event.WindowAdapter;
29
+import java.awt.event.WindowEvent;
30
+import javax.inject.Provider;
31
+
32
+/**
33
+ * Simple provider for {@link StandardDialog} based windows.
34
+ *
35
+ * <p>This provider will cache instances that are created until the windows are closed. Once a
36
+ * window has been closed, the next call to {@link #get()} or {@link #displayOrRequestFocus()}
37
+ * will result in a new instance being created.
38
+ *
39
+ * @param <T> The type of dialog that will be managed.
40
+ */
41
+public class DialogProvider<T extends StandardDialog> {
42
+
43
+    /** Provider used to generate new instances. */
44
+    private final Provider<T> provider;
45
+    /** The existing instance being displayed, if any. */
46
+    private T instance;
47
+
48
+    /**
49
+     * Creates a new instance of {@link DialogProvider}.
50
+     *
51
+     * @param provider The provider to use to generate new instances of the dialog, when required.
52
+     */
53
+    public DialogProvider(final Provider<T> provider) {
54
+        this.provider = provider;
55
+    }
56
+
57
+    /**
58
+     * Gets an instance of the dialog provided by this class.
59
+     *
60
+     * <p>If there is an existing instance that hasn't been closed, it will be returned. Otherwise
61
+     * a new instance will be created and returned. New instances will not be automatically be
62
+     * displayed to the user - use {@link #displayOrRequestFocus()} to do so.
63
+     *
64
+     * <p>This method <em>must</em> be called on the Event Despatch Thread.
65
+     *
66
+     * @return An instance of the dialog.
67
+     */
68
+    public T get() {
69
+        checkOnEDT();
70
+        if (instance == null) {
71
+            instance = provider.get();
72
+            instance.addWindowListener(new Listener());
73
+        }
74
+        return instance;
75
+    }
76
+
77
+    /**
78
+     * Ensures the dialog is visible to the user.
79
+     *
80
+     * <p>If no dialog currently exists, a new one will be created and displayed to the user. If
81
+     * a dialog existed prior to this method being invoked, it will request focus to bring it to
82
+     * the user's attention.
83
+     *
84
+     * <p>This method <em>must</em> be called on the Event Despatch Thread.
85
+     */
86
+    public void displayOrRequestFocus() {
87
+        checkOnEDT();
88
+        get().displayOrRequestFocus();
89
+    }
90
+
91
+    /**
92
+     * Listens to window closing events to remove the cached instance of the dialog.
93
+     */
94
+    private class Listener extends WindowAdapter {
95
+
96
+        @Override
97
+        public void windowClosing(final WindowEvent e) {
98
+            super.windowClosing(e);
99
+            instance = null;
100
+        }
101
+
102
+    }
103
+
104
+}

src/com/dmdirc/addons/ui_swing/SwingModule.java → src/com/dmdirc/addons/ui_swing/injection/SwingModule.java View File

@@ -20,11 +20,17 @@
20 20
  * SOFTWARE.
21 21
  */
22 22
 
23
-package com.dmdirc.addons.ui_swing;
23
+package com.dmdirc.addons.ui_swing.injection;
24 24
 
25 25
 import com.dmdirc.ClientModule;
26 26
 import com.dmdirc.ClientModule.GlobalConfig;
27 27
 import com.dmdirc.ServerManager;
28
+import com.dmdirc.addons.ui_swing.MainFrame;
29
+import com.dmdirc.addons.ui_swing.QuitWorker;
30
+import com.dmdirc.addons.ui_swing.SwingController;
31
+import com.dmdirc.addons.ui_swing.SwingManager;
32
+import com.dmdirc.addons.ui_swing.SwingWindowFactory;
33
+import com.dmdirc.addons.ui_swing.UIUtilities;
28 34
 import com.dmdirc.addons.ui_swing.commands.ChannelSettings;
29 35
 import com.dmdirc.addons.ui_swing.commands.Input;
30 36
 import com.dmdirc.addons.ui_swing.commands.PopInCommand;

Loading…
Cancel
Save