Browse Source

Make WhoisNumericFormatter a bit more sane.

Implement SystemLifecycleComponent instead of making the
ActionManager create one and forget about it...
pull/325/head
Chris Smith 9 years ago
parent
commit
01517bde0f

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

@@ -38,6 +38,7 @@ import com.dmdirc.interfaces.CommandController;
38 38
 import com.dmdirc.interfaces.ConnectionFactory;
39 39
 import com.dmdirc.interfaces.ConnectionManager;
40 40
 import com.dmdirc.interfaces.LifecycleController;
41
+import com.dmdirc.interfaces.SystemLifecycleComponent;
41 42
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
42 43
 import com.dmdirc.interfaces.config.IdentityController;
43 44
 import com.dmdirc.plugins.PluginModule;
@@ -45,6 +46,7 @@ import com.dmdirc.ui.IconManager;
45 46
 import com.dmdirc.ui.messages.ColourManager;
46 47
 import com.dmdirc.ui.messages.ColourManagerFactory;
47 48
 import com.dmdirc.ui.messages.UiMessagesModule;
49
+import com.dmdirc.ui.messages.WhoisNumericFormatter;
48 50
 import com.dmdirc.ui.messages.sink.MessagesModule;
49 51
 import com.dmdirc.ui.themes.ThemeManager;
50 52
 import com.dmdirc.updater.UpdaterModule;
@@ -188,4 +190,10 @@ public class ClientModule {
188 190
         return objectGraph;
189 191
     }
190 192
 
193
+    @Provides(type = Provides.Type.SET)
194
+    public SystemLifecycleComponent getWhoisNumericFormatter(
195
+            final WhoisNumericFormatter formatter) {
196
+        return formatter;
197
+    }
198
+
191 199
 }

+ 0
- 3
src/com/dmdirc/actions/ActionManager.java View File

@@ -24,7 +24,6 @@ package com.dmdirc.actions;
24 24
 
25 25
 import com.dmdirc.DMDircMBassador;
26 26
 import com.dmdirc.Precondition;
27
-import com.dmdirc.ui.messages.WhoisNumericFormatter;
28 27
 import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
29 28
 import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
30 29
 import com.dmdirc.config.ConfigBinding;
@@ -144,8 +143,6 @@ public class ActionManager implements ActionController {
144 143
         registerComparisons(colourComparisons.getComparisons());
145 144
         registerComponents(CoreActionComponent.values());
146 145
 
147
-        new WhoisNumericFormatter(identityManager.getAddonSettings(), eventBus).register();
148
-
149 146
         eventBus.subscribe(this);
150 147
     }
151 148
 

+ 25
- 18
src/com/dmdirc/ui/messages/WhoisNumericFormatter.java View File

@@ -22,26 +22,29 @@
22 22
 
23 23
 package com.dmdirc.ui.messages;
24 24
 
25
+import com.dmdirc.ClientModule;
25 26
 import com.dmdirc.DMDircMBassador;
26 27
 import com.dmdirc.events.ServerDisconnectedEvent;
27 28
 import com.dmdirc.events.ServerNumericEvent;
28 29
 import com.dmdirc.interfaces.Connection;
30
+import com.dmdirc.interfaces.SystemLifecycleComponent;
29 31
 import com.dmdirc.interfaces.config.ConfigProvider;
30 32
 
31 33
 import java.util.HashMap;
32 34
 import java.util.Map;
33 35
 
36
+import javax.inject.Inject;
37
+import javax.inject.Singleton;
38
+
34 39
 import net.engio.mbassy.listener.Handler;
35
-import net.engio.mbassy.listener.Listener;
36
-import net.engio.mbassy.listener.References;
37 40
 
38 41
 /**
39 42
  * Listens for whois-like numeric events and automatically formats them.
40 43
  *
41 44
  * @since 0.6.3
42 45
  */
43
-@Listener(references = References.Strong)
44
-public class WhoisNumericFormatter {
46
+@Singleton
47
+public class WhoisNumericFormatter implements SystemLifecycleComponent {
45 48
 
46 49
     /** The name of the target of any current whois requests. */
47 50
     private final Map<Connection, String> targets = new HashMap<>();
@@ -57,19 +60,14 @@ public class WhoisNumericFormatter {
57 60
      * @param identity The identity to write formatters to
58 61
      * @param eventBus The event bus to subscribe to events on
59 62
      */
60
-    public WhoisNumericFormatter(final ConfigProvider identity,
63
+    @Inject
64
+    public WhoisNumericFormatter(
65
+            @ClientModule.AddonConfig final ConfigProvider identity,
61 66
             final DMDircMBassador eventBus) {
62 67
         this.identity = identity;
63 68
         this.eventBus = eventBus;
64 69
     }
65 70
 
66
-    /**
67
-     * Registers this this whois numeric formatter with the global actions manager.
68
-     */
69
-    public void register() {
70
-        eventBus.subscribe(this);
71
-    }
72
-
73 71
     /**
74 72
      * Handles a server disconnected event. This clears any entry for that server in the
75 73
      * {@link #targets} map.
@@ -107,17 +105,17 @@ public class WhoisNumericFormatter {
107 105
                         && arguments[3].equals(targets.get(server))) {
108 106
                     // This numeric should be automatically formatted.
109 107
 
110
-                    if (!event.getDisplayFormat().isEmpty()) {
111
-                        // There's a custom format. We'll see if we need to
112
-                        // add a formatter or notification settings for it
113
-                        // anyway.
114
-                        ensureExists(event.getDisplayFormat(), arguments.length);
115
-                    } else {
108
+                    if (event.getDisplayFormat().isEmpty()) {
116 109
                         // No custom formatter, switch it to an auto whois
117 110
                         // format and target.
118 111
                         final String target = "numeric_autowhois_" + (arguments.length - 4);
119 112
                         ensureExists(target, arguments.length);
120 113
                         event.setDisplayFormat(target);
114
+                    } else {
115
+                        // There's a custom format. We'll see if we need to
116
+                        // add a formatter or notification settings for it
117
+                        // anyway.
118
+                        ensureExists(event.getDisplayFormat(), arguments.length);
121 119
                     }
122 120
                 }
123 121
                 break;
@@ -147,4 +145,13 @@ public class WhoisNumericFormatter {
147 145
         }
148 146
     }
149 147
 
148
+    @Override
149
+    public void startUp() {
150
+        eventBus.subscribe(this);
151
+    }
152
+
153
+    @Override
154
+    public void shutDown() {
155
+        eventBus.unsubscribe(this);
156
+    }
150 157
 }

Loading…
Cancel
Save