Pārlūkot izejas kodu

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 gadus atpakaļ
vecāks
revīzija
18e0c6b275

+ 15
- 0
src/com/dmdirc/ClientModule.java Parādīt failu

@@ -46,6 +46,7 @@ import com.dmdirc.plugins.PluginInfo;
46 46
 import com.dmdirc.plugins.PluginManager;
47 47
 import com.dmdirc.ui.WarningDialog;
48 48
 import com.dmdirc.ui.core.components.StatusBarManager;
49
+import com.dmdirc.ui.messages.ColourManager;
49 50
 import com.dmdirc.ui.themes.ThemeManager;
50 51
 import com.dmdirc.updater.UpdateChecker;
51 52
 import com.dmdirc.updater.Version;
@@ -338,6 +339,20 @@ public class ClientModule {
338 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 357
      * Called when the global config cannot be loaded due to an error. This
343 358
      * method informs the user of the problem and installs a new default config

+ 10
- 3
src/com/dmdirc/commandparser/CommandLoader.java Parādīt failu

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

+ 25
- 1
src/com/dmdirc/commandparser/commands/channel/SetNickColour.java Parādīt failu

@@ -33,6 +33,7 @@ import com.dmdirc.commandparser.commands.Command;
33 33
 import com.dmdirc.commandparser.commands.IntelligentCommand;
34 34
 import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
35 35
 import com.dmdirc.commandparser.commands.context.CommandContext;
36
+import com.dmdirc.interfaces.CommandController;
36 37
 import com.dmdirc.parser.interfaces.ChannelClientInfo;
37 38
 import com.dmdirc.ui.Colour;
38 39
 import com.dmdirc.ui.input.AdditionalTabTargets;
@@ -50,6 +51,29 @@ public class SetNickColour extends Command implements IntelligentCommand {
50 51
             + "set the specified person's display colour",
51 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 77
     /** {@inheritDoc} */
54 78
     @Override
55 79
     public void execute(final FrameContainer origin,
@@ -94,7 +118,7 @@ public class SetNickColour extends Command implements IntelligentCommand {
94 118
             channel.refreshClients();
95 119
         } else {
96 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 122
             if (newColour == null) {
99 123
                 sendLine(origin, args.isSilent(), FORMAT_ERROR, "Invalid colour specified.");
100 124
                 return;

+ 13
- 1
src/com/dmdirc/commandparser/commands/global/Notify.java Parādīt failu

@@ -45,6 +45,18 @@ public class Notify extends Command implements IntelligentCommand {
45 45
             "notify <colour> - sets the notification colour for this window",
46 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 60
     /** {@inheritDoc} */
49 61
     @Override
50 62
     public void execute(final FrameContainer origin,
@@ -54,7 +66,7 @@ public class Notify extends Command implements IntelligentCommand {
54 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 71
         if (colour == null) {
60 72
             showUsage(origin, args.isSilent(), "notify",

+ 29
- 4
src/com/dmdirc/config/ConfigSource.java Parādīt failu

@@ -36,6 +36,8 @@ import java.util.ArrayList;
36 36
 import java.util.Arrays;
37 37
 import java.util.List;
38 38
 
39
+import javax.inject.Provider;
40
+
39 41
 /**
40 42
  * Defines methods to get options from a config source in various forms.
41 43
  * <p>
@@ -56,7 +58,7 @@ public abstract class ConfigSource {
56 58
 
57 59
     /** A permissive validator to use when callers don't specify one. */
58 60
     private static final Validator<String> PERMISSIVE_VALIDATOR
59
-            = new PermissiveValidator<String>();
61
+            = new PermissiveValidator<>();
60 62
 
61 63
     /** A validator for integer settings. */
62 64
     private static final Validator<String> INT_VALIDATOR
@@ -66,6 +68,29 @@ public abstract class ConfigSource {
66 68
     private static final Validator<String> COLOUR_VALIDATOR
67 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 95
      * Retrieves the specified option from this config source.
71 96
      *
@@ -226,7 +251,7 @@ public abstract class ConfigSource {
226 251
 
227 252
         @SuppressWarnings("unchecked")
228 253
         final Validator<String> newValidator = required && fallbacks.length == 0
229
-                ? new ValidatorChain<String>(new DisabledOptionValidator(), validator)
254
+                ? new ValidatorChain<>(new DisabledOptionValidator(), validator)
230 255
                 : validator;
231 256
 
232 257
         if (!hasOption(domain, option, newValidator)
@@ -282,7 +307,7 @@ public abstract class ConfigSource {
282 307
             final String ... fallbacks) {
283 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,7 +331,7 @@ public abstract class ConfigSource {
306 331
      */
307 332
     public List<String> getOptionList(final String domain, final String option,
308 333
             final boolean trimEmpty) {
309
-        final List<String> res = new ArrayList<String>();
334
+        final List<String> res = new ArrayList<>();
310 335
 
311 336
         if (hasOption(domain, option, PERMISSIVE_VALIDATOR)) {
312 337
             for (String line : getOption(domain, option).split("\n")) {

+ 32
- 2
src/com/dmdirc/ui/messages/ColourManager.java Parādīt failu

@@ -32,11 +32,13 @@ import com.dmdirc.ui.Colour;
32 32
 import java.util.HashMap;
33 33
 import java.util.Map;
34 34
 
35
+import javax.inject.Provider;
36
+
35 37
 /**
36 38
  * The colour manager manages the colour scheme for the IRC client. It allows
37 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 43
     /** Default colours used for the standard 16 IRC colours. */
42 44
     private static final Colour[] DEFAULT_COLOURS = {
@@ -161,7 +163,7 @@ public final class ColourManager {
161 163
      */
162 164
     @Deprecated
163 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,6 +270,7 @@ public final class ColourManager {
268 270
      *
269 271
      * @return An instance of the colour manager.
270 272
      */
273
+    @Deprecated
271 274
     public static synchronized ColourManager getColourManager() {
272 275
         if (instance == null) {
273 276
             instance = new ColourManager(IdentityManager.getIdentityManager().getGlobalConfiguration());
@@ -276,4 +279,31 @@ public final class ColourManager {
276 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 Parādīt failu

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

+ 16
- 13
test/com/dmdirc/commandparser/commands/channel/SetNickColourTest.java Parādīt failu

@@ -26,33 +26,36 @@ import com.dmdirc.Channel;
26 26
 import com.dmdirc.FrameContainer;
27 27
 import com.dmdirc.commandparser.CommandArguments;
28 28
 import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
29
+import com.dmdirc.interfaces.CommandController;
30
+import com.dmdirc.ui.messages.ColourManager;
29 31
 
30 32
 import org.junit.Before;
31
-import org.junit.BeforeClass;
32 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 38
 import static org.mockito.Mockito.*;
35 39
 
40
+@RunWith(MockitoJUnitRunner.class)
36 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 48
     @Before
46 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 55
     @Test
53 56
     public void testUsageNoArgs() {
54 57
         final FrameContainer tiw = mock(FrameContainer.class);
55
-        command.execute(tiw, new CommandArguments("/foo"),
58
+        command.execute(tiw, new CommandArguments(controller, "/foo"),
56 59
                 new ChannelCommandContext(null, SetNickColour.INFO, channel));
57 60
 
58 61
         verify(tiw).addLine(eq("commandUsage"), anyChar(), anyString(), anyString());
@@ -61,7 +64,7 @@ public class SetNickColourTest {
61 64
     @Test
62 65
     public void testUsageNicklist() {
63 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 68
                 new ChannelCommandContext(null, SetNickColour.INFO, channel));
66 69
 
67 70
         verify(tiw).addLine(eq("commandUsage"), anyChar(), anyString(), anyString());
@@ -70,7 +73,7 @@ public class SetNickColourTest {
70 73
     @Test
71 74
     public void testUsageText() {
72 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 77
                 new ChannelCommandContext(null, SetNickColour.INFO, channel));
75 78
 
76 79
         verify(tiw).addLine(eq("commandUsage"), anyChar(), anyString(), anyString());

Notiek ielāde…
Atcelt
Saglabāt