Browse Source

Use DI in the server list dialog plugin.

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

+ 2
- 2
src/com/dmdirc/addons/lagdisplay/LagDisplayPlugin.java View File

@@ -227,11 +227,11 @@ public final class LagDisplayPlugin extends BasePlugin implements
227 227
         } else if (useAlternate && type.equals(CoreActionType.SERVER_PINGSENT)) {
228 228
             ((Connection) arguments[0]).getParser().sendRawMessage("LAGCHECK_" + new Date().getTime());
229 229
         } else if (useAlternate && type.equals(CoreActionType.SERVER_NUMERIC)
230
-                && ((Integer) arguments[1]).intValue() == 421
230
+                && ((Integer) arguments[1]) == 421
231 231
                 && ((String[]) arguments[2])[3].startsWith("LAGCHECK_")) {
232 232
             try {
233 233
                 final long sent = Long.parseLong(((String[]) arguments[2])[3].substring(9));
234
-                final Long duration = Long.valueOf(new Date().getTime() - sent);
234
+                final Long duration = new Date().getTime() - sent;
235 235
                 final String value = formatTime(duration);
236 236
 
237 237
                 pings.put((Connection) arguments[0], value);

+ 78
- 0
src/com/dmdirc/addons/serverlistdialog/ServerListController.java View File

@@ -0,0 +1,78 @@
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.serverlistdialog;
24
+
25
+import com.dmdirc.addons.ui_swing.components.menubar.MenuBar;
26
+
27
+import java.awt.event.ActionEvent;
28
+import java.awt.event.ActionListener;
29
+import javax.inject.Inject;
30
+import javax.inject.Provider;
31
+import javax.swing.JMenuItem;
32
+
33
+/**
34
+ * Handles registering of the server list menu items, and creation of the server list dialog.
35
+ */
36
+public class ServerListController {
37
+
38
+    private final Provider<ServerListDialog> dialogProvider;
39
+    private final MenuBar menuBar;
40
+
41
+    @Inject
42
+    public ServerListController(
43
+            final Provider<ServerListDialog> dialogProvider,
44
+            final MenuBar menuBar) {
45
+        this.dialogProvider = dialogProvider;
46
+        this.menuBar = menuBar;
47
+    }
48
+
49
+    /**
50
+     * Adds the 'Server lists' menu item to the app's main menu.
51
+     */
52
+    public void addMenu() {
53
+        final JMenuItem item = new JMenuItem("Server lists");
54
+        item.setMnemonic('l');
55
+        item.addActionListener(new MenuActionListener());
56
+        menuBar.addMenuItem("Server", item);
57
+    }
58
+
59
+    /**
60
+     * Removes the 'Server lists' menu item from the app's main menu.
61
+     */
62
+    public void removeMenu() {
63
+        // TODO: Add ability to remove items to MenuBar, remove the server list item when unloaded.
64
+    }
65
+
66
+    /**
67
+     * Listener that gets and displays a server list dialog when an action is performed.
68
+     */
69
+    private class MenuActionListener implements ActionListener {
70
+
71
+        @Override
72
+        public void actionPerformed(final ActionEvent e) {
73
+            dialogProvider.get().display();
74
+        }
75
+
76
+    }
77
+
78
+}

+ 14
- 5
src/com/dmdirc/addons/serverlistdialog/ServerListDialog.java View File

@@ -22,10 +22,13 @@
22 22
 
23 23
 package com.dmdirc.addons.serverlistdialog;
24 24
 
25
+import com.dmdirc.ServerManager;
25 26
 import com.dmdirc.actions.wrappers.PerformWrapper;
26 27
 import com.dmdirc.addons.serverlists.ServerGroupItem;
28
+import com.dmdirc.addons.ui_swing.MainFrame;
27 29
 import com.dmdirc.addons.ui_swing.SwingController;
28 30
 import com.dmdirc.addons.ui_swing.components.LockedLayer;
31
+import com.dmdirc.addons.ui_swing.dialogs.DialogManager;
29 32
 import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
30 33
 import com.dmdirc.ui.core.util.URLHandler;
31 34
 
@@ -34,6 +37,7 @@ import java.awt.event.ActionEvent;
34 37
 import java.awt.event.ActionListener;
35 38
 import java.awt.image.ColorConvertOp;
36 39
 
40
+import javax.inject.Inject;
37 41
 import javax.swing.JButton;
38 42
 
39 43
 import net.miginfocom.swing.MigLayout;
@@ -78,17 +82,22 @@ public class ServerListDialog extends StandardDialog implements
78 82
      * @param controller Swing controller
79 83
      * @param urlHandler The URL Handler to use to handle clicked links
80 84
      * @param performWrapper The wrapper to use for the perform tab
85
+     * @param serverListModel The model to use for the dialog.
86
+     * @param dialogManager The manager to register the dialog with.
87
+     * @param mainFrame The main frame that owns the dialog.
81 88
      */
89
+    @Inject
82 90
     public ServerListDialog(
83 91
             final SwingController controller,
84 92
             final URLHandler urlHandler,
85
-            final PerformWrapper performWrapper) {
86
-        super(controller.getDialogManager(), controller.getMainFrame(), ModalityType.MODELESS);
93
+            final PerformWrapper performWrapper,
94
+            final ServerListModel serverListModel,
95
+            final DialogManager dialogManager,
96
+            final MainFrame mainFrame) {
97
+        super(dialogManager, mainFrame, ModalityType.MODELESS);
87 98
 
88 99
         setTitle("Server List");
89
-        model = new ServerListModel(controller.getPluginManager(),
90
-                controller.getServerManager(), controller.getIdentityManager(),
91
-                controller.getIdentityFactory());
100
+        model = serverListModel;
92 101
         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
93 102
 
94 103
         connectButton = new JButton("Connect");

+ 17
- 34
src/com/dmdirc/addons/serverlistdialog/ServerListDialogPlugin.java View File

@@ -22,51 +22,34 @@
22 22
 
23 23
 package com.dmdirc.addons.serverlistdialog;
24 24
 
25
-import com.dmdirc.actions.wrappers.PerformWrapper;
26
-import com.dmdirc.addons.ui_swing.SwingController;
25
+import com.dmdirc.plugins.PluginInfo;
27 26
 import com.dmdirc.plugins.implementations.BasePlugin;
28 27
 
29
-import java.awt.event.ActionEvent;
30
-import java.awt.event.ActionListener;
31
-
32
-import javax.swing.JMenuItem;
28
+import dagger.ObjectGraph;
33 29
 
34 30
 /**
35 31
  * Server list dialog plugin.
36 32
  */
37
-public class ServerListDialogPlugin extends BasePlugin implements ActionListener {
38
-
39
-    /** Swing controller. */
40
-    private final SwingController controller;
33
+public class ServerListDialogPlugin extends BasePlugin {
41 34
 
42
-    /** The wrapper to use for modifying performs. */
43
-    private final PerformWrapper performWrapper;
35
+    /** Controller that manages the server list. */
36
+    private ServerListController controller;
44 37
 
45
-    /**
46
-     * Creates a new server list dialog plugin.
47
-     *
48
-     * @param swingController The controller that will own the dialog.
49
-     * @param performWrapper The wrapper to use for modifying performs.
50
-     */
51
-    public ServerListDialogPlugin(
52
-            final SwingController swingController,
53
-            final PerformWrapper performWrapper) {
54
-        this.performWrapper = performWrapper;
38
+    @Override
39
+    public void onUnload() {
40
+        super.onUnload();
55 41
 
56
-        controller = swingController;
57
-        final JMenuItem item = new JMenuItem("Server lists");
58
-        item.setMnemonic('l');
59
-        item.addActionListener(this);
60
-        controller.getMainFrame().getJMenuBar().addMenuItem("Server", item);
42
+        controller.removeMenu();
43
+        controller = null;
61 44
     }
62 45
 
63
-    /**
64
-     * {@inheritDoc}
65
-     *
66
-     * @param e Action event
67
-     */
68 46
     @Override
69
-    public void actionPerformed(final ActionEvent e) {
70
-        new ServerListDialog(controller, controller.getUrlHandler(), performWrapper).display();
47
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
48
+        super.load(pluginInfo, graph);
49
+        setObjectGraph(graph.plus(new ServerListModule()));
50
+
51
+        controller = getObjectGraph().get(ServerListController.class);
52
+        controller.addMenu();
71 53
     }
54
+
72 55
 }

+ 5
- 7
src/com/dmdirc/addons/serverlistdialog/ServerListModel.java View File

@@ -32,14 +32,13 @@ import com.dmdirc.addons.serverlists.ServerGroup;
32 32
 import com.dmdirc.addons.serverlists.ServerGroupItem;
33 33
 import com.dmdirc.addons.serverlists.ServerList;
34 34
 import com.dmdirc.interfaces.config.IdentityController;
35
-import com.dmdirc.interfaces.config.IdentityFactory;
36
-import com.dmdirc.plugins.PluginManager;
37 35
 import com.dmdirc.util.collections.ListenerList;
38 36
 
39 37
 import java.io.IOException;
40 38
 import java.net.URI;
41 39
 import java.util.List;
42 40
 
41
+import javax.inject.Inject;
43 42
 import javax.swing.tree.DefaultMutableTreeNode;
44 43
 import javax.swing.tree.DefaultTreeModel;
45 44
 
@@ -62,19 +61,18 @@ public class ServerListModel {
62 61
     /**
63 62
      * Creates a new server list model.
64 63
      *
65
-     * @param pluginManager PluginManager currently in use.
66 64
      * @param serverManager ServerManager currently in use.
67 65
      * @param identityController The controller to read/write settings with.
68
-     * @param identityFactory The factory to create identities with.
66
+     * @param serverList The server list to use for the top-level entries.
69 67
      */
68
+    @Inject
70 69
     public ServerListModel(
71
-            final PluginManager pluginManager,
72 70
             final ServerManager serverManager,
73 71
             final IdentityController identityController,
74
-            final IdentityFactory identityFactory) {
72
+            final ServerList serverList) {
75 73
         this.serverManager = serverManager;
76 74
         this.identityController = identityController;
77
-        list = new ServerList(pluginManager, serverManager, identityController, identityFactory);
75
+        list = serverList;
78 76
         listeners = new ListenerList();
79 77
     }
80 78
 

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

@@ -0,0 +1,35 @@
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.serverlistdialog;
24
+
25
+import com.dmdirc.addons.ui_swing.SwingModule;
26
+
27
+import dagger.Module;
28
+
29
+/**
30
+ * DI module for the server list plugin.
31
+ */
32
+@Module(injects = {ServerListController.class}, addsTo = SwingModule.class)
33
+public class ServerListModule {
34
+
35
+}

+ 3
- 0
src/com/dmdirc/addons/serverlists/ServerList.java View File

@@ -39,6 +39,8 @@ import java.util.Collections;
39 39
 import java.util.HashMap;
40 40
 import java.util.Map;
41 41
 
42
+import javax.inject.Inject;
43
+
42 44
 /**
43 45
  * Maintains a list of top level {@link ServerGroup}s and handles reading and
44 46
  * writing of the lists to disk.
@@ -67,6 +69,7 @@ public class ServerList implements ConfigProviderListener {
67 69
      * @param identityController The controller to read/write settings with.
68 70
      * @param identityFactory The factory to create new identities with.
69 71
      */
72
+    @Inject
70 73
     public ServerList(
71 74
             final PluginManager pluginManager,
72 75
             final ServerManager serverManager,

Loading…
Cancel
Save