Parcourir la source

Move more things to EventBus.

Change-Id: I91d7386459109e06663757f32c1c7449ffff034a
Reviewed-on: http://gerrit.dmdirc.com/3239
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
pull/1/head
Chris Smith il y a 10 ans
Parent
révision
2edb252e99

+ 14
- 8
src/com/dmdirc/Channel.java Voir le fichier

@@ -26,6 +26,7 @@ import com.dmdirc.actions.ActionManager;
26 26
 import com.dmdirc.actions.CoreActionType;
27 27
 import com.dmdirc.commandparser.CommandType;
28 28
 import com.dmdirc.commandparser.parsers.ChannelCommandParser;
29
+import com.dmdirc.events.ChannelClosedEvent;
29 30
 import com.dmdirc.interfaces.CommandController;
30 31
 import com.dmdirc.interfaces.Connection;
31 32
 import com.dmdirc.interfaces.NicklistListener;
@@ -49,6 +50,8 @@ import com.dmdirc.util.annotations.factory.Unbound;
49 50
 import com.dmdirc.util.collections.ListenerList;
50 51
 import com.dmdirc.util.collections.RollingList;
51 52
 
53
+import com.google.common.eventbus.EventBus;
54
+
52 55
 import java.util.ArrayList;
53 56
 import java.util.Arrays;
54 57
 import java.util.Collection;
@@ -88,6 +91,7 @@ public class Channel extends MessageTarget implements ConfigChangeListener {
88 91
     private volatile boolean showModePrefix;
89 92
     /** Whether we should show colours in nicks. */
90 93
     private volatile boolean showColours;
94
+    private final EventBus eventBus;
91 95
 
92 96
     /**
93 97
      * Creates a new instance of Channel.
@@ -99,6 +103,7 @@ public class Channel extends MessageTarget implements ConfigChangeListener {
99 103
      * @param commandController   The controller to load commands from.
100 104
      * @param messageSinkManager  The sink manager to use to despatch messages.
101 105
      * @param urlBuilder          The URL builder to use when finding icons.
106
+     * @param eventBus            The bus to despatch events onto.
102 107
      */
103 108
     public Channel(
104 109
             @Unbound final Server newServer,
@@ -107,7 +112,8 @@ public class Channel extends MessageTarget implements ConfigChangeListener {
107 112
             final TabCompleterFactory tabCompleterFactory,
108 113
             final CommandController commandController,
109 114
             final MessageSinkManager messageSinkManager,
110
-            final URLBuilder urlBuilder) {
115
+            final URLBuilder urlBuilder,
116
+            final EventBus eventBus) {
111 117
         super("channel-inactive", newChannelInfo.getName(),
112 118
                 Styliser.stipControlCodes(newChannelInfo.getName()),
113 119
                 configMigrator.getConfigProvider(),
@@ -115,13 +121,14 @@ public class Channel extends MessageTarget implements ConfigChangeListener {
115 121
                 messageSinkManager,
116 122
                 urlBuilder,
117 123
                 Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
118
-                WindowComponent.INPUTFIELD.getIdentifier(),
119
-                WindowComponent.TOPICBAR.getIdentifier(),
120
-                WindowComponent.USERLIST.getIdentifier()));
124
+                        WindowComponent.INPUTFIELD.getIdentifier(),
125
+                        WindowComponent.TOPICBAR.getIdentifier(),
126
+                        WindowComponent.USERLIST.getIdentifier()));
121 127
 
122 128
         this.configMigrator = configMigrator;
123
-        channelInfo = newChannelInfo;
124
-        server = newServer;
129
+        this.channelInfo = newChannelInfo;
130
+        this.server = newServer;
131
+        this.eventBus = eventBus;
125 132
 
126 133
         getConfigManager().addChangeListener("channel", this);
127 134
         getConfigManager().addChangeListener("ui", "shownickcoloursintext", this);
@@ -328,8 +335,7 @@ public class Channel extends MessageTarget implements ConfigChangeListener {
328 335
         }
329 336
 
330 337
         // Trigger action for the window closing
331
-        ActionManager.getActionManager().triggerEvent(
332
-                CoreActionType.CHANNEL_CLOSED, null, this);
338
+        eventBus.post(new ChannelClosedEvent(this));
333 339
 
334 340
         // Inform any parents that the window is closing
335 341
         server.delChannel(channelInfo.getName());

+ 2
- 2
src/com/dmdirc/Main.java Voir le fichier

@@ -25,9 +25,9 @@ package com.dmdirc;
25 25
 import com.dmdirc.GlobalWindow.GlobalWindowManager;
26 26
 import com.dmdirc.actions.ActionManager;
27 27
 import com.dmdirc.actions.ColourActionComparison;
28
-import com.dmdirc.actions.CoreActionType;
29 28
 import com.dmdirc.commandline.CommandLineParser;
30 29
 import com.dmdirc.commandparser.CommandManager;
30
+import com.dmdirc.events.ClientClosedEvent;
31 31
 import com.dmdirc.events.ClientOpenedEvent;
32 32
 import com.dmdirc.interfaces.CommandController.CommandDetails;
33 33
 import com.dmdirc.interfaces.config.IdentityController;
@@ -174,7 +174,7 @@ public class Main {
174 174
             /** {@inheritDoc} */
175 175
             @Override
176 176
             public void run() {
177
-                actionManager.triggerEvent(CoreActionType.CLIENT_CLOSED, null);
177
+                eventBus.post(new ClientClosedEvent());
178 178
                 serverManager.disconnectAll("Unexpected shutdown");
179 179
                 identityManager.saveAll();
180 180
             }

+ 15
- 8
src/com/dmdirc/Server.java Voir le fichier

@@ -27,6 +27,7 @@ import com.dmdirc.actions.ActionManager;
27 27
 import com.dmdirc.actions.CoreActionType;
28 28
 import com.dmdirc.commandparser.CommandType;
29 29
 import com.dmdirc.commandparser.parsers.CommandParser;
30
+import com.dmdirc.events.ChannelOpenedEvent;
30 31
 import com.dmdirc.interfaces.AwayStateListener;
31 32
 import com.dmdirc.interfaces.Connection;
32 33
 import com.dmdirc.interfaces.InviteListener;
@@ -64,6 +65,8 @@ import com.dmdirc.util.URLBuilder;
64 65
 import com.dmdirc.util.annotations.factory.Factory;
65 66
 import com.dmdirc.util.annotations.factory.Unbound;
66 67
 
68
+import com.google.common.eventbus.EventBus;
69
+
67 70
 import java.net.URI;
68 71
 import java.net.URISyntaxException;
69 72
 import java.security.cert.CertificateException;
@@ -171,6 +174,8 @@ public class Server extends WritableFrameContainer implements ConfigChangeListen
171 174
     private final QueryFactory queryFactory;
172 175
     /** Factory to use for creating raw windows. */
173 176
     private final RawFactory rawFactory;
177
+    /** The event bus to despatch events onto. */
178
+    private final EventBus eventBus;
174 179
     /** The config provider to write user settings to. */
175 180
     private final ConfigProvider userSettings;
176 181
     /** The manager to use to add status bar messages. */
@@ -194,6 +199,7 @@ public class Server extends WritableFrameContainer implements ConfigChangeListen
194 199
      * @param queryFactory        The factory to use to create queries.
195 200
      * @param rawFactory          The factory to use to create raw windows.
196 201
      * @param urlBuilder          The URL builder to use when finding icons.
202
+     * @param eventBus            The event bus to despatch events onto.
197 203
      * @param userSettings        The config provider to write user settings to.
198 204
      * @param uri                 The address of the server to connect to
199 205
      * @param profile             The profile to use
@@ -212,6 +218,7 @@ public class Server extends WritableFrameContainer implements ConfigChangeListen
212 218
             final QueryFactory queryFactory,
213 219
             final RawFactory rawFactory,
214 220
             final URLBuilder urlBuilder,
221
+            final EventBus eventBus,
215 222
             @SuppressWarnings("qualifiers") @UserConfig final ConfigProvider userSettings,
216 223
             @Unbound final URI uri,
217 224
             @Unbound final ConfigProvider profile) {
@@ -223,9 +230,9 @@ public class Server extends WritableFrameContainer implements ConfigChangeListen
223 230
                 messageSinkManager,
224 231
                 urlBuilder,
225 232
                 Arrays.asList(
226
-                WindowComponent.TEXTAREA.getIdentifier(),
227
-                WindowComponent.INPUTFIELD.getIdentifier(),
228
-                WindowComponent.CERTIFICATE_VIEWER.getIdentifier()));
233
+                        WindowComponent.TEXTAREA.getIdentifier(),
234
+                        WindowComponent.INPUTFIELD.getIdentifier(),
235
+                        WindowComponent.CERTIFICATE_VIEWER.getIdentifier()));
229 236
 
230 237
         this.manager = manager;
231 238
         this.parserFactory = parserFactory;
@@ -235,6 +242,7 @@ public class Server extends WritableFrameContainer implements ConfigChangeListen
235 242
         this.channelFactory = channelFactory;
236 243
         this.queryFactory = queryFactory;
237 244
         this.rawFactory = rawFactory;
245
+        this.eventBus = eventBus;
238 246
         this.userSettings = userSettings;
239 247
         this.statusBarManager = statusBarManager;
240 248
 
@@ -611,7 +619,7 @@ public class Server extends WritableFrameContainer implements ConfigChangeListen
611 619
     public Channel addChannel(final ChannelInfo chan) {
612 620
         return addChannel(chan, !backgroundChannels.contains(chan.getName())
613 621
                 || getConfigManager().getOptionBool(DOMAIN_GENERAL,
614
-                "hidechannels"));
622
+                        "hidechannels"));
615 623
     }
616 624
 
617 625
     /** {@inheritDoc} */
@@ -635,8 +643,7 @@ public class Server extends WritableFrameContainer implements ConfigChangeListen
635 643
             final Channel newChan = channelFactory.getChannel(this, chan, channelConfig);
636 644
 
637 645
             windowManager.addWindow(this, newChan, focus);
638
-            ActionManager.getActionManager().triggerEvent(CoreActionType.CHANNEL_OPENED, null,
639
-                    newChan);
646
+            eventBus.post(new ChannelOpenedEvent(newChan));
640 647
 
641 648
             tabCompleter.addEntry(TabCompletionType.CHANNEL, chan.getName());
642 649
             channels.put(converter.toLowerCase(chan.getName()), newChan);
@@ -1497,8 +1504,8 @@ public class Server extends WritableFrameContainer implements ConfigChangeListen
1497 1504
             Logger.appError(ErrorLevel.LOW, missing.toString() + " ["
1498 1505
                     + parser.getServerSoftwareType() + "]",
1499 1506
                     new MissingModeAliasException(getNetwork(), parser,
1500
-                    getConfigManager().getOption("identity",
1501
-                    "modealiasversion"), missing.toString()));
1507
+                            getConfigManager().getOption("identity",
1508
+                                    "modealiasversion"), missing.toString()));
1502 1509
         }
1503 1510
     }
1504 1511
 

+ 12
- 10
src/com/dmdirc/actions/ActionManager.java Voir le fichier

@@ -26,6 +26,7 @@ import com.dmdirc.Precondition;
26 26
 import com.dmdirc.ServerManager;
27 27
 import com.dmdirc.actions.internal.WhoisNumericFormatter;
28 28
 import com.dmdirc.config.ConfigBinding;
29
+import com.dmdirc.events.ClientClosedEvent;
29 30
 import com.dmdirc.events.DMDircEvent;
30 31
 import com.dmdirc.interfaces.ActionController;
31 32
 import com.dmdirc.interfaces.ActionListener;
@@ -182,16 +183,6 @@ public class ActionManager implements ActionController {
182 183
 
183 184
         new WhoisNumericFormatter(identityManager.getAddonSettings()).register();
184 185
 
185
-        // Register a listener for the closing event, so we can save actions
186
-        registerListener(new ActionListener() {
187
-            /** {@inheritDoc} */
188
-            @Override
189
-            public void processEvent(final ActionType type, final StringBuffer format,
190
-                    final Object... arguments) {
191
-                saveAllActions();
192
-            }
193
-        }, CoreActionType.CLIENT_CLOSED);
194
-
195 186
         eventBus.register(this);
196 187
     }
197 188
 
@@ -205,6 +196,17 @@ public class ActionManager implements ActionController {
205 196
         }
206 197
     }
207 198
 
199
+    /**
200
+     * Saves all actions when the client is being closed.
201
+     *
202
+     * @param event The event that was raised.
203
+     */
204
+    @Subscribe
205
+    public void handleClientClosed(final ClientClosedEvent event) {
206
+        LOG.debug("Client closed - saving all actions");
207
+        saveAllActions();
208
+    }
209
+
208 210
     /** {@inheritDoc} */
209 211
     @Override
210 212
     public void registerSetting(final String name, final String value) {

+ 36
- 0
src/com/dmdirc/events/ChannelClosedEvent.java Voir le fichier

@@ -0,0 +1,36 @@
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.events;
24
+
25
+import com.dmdirc.Channel;
26
+
27
+/**
28
+ * Event raised when a channel is closed.
29
+ */
30
+public class ChannelClosedEvent extends ChannelEvent {
31
+
32
+    public ChannelClosedEvent(final Channel channel) {
33
+        super(channel);
34
+    }
35
+
36
+}

+ 50
- 0
src/com/dmdirc/events/ChannelEvent.java Voir le fichier

@@ -0,0 +1,50 @@
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.events;
24
+
25
+import com.dmdirc.Channel;
26
+
27
+import static com.google.common.base.Preconditions.checkNotNull;
28
+
29
+/**
30
+ * Base type for events that occur in channels.
31
+ */
32
+public abstract class ChannelEvent extends DMDircEvent {
33
+
34
+    /** The channel that this event occurred on. */
35
+    private final Channel channel;
36
+
37
+    public ChannelEvent(final long timestamp, final Channel channel) {
38
+        super(timestamp);
39
+        this.channel = checkNotNull(channel);
40
+    }
41
+
42
+    public ChannelEvent(final Channel channel) {
43
+        this.channel = checkNotNull(channel);
44
+    }
45
+
46
+    public Channel getChannel() {
47
+        return channel;
48
+    }
49
+
50
+}

+ 36
- 0
src/com/dmdirc/events/ChannelOpenedEvent.java Voir le fichier

@@ -0,0 +1,36 @@
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.events;
24
+
25
+import com.dmdirc.Channel;
26
+
27
+/**
28
+ * Event raised when a channel is opened.
29
+ */
30
+public class ChannelOpenedEvent extends ChannelEvent {
31
+
32
+    public ChannelOpenedEvent(final Channel channel) {
33
+        super(channel);
34
+    }
35
+
36
+}

+ 30
- 0
src/com/dmdirc/events/ClientClosedEvent.java Voir le fichier

@@ -0,0 +1,30 @@
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.events;
24
+
25
+/**
26
+ * Fired when the client is being closed.
27
+ */
28
+public class ClientClosedEvent extends DMDircEvent {
29
+
30
+}

+ 4
- 0
test/com/dmdirc/ServerTest.java Voir le fichier

@@ -33,6 +33,8 @@ import com.dmdirc.ui.core.components.StatusBarManager;
33 33
 import com.dmdirc.ui.input.TabCompleterFactory;
34 34
 import com.dmdirc.util.URLBuilder;
35 35
 
36
+import com.google.common.eventbus.EventBus;
37
+
36 38
 import java.net.URI;
37 39
 
38 40
 import org.junit.Before;
@@ -62,6 +64,7 @@ public class ServerTest {
62 64
     @Mock private RawFactory rawFactory;
63 65
     @Mock private StatusBarManager statusBarManager;
64 66
     @Mock private URLBuilder urlBuilder;
67
+    @Mock private EventBus eventBus;
65 68
 
66 69
     private Server server;
67 70
 
@@ -85,6 +88,7 @@ public class ServerTest {
85 88
                 queryFactory,
86 89
                 rawFactory,
87 90
                 urlBuilder,
91
+                eventBus,
88 92
                 userConfig,
89 93
                 new URI("irc-test://255.255.255.255"),
90 94
                 profile);

Chargement…
Annuler
Enregistrer