Bläddra i källkod

Inject some action wrappers.

Change-Id: Iee5c2f5062ad1ae3237731fb79b1e03f54566b1f
Reviewed-on: http://gerrit.dmdirc.com/2694
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.8rc1
Chris Smith 10 år sedan
förälder
incheckning
263d9e330d

+ 44
- 2
src/com/dmdirc/ClientModule.java Visa fil

@@ -22,7 +22,10 @@
22 22
 
23 23
 package com.dmdirc;
24 24
 
25
+import com.dmdirc.actions.ActionGroup;
25 26
 import com.dmdirc.actions.ActionManager;
27
+import com.dmdirc.actions.wrappers.AliasWrapper;
28
+import com.dmdirc.actions.wrappers.PerformWrapper;
26 29
 import com.dmdirc.commandline.CommandLineOptionsModule;
27 30
 import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
28 31
 import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
@@ -53,6 +56,9 @@ import java.awt.GraphicsEnvironment;
53 56
 import java.io.File;
54 57
 import java.text.SimpleDateFormat;
55 58
 import java.util.Date;
59
+import java.util.Set;
60
+
61
+import javax.inject.Provider;
56 62
 import javax.inject.Singleton;
57 63
 
58 64
 import dagger.Module;
@@ -121,12 +127,18 @@ public class ClientModule {
121 127
      *
122 128
      * @param serverManager The server manager to use to iterate servers.
123 129
      * @param identityController The identity controller to use to look up settings.
130
+     * @param aliasWrapperProvider Provider for {@link AliasWrapper}s.
131
+     * @param performWrapperProvider Provider for {@link PerformWrapper}s.
124 132
      * @return An unitialised action manager.
125 133
      */
126 134
     @Provides
127 135
     @Singleton
128
-    public ActionManager getActionManager(final ServerManager serverManager, final IdentityController identityController) {
129
-        final ActionManager actionManager = new ActionManager(serverManager, identityController);
136
+    public ActionManager getActionManager(
137
+            final ServerManager serverManager,
138
+            final IdentityController identityController,
139
+            final Provider<Set<ActionGroup>> actionWrappersProvider) {
140
+        final ActionManager actionManager = new ActionManager(serverManager,
141
+                identityController, actionWrappersProvider);
130 142
         ActionManager.setActionManager(actionManager);
131 143
         return actionManager;
132 144
     }
@@ -296,6 +308,36 @@ public class ClientModule {
296 308
         return manager;
297 309
     }
298 310
 
311
+    /**
312
+     * Gets the alias actions wrapper.
313
+     *
314
+     * @param commandController The controller to register commands with
315
+     * @param serverManager The manager to use to iterate servers.
316
+     * @return An alias wrapper to use in the client.
317
+     */
318
+    @Provides(type = Provides.Type.SET)
319
+    @Singleton
320
+    public ActionGroup getAliasWrapper(
321
+            final CommandController commandController,
322
+            final ServerManager serverManager) {
323
+        final AliasWrapper wrapper = new AliasWrapper(commandController, serverManager);
324
+        AliasWrapper.setAliasWrapper(wrapper);
325
+        return wrapper;
326
+    }
327
+
328
+    /**
329
+     * Gets the performs actions wrapper.
330
+     *
331
+     * @return An performs wrapper to use in the client.
332
+     */
333
+    @Provides(type = Provides.Type.SET)
334
+    @Singleton
335
+    public ActionGroup getPerformWrapper() {
336
+        final PerformWrapper wrapper = new PerformWrapper();
337
+        PerformWrapper.setPerformWrapper(wrapper);
338
+        return wrapper;
339
+    }
340
+
299 341
     /**
300 342
      * Called when the global config cannot be loaded due to an error. This
301 343
      * method informs the user of the problem and installs a new default config

+ 1
- 0
src/com/dmdirc/Main.java Visa fil

@@ -95,6 +95,7 @@ public class Main implements LifecycleController {
95 95
      * @param commandManager Unused for now - TODO: remove me when it's injected somewhere sensible.
96 96
      * @param messageSinkManager Unused for now - TODO: remove me when it's injected somewhere sensible.
97 97
      * @param themeManager Unused for now - TODO: remove me when it's injected somewhere sensible.
98
+     * @param aliasWrapper Alias wrapper to provide to the actions manager.
98 99
      * @param corePluginExtractor Extractor to use for core plugins.
99 100
      */
100 101
     @Inject

+ 23
- 8
src/com/dmdirc/actions/ActionManager.java Visa fil

@@ -25,8 +25,6 @@ package com.dmdirc.actions;
25 25
 import com.dmdirc.Precondition;
26 26
 import com.dmdirc.ServerManager;
27 27
 import com.dmdirc.actions.internal.WhoisNumericFormatter;
28
-import com.dmdirc.actions.wrappers.AliasWrapper;
29
-import com.dmdirc.actions.wrappers.PerformWrapper;
30 28
 import com.dmdirc.config.ConfigBinding;
31 29
 import com.dmdirc.interfaces.ActionController;
32 30
 import com.dmdirc.interfaces.ActionListener;
@@ -49,6 +47,9 @@ import java.util.Collections;
49 47
 import java.util.HashMap;
50 48
 import java.util.List;
51 49
 import java.util.Map;
50
+import java.util.Set;
51
+
52
+import javax.inject.Provider;
52 53
 
53 54
 import lombok.extern.slf4j.Slf4j;
54 55
 
@@ -67,6 +68,9 @@ public class ActionManager implements ActionController {
67 68
     /** The ServerManager currently in use. */
68 69
     private final ServerManager serverManager;
69 70
 
71
+    /** Provider for action wrappers. */
72
+    private final Provider<Set<ActionGroup>> actionWrappersProvider;
73
+
70 74
     /** A list of registered action types. */
71 75
     private final List<ActionType> types = new ArrayList<>();
72 76
 
@@ -100,11 +104,15 @@ public class ActionManager implements ActionController {
100 104
      *
101 105
      * @param serverManager The ServerManager in use.
102 106
      * @param identityManager The IdentityManager to load configuration from.
107
+     * @param actionWrappersProvider Provider of action wrappers.
103 108
      */
104
-    public ActionManager(final ServerManager serverManager, final IdentityController identityManager) {
109
+    public ActionManager(
110
+            final ServerManager serverManager,
111
+            final IdentityController identityManager,
112
+            final Provider<Set<ActionGroup>> actionWrappersProvider) {
105 113
         this.serverManager = serverManager;
106 114
         this.identityManager = identityManager;
107
-        this.identityManager.getGlobalConfiguration().getBinder().bind(this, ActionManager.class);
115
+        this.actionWrappersProvider = actionWrappersProvider;
108 116
     }
109 117
 
110 118
     /**
@@ -135,17 +143,24 @@ public class ActionManager implements ActionController {
135 143
         return me;
136 144
     }
137 145
 
138
-    /** {@inheritDoc} */
139
-    @Override
146
+    /**
147
+     * Initialiases the actions manager.
148
+     *
149
+     * @param aliasWrapper The wrapper to use for aliases.
150
+     * @param performWrapper The wrapper to use for performs.
151
+     */
140 152
     public void initialise() {
141 153
         log.info("Initialising the actions manager");
142 154
 
155
+        identityManager.getGlobalConfiguration().getBinder().bind(this, ActionManager.class);
156
+
143 157
         registerTypes(CoreActionType.values());
144 158
         registerComparisons(CoreActionComparison.values());
145 159
         registerComponents(CoreActionComponent.values());
146 160
 
147
-        addGroup(AliasWrapper.getAliasWrapper());
148
-        addGroup(PerformWrapper.getPerformWrapper());
161
+        for (ActionGroup wrapper : actionWrappersProvider.get()) {
162
+            addGroup(wrapper);
163
+        }
149 164
 
150 165
         new WhoisNumericFormatter(identityManager.getGlobalAddonIdentity()).register();
151 166
 

+ 14
- 9
src/com/dmdirc/actions/wrappers/AliasWrapper.java Visa fil

@@ -28,9 +28,7 @@ import com.dmdirc.ServerManager;
28 28
 import com.dmdirc.actions.Action;
29 29
 import com.dmdirc.actions.ActionCondition;
30 30
 import com.dmdirc.actions.ActionGroup;
31
-import com.dmdirc.actions.ActionManager;
32 31
 import com.dmdirc.actions.CoreActionType;
33
-import com.dmdirc.commandparser.CommandManager;
34 32
 import com.dmdirc.interfaces.CommandController;
35 33
 import com.dmdirc.logger.ErrorLevel;
36 34
 import com.dmdirc.logger.Logger;
@@ -48,7 +46,7 @@ public class AliasWrapper extends ActionGroup {
48 46
     private static AliasWrapper me;
49 47
 
50 48
     /** A list of registered alias names. */
51
-    private final List<String> aliases = new ArrayList<String>();
49
+    private final List<String> aliases = new ArrayList<>();
52 50
 
53 51
     /** Command controller to get command info from. */
54 52
     private final CommandController commandController;
@@ -73,21 +71,28 @@ public class AliasWrapper extends ActionGroup {
73 71
      *
74 72
      * @return A singleton instance of AliasWrapper
75 73
      */
76
-    public static synchronized AliasWrapper getAliasWrapper() {
77
-        if (me == null) {
78
-            me = new AliasWrapper(CommandManager.getCommandManager(), ActionManager.getActionManager().getServerManager());
79
-        }
80
-
74
+    @Deprecated
75
+    public static AliasWrapper getAliasWrapper() {
81 76
         return me;
82 77
     }
83 78
 
79
+    /**
80
+     * Sets the alias wrapper that will be used as a singleton instance.
81
+     *
82
+     * @param wrapper The wrapper to use as a singleton.
83
+     */
84
+    @Deprecated
85
+    public static void setAliasWrapper(final AliasWrapper wrapper) {
86
+        me = wrapper;
87
+    }
88
+
84 89
     /**
85 90
      * Retrieves a list of alias names registered with this wrapper.
86 91
      *
87 92
      * @return A list of alias names
88 93
      */
89 94
     public List<String> getAliases() {
90
-        return new ArrayList<String>(aliases);
95
+        return new ArrayList<>(aliases);
91 96
     }
92 97
 
93 98
     /** {@inheritDoc} */

+ 15
- 4
src/com/dmdirc/actions/wrappers/PerformWrapper.java Visa fil

@@ -44,7 +44,7 @@ import java.util.List;
44 44
 public class PerformWrapper extends ActionGroup {
45 45
 
46 46
     /** A singleton instance of the Perform Wrapper. */
47
-    private static final PerformWrapper ME = new PerformWrapper();
47
+    private static PerformWrapper me;
48 48
 
49 49
     /** The component name for per-profile perform conditions. */
50 50
     private static final String PP_COMP_NAME = "SERVER_PROFILE.IDENTITY_NAME";
@@ -52,7 +52,7 @@ public class PerformWrapper extends ActionGroup {
52 52
     /**
53 53
      * Creates a new instance of PerformWrapper.
54 54
      */
55
-    private PerformWrapper() {
55
+    public PerformWrapper() {
56 56
         super("performs");
57 57
     }
58 58
 
@@ -61,8 +61,19 @@ public class PerformWrapper extends ActionGroup {
61 61
      *
62 62
      * @return A singleton instance of PerformWrapper
63 63
      */
64
+    @Deprecated
64 65
     public static PerformWrapper getPerformWrapper() {
65
-        return ME;
66
+        return me;
67
+    }
68
+
69
+    /**
70
+     * Sets the singleton instance of the perform wrapper.
71
+     *
72
+     * @param wrapper The new singleton instance.
73
+     */
74
+    @Deprecated
75
+    public static void setPerformWrapper(final PerformWrapper wrapper) {
76
+        me = wrapper;
66 77
     }
67 78
 
68 79
     /** {@inheritDoc} */
@@ -217,7 +228,7 @@ public class PerformWrapper extends ActionGroup {
217 228
      */
218 229
     private Action createAction(final String server, final String network,
219 230
             final String profile) {
220
-        final List<ActionCondition> conditions = new ArrayList<ActionCondition>();
231
+        final List<ActionCondition> conditions = new ArrayList<>();
221 232
         final CoreActionComponent component =
222 233
                 server.isEmpty() ? CoreActionComponent.SERVER_NETWORK
223 234
                 : CoreActionComponent.SERVER_NAME;

+ 0
- 5
src/com/dmdirc/interfaces/ActionController.java Visa fil

@@ -189,11 +189,6 @@ public interface ActionController {
189 189
      */
190 190
     ActionType getType(final String type);
191 191
 
192
-    /**
193
-     * Initialises the action manager.
194
-     */
195
-    void initialise();
196
-
197 192
     /**
198 193
      * Loads actions from the user's directory.
199 194
      */

+ 27
- 3
test/com/dmdirc/TestMain.java Visa fil

@@ -1,5 +1,6 @@
1 1
 package com.dmdirc;
2 2
 
3
+import com.dmdirc.actions.ActionGroup;
3 4
 import com.dmdirc.actions.ActionManager;
4 5
 import com.dmdirc.commandline.CommandLineParser;
5 6
 import com.dmdirc.commandparser.CommandManager;
@@ -10,11 +11,17 @@ import com.dmdirc.updater.manager.UpdateManager;
10 11
 
11 12
 import java.io.File;
12 13
 import java.io.IOException;
14
+import java.util.Collections;
15
+
16
+import javax.inject.Provider;
13 17
 import static org.mockito.Mockito.*;
14 18
 
15 19
 /**
16 20
  * Main subclass to init things needed for testing.
21
+ *
22
+ * @deprecated Tests shouldn't need the entire universe created.
17 23
  */
24
+@Deprecated
18 25
 public class TestMain extends Main {
19 26
 
20 27
     private static Main instance;
@@ -84,8 +91,11 @@ public class TestMain extends Main {
84 91
                 IdentityManager.getIdentityManager().loadVersionIdentity();
85 92
 
86 93
                 final ServerManager serverManager = mock(ServerManager.class);
94
+                final CommandManager commandManager = new CommandManager(serverManager);
87 95
 
88
-                final ActionManager actionManager = new ActionManager(serverManager, identityManager);
96
+                final ActionManager actionManager = new ActionManager(
97
+                        serverManager, identityManager,
98
+                        new DummyProvider<>(Collections.<ActionGroup>emptySet()));
89 99
                 ActionManager.setActionManager(actionManager);
90 100
 
91 101
                 final PluginManager pluginManager = new PluginManager(
@@ -95,8 +105,8 @@ public class TestMain extends Main {
95 105
                         new CorePluginExtractor(pluginManager, pluginDirectory);
96 106
 
97 107
                 instance = new TestMain(identityManager, serverManager,
98
-                        actionManager, null, pluginManager,
99
-                        new CommandManager(serverManager), corePluginExtractor);
108
+                        actionManager, null, pluginManager, commandManager,
109
+                        corePluginExtractor);
100 110
                 instance.init();
101 111
             } catch (IOException ex) {
102 112
                 // Blargh.
@@ -104,4 +114,18 @@ public class TestMain extends Main {
104 114
         }
105 115
         return instance;
106 116
     }
117
+
118
+    private static class DummyProvider<T> implements Provider<T> {
119
+
120
+        private final T value;
121
+
122
+        public DummyProvider(T value) {
123
+            this.value = value;
124
+        }
125
+
126
+        @Override
127
+        public T get() {
128
+            return value;
129
+        }
130
+    }
107 131
 }

Laddar…
Avbryt
Spara