|
@@ -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
|
}
|