Browse Source

EventBus the prefs events.

Change-Id: Ia3a86ce30a3ad96c2c67597dba341b2b10162c8e
Depends-On: I27e1059cc3228013d30bab85549434dea28f236c
Reviewed-on: http://gerrit.dmdirc.com/3417
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
pull/1/head
Greg Holmes 10 years ago
parent
commit
7037a54c7d

+ 2
- 1
src/com/dmdirc/ClientModule.java View File

@@ -221,13 +221,14 @@ public class ClientModule {
221 221
     @Provides
222 222
     @Singleton
223 223
     public PluginManager getPluginManager(
224
+            final EventBus eventBus,
224 225
             final IdentityController identityController,
225 226
             final ActionController actionController,
226 227
             final UpdateManager updateManager,
227 228
             final Provider<PluginInjectorInitialiser> initialiserProvider,
228 229
             final ObjectGraph objectGraph,
229 230
             @Directory(DirectoryType.PLUGINS) final String directory) {
230
-        final PluginManager manager = new PluginManager(identityController,
231
+        final PluginManager manager = new PluginManager(eventBus, identityController,
231 232
                 actionController, updateManager, initialiserProvider, objectGraph, directory);
232 233
         final CorePluginExtractor extractor = new CorePluginExtractor(manager, directory);
233 234
         checkBundledPlugins(extractor, manager, identityController.getGlobalConfiguration());

+ 15
- 10
src/com/dmdirc/config/prefs/PreferencesDialogModel.java View File

@@ -22,8 +22,8 @@
22 22
 
23 23
 package com.dmdirc.config.prefs;
24 24
 
25
-import com.dmdirc.actions.ActionManager;
26
-import com.dmdirc.actions.CoreActionType;
25
+import com.dmdirc.events.ClientPrefsClosedEvent;
26
+import com.dmdirc.events.ClientPrefsOpenedEvent;
27 27
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
28 28
 import com.dmdirc.interfaces.config.ConfigProvider;
29 29
 import com.dmdirc.plugins.PluginManager;
@@ -32,12 +32,16 @@ import com.dmdirc.util.collections.ListenerList;
32 32
 import com.dmdirc.util.validators.NumericalValidator;
33 33
 import com.dmdirc.util.validators.OptionalValidator;
34 34
 
35
+import com.google.common.eventbus.EventBus;
36
+
35 37
 import java.util.ArrayList;
36 38
 import java.util.Collections;
37 39
 import java.util.HashMap;
38 40
 import java.util.List;
39 41
 import java.util.Map;
40 42
 
43
+import javax.inject.Inject;
44
+
41 45
 /**
42 46
  * Manages categories that should appear in the preferences dialog.
43 47
  */
@@ -61,8 +65,8 @@ public class PreferencesDialogModel {
61 65
     private final ConfigProvider identity;
62 66
     /** Plugin manager. */
63 67
     private final PluginManager pluginManager;
64
-    /** Action Manager. */
65
-    private final ActionManager actionManager;
68
+    /** Event bus to post events on. */
69
+    private final EventBus eventBus;
66 70
 
67 71
     /**
68 72
      * Creates a new instance of PreferencesDialogModel.
@@ -73,29 +77,30 @@ public class PreferencesDialogModel {
73 77
      * @param urlHandlerPanel UI specific URL panel
74 78
      * @param configManager   Config manager to read settings from
75 79
      * @param identity        Identity to write settings to
76
-     * @param actionManager   Action manager to register and trigger actions with
77 80
      * @param pluginManager   Plugin manager to retrieve plugins from
81
+     * @param eventBus        Event bus to post events on
78 82
      */
83
+    @Inject
79 84
     public PreferencesDialogModel(final PreferencesInterface pluginPanel,
80 85
             final PreferencesInterface themePanel,
81 86
             final PreferencesInterface updatesPanel,
82 87
             final PreferencesInterface urlHandlerPanel,
83 88
             final AggregateConfigProvider configManager,
84 89
             final ConfigProvider identity,
85
-            final ActionManager actionManager,
86
-            final PluginManager pluginManager) {
90
+            final PluginManager pluginManager,
91
+            final EventBus eventBus) {
87 92
         this.pluginPanel = pluginPanel;
88 93
         this.themePanel = themePanel;
89 94
         this.updatesPanel = updatesPanel;
90 95
         this.urlHandlerPanel = urlHandlerPanel;
91 96
         this.configManager = configManager;
92 97
         this.identity = identity;
93
-        this.actionManager = actionManager;
94 98
         this.pluginManager = pluginManager;
99
+        this.eventBus = eventBus;
95 100
 
96 101
         addDefaultCategories();
97 102
 
98
-        actionManager.triggerEvent(CoreActionType.CLIENT_PREFS_OPENED, null, this);
103
+        eventBus.post(new ClientPrefsOpenedEvent(this));
99 104
     }
100 105
 
101 106
     public AggregateConfigProvider getConfigManager() {
@@ -626,7 +631,7 @@ public class PreferencesDialogModel {
626 631
      * @since 0.6
627 632
      */
628 633
     public void close() {
629
-        actionManager.triggerEvent(CoreActionType.CLIENT_PREFS_CLOSED, null);
634
+        eventBus.post(new ClientPrefsClosedEvent());
630 635
     }
631 636
 
632 637
 }

+ 10
- 10
src/com/dmdirc/config/prefs/PreferencesManager.java View File

@@ -22,13 +22,15 @@
22 22
 
23 23
 package com.dmdirc.config.prefs;
24 24
 
25
-import com.dmdirc.actions.CoreActionType;
26
-import com.dmdirc.interfaces.ActionController;
25
+import com.dmdirc.events.ConnectionPrefsRequestedEvent;
26
+import com.dmdirc.events.GroupChatPrefsRequestedEvent;
27 27
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
28 28
 import com.dmdirc.interfaces.config.ConfigProvider;
29 29
 import com.dmdirc.util.validators.NumericalValidator;
30 30
 import com.dmdirc.util.validators.OptionalValidator;
31 31
 
32
+import com.google.common.eventbus.EventBus;
33
+
32 34
 import javax.inject.Inject;
33 35
 
34 36
 /**
@@ -38,12 +40,12 @@ import javax.inject.Inject;
38 40
  */
39 41
 public class PreferencesManager {
40 42
 
41
-    /** The action controller to fire events on. */
42
-    private final ActionController actionController;
43
+    /** Event bus to public events on. */
44
+    private final EventBus eventBus;
43 45
 
44 46
     @Inject
45
-    public PreferencesManager(final ActionController actionController) {
46
-        this.actionController = actionController;
47
+    public PreferencesManager(final EventBus eventBus) {
48
+        this.eventBus = eventBus;
47 49
     }
48 50
 
49 51
     /**
@@ -125,8 +127,7 @@ public class PreferencesManager {
125 127
                 "Reconnect message", "Default quit message to use when reconnecting",
126 128
                 manager, identity));
127 129
 
128
-        actionController.triggerEvent(CoreActionType.CLIENT_PREFS_REQUESTED,
129
-                null, category, Boolean.TRUE);
130
+        eventBus.post(new ConnectionPrefsRequestedEvent(category));
130 131
 
131 132
         return category;
132 133
     }
@@ -239,8 +240,7 @@ public class PreferencesManager {
239 240
                 "Number of items of input history to keep",
240 241
                 manager, identity));
241 242
 
242
-        actionController.triggerEvent(CoreActionType.CLIENT_PREFS_REQUESTED,
243
-                null, category, Boolean.FALSE);
243
+        eventBus.post(new GroupChatPrefsRequestedEvent(category));
244 244
 
245 245
         return category;
246 246
     }

+ 30
- 29
src/com/dmdirc/plugins/PluginManager.java View File

@@ -23,10 +23,9 @@
23 23
 package com.dmdirc.plugins;
24 24
 
25 25
 import com.dmdirc.actions.CoreActionType;
26
-import com.dmdirc.config.prefs.PreferencesDialogModel;
26
+import com.dmdirc.events.ClientPrefsClosedEvent;
27
+import com.dmdirc.events.ClientPrefsOpenedEvent;
27 28
 import com.dmdirc.interfaces.ActionController;
28
-import com.dmdirc.interfaces.ActionListener;
29
-import com.dmdirc.interfaces.actions.ActionType;
30 29
 import com.dmdirc.interfaces.config.IdentityController;
31 30
 import com.dmdirc.logger.ErrorLevel;
32 31
 import com.dmdirc.logger.Logger;
@@ -34,6 +33,9 @@ import com.dmdirc.updater.components.PluginComponent;
34 33
 import com.dmdirc.updater.manager.UpdateManager;
35 34
 import com.dmdirc.util.collections.MapList;
36 35
 
36
+import com.google.common.eventbus.EventBus;
37
+import com.google.common.eventbus.Subscribe;
38
+
37 39
 import java.io.File;
38 40
 import java.net.MalformedURLException;
39 41
 import java.net.URL;
@@ -54,7 +56,7 @@ import dagger.ObjectGraph;
54 56
 /**
55 57
  * Searches for and manages plugins and services.
56 58
  */
57
-public class PluginManager implements ActionListener, ServiceManager {
59
+public class PluginManager implements ServiceManager {
58 60
 
59 61
     /** List of known plugins' file names to their corresponding {@link PluginInfo} objects. */
60 62
     private final Map<String, PluginInfo> knownPlugins = new HashMap<>();
@@ -80,6 +82,7 @@ public class PluginManager implements ActionListener, ServiceManager {
80 82
     /**
81 83
      * Creates a new instance of PluginManager.
82 84
      *
85
+     * @param eventBus            The event bus to subscribe to events on
83 86
      * @param identityController  The identity controller to use for configuration options.
84 87
      * @param actionController    The action controller to use for events.
85 88
      * @param updateManager       The update manager to inform about plugins.
@@ -88,6 +91,7 @@ public class PluginManager implements ActionListener, ServiceManager {
88 91
      * @param directory           The directory to load plugins from.
89 92
      */
90 93
     public PluginManager(
94
+            final EventBus eventBus,
91 95
             final IdentityController identityController,
92 96
             final ActionController actionController,
93 97
             final UpdateManager updateManager,
@@ -102,9 +106,7 @@ public class PluginManager implements ActionListener, ServiceManager {
102 106
         this.globalClassLoader = new GlobalClassLoader(this);
103 107
         this.objectGraph = objectGraph;
104 108
 
105
-        actionController.registerListener(this,
106
-                CoreActionType.CLIENT_PREFS_OPENED,
107
-                CoreActionType.CLIENT_PREFS_CLOSED);
109
+        eventBus.register(this);
108 110
     }
109 111
 
110 112
     /**
@@ -571,32 +573,31 @@ public class PluginManager implements ActionListener, ServiceManager {
571 573
         return new ArrayList<>(knownPlugins.values());
572 574
     }
573 575
 
574
-    /** {@inheritDoc} */
575
-    @Override
576
-    public void processEvent(final ActionType type, final StringBuffer format,
577
-            final Object... arguments) {
578
-        if (type.equals(CoreActionType.CLIENT_PREFS_OPENED)) {
579
-            for (PluginInfo pi : getPluginInfos()) {
580
-                if (!pi.isLoaded() && !pi.isTempLoaded()) {
581
-                    pi.loadPluginTemp();
582
-                }
583
-                if (pi.isLoaded() || pi.isTempLoaded()) {
584
-                    try {
585
-                        pi.getPlugin().showConfig((PreferencesDialogModel) arguments[0]);
586
-                    } catch (LinkageError | Exception le) {
587
-                        Logger.appError(ErrorLevel.MEDIUM,
588
-                                "Unable to show plugin configuration for "
589
-                                + pi.getMetaData().getFriendlyName(), le);
590
-                    }
591
-                }
576
+    @Subscribe
577
+    public void handlePrefsOpened(final ClientPrefsOpenedEvent event) {
578
+        for (PluginInfo pi : getPluginInfos()) {
579
+            if (!pi.isLoaded() && !pi.isTempLoaded()) {
580
+                pi.loadPluginTemp();
592 581
             }
593
-        } else if (type.equals(CoreActionType.CLIENT_PREFS_CLOSED)) {
594
-            for (PluginInfo pi : getPluginInfos()) {
595
-                if (pi.isTempLoaded()) {
596
-                    pi.unloadPlugin();
582
+            if (pi.isLoaded() || pi.isTempLoaded()) {
583
+                try {
584
+                    pi.getPlugin().showConfig(event.getModel());
585
+                } catch (LinkageError | Exception le) {
586
+                    Logger.appError(ErrorLevel.MEDIUM,
587
+                            "Unable to show plugin configuration for "
588
+                            + pi.getMetaData().getFriendlyName(), le);
597 589
                 }
598 590
             }
599 591
         }
600 592
     }
601 593
 
594
+    @Subscribe
595
+    public void handlePrefsClosed(final ClientPrefsClosedEvent event) {
596
+        for (PluginInfo pi : getPluginInfos()) {
597
+            if (pi.isTempLoaded()) {
598
+                pi.unloadPlugin();
599
+            }
600
+        }
601
+    }
602
+
602 603
 }

+ 32
- 23
test/com/dmdirc/config/prefs/PreferencesDialogModelTest.java View File

@@ -22,32 +22,41 @@
22 22
 
23 23
 package com.dmdirc.config.prefs;
24 24
 
25
-import com.dmdirc.actions.ActionManager;
26
-import com.dmdirc.actions.CoreActionType;
25
+import com.dmdirc.events.ClientPrefsClosedEvent;
26
+import com.dmdirc.events.ClientPrefsOpenedEvent;
27 27
 import com.dmdirc.interfaces.ActionListener;
28 28
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
29 29
 import com.dmdirc.plugins.PluginManager;
30 30
 import com.dmdirc.plugins.Service;
31 31
 
32
+import com.google.common.eventbus.EventBus;
33
+
32 34
 import java.util.ArrayList;
33 35
 import java.util.List;
34 36
 
35 37
 import org.junit.Before;
36 38
 import org.junit.Test;
37
-
38
-import static org.junit.Assert.*;
39
-import static org.mockito.Mockito.*;
40
-
39
+import org.junit.runner.RunWith;
40
+import org.mockito.Mock;
41
+import org.mockito.runners.MockitoJUnitRunner;
42
+
43
+import static org.junit.Assert.assertFalse;
44
+import static org.junit.Assert.assertNotNull;
45
+import static org.junit.Assert.assertNull;
46
+import static org.junit.Assert.assertTrue;
47
+import static org.mockito.Matchers.isA;
48
+import static org.mockito.Mockito.mock;
49
+import static org.mockito.Mockito.verify;
50
+import static org.mockito.Mockito.when;
51
+
52
+@RunWith(MockitoJUnitRunner.class)
41 53
 public class PreferencesDialogModelTest {
42 54
 
43
-    private ActionManager actionManager;
44
-    private PluginManager pluginManager;
55
+    @Mock private EventBus eventBus;
56
+    @Mock private PluginManager pluginManager;
45 57
 
46 58
     @Before
47 59
     public void setup() {
48
-        actionManager = mock(ActionManager.class);
49
-        pluginManager = mock(PluginManager.class);
50
-
51 60
         final List<Service> services = new ArrayList<>();
52 61
         final Service tabcompleter = mock(Service.class);
53 62
         when(tabcompleter.getName()).thenReturn("tabber");
@@ -61,7 +70,7 @@ public class PreferencesDialogModelTest {
61 70
         AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
62 71
         when(cm.getOption("domain", "option")).thenReturn("fallback");
63 72
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
64
-                null, null, cm, null, actionManager, pluginManager);
73
+                null, null, cm, null, pluginManager, eventBus);
65 74
         assertNotNull(pm.getCategory("General"));
66 75
         assertNotNull(pm.getCategory("Connection"));
67 76
         assertNotNull(pm.getCategory("Messages"));
@@ -78,7 +87,7 @@ public class PreferencesDialogModelTest {
78 87
         when(cm.getOption("domain", "option")).thenReturn("fallback");
79 88
         final PreferencesCategory category = mock(PreferencesCategory.class);
80 89
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
81
-                null, null, cm, null, actionManager, pluginManager);
90
+                null, null, cm, null, pluginManager, eventBus);
82 91
         pm.addCategory(category);
83 92
         pm.dismiss();
84 93
 
@@ -93,7 +102,7 @@ public class PreferencesDialogModelTest {
93 102
         when(cm.getOption("domain", "option")).thenReturn("fallback");
94 103
 
95 104
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
96
-                null, null, cm, null, actionManager, pluginManager);
105
+                null, null, cm, null, pluginManager, eventBus);
97 106
         pm.addCategory(category);
98 107
         assertFalse(pm.save());
99 108
 
@@ -108,7 +117,7 @@ public class PreferencesDialogModelTest {
108 117
         when(cm.getOption("domain", "option")).thenReturn("fallback");
109 118
 
110 119
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
111
-                null, null, cm, null, actionManager, pluginManager);
120
+                null, null, cm, null, pluginManager, eventBus);
112 121
         pm.addCategory(category);
113 122
         assertTrue(pm.save());
114 123
 
@@ -121,7 +130,7 @@ public class PreferencesDialogModelTest {
121 130
         when(cm.getOption("domain", "option")).thenReturn("fallback");
122 131
 
123 132
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
124
-                null, null, cm, null, actionManager, pluginManager);
133
+                null, null, cm, null, pluginManager, eventBus);
125 134
         assertNull(pm.getCategory("unittest123"));
126 135
     }
127 136
 
@@ -131,7 +140,7 @@ public class PreferencesDialogModelTest {
131 140
         when(cm.getOption("domain", "option")).thenReturn("fallback");
132 141
 
133 142
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
134
-                null, null, cm, null, actionManager, pluginManager);
143
+                null, null, cm, null, pluginManager, eventBus);
135 144
         assertNotNull(pm.getCategories());
136 145
         assertFalse(pm.getCategories().isEmpty());
137 146
 
@@ -146,7 +155,7 @@ public class PreferencesDialogModelTest {
146 155
         when(cm.getOption("domain", "option")).thenReturn("fallback");
147 156
 
148 157
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
149
-                null, null, cm, null, actionManager, pluginManager);
158
+                null, null, cm, null, pluginManager, eventBus);
150 159
         final PreferencesInterface tpi = mock(PreferencesInterface.class);
151 160
 
152 161
         pm.registerSaveListener(tpi);
@@ -160,9 +169,9 @@ public class PreferencesDialogModelTest {
160 169
         when(cm.getOption("domain", "option")).thenReturn("fallback");
161 170
 
162 171
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
163
-                null, null, cm, null, actionManager, pluginManager);
172
+                null, null, cm, null, pluginManager, eventBus);
164 173
 
165
-        verify(actionManager).triggerEvent(CoreActionType.CLIENT_PREFS_OPENED, null, pm);
174
+        verify(eventBus).post(isA(ClientPrefsOpenedEvent.class));
166 175
     }
167 176
 
168 177
     @Test
@@ -171,10 +180,10 @@ public class PreferencesDialogModelTest {
171 180
         AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
172 181
         when(cm.getOption("domain", "option")).thenReturn("fallback");
173 182
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
174
-                null, null, cm, null, actionManager, pluginManager);
183
+                null, null, cm, null, pluginManager, eventBus);
175 184
         pm.close();
176 185
 
177
-        verify(actionManager).triggerEvent(CoreActionType.CLIENT_PREFS_CLOSED, null);
186
+        verify(eventBus).post(isA(ClientPrefsClosedEvent.class));
178 187
     }
179 188
 
180 189
     @Test
@@ -183,7 +192,7 @@ public class PreferencesDialogModelTest {
183 192
         when(cm.getOption("domain", "option")).thenReturn("fallback");
184 193
 
185 194
         final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
186
-                null, null, cm, null, actionManager, pluginManager);
195
+                null, null, cm, null, pluginManager, eventBus);
187 196
         final PreferencesCategory category = mock(PreferencesCategory.class);
188 197
         final PreferencesInterface tpi = mock(PreferencesInterface.class);
189 198
         when(category.hasObject()).thenReturn(true);

+ 17
- 27
test/com/dmdirc/config/prefs/PreferencesManagerTest.java View File

@@ -22,48 +22,38 @@
22 22
 
23 23
 package com.dmdirc.config.prefs;
24 24
 
25
-import com.dmdirc.actions.CoreActionType;
26
-import com.dmdirc.interfaces.ActionController;
25
+import com.dmdirc.events.ConnectionPrefsRequestedEvent;
26
+import com.dmdirc.events.GroupChatPrefsRequestedEvent;
27 27
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
28 28
 
29
-import org.junit.Before;
29
+import com.google.common.eventbus.EventBus;
30
+
30 31
 import org.junit.Test;
32
+import org.junit.runner.RunWith;
33
+import org.mockito.Mock;
34
+import org.mockito.runners.MockitoJUnitRunner;
31 35
 
32
-import static org.mockito.Mockito.*;
36
+import static org.mockito.Matchers.isA;
37
+import static org.mockito.Mockito.verify;
33 38
 
39
+@RunWith(MockitoJUnitRunner.class)
34 40
 public class PreferencesManagerTest {
35 41
 
36
-    private ActionController actionController;
37
-    private AggregateConfigProvider configManager;
38
-    private PreferencesManager manager;
39
-
40
-    @Before
41
-    public void setup() {
42
-        this.actionController = mock(ActionController.class);
43
-        this.configManager = mock(AggregateConfigProvider.class);
44
-        this.manager = new PreferencesManager(actionController);
45
-    }
42
+    @Mock private EventBus eventBus;
43
+    @Mock private AggregateConfigProvider configManager;
46 44
 
47 45
     @Test
48 46
     public void testGettingChannelPrefsRaisesAction() {
49
-        this.manager.getChannelSettings(this.configManager, null);
50
-
51
-        verify(this.actionController).triggerEvent(
52
-                eq(CoreActionType.CLIENT_PREFS_REQUESTED),
53
-                (StringBuffer) isNull(),
54
-                any(PreferencesCategory.class),
55
-                eq(Boolean.FALSE));
47
+        new PreferencesManager(eventBus).getChannelSettings(this.configManager, null);
48
+        verify(eventBus).post(isA(GroupChatPrefsRequestedEvent.class));
56 49
     }
57 50
 
58 51
     @Test
59 52
     public void testGettingServerPrefsRaisesAction() {
60
-        this.manager.getServerSettings(this.configManager, null);
53
+        new PreferencesManager(eventBus).getServerSettings(this.configManager, null);
61 54
 
62
-        verify(this.actionController).triggerEvent(
63
-                eq(CoreActionType.CLIENT_PREFS_REQUESTED),
64
-                (StringBuffer) isNull(),
65
-                any(PreferencesCategory.class),
66
-                eq(Boolean.TRUE));
55
+        verify(eventBus).post(isA(GroupChatPrefsRequestedEvent.class));
56
+        verify(eventBus).post(isA(ConnectionPrefsRequestedEvent.class));
67 57
     }
68 58
 
69 59
 }

Loading…
Cancel
Save