Browse Source

Tidy up some ColourManager refs and inject.

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

+ 15
- 0
src/com/dmdirc/ClientModule.java View File

46
 import com.dmdirc.plugins.PluginManager;
46
 import com.dmdirc.plugins.PluginManager;
47
 import com.dmdirc.ui.WarningDialog;
47
 import com.dmdirc.ui.WarningDialog;
48
 import com.dmdirc.ui.core.components.StatusBarManager;
48
 import com.dmdirc.ui.core.components.StatusBarManager;
49
+import com.dmdirc.ui.messages.ColourManager;
49
 import com.dmdirc.ui.themes.ThemeManager;
50
 import com.dmdirc.ui.themes.ThemeManager;
50
 import com.dmdirc.updater.UpdateChecker;
51
 import com.dmdirc.updater.UpdateChecker;
51
 import com.dmdirc.updater.Version;
52
 import com.dmdirc.updater.Version;
338
         return wrapper;
339
         return wrapper;
339
     }
340
     }
340
 
341
 
342
+    /**
343
+     * Gets the colour manager.
344
+     *
345
+     * @param controller The identity controller to read settings from.
346
+     * @return A colour manager for the client.
347
+     */
348
+    @Provides
349
+    @Singleton
350
+    public ColourManager getColourManager(final IdentityController controller) {
351
+        final ColourManager manager = new ColourManager(controller.getGlobalConfiguration());
352
+        ColourManager.setColourManager(manager);
353
+        return manager;
354
+    }
355
+
341
     /**
356
     /**
342
      * Called when the global config cannot be loaded due to an error. This
357
      * Called when the global config cannot be loaded due to an error. This
343
      * method informs the user of the problem and installs a new default config
358
      * method informs the user of the problem and installs a new default config

+ 10
- 3
src/com/dmdirc/commandparser/CommandLoader.java View File

71
 import com.dmdirc.interfaces.ActionController;
71
 import com.dmdirc.interfaces.ActionController;
72
 import com.dmdirc.interfaces.CommandController;
72
 import com.dmdirc.interfaces.CommandController;
73
 import com.dmdirc.plugins.PluginManager;
73
 import com.dmdirc.plugins.PluginManager;
74
+import com.dmdirc.ui.messages.ColourManager;
74
 
75
 
75
 import javax.inject.Inject;
76
 import javax.inject.Inject;
76
 
77
 
94
     /** The action controller to pass to action-aware commands. */
95
     /** The action controller to pass to action-aware commands. */
95
     private final ActionController actionController;
96
     private final ActionController actionController;
96
 
97
 
98
+    /** The colour manager to pass to colourful commands. */
99
+    private final ColourManager colourManager;
100
+
97
     /**
101
     /**
98
      * Creates a new instance of {@link CommandLoader}.
102
      * Creates a new instance of {@link CommandLoader}.
99
      *
103
      *
101
      * @param serverManager The server manager to pass to server-related commands.
105
      * @param serverManager The server manager to pass to server-related commands.
102
      * @param pluginManager The plugin manager to pass to plugin-dependent commands.
106
      * @param pluginManager The plugin manager to pass to plugin-dependent commands.
103
      * @param identityManager The identity manager to pass to config-related commands.
107
      * @param identityManager The identity manager to pass to config-related commands.
108
+     * @param colourManager The colour manager to give to colourful commands.
104
      */
109
      */
105
     @Inject
110
     @Inject
106
     public CommandLoader(
111
     public CommandLoader(
108
             final ServerManager serverManager,
113
             final ServerManager serverManager,
109
             final PluginManager pluginManager,
114
             final PluginManager pluginManager,
110
             final IdentityManager identityManager,
115
             final IdentityManager identityManager,
111
-            final ActionController actionController) {
116
+            final ActionController actionController,
117
+            final ColourManager colourManager) {
112
         this.lifecycleController = lifecycleController;
118
         this.lifecycleController = lifecycleController;
113
         this.serverManager = serverManager;
119
         this.serverManager = serverManager;
114
         this.pluginManager = pluginManager;
120
         this.pluginManager = pluginManager;
115
         this.identityManager = identityManager;
121
         this.identityManager = identityManager;
116
         this.actionController = actionController;
122
         this.actionController = actionController;
123
+        this.colourManager = colourManager;
117
     }
124
     }
118
 
125
 
119
     /**
126
     /**
133
         manager.registerCommand(new Mode(), Mode.INFO);
140
         manager.registerCommand(new Mode(), Mode.INFO);
134
         manager.registerCommand(new Names(), Names.INFO);
141
         manager.registerCommand(new Names(), Names.INFO);
135
         manager.registerCommand(new Part(), Part.INFO);
142
         manager.registerCommand(new Part(), Part.INFO);
136
-        manager.registerCommand(new SetNickColour(), SetNickColour.INFO);
143
+        manager.registerCommand(new SetNickColour(colourManager), SetNickColour.INFO);
137
         manager.registerCommand(new ShowTopic(), ShowTopic.INFO);
144
         manager.registerCommand(new ShowTopic(), ShowTopic.INFO);
138
 
145
 
139
         // Server commands
146
         // Server commands
171
         manager.registerCommand(new Help(), Help.INFO);
178
         manager.registerCommand(new Help(), Help.INFO);
172
         manager.registerCommand(new Ifplugin(pluginManager), Ifplugin.INFO);
179
         manager.registerCommand(new Ifplugin(pluginManager), Ifplugin.INFO);
173
         manager.registerCommand(new NewServer(serverManager, pluginManager, identityManager), NewServer.INFO);
180
         manager.registerCommand(new NewServer(serverManager, pluginManager, identityManager), NewServer.INFO);
174
-        manager.registerCommand(new Notify(), Notify.INFO);
181
+        manager.registerCommand(new Notify(colourManager), Notify.INFO);
175
         manager.registerCommand(new LoadPlugin(pluginManager), LoadPlugin.INFO);
182
         manager.registerCommand(new LoadPlugin(pluginManager), LoadPlugin.INFO);
176
         manager.registerCommand(new UnloadPlugin(pluginManager), UnloadPlugin.INFO);
183
         manager.registerCommand(new UnloadPlugin(pluginManager), UnloadPlugin.INFO);
177
         manager.registerCommand(new OpenWindow(), OpenWindow.INFO);
184
         manager.registerCommand(new OpenWindow(), OpenWindow.INFO);

+ 25
- 1
src/com/dmdirc/commandparser/commands/channel/SetNickColour.java View File

33
 import com.dmdirc.commandparser.commands.IntelligentCommand;
33
 import com.dmdirc.commandparser.commands.IntelligentCommand;
34
 import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
34
 import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
35
 import com.dmdirc.commandparser.commands.context.CommandContext;
35
 import com.dmdirc.commandparser.commands.context.CommandContext;
36
+import com.dmdirc.interfaces.CommandController;
36
 import com.dmdirc.parser.interfaces.ChannelClientInfo;
37
 import com.dmdirc.parser.interfaces.ChannelClientInfo;
37
 import com.dmdirc.ui.Colour;
38
 import com.dmdirc.ui.Colour;
38
 import com.dmdirc.ui.input.AdditionalTabTargets;
39
 import com.dmdirc.ui.input.AdditionalTabTargets;
50
             + "set the specified person's display colour",
51
             + "set the specified person's display colour",
51
             CommandType.TYPE_CHANNEL);
52
             CommandType.TYPE_CHANNEL);
52
 
53
 
54
+    /** Manager to use to convert colours. */
55
+    private final ColourManager colourManager;
56
+
57
+    /**
58
+     * Creates a new instance of the {@link SetNickColour} command.
59
+     *
60
+     * @param colourManager The colour manager to use to convert colours.
61
+     */
62
+    public SetNickColour(final ColourManager colourManager) {
63
+        this.colourManager = colourManager;
64
+    }
65
+
66
+    /**
67
+     * Creates a new instance of the {@link SetNickColour} command.
68
+     *
69
+     * @param controller The command controller that owns this command.
70
+     * @param colourManager The colour manager to use to convert colours.
71
+     */
72
+    public SetNickColour(final CommandController controller, final ColourManager colourManager) {
73
+        super(controller);
74
+        this.colourManager = colourManager;
75
+    }
76
+
53
     /** {@inheritDoc} */
77
     /** {@inheritDoc} */
54
     @Override
78
     @Override
55
     public void execute(final FrameContainer origin,
79
     public void execute(final FrameContainer origin,
94
             channel.refreshClients();
118
             channel.refreshClients();
95
         } else {
119
         } else {
96
             // We're setting the colour
120
             // We're setting the colour
97
-            final Colour newColour = ColourManager.parseColour(args.getArguments()[offset], null);
121
+            final Colour newColour = colourManager.getColourFromString(args.getArguments()[offset], null);
98
             if (newColour == null) {
122
             if (newColour == null) {
99
                 sendLine(origin, args.isSilent(), FORMAT_ERROR, "Invalid colour specified.");
123
                 sendLine(origin, args.isSilent(), FORMAT_ERROR, "Invalid colour specified.");
100
                 return;
124
                 return;

+ 13
- 1
src/com/dmdirc/commandparser/commands/global/Notify.java View File

45
             "notify <colour> - sets the notification colour for this window",
45
             "notify <colour> - sets the notification colour for this window",
46
             CommandType.TYPE_GLOBAL);
46
             CommandType.TYPE_GLOBAL);
47
 
47
 
48
+    /** Manager to use to convert colours. */
49
+    private final ColourManager colourManager;
50
+
51
+    /**
52
+     * Creates a new instance of the {@link Notify} command.
53
+     *
54
+     * @param colourManager The colour manager to use to convert colours.
55
+     */
56
+    public Notify(final ColourManager colourManager) {
57
+        this.colourManager = colourManager;
58
+    }
59
+
48
     /** {@inheritDoc} */
60
     /** {@inheritDoc} */
49
     @Override
61
     @Override
50
     public void execute(final FrameContainer origin,
62
     public void execute(final FrameContainer origin,
54
             return;
66
             return;
55
         }
67
         }
56
 
68
 
57
-        final Colour colour = ColourManager.parseColour(args.getArguments()[0], null);
69
+        final Colour colour = colourManager.getColourFromString(args.getArguments()[0], null);
58
 
70
 
59
         if (colour == null) {
71
         if (colour == null) {
60
             showUsage(origin, args.isSilent(), "notify",
72
             showUsage(origin, args.isSilent(), "notify",

+ 29
- 4
src/com/dmdirc/config/ConfigSource.java View File

36
 import java.util.Arrays;
36
 import java.util.Arrays;
37
 import java.util.List;
37
 import java.util.List;
38
 
38
 
39
+import javax.inject.Provider;
40
+
39
 /**
41
 /**
40
  * Defines methods to get options from a config source in various forms.
42
  * Defines methods to get options from a config source in various forms.
41
  * <p>
43
  * <p>
56
 
58
 
57
     /** A permissive validator to use when callers don't specify one. */
59
     /** A permissive validator to use when callers don't specify one. */
58
     private static final Validator<String> PERMISSIVE_VALIDATOR
60
     private static final Validator<String> PERMISSIVE_VALIDATOR
59
-            = new PermissiveValidator<String>();
61
+            = new PermissiveValidator<>();
60
 
62
 
61
     /** A validator for integer settings. */
63
     /** A validator for integer settings. */
62
     private static final Validator<String> INT_VALIDATOR
64
     private static final Validator<String> INT_VALIDATOR
66
     private static final Validator<String> COLOUR_VALIDATOR
68
     private static final Validator<String> COLOUR_VALIDATOR
67
             = new OptionalValidator(new ColourValidator());
69
             = new OptionalValidator(new ColourValidator());
68
 
70
 
71
+    /** Manager to use to convert colours. */
72
+    private final Provider<ColourManager> colourManager;
73
+
74
+    /**
75
+     * Creates a new instance of {@link ConfigSource}.
76
+     *
77
+     * @param colourManager The colour manager to use to convert colours.
78
+     */
79
+    public ConfigSource(final Provider<ColourManager> colourManager) {
80
+        this.colourManager = colourManager;
81
+    }
82
+
83
+    /**
84
+     * Creates a new instance of {@link ConfigSource} using the singleton
85
+     * colour manager.
86
+     *
87
+     * @deprecated Should pass in a {@link ColourManager}.
88
+     */
89
+    @Deprecated
90
+    public ConfigSource() {
91
+        this(ColourManager.getColourManagerProvider());
92
+    }
93
+
69
     /**
94
     /**
70
      * Retrieves the specified option from this config source.
95
      * Retrieves the specified option from this config source.
71
      *
96
      *
226
 
251
 
227
         @SuppressWarnings("unchecked")
252
         @SuppressWarnings("unchecked")
228
         final Validator<String> newValidator = required && fallbacks.length == 0
253
         final Validator<String> newValidator = required && fallbacks.length == 0
229
-                ? new ValidatorChain<String>(new DisabledOptionValidator(), validator)
254
+                ? new ValidatorChain<>(new DisabledOptionValidator(), validator)
230
                 : validator;
255
                 : validator;
231
 
256
 
232
         if (!hasOption(domain, option, newValidator)
257
         if (!hasOption(domain, option, newValidator)
282
             final String ... fallbacks) {
307
             final String ... fallbacks) {
283
         final String value = getOptionString(domain, option, true, COLOUR_VALIDATOR, fallbacks);
308
         final String value = getOptionString(domain, option, true, COLOUR_VALIDATOR, fallbacks);
284
 
309
 
285
-        return value == null ? null : ColourManager.parseColour(value, null);
310
+        return value == null ? null : colourManager.get().getColourFromString(value, null);
286
     }
311
     }
287
 
312
 
288
     /**
313
     /**
306
      */
331
      */
307
     public List<String> getOptionList(final String domain, final String option,
332
     public List<String> getOptionList(final String domain, final String option,
308
             final boolean trimEmpty) {
333
             final boolean trimEmpty) {
309
-        final List<String> res = new ArrayList<String>();
334
+        final List<String> res = new ArrayList<>();
310
 
335
 
311
         if (hasOption(domain, option, PERMISSIVE_VALIDATOR)) {
336
         if (hasOption(domain, option, PERMISSIVE_VALIDATOR)) {
312
             for (String line : getOption(domain, option).split("\n")) {
337
             for (String line : getOption(domain, option).split("\n")) {

+ 32
- 2
src/com/dmdirc/ui/messages/ColourManager.java View File

32
 import java.util.HashMap;
32
 import java.util.HashMap;
33
 import java.util.Map;
33
 import java.util.Map;
34
 
34
 
35
+import javax.inject.Provider;
36
+
35
 /**
37
 /**
36
  * The colour manager manages the colour scheme for the IRC client. It allows
38
  * The colour manager manages the colour scheme for the IRC client. It allows
37
  * other components to use IRC colour codes instead of absolute colours.
39
  * other components to use IRC colour codes instead of absolute colours.
38
  */
40
  */
39
-public final class ColourManager {
41
+public class ColourManager {
40
 
42
 
41
     /** Default colours used for the standard 16 IRC colours. */
43
     /** Default colours used for the standard 16 IRC colours. */
42
     private static final Colour[] DEFAULT_COLOURS = {
44
     private static final Colour[] DEFAULT_COLOURS = {
161
      */
163
      */
162
     @Deprecated
164
     @Deprecated
163
     public static Colour parseColour(final String spec) {
165
     public static Colour parseColour(final String spec) {
164
-        return parseColour(spec, Colour.WHITE);
166
+        return instance.getColourFromString(spec, Colour.WHITE);
165
     }
167
     }
166
 
168
 
167
     /**
169
     /**
268
      *
270
      *
269
      * @return An instance of the colour manager.
271
      * @return An instance of the colour manager.
270
      */
272
      */
273
+    @Deprecated
271
     public static synchronized ColourManager getColourManager() {
274
     public static synchronized ColourManager getColourManager() {
272
         if (instance == null) {
275
         if (instance == null) {
273
             instance = new ColourManager(IdentityManager.getIdentityManager().getGlobalConfiguration());
276
             instance = new ColourManager(IdentityManager.getIdentityManager().getGlobalConfiguration());
276
         return instance;
279
         return instance;
277
     }
280
     }
278
 
281
 
282
+    /**
283
+     * Gets a provider of a colour manager for use in the future.
284
+     *
285
+     * @return A colour manager provider
286
+     * @deprecated Should be injected instead.
287
+     */
288
+    @Deprecated
289
+    public static Provider<ColourManager> getColourManagerProvider() {
290
+        return new Provider<ColourManager>() {
291
+            /** {@inheritDoc} */
292
+            @Override
293
+            public ColourManager get() {
294
+                return instance;
295
+            }
296
+        };
297
+    }
298
+
299
+    /**
300
+     * Sets the singleton instance of the colour manager to use.
301
+     *
302
+     * @param colourManager The colour manager to use.
303
+     */
304
+    @Deprecated
305
+    public static void setColourManager(final ColourManager colourManager) {
306
+        instance = colourManager;
307
+    }
308
+
279
 }
309
 }

+ 3
- 1
test/com/dmdirc/commandparser/commands/HelpTest.java View File

31
 import com.dmdirc.config.InvalidIdentityFileException;
31
 import com.dmdirc.config.InvalidIdentityFileException;
32
 import com.dmdirc.interfaces.ActionController;
32
 import com.dmdirc.interfaces.ActionController;
33
 import com.dmdirc.plugins.PluginManager;
33
 import com.dmdirc.plugins.PluginManager;
34
+import com.dmdirc.ui.messages.ColourManager;
34
 
35
 
35
 import java.util.LinkedList;
36
 import java.util.LinkedList;
36
 import java.util.List;
37
 import java.util.List;
68
                 serverManager,
69
                 serverManager,
69
                 mock(PluginManager.class),
70
                 mock(PluginManager.class),
70
                 mock(IdentityManager.class),
71
                 mock(IdentityManager.class),
71
-                mock(ActionController.class)).loadCommands(commandManager);
72
+                mock(ActionController.class),
73
+                mock(ColourManager.class)).loadCommands(commandManager);
72
 
74
 
73
         for (CommandType type : CommandType.values()) {
75
         for (CommandType type : CommandType.values()) {
74
             for (CommandInfo command : commandManager.getCommands(type).keySet()) {
76
             for (CommandInfo command : commandManager.getCommands(type).keySet()) {

+ 16
- 13
test/com/dmdirc/commandparser/commands/channel/SetNickColourTest.java View File

26
 import com.dmdirc.FrameContainer;
26
 import com.dmdirc.FrameContainer;
27
 import com.dmdirc.commandparser.CommandArguments;
27
 import com.dmdirc.commandparser.CommandArguments;
28
 import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
28
 import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
29
+import com.dmdirc.interfaces.CommandController;
30
+import com.dmdirc.ui.messages.ColourManager;
29
 
31
 
30
 import org.junit.Before;
32
 import org.junit.Before;
31
-import org.junit.BeforeClass;
32
 import org.junit.Test;
33
 import org.junit.Test;
34
+import org.junit.runner.RunWith;
35
+import org.mockito.Mock;
36
+import org.mockito.runners.MockitoJUnitRunner;
33
 
37
 
34
 import static org.mockito.Mockito.*;
38
 import static org.mockito.Mockito.*;
35
 
39
 
40
+@RunWith(MockitoJUnitRunner.class)
36
 public class SetNickColourTest {
41
 public class SetNickColourTest {
37
 
42
 
38
-    private Channel channel;
39
-
40
-    @BeforeClass
41
-    public static void setUpClass() throws Exception {
42
-        TestMain.getTestMain();
43
-    }
43
+    @Mock private Channel channel;
44
+    @Mock private ColourManager colourManager;
45
+    @Mock private CommandController controller;
46
+    private SetNickColour command;
44
 
47
 
45
     @Before
48
     @Before
46
     public void setUp() {
49
     public void setUp() {
47
-        channel = mock(Channel.class);
50
+        command = new SetNickColour(controller, colourManager);
51
+        when(controller.getCommandChar()).thenReturn('/');
52
+        when(controller.getSilenceChar()).thenReturn('.');
48
     }
53
     }
49
 
54
 
50
-    private final SetNickColour command = new SetNickColour();
51
-
52
     @Test
55
     @Test
53
     public void testUsageNoArgs() {
56
     public void testUsageNoArgs() {
54
         final FrameContainer tiw = mock(FrameContainer.class);
57
         final FrameContainer tiw = mock(FrameContainer.class);
55
-        command.execute(tiw, new CommandArguments("/foo"),
58
+        command.execute(tiw, new CommandArguments(controller, "/foo"),
56
                 new ChannelCommandContext(null, SetNickColour.INFO, channel));
59
                 new ChannelCommandContext(null, SetNickColour.INFO, channel));
57
 
60
 
58
         verify(tiw).addLine(eq("commandUsage"), anyChar(), anyString(), anyString());
61
         verify(tiw).addLine(eq("commandUsage"), anyChar(), anyString(), anyString());
61
     @Test
64
     @Test
62
     public void testUsageNicklist() {
65
     public void testUsageNicklist() {
63
         final FrameContainer tiw = mock(FrameContainer.class);
66
         final FrameContainer tiw = mock(FrameContainer.class);
64
-        command.execute(tiw, new CommandArguments("/foo --nicklist"),
67
+        command.execute(tiw, new CommandArguments(controller, "/foo --nicklist"),
65
                 new ChannelCommandContext(null, SetNickColour.INFO, channel));
68
                 new ChannelCommandContext(null, SetNickColour.INFO, channel));
66
 
69
 
67
         verify(tiw).addLine(eq("commandUsage"), anyChar(), anyString(), anyString());
70
         verify(tiw).addLine(eq("commandUsage"), anyChar(), anyString(), anyString());
70
     @Test
73
     @Test
71
     public void testUsageText() {
74
     public void testUsageText() {
72
         final FrameContainer tiw = mock(FrameContainer.class);
75
         final FrameContainer tiw = mock(FrameContainer.class);
73
-        command.execute(tiw, new CommandArguments("/foo --text"),
76
+        command.execute(tiw, new CommandArguments(controller, "/foo --text"),
74
                 new ChannelCommandContext(null, SetNickColour.INFO, channel));
77
                 new ChannelCommandContext(null, SetNickColour.INFO, channel));
75
 
78
 
76
         verify(tiw).addLine(eq("commandUsage"), anyChar(), anyString(), anyString());
79
         verify(tiw).addLine(eq("commandUsage"), anyChar(), anyString(), anyString());

Loading…
Cancel
Save