Browse Source

Abstracted action model

git-svn-id: http://svn.dmdirc.com/trunk@2477 00569f92-eb28-0410-84fd-f71c24880f
tags/0.5.5
Chris Smith 16 years ago
parent
commit
aeec3b0687

+ 24
- 186
src/com/dmdirc/actions/Action.java View File

@@ -22,22 +22,14 @@
22 22
 
23 23
 package com.dmdirc.actions;
24 24
 
25
-import com.dmdirc.Main;
26
-import com.dmdirc.ServerManager;
27
-import com.dmdirc.WritableFrameContainer;
28
-import com.dmdirc.commandparser.parsers.CommandParser;
29
-import com.dmdirc.commandparser.parsers.GlobalCommandParser;
30 25
 import com.dmdirc.logger.ErrorLevel;
31 26
 import com.dmdirc.logger.Logger;
32
-import com.dmdirc.ui.interfaces.InputWindow;
33
-import com.dmdirc.ui.interfaces.Window;
34 27
 
35 28
 import java.io.File;
36 29
 import java.io.FileInputStream;
37 30
 import java.io.FileOutputStream;
38 31
 import java.io.IOException;
39 32
 import java.io.Serializable;
40
-import java.util.ArrayList;
41 33
 import java.util.Arrays;
42 34
 import java.util.List;
43 35
 import java.util.Properties;
@@ -47,7 +39,7 @@ import java.util.Properties;
47 39
  *
48 40
  * @author chris
49 41
  */
50
-public class Action implements Serializable {
42
+public class Action extends ActionModel implements Serializable {
51 43
     
52 44
     /**
53 45
      * A version number for this class. It should be changed whenever the class
@@ -55,31 +47,13 @@ public class Action implements Serializable {
55 47
      * objects being unserialized with the new class).
56 48
      */
57 49
     private static final long serialVersionUID = 1;
58
-    
59
-    /** The group this action belongs to. */
60
-    private String group;
61
-    
62
-    /** The name of this action. */
63
-    private String name;
64
-    
50
+       
65 51
     /** The file containing this action. */
66 52
     private File file;
67 53
     
68 54
     /** The properties read for this action. */
69 55
     private Properties properties;
70
-    
71
-    /** The ActionTypes that trigger this action. */
72
-    private ActionType[] triggers;
73
-    
74
-    /** The commands to execute if this action is triggered. */
75
-    private String[] response;
76
-    
77
-    /** The change that should be made to the format string, if any. */
78
-    private String newFormat = null;
79
-    
80
-    /** The conditions for this action. */
81
-    private List<ActionCondition> conditions = new ArrayList<ActionCondition>();
82
-    
56
+        
83 57
     /**
84 58
      * Creates a new instance of Action. The group and name specified must
85 59
      * be the group and name of a valid action already saved to disk.
@@ -88,8 +62,7 @@ public class Action implements Serializable {
88 62
      * @param name The name of the action
89 63
      */
90 64
     public Action(final String group, final String name) {
91
-        this.group = group;
92
-        this.name = name;
65
+        super(group, name);
93 66
         
94 67
         final String fs = System.getProperty("file.separator");
95 68
         final String location = ActionManager.getDirectory() + group + fs + name;
@@ -124,12 +97,7 @@ public class Action implements Serializable {
124 97
     public Action(final String group, final String name,
125 98
             final ActionType[] triggers, final String[] response,
126 99
             final List<ActionCondition> conditions, final String newFormat) {
127
-        this.group = group;
128
-        this.name = name;
129
-        this.triggers = triggers.clone();
130
-        this.response = response.clone();
131
-        this.conditions = conditions;
132
-        this.newFormat = newFormat;
100
+        super(group, name, triggers, response, conditions, newFormat);
133 101
         
134 102
         final String fs = System.getProperty("file.separator");
135 103
         final String dir = ActionManager.getDirectory() + group + fs;
@@ -364,23 +332,29 @@ public class Action implements Serializable {
364 332
      * Renames this action to the specified new name.
365 333
      *
366 334
      * @param newName The new name for this action
335
+     * @deprecated Use setName instead
367 336
      */
337
+    @Deprecated
368 338
     public void rename(final String newName) {
339
+        setName(newName);
340
+    }
341
+    
342
+    /** {@inheritDoc} */
343
+    @Override
344
+    public void setName(final String newName) {
345
+        super.setName(newName);
346
+        
369 347
         file.delete();
370 348
         
371 349
         file = new File(file.getParent() + System.getProperty("file.separator") + newName);
372
-        name = newName;
373 350
         
374 351
         save();
375
-    }
352
+    }    
376 353
     
377
-    /**
378
-     * Sets the group of this action.
379
-     *
380
-     * @param newGroup The new group for this action
381
-     */
354
+    /** {@inheritDoc} */
355
+    @Override
382 356
     public void setGroup(final String newGroup) {
383
-        group = newGroup;
357
+        super.setGroup(newGroup);
384 358
         
385 359
         final String fs = System.getProperty("file.separator");
386 360
         final String location = ActionManager.getDirectory() + group + fs + name;
@@ -398,149 +372,13 @@ public class Action implements Serializable {
398 372
         file.delete();
399 373
     }
400 374
     
401
-    /**
402
-     * Retrieves a list of this action's conditions.
403
-     *
404
-     * @return A list of this action's conditions
405
-     */
406
-    public List<ActionCondition> getConditions() {
407
-        return conditions;
408
-    }
409
-    
410
-    /**
411
-     * Sets this action's conditions.
412
-     *
413
-     * @param conditions A list of conditions to use
414
-     */
415
-    public void setConditions(final List<ActionCondition> conditions) {
416
-        this.conditions = conditions;
417
-    }
418
-    
419
-    /**
420
-     * Retrieves this action's triggers.
421
-     *
422
-     * @return The triggers used by this action
423
-     */
424
-    public ActionType[] getTriggers() {
425
-        return triggers.clone();
426
-    }
427
-    
428
-    /**
429
-     * Sets this action's triggers.
430
-     *
431
-     * @param triggers The new triggers to use
432
-     */
433
-    public void setTriggers(final ActionType[] triggers) {
434
-        this.triggers = triggers.clone();
435
-        
436
-        ActionManager.unregisterAction(this);
437
-        ActionManager.registerAction(this);
438
-    }
439
-    
440
-    /**
441
-     * Retrieves this action's new format setting.
442
-     *
443
-     * @return The format that this action will use, or null if no change
444
-     */
445
-    public String getNewFormat() {
446
-        return newFormat;
447
-    }
448
-    
449
-    /**
450
-     * Sets this action's new format setting.
451
-     *
452
-     * @param newFormat The new 'new format' setting
453
-     */
454
-    public void setNewFormat(final String newFormat) {
455
-        this.newFormat = newFormat;
456
-    }
457
-    
458
-    /**
459
-     * Retrieves this action's response.
460
-     *
461
-     * @return The commands that will be executed if this action is triggered
462
-     */
463
-    public String[] getResponse() {
464
-        return response.clone();
465
-    }
466
-    
467
-    /**
468
-     * Sets this action's response.
469
-     *
470
-     * @param response The new response to use
471
-     */
472
-    public void setResponse(final String[] response) {
473
-        this.response = response.clone();
474
-    }
475
-    
476
-    /**
477
-     * Retrieves this action's group name.
478
-     *
479
-     * @return This action's group name
480
-     */
481
-    public String getGroup() {
482
-        return group;
483
-    }
484
-    
485
-    /**
486
-     * Retrieves this action's name.
487
-     *
488
-     * @return This action's name
489
-     */
490
-    public String getName() {
491
-        return name;
492
-    }
493
-    
494
-    /**
495
-     * Triggers this action.
496
-     *
497
-     * @param format The format of the message that's going to be displayed.
498
-     * @param arguments The arguments from the action that caused this trigger.
499
-     */
500
-    public void trigger(final StringBuffer format, final Object ... arguments) {
501
-        final ActionSubstitutor sub = new ActionSubstitutor(triggers[0]);
502
-        
503
-        for (ActionCondition condition : conditions) {
504
-            if (!condition.test(sub, arguments)) {
505
-                return;
506
-            }
507
-        }
508
-        
509
-        final Window active = Main.getUI().getMainWindow().getActiveFrame();
510
-        InputWindow cw;
511
-        CommandParser cp = null;
512
-        
513
-        if (arguments.length > 0 && arguments[0] instanceof WritableFrameContainer) {
514
-            cw = ((WritableFrameContainer) arguments[0]).getFrame();
515
-        } else if (active instanceof InputWindow) {
516
-            cw = (InputWindow) Main.getUI().getMainWindow().getActiveFrame();
517
-        } else if (ServerManager.getServerManager().numServers() > 0) {
518
-            cw = ServerManager.getServerManager().getServers().get(0).getFrame();
519
-        } else {
520
-            cw = null;
521
-            cp = GlobalCommandParser.getGlobalCommandParser();
522
-        }
523
-        
524
-        if (cw != null) {
525
-            cp = cw.getCommandParser();
526
-        }
527
-        
528
-        for (String command : response) {
529
-            cp.parseCommand(cw, new ActionSubstitutor(triggers[0]).doSubstitution(command, arguments));
530
-        }
531
-        
532
-        if (newFormat != null && format != null) {
533
-            format.setLength(0);
534
-            format.append(newFormat);
535
-        }
536
-    }
537
-    
538 375
     /** {@inheritDoc} */
376
+    @Override
539 377
     public String toString() {
540
-        return "[name=" + group + "/" + name + ", triggers="
541
-                + Arrays.toString(triggers) + ", response="
542
-                + Arrays.toString(response) + ", "
543
-                + conditions + ", format='" + newFormat + "']";
378
+        final String parent = super.toString();
379
+        
380
+        return parent.substring(0, parent.length() - 1)
381
+                + ",file=" + file + "]";
544 382
     }
545 383
     
546 384
 }

+ 12
- 1
src/com/dmdirc/actions/ActionManager.java View File

@@ -72,7 +72,7 @@ public final class ActionManager {
72 72
     /** Indicates whether or not user actions should be killed (not processed). */
73 73
     private static boolean killSwitch
74 74
             = IdentityManager.getGlobalConfig().getOptionBool("actions", "killswitch", false);
75
-    
75
+   
76 76
     /** Creates a new instance of ActionManager. */
77 77
     private ActionManager() {
78 78
         // Shouldn't be instansiated
@@ -301,6 +301,17 @@ public final class ActionManager {
301 301
         }
302 302
     }
303 303
     
304
+    /**
305
+     * Reregisters the specified action. Should be used when the action's
306
+     * triggers change.
307
+     * 
308
+     * @param action The action to be reregistered
309
+     */
310
+    public static void reregisterAction(final Action action) {
311
+        unregisterAction(action);
312
+        reregisterAction(action);
313
+    }
314
+    
304 315
     /**
305 316
      * Deletes the specified action.
306 317
      *

+ 254
- 0
src/com/dmdirc/actions/ActionModel.java View File

@@ -0,0 +1,254 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.actions;
24
+
25
+import com.dmdirc.Main;
26
+import com.dmdirc.ServerManager;
27
+import com.dmdirc.WritableFrameContainer;
28
+import com.dmdirc.commandparser.parsers.CommandParser;
29
+import com.dmdirc.commandparser.parsers.GlobalCommandParser;
30
+import com.dmdirc.ui.interfaces.InputWindow;
31
+import com.dmdirc.ui.interfaces.Window;
32
+import java.util.ArrayList;
33
+import java.util.Arrays;
34
+import java.util.List;
35
+
36
+/**
37
+ * Represents the basic model of an action, and its triggering mechanism.
38
+ * Saving and loading are handled by the Action class.
39
+ *
40
+ * @author chris
41
+ */
42
+public class ActionModel {
43
+
44
+    /** The group this action belongs to. */
45
+    protected String group;
46
+
47
+    /** The name of this action. */
48
+    protected String name;
49
+
50
+    /** The ActionTypes that trigger this action. */
51
+    protected ActionType[] triggers;
52
+
53
+    /** The commands to execute if this action is triggered. */
54
+    protected String[] response;
55
+
56
+    /** The change that should be made to the format string, if any. */
57
+    protected String newFormat = null;
58
+
59
+    /** The conditions for this action. */
60
+    protected List<ActionCondition> conditions = new ArrayList<ActionCondition>();
61
+    
62
+    /**
63
+     * Creates a new instance of ActionModel with the specified properties.
64
+     *
65
+     * @param group The group the action belongs to
66
+     * @param name The name of the action
67
+     */
68
+    public ActionModel(final String group, final String name) {
69
+        this.group = group;
70
+        this.name = name;
71
+    }    
72
+    
73
+    /**
74
+     * Creates a new instance of ActionModel with the specified properties.
75
+     *
76
+     * @param group The group the action belongs to
77
+     * @param name The name of the action
78
+     * @param triggers The triggers to use
79
+     * @param response The response to use
80
+     * @param conditions The conditions to use
81
+     * @param newFormat The new formatter to use
82
+     */
83
+    public ActionModel(final String group, final String name,
84
+            final ActionType[] triggers, final String[] response,
85
+            final List<ActionCondition> conditions, final String newFormat) {
86
+        this.group = group;
87
+        this.name = name;
88
+        this.triggers = triggers.clone();
89
+        this.response = response.clone();
90
+        this.conditions = conditions;
91
+        this.newFormat = newFormat;        
92
+    }
93
+
94
+    /**
95
+     * Triggers this action.
96
+     *
97
+     * @param format The format of the message that's going to be displayed.
98
+     * @param arguments The arguments from the action that caused this trigger.
99
+     */
100
+    public void trigger(final StringBuffer format, final Object... arguments) {
101
+        final ActionSubstitutor sub = new ActionSubstitutor(triggers[0]);
102
+
103
+        for (ActionCondition condition : conditions) {
104
+            if (!condition.test(sub, arguments)) {
105
+                return;
106
+            }
107
+        }
108
+
109
+        final Window active = Main.getUI().getMainWindow().getActiveFrame();
110
+        InputWindow cw;
111
+        CommandParser cp = null;
112
+
113
+        if (arguments.length > 0 && arguments[0] instanceof WritableFrameContainer) {
114
+            cw = ((WritableFrameContainer) arguments[0]).getFrame();
115
+        } else if (active instanceof InputWindow) {
116
+            cw = (InputWindow) Main.getUI().getMainWindow().getActiveFrame();
117
+        } else if (ServerManager.getServerManager().numServers() > 0) {
118
+            cw = ServerManager.getServerManager().getServers().get(0).getFrame();
119
+        } else {
120
+            cw = null;
121
+            cp = GlobalCommandParser.getGlobalCommandParser();
122
+        }
123
+
124
+        if (cw != null) {
125
+            cp = cw.getCommandParser();
126
+        }
127
+
128
+        for (String command : response) {
129
+            cp.parseCommand(cw, new ActionSubstitutor(triggers[0]).doSubstitution(command, arguments));
130
+        }
131
+
132
+        if (newFormat != null && format != null) {
133
+            format.setLength(0);
134
+            format.append(newFormat);
135
+        }
136
+    }
137
+
138
+    /**
139
+     * Retrieves a list of this action's conditions.
140
+     *
141
+     * @return A list of this action's conditions
142
+     */
143
+    public List<ActionCondition> getConditions() {
144
+        return conditions;
145
+    }
146
+
147
+    /**
148
+     * Sets this action's conditions.
149
+     *
150
+     * @param conditions A list of conditions to use
151
+     */
152
+    public void setConditions(final List<ActionCondition> conditions) {
153
+        this.conditions = conditions;
154
+    }
155
+
156
+    /**
157
+     * Retrieves this action's triggers.
158
+     *
159
+     * @return The triggers used by this action
160
+     */
161
+    public ActionType[] getTriggers() {
162
+        return triggers.clone();
163
+    }
164
+
165
+    /**
166
+     * Sets this action's triggers.
167
+     *
168
+     * @param triggers The new triggers to use
169
+     */
170
+    public void setTriggers(final ActionType[] triggers) {
171
+        this.triggers = triggers.clone();
172
+    }
173
+
174
+    /**
175
+     * Retrieves this action's new format setting.
176
+     *
177
+     * @return The format that this action will use, or null if no change
178
+     */
179
+    public String getNewFormat() {
180
+        return newFormat;
181
+    }
182
+
183
+    /**
184
+     * Sets this action's new format setting.
185
+     *
186
+     * @param newFormat The new 'new format' setting
187
+     */
188
+    public void setNewFormat(final String newFormat) {
189
+        this.newFormat = newFormat;
190
+    }
191
+
192
+    /**
193
+     * Retrieves this action's response.
194
+     *
195
+     * @return The commands that will be executed if this action is triggered
196
+     */
197
+    public String[] getResponse() {
198
+        return response.clone();
199
+    }
200
+
201
+    /**
202
+     * Sets this action's response.
203
+     *
204
+     * @param response The new response to use
205
+     */
206
+    public void setResponse(final String[] response) {
207
+        this.response = response.clone();
208
+    }
209
+
210
+    /**
211
+     * Retrieves this action's group name.
212
+     *
213
+     * @return This action's group name
214
+     */
215
+    public String getGroup() {
216
+        return group;
217
+    }
218
+    
219
+    /**
220
+     * Sets the group of this action.
221
+     *
222
+     * @param newGroup The new group for this action
223
+     */
224
+    public void setGroup(final String newGroup) {
225
+        group = newGroup;
226
+    }
227
+
228
+    /**
229
+     * Retrieves this action's name.
230
+     *
231
+     * @return This action's name
232
+     */
233
+    public String getName() {
234
+        return name;
235
+    }
236
+    
237
+    /**
238
+     * Sets the name of this action.
239
+     *
240
+     * @param newName The new name for this action
241
+     */
242
+    public void setName(final String newName) {
243
+        name = newName;
244
+    }
245
+    
246
+    /** {@inheritDoc} */
247
+    @Override
248
+    public String toString() {
249
+        return "[name=" + group + "/" + name + ", triggers="
250
+                + Arrays.toString(triggers) + ", response="
251
+                + Arrays.toString(response) + ", "
252
+                + conditions + ", format='" + newFormat + "']";
253
+    }    
254
+}

Loading…
Cancel
Save