|
@@ -22,9 +22,9 @@
|
22
|
22
|
|
23
|
23
|
package com.dmdirc.addons.ui_swing.components.performpanel;
|
24
|
24
|
|
25
|
|
-import com.dmdirc.actions.wrappers.PerformWrapper;
|
26
|
|
-import com.dmdirc.actions.wrappers.PerformWrapper.PerformDescription;
|
27
|
25
|
import com.dmdirc.addons.ui_swing.components.inputfields.TextAreaInputField;
|
|
26
|
+import com.dmdirc.commandparser.auto.AutoCommand;
|
|
27
|
+import com.dmdirc.commandparser.auto.AutoCommandManager;
|
28
|
28
|
import com.dmdirc.interfaces.config.AggregateConfigProvider;
|
29
|
29
|
import com.dmdirc.ui.IconManager;
|
30
|
30
|
import com.dmdirc.ui.messages.ColourManagerFactory;
|
|
@@ -33,7 +33,6 @@ import java.util.Collection;
|
33
|
33
|
import java.util.Collections;
|
34
|
34
|
import java.util.HashMap;
|
35
|
35
|
import java.util.Map;
|
36
|
|
-import java.util.Map.Entry;
|
37
|
36
|
|
38
|
37
|
import javax.swing.JPanel;
|
39
|
38
|
import javax.swing.JScrollPane;
|
|
@@ -54,11 +53,11 @@ public class PerformPanel extends JPanel {
|
54
|
53
|
/** Text area that the perform is displayed in. */
|
55
|
54
|
private final JTextArea performSpace;
|
56
|
55
|
/** Perform wrapper to read/write performs to. */
|
57
|
|
- private final PerformWrapper performWrapper;
|
58
|
|
- /** Map of performs this panel can display. */
|
59
|
|
- private final Map<PerformDescription, String[]> performs = new HashMap<>();
|
60
|
|
- /** The perform that is displayed in the text area. */
|
61
|
|
- private PerformDescription visiblePerform;
|
|
56
|
+ private final AutoCommandManager autoCommandManager;
|
|
57
|
+ /** Map of original auto commands to their modified forms. */
|
|
58
|
+ private final Map<AutoCommand, AutoCommand> autoCommands = new HashMap<>();
|
|
59
|
+ /** The auto command that is displayed in the text area. */
|
|
60
|
+ private AutoCommand visiblePerform;
|
62
|
61
|
|
63
|
62
|
/**
|
64
|
63
|
* Creates a new instance of PerformPanel that has no knowledge of any performs at the time of
|
|
@@ -68,15 +67,15 @@ public class PerformPanel extends JPanel {
|
68
|
67
|
*
|
69
|
68
|
* @param iconManager Icon manager
|
70
|
69
|
* @param config Config to read settings from
|
71
|
|
- * @param performWrapper Perform wrapper to read/write performs to.
|
|
70
|
+ * @param autoCommandManager Perform wrapper to read/write performs to.
|
72
|
71
|
*/
|
73
|
72
|
public PerformPanel(
|
74
|
73
|
final IconManager iconManager,
|
75
|
74
|
final ColourManagerFactory colourManagerFactory,
|
76
|
75
|
final AggregateConfigProvider config,
|
77
|
|
- final PerformWrapper performWrapper) {
|
78
|
|
- this(iconManager, colourManagerFactory, config, performWrapper,
|
79
|
|
- Collections.<PerformDescription>emptyList());
|
|
76
|
+ final AutoCommandManager autoCommandManager) {
|
|
77
|
+ this(iconManager, colourManagerFactory, config, autoCommandManager,
|
|
78
|
+ Collections.<AutoCommand>emptyList());
|
80
|
79
|
}
|
81
|
80
|
|
82
|
81
|
/**
|
|
@@ -87,42 +86,31 @@ public class PerformPanel extends JPanel {
|
87
|
86
|
*
|
88
|
87
|
* @param iconManager Icon manager
|
89
|
88
|
* @param config Config to read settings from
|
90
|
|
- * @param performWrapper Perform wrapper to read/write performs to.
|
|
89
|
+ * @param autoCommandManager Perform wrapper to read/write performs to.
|
91
|
90
|
* @param performs Collection of PerformDescriptions to initialise
|
92
|
91
|
*/
|
93
|
92
|
public PerformPanel(
|
94
|
93
|
final IconManager iconManager,
|
95
|
94
|
final ColourManagerFactory colourManagerFactory,
|
96
|
95
|
final AggregateConfigProvider config,
|
97
|
|
- final PerformWrapper performWrapper,
|
98
|
|
- final Collection<PerformDescription> performs) {
|
|
96
|
+ final AutoCommandManager autoCommandManager,
|
|
97
|
+ final Collection<AutoCommand> performs) {
|
99
|
98
|
|
100
|
|
- this.performWrapper = performWrapper;
|
|
99
|
+ this.autoCommandManager = autoCommandManager;
|
101
|
100
|
|
102
|
|
- performs.forEach(this::addPerform);
|
|
101
|
+ performs.forEach(this::addCommand);
|
103
|
102
|
setLayout(new MigLayout("ins 0, fill"));
|
104
|
103
|
performSpace = new TextAreaInputField(iconManager, colourManagerFactory, config, "");
|
105
|
104
|
add(new JScrollPane(performSpace), "grow, push");
|
106
|
105
|
}
|
107
|
106
|
|
108
|
107
|
/**
|
109
|
|
- * This will add a perform to the internal cache so that switching performs is more efficient.
|
110
|
|
- * Switching performs can be achieved using
|
111
|
|
- * {@link #switchPerform(com.dmdirc.actions.wrappers.PerformWrapper.PerformDescription) }
|
|
108
|
+ * This will add a command to the internal cache to track changes.
|
112
|
109
|
*
|
113
|
|
- * @param perform PerformDescription to add
|
|
110
|
+ * @param command Auto command to add
|
114
|
111
|
*/
|
115
|
|
- public void addPerform(final PerformDescription perform) {
|
116
|
|
- performs.put(perform, performWrapper.getPerform(perform));
|
117
|
|
- }
|
118
|
|
-
|
119
|
|
- /**
|
120
|
|
- * Deletes a perform from the map of performs this PerformPanel can modify.
|
121
|
|
- *
|
122
|
|
- * @param perform PerformDescription to remove
|
123
|
|
- */
|
124
|
|
- public void delPerform(final PerformDescription perform) {
|
125
|
|
- performs.remove(perform);
|
|
112
|
+ public void addCommand(final AutoCommand command) {
|
|
113
|
+ autoCommands.putIfAbsent(command, command);
|
126
|
114
|
}
|
127
|
115
|
|
128
|
116
|
/**
|
|
@@ -130,12 +118,13 @@ public class PerformPanel extends JPanel {
|
130
|
118
|
*/
|
131
|
119
|
public void savePerform() {
|
132
|
120
|
if (visiblePerform != null) {
|
133
|
|
- performs.put(visiblePerform, performSpace.getText().split("\n"));
|
134
|
|
- }
|
135
|
|
- for (final Entry<PerformDescription, String[]> perform
|
136
|
|
- : performs.entrySet()) {
|
137
|
|
- performWrapper.setPerform(perform.getKey(), perform.getValue());
|
|
121
|
+ autoCommands.put(visiblePerform,
|
|
122
|
+ createAutoCommand(visiblePerform, performSpace.getText()));
|
138
|
123
|
}
|
|
124
|
+
|
|
125
|
+ autoCommands.entrySet().parallelStream()
|
|
126
|
+ .filter(e -> !e.getKey().equals(e.getValue()))
|
|
127
|
+ .forEach(e -> autoCommandManager.replaceAutoCommand(e.getKey(), e.getValue()));
|
139
|
128
|
}
|
140
|
129
|
|
141
|
130
|
/**
|
|
@@ -145,38 +134,33 @@ public class PerformPanel extends JPanel {
|
145
|
134
|
*
|
146
|
135
|
* @param perform Perform to display in the text area
|
147
|
136
|
*/
|
148
|
|
- public void switchPerform(final PerformDescription perform) {
|
149
|
|
- if (perform != null && !performs.containsKey(perform)) {
|
150
|
|
- addPerform(perform);
|
|
137
|
+ public void switchPerform(final AutoCommand perform) {
|
|
138
|
+ if (perform != null && !autoCommands.containsKey(perform)) {
|
|
139
|
+ addCommand(perform);
|
151
|
140
|
}
|
152
|
141
|
if (visiblePerform != null) {
|
153
|
|
- performs.put(visiblePerform, performSpace.getText().split("\n"));
|
|
142
|
+ autoCommands.put(visiblePerform,
|
|
143
|
+ createAutoCommand(visiblePerform, performSpace.getText()));
|
154
|
144
|
}
|
155
|
145
|
if (perform == null) {
|
156
|
146
|
performSpace.setText("");
|
157
|
147
|
} else {
|
158
|
|
- performSpace.setText(implode(performs.get(perform)));
|
|
148
|
+ performSpace.setText(perform.getResponse());
|
159
|
149
|
}
|
160
|
150
|
performSpace.setEnabled(perform != null);
|
161
|
151
|
visiblePerform = perform;
|
162
|
152
|
}
|
163
|
153
|
|
164
|
154
|
/**
|
165
|
|
- * Implodes the specified string array, joining each line with a LF.
|
166
|
|
- *
|
167
|
|
- * @param lines The lines to be joined together
|
|
155
|
+ * Creates a new auto command based on the existing one, but with a different response.
|
168
|
156
|
*
|
169
|
|
- * @return A string containing each element of lines, separated by a LF.
|
|
157
|
+ * @param existing The existing auto command to model on.
|
|
158
|
+ * @param text The new text to use for the response.
|
|
159
|
+ * @return A new AutoCommand with the same target as the existing one, and the given text.
|
170
|
160
|
*/
|
171
|
|
- private String implode(final String... lines) {
|
172
|
|
- final StringBuilder res = new StringBuilder();
|
173
|
|
-
|
174
|
|
- for (final String line : lines) {
|
175
|
|
- res.append('\n');
|
176
|
|
- res.append(line);
|
177
|
|
- }
|
178
|
|
-
|
179
|
|
- return res.length() == 0 ? "" : res.substring(1);
|
|
161
|
+ private static AutoCommand createAutoCommand(final AutoCommand existing, final String text) {
|
|
162
|
+ return AutoCommand.create(existing.getServer(), existing.getNetwork(),
|
|
163
|
+ existing.getProfile(), text);
|
180
|
164
|
}
|
181
|
165
|
|
182
|
166
|
}
|