Bläddra i källkod

Remove Action events.

Change-Id: I67b558de082c1ce196800d8b0aafa2d6ce87b298
Reviewed-on: http://gerrit.dmdirc.com/3455
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
pull/1/head
Greg Holmes 10 år sedan
förälder
incheckning
ac3c86a653

+ 18
- 6
src/com/dmdirc/actions/Action.java Visa fil

@@ -26,6 +26,9 @@ import com.dmdirc.GlobalWindow;
26 26
 import com.dmdirc.commandparser.parsers.GlobalCommandParser;
27 27
 import com.dmdirc.config.prefs.PreferencesSetting;
28 28
 import com.dmdirc.config.prefs.PreferencesType;
29
+import com.dmdirc.events.ActionCreatedEvent;
30
+import com.dmdirc.events.ActionDeletedEvent;
31
+import com.dmdirc.events.ActionUpdatedEvent;
29 32
 import com.dmdirc.interfaces.ActionController;
30 33
 import com.dmdirc.interfaces.actions.ActionComparison;
31 34
 import com.dmdirc.interfaces.actions.ActionComponent;
@@ -38,6 +41,8 @@ import com.dmdirc.updater.Version;
38 41
 import com.dmdirc.util.io.ConfigFile;
39 42
 import com.dmdirc.util.io.InvalidConfigFileException;
40 43
 
44
+import com.google.common.eventbus.EventBus;
45
+
41 46
 import java.io.File;
42 47
 import java.io.IOException;
43 48
 import java.util.ArrayList;
@@ -73,6 +78,8 @@ public class Action extends ActionModel implements ConfigChangeListener {
73 78
     private final IdentityController identityController;
74 79
     /** The controller to use to retrieve components, comparisons, etc. */
75 80
     private final ActionController actionController;
81
+    /** Event bus to post events to. */
82
+    private final EventBus eventBus;
76 83
     /** The config file we're using. */
77 84
     protected ConfigFile config;
78 85
     /** The location of the file we're reading/saving. */
@@ -82,6 +89,7 @@ public class Action extends ActionModel implements ConfigChangeListener {
82 89
      * Creates a new instance of Action. The group and name specified must be the group and name of
83 90
      * a valid action already saved to disk.
84 91
      *
92
+     * @param eventBus                    Event bus to post events to
85 93
      * @param globalCommandParserProvider Provider of global command parsers for triggering actions.
86 94
      * @param globalWindowProvider        Provider of global windows for triggering actions.
87 95
      * @param substitutorFactory          Factory to use to create action substitutors.
@@ -92,6 +100,7 @@ public class Action extends ActionModel implements ConfigChangeListener {
92 100
      * @param name                        The name of the action
93 101
      */
94 102
     public Action(
103
+            final EventBus eventBus,
95 104
             final Provider<GlobalCommandParser> globalCommandParserProvider,
96 105
             final Provider<GlobalWindow> globalWindowProvider,
97 106
             final ActionSubstitutorFactory substitutorFactory,
@@ -102,6 +111,7 @@ public class Action extends ActionModel implements ConfigChangeListener {
102 111
             final String name) {
103 112
         super(globalCommandParserProvider, globalWindowProvider, substitutorFactory, group, name);
104 113
 
114
+        this.eventBus = eventBus;
105 115
         this.actionController = actionController;
106 116
         this.identityController = identityController;
107 117
         this.actionsDirectory = actionsDirectory;
@@ -126,6 +136,7 @@ public class Action extends ActionModel implements ConfigChangeListener {
126 136
     /**
127 137
      * Creates a new instance of Action with the specified properties and saves it to disk.
128 138
      *
139
+     * @param eventBus                    Event bus to post events to
129 140
      * @param globalCommandParserProvider Provider of global command parsers for triggering actions.
130 141
      * @param globalWindowProvider        Provider of global windows for triggering actions.
131 142
      * @param substitutorFactory          Factory to use to create action substitutors.
@@ -141,6 +152,7 @@ public class Action extends ActionModel implements ConfigChangeListener {
141 152
      * @param newFormat                   The new formatter to use
142 153
      */
143 154
     public Action(
155
+            final EventBus eventBus,
144 156
             final Provider<GlobalCommandParser> globalCommandParserProvider,
145 157
             final Provider<GlobalWindow> globalWindowProvider,
146 158
             final ActionSubstitutorFactory substitutorFactory,
@@ -157,6 +169,7 @@ public class Action extends ActionModel implements ConfigChangeListener {
157 169
         super(globalCommandParserProvider, globalWindowProvider, substitutorFactory, group, name,
158 170
                 triggers, response, conditions, conditionTree, newFormat);
159 171
 
172
+        this.eventBus = eventBus;
160 173
         this.actionController = actionController;
161 174
         this.identityController = identityController;
162 175
         this.actionsDirectory = actionsDirectory;
@@ -165,8 +178,6 @@ public class Action extends ActionModel implements ConfigChangeListener {
165 178
 
166 179
         new File(actionsDirectory + group).mkdirs();
167 180
 
168
-        actionController.triggerEvent(CoreActionType.ACTION_CREATED, null, this);
169
-
170 181
         save();
171 182
 
172 183
         identityController.getGlobalConfiguration().addChangeListener("disable_action",
@@ -174,6 +185,8 @@ public class Action extends ActionModel implements ConfigChangeListener {
174 185
         checkDisabled();
175 186
 
176 187
         actionController.addAction(this);
188
+        //TODO This needs to be done somewhere else, remove eventbus when it is.
189
+        eventBus.post(new ActionCreatedEvent(this));
177 190
     }
178 191
 
179 192
     /**
@@ -406,7 +419,7 @@ public class Action extends ActionModel implements ConfigChangeListener {
406 419
                     + group + "/" + name + ": " + ex.getMessage());
407 420
         }
408 421
 
409
-        actionController.triggerEvent(CoreActionType.ACTION_UPDATED, null, this);
422
+        eventBus.post(new ActionUpdatedEvent(this));
410 423
     }
411 424
 
412 425
     /**
@@ -565,7 +578,7 @@ public class Action extends ActionModel implements ConfigChangeListener {
565 578
      * Deletes this action.
566 579
      */
567 580
     public void delete() {
568
-        actionController.triggerEvent(CoreActionType.ACTION_DELETED, null, getGroup(), getName());
581
+        eventBus.post(new ActionDeletedEvent(actionController.getOrCreateGroup(getGroup()), this));
569 582
         new File(location).delete();
570 583
     }
571 584
 
@@ -573,8 +586,7 @@ public class Action extends ActionModel implements ConfigChangeListener {
573 586
     public String toString() {
574 587
         final String parent = super.toString();
575 588
 
576
-        return parent.substring(0, parent.length() - 1)
577
-                + ",location=" + location + "]";
589
+        return parent.substring(0, parent.length() - 1) + ",location=" + location + "]";
578 590
     }
579 591
 
580 592
     @Override

+ 9
- 0
src/com/dmdirc/actions/ActionFactory.java Visa fil

@@ -30,6 +30,8 @@ import com.dmdirc.interfaces.ActionController;
30 30
 import com.dmdirc.interfaces.actions.ActionType;
31 31
 import com.dmdirc.interfaces.config.IdentityController;
32 32
 
33
+import com.google.common.eventbus.EventBus;
34
+
33 35
 import java.util.List;
34 36
 
35 37
 import javax.inject.Inject;
@@ -54,10 +56,13 @@ public class ActionFactory {
54 56
     private final ActionSubstitutorFactory substitutorFactory;
55 57
     /** The base directory to store actions in. */
56 58
     private final String actionsDirectory;
59
+    /** Event bus to post events on. */
60
+    private final EventBus eventBus;
57 61
 
58 62
     /**
59 63
      * Creates a new instance of {@link ActionFactory}.
60 64
      *
65
+     * @param eventBus                    The event bus to post events on.
61 66
      * @param actionController            The controller that will own actions.
62 67
      * @param identityController          The controller to use to retrieve and update settings.
63 68
      * @param globalCommandParserProvider The global command parser to use for actions without
@@ -68,12 +73,14 @@ public class ActionFactory {
68 73
      */
69 74
     @Inject
70 75
     public ActionFactory(
76
+            final EventBus eventBus,
71 77
             final Provider<ActionController> actionController,
72 78
             final Provider<IdentityController> identityController,
73 79
             final Provider<GlobalCommandParser> globalCommandParserProvider,
74 80
             final Provider<GlobalWindow> globalWindowProvider,
75 81
             final ActionSubstitutorFactory substitutorFactory,
76 82
             @Directory(DirectoryType.ACTIONS) final String actionsDirectory) {
83
+        this.eventBus = eventBus;
77 84
         this.actionController = actionController;
78 85
         this.identityController = identityController;
79 86
         this.globalCommandParserProvider = globalCommandParserProvider;
@@ -93,6 +100,7 @@ public class ActionFactory {
93 100
      */
94 101
     public Action getAction(final String group, final String name) {
95 102
         return new Action(
103
+                eventBus,
96 104
                 globalCommandParserProvider,
97 105
                 globalWindowProvider,
98 106
                 substitutorFactory,
@@ -121,6 +129,7 @@ public class ActionFactory {
121 129
             final List<ActionCondition> conditions,
122 130
             final ConditionTree conditionTree, final String newFormat) {
123 131
         return new Action(
132
+                eventBus,
124 133
                 globalCommandParserProvider,
125 134
                 globalWindowProvider,
126 135
                 substitutorFactory,

+ 3
- 0
src/com/dmdirc/actions/CoreActionType.java Visa fil

@@ -229,10 +229,13 @@ public enum CoreActionType implements ActionType {
229 229
     @Deprecated
230 230
     PLUGIN_REFRESH(PluginEvents.PLUGIN_EVENT, "Plugins refreshed"),
231 231
     /** Action created. */
232
+    @Deprecated
232 233
     ACTION_CREATED(ActionEvents.ACTION_EVENT, "Action created"),
233 234
     /** Action updated. */
235
+    @Deprecated
234 236
     ACTION_UPDATED(ActionEvents.ACTION_EVENT, "Action updated"),
235 237
     /** Action deleted. */
238
+    @Deprecated
236 239
     ACTION_DELETED(ActionEvents.ACTION_DELETED, "Action deleted"),
237 240
     /** Channel clicked. */
238 241
     @Deprecated

+ 2
- 1
src/com/dmdirc/actions/metatypes/ActionEvents.java Visa fil

@@ -23,6 +23,7 @@
23 23
 package com.dmdirc.actions.metatypes;
24 24
 
25 25
 import com.dmdirc.actions.Action;
26
+import com.dmdirc.actions.ActionGroup;
26 27
 import com.dmdirc.interfaces.actions.ActionMetaType;
27 28
 
28 29
 /**
@@ -33,7 +34,7 @@ public enum ActionEvents implements ActionMetaType {
33 34
     /** Action event type. */
34 35
     ACTION_EVENT(new String[]{"action"}, Action.class),
35 36
     /** Action event type. */
36
-    ACTION_DELETED(new String[]{"Action group", "Action name"}, Action.class, String.class);
37
+    ACTION_DELETED(new String[]{"Action group", "Action name"}, ActionGroup.class, String.class);
37 38
     /** The names of the arguments for this meta type. */
38 39
     private String[] argNames;
39 40
     /** The classes of the arguments for this meta type. */

+ 36
- 0
src/com/dmdirc/events/ActionCreatedEvent.java Visa fil

@@ -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.actions.Action;
26
+
27
+/**
28
+ * Fired on the creation of an action.
29
+ */
30
+public class ActionCreatedEvent extends ActionEvent {
31
+
32
+    public ActionCreatedEvent(final Action action) {
33
+        super(action);
34
+    }
35
+
36
+}

+ 47
- 0
src/com/dmdirc/events/ActionDeletedEvent.java Visa fil

@@ -0,0 +1,47 @@
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.actions.Action;
26
+import com.dmdirc.actions.ActionGroup;
27
+
28
+/**
29
+ * Fired when an action is deleted.
30
+ */
31
+public class ActionDeletedEvent extends ActionEvent {
32
+
33
+    private final ActionGroup group;
34
+
35
+    public ActionDeletedEvent(final ActionGroup group, final Action action) {
36
+        super(action);
37
+        this.group = group;
38
+    }
39
+
40
+    public ActionGroup getGroup() {
41
+        return group;
42
+    }
43
+
44
+    public String getName() {
45
+        return getAction().getName();
46
+    }
47
+}

+ 12
- 0
src/com/dmdirc/events/ActionEvent.java Visa fil

@@ -22,9 +22,21 @@
22 22
 
23 23
 package com.dmdirc.events;
24 24
 
25
+import com.dmdirc.actions.Action;
26
+
25 27
 /**
26 28
  * Base class for all action events.
27 29
  */
28 30
 public abstract class ActionEvent extends DMDircEvent {
29 31
 
32
+    private final Action action;
33
+
34
+    public ActionEvent(final Action action) {
35
+        this.action = action;
36
+    }
37
+
38
+    public Action getAction() {
39
+        return action;
40
+    }
41
+
30 42
 }

+ 36
- 0
src/com/dmdirc/events/ActionUpdatedEvent.java Visa fil

@@ -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.actions.Action;
26
+
27
+/**
28
+ * Fired when an action is updated.
29
+ */
30
+public class ActionUpdatedEvent extends ActionEvent {
31
+
32
+    public ActionUpdatedEvent(final Action action) {
33
+        super(action);
34
+    }
35
+
36
+}

+ 21
- 21
test/com/dmdirc/actions/ActionTest.java Visa fil

@@ -36,6 +36,8 @@ import com.dmdirc.logger.Logger;
36 36
 import com.dmdirc.util.io.ConfigFile;
37 37
 import com.dmdirc.util.io.InvalidConfigFileException;
38 38
 
39
+import com.google.common.eventbus.EventBus;
40
+
39 41
 import java.io.File;
40 42
 import java.io.IOException;
41 43
 import java.util.ArrayList;
@@ -68,6 +70,7 @@ public class ActionTest {
68 70
 
69 71
     @Rule public final TemporaryFolder folder = new TemporaryFolder();
70 72
 
73
+    @Mock private EventBus eventBus;
71 74
     @Mock private Provider<GlobalCommandParser> gcpProvider;
72 75
     @Mock private Provider<GlobalWindow> gwProvider;
73 76
     @Mock private ActionSubstitutorFactory substitutorFactory;
@@ -105,7 +108,7 @@ public class ActionTest {
105 108
 
106 109
     @Test
107 110
     public void testSave() {
108
-        new Action(gcpProvider, gwProvider, substitutorFactory, actionController,
111
+        new Action(eventBus, gcpProvider, gwProvider, substitutorFactory, actionController,
109 112
                 identityController, getTempDirectory(), "unit-test", "test1", new ActionType[0],
110 113
                 new String[0], new ArrayList<ActionCondition>(),
111 114
                 ConditionTree.createConjunction(0), null);
@@ -116,7 +119,8 @@ public class ActionTest {
116 119
 
117 120
     @Test
118 121
     public void testSetGroup() {
119
-        Action action = new Action(gcpProvider, gwProvider, substitutorFactory, actionController,
122
+        Action action = new Action(eventBus, gcpProvider, gwProvider, substitutorFactory,
123
+                actionController,
120 124
                 identityController, getTempDirectory(),
121 125
                 "unit-test", "test1", new ActionType[0],
122 126
                 new String[0], new ArrayList<ActionCondition>(),
@@ -124,33 +128,29 @@ public class ActionTest {
124 128
         action.setGroup("unit-test-two");
125 129
 
126 130
         assertFalse("setGroup must remove old file",
127
-                new File(getTempDirectory(), "unit-test" + File.separator + "test1").
128
-                isFile());
131
+                new File(getTempDirectory(), "unit-test" + File.separator + "test1").isFile());
129 132
         assertTrue("setGroup must create new file",
130
-                new File(getTempDirectory(), "unit-test-two" + File.separator + "test1").
131
-                isFile());
133
+                new File(getTempDirectory(), "unit-test-two" + File.separator + "test1").isFile());
132 134
     }
133 135
 
134 136
     @Test
135 137
     public void testSetName() {
136
-        Action action = new Action(gcpProvider, gwProvider, substitutorFactory, actionController,
137
-                identityController, getTempDirectory(),
138
-                "unit-test", "test1", new ActionType[0],
139
-                new String[0], new ArrayList<ActionCondition>(),
138
+        Action action = new Action(eventBus, gcpProvider, gwProvider, substitutorFactory,
139
+                actionController, identityController, getTempDirectory(), "unit-test",
140
+                "test1", new ActionType[0], new String[0], new ArrayList<ActionCondition>(),
140 141
                 ConditionTree.createConjunction(0), null);
141 142
         action.setName("test2");
142 143
 
143 144
         assertFalse("setName must remove old file",
144
-                new File(getTempDirectory(), "unit-test" + File.separator + "test1").
145
-                isFile());
145
+                new File(getTempDirectory(), "unit-test" + File.separator + "test1").isFile());
146 146
         assertTrue("setName must create new file",
147
-                new File(getTempDirectory(), "unit-test" + File.separator + "test2").
148
-                isFile());
147
+                new File(getTempDirectory(), "unit-test" + File.separator + "test2").isFile());
149 148
     }
150 149
 
151 150
     @Test
152 151
     public void testDelete() {
153
-        Action action = new Action(gcpProvider, gwProvider, substitutorFactory, actionController,
152
+        Action action = new Action(eventBus, gcpProvider, gwProvider, substitutorFactory,
153
+                actionController,
154 154
                 identityController, getTempDirectory(),
155 155
                 "unit-test", "test1", new ActionType[0],
156 156
                 new String[0], new ArrayList<ActionCondition>(),
@@ -164,9 +164,9 @@ public class ActionTest {
164 164
 
165 165
     @Test
166 166
     public void testRead() throws IOException, InvalidConfigFileException {
167
-        Action action = new Action(gcpProvider, gwProvider, substitutorFactory, actionController,
168
-                identityController, getTempDirectory(),
169
-                "unit-test", "doesn't_exist");
167
+        Action action = new Action(eventBus, gcpProvider, gwProvider, substitutorFactory,
168
+                actionController, identityController, getTempDirectory(), "unit-test",
169
+                "doesn't_exist");
170 170
         action.config = new ConfigFile(getClass().getResourceAsStream("action1"));
171 171
         action.config.read();
172 172
         action.loadActionFromConfig();
@@ -183,9 +183,9 @@ public class ActionTest {
183 183
 
184 184
     @Test
185 185
     public void testMultipleGroups() throws IOException, InvalidConfigFileException {
186
-        Action action = new Action(gcpProvider, gwProvider, substitutorFactory, actionController,
187
-                identityController, getTempDirectory(),
188
-                "unit-test", "doesn't_exist");
186
+        Action action = new Action(eventBus, gcpProvider, gwProvider, substitutorFactory,
187
+                actionController, identityController, getTempDirectory(), "unit-test",
188
+                "doesn't_exist");
189 189
         action.config = new ConfigFile(getClass().getResourceAsStream("action_multisettings"));
190 190
         action.config.read();
191 191
         action.loadActionFromConfig();

Laddar…
Avbryt
Spara