Browse Source

Switch SSD to use AutoCommands.

pull/233/head
Chris Smith 9 years ago
parent
commit
d6715dd48a

+ 39
- 55
ui_swing/src/com/dmdirc/addons/ui_swing/components/performpanel/PerformPanel.java View File

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

+ 10
- 13
ui_swing/src/com/dmdirc/addons/ui_swing/components/renderers/PerformRenderer.java View File

@@ -22,7 +22,9 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.components.renderers;
24 24
 
25
-import com.dmdirc.actions.wrappers.PerformWrapper.PerformDescription;
25
+import com.dmdirc.commandparser.auto.AutoCommand;
26
+
27
+import java.util.Optional;
26 28
 
27 29
 import javax.swing.JLabel;
28 30
 import javax.swing.ListCellRenderer;
@@ -32,7 +34,7 @@ import javax.swing.ListCellRenderer;
32 34
  *
33 35
  * @since 0.6.4
34 36
  */
35
-public class PerformRenderer extends DMDircListCellRenderer<PerformDescription> {
37
+public class PerformRenderer extends DMDircListCellRenderer<AutoCommand> {
36 38
 
37 39
     /** A version number for this class. */
38 40
     private static final long serialVersionUID = 1;
@@ -42,24 +44,19 @@ public class PerformRenderer extends DMDircListCellRenderer<PerformDescription>
42 44
      *
43 45
      * @param renderer Parent renderer
44 46
      */
45
-    public PerformRenderer(final ListCellRenderer<? super PerformDescription> renderer) {
47
+    public PerformRenderer(final ListCellRenderer<? super AutoCommand> renderer) {
46 48
         super(renderer);
47 49
     }
48 50
 
49 51
     @Override
50
-    protected void renderValue(final JLabel label, final PerformDescription value,
52
+    protected void renderValue(final JLabel label, final AutoCommand value,
51 53
             final int index, final boolean isSelected,
52 54
             final boolean cellHasFocus) {
53
-        final String target = value.getTarget();
54
-        final String profile = value.getProfile();
55
+        final String target = value.getServer().orElse(value.getNetwork().orElse(""));
56
+        final Optional<String> profile = value.getProfile();
55 57
         final String type = value.getType().toString();
56
-        String friendlyText = type + " perform (" + target + ") ";
57
-
58
-        if (profile == null) {
59
-            friendlyText += "Any profile";
60
-        } else {
61
-            friendlyText += "This profile (" + profile + ")";
62
-        }
58
+        final String friendlyText = type + " perform (" + target + ") "
59
+                + profile.map(p -> "This profile (" + p + ')').orElse("Any profile");
63 60
         label.setText(friendlyText);
64 61
     }
65 62
 

+ 23
- 30
ui_swing/src/com/dmdirc/addons/ui_swing/dialogs/serversetting/PerformTab.java View File

@@ -22,12 +22,11 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.dialogs.serversetting;
24 24
 
25
-import com.dmdirc.actions.wrappers.PerformType;
26
-import com.dmdirc.actions.wrappers.PerformWrapper;
27
-import com.dmdirc.actions.wrappers.PerformWrapper.PerformDescription;
28 25
 import com.dmdirc.addons.ui_swing.UIUtilities;
29 26
 import com.dmdirc.addons.ui_swing.components.performpanel.PerformPanel;
30 27
 import com.dmdirc.addons.ui_swing.components.renderers.PerformRenderer;
28
+import com.dmdirc.commandparser.auto.AutoCommand;
29
+import com.dmdirc.commandparser.auto.AutoCommandManager;
31 30
 import com.dmdirc.interfaces.Connection;
32 31
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
33 32
 import com.dmdirc.ui.IconManager;
@@ -37,6 +36,7 @@ import java.awt.event.ActionEvent;
37 36
 import java.awt.event.ActionListener;
38 37
 import java.util.ArrayList;
39 38
 import java.util.Collection;
39
+import java.util.Optional;
40 40
 
41 41
 import javax.swing.DefaultComboBoxModel;
42 42
 import javax.swing.JComboBox;
@@ -53,29 +53,20 @@ public class PerformTab extends JPanel implements ActionListener {
53 53
     private static final long serialVersionUID = 1;
54 54
     /** Parent connection. */
55 55
     private final Connection connection;
56
-    /** Perform wrapper to read/write performs to. */
57
-    private final PerformWrapper wrapper;
56
+    /** Command manager wrapper to read/write performs to. */
57
+    private final AutoCommandManager autoCommandManager;
58 58
     /** Network/server combo box. */
59
-    private JComboBox<PerformDescription> target;
59
+    private JComboBox<AutoCommand> target;
60 60
     /** Perform panel. */
61 61
     private PerformPanel performPanel;
62 62
 
63
-    /**
64
-     * Creates a new instance of IgnoreList.
65
-     *
66
-     * @param iconManager Icon manager
67
-     * @param config      Config to read settings from
68
-     * @param wrapper     Perform wrapper to read/write performs to.
69
-     * @param connection  Connection whose perform should be displayed.
70
-     */
71 63
     public PerformTab(
72 64
             final IconManager iconManager,
73 65
             final ColourManagerFactory colourManagerFactory,
74 66
             final AggregateConfigProvider config,
75
-            final PerformWrapper wrapper,
67
+            final AutoCommandManager autoCommandManager,
76 68
             final Connection connection) {
77
-
78
-        this.wrapper = wrapper;
69
+        this.autoCommandManager = autoCommandManager;
79 70
         this.connection = connection;
80 71
 
81 72
         setOpaque(UIUtilities.getTabbedPaneOpaque());
@@ -94,21 +85,23 @@ public class PerformTab extends JPanel implements ActionListener {
94 85
             final AggregateConfigProvider config) {
95 86
         setLayout(new MigLayout("fill"));
96 87
 
97
-        final DefaultComboBoxModel<PerformDescription> model = new DefaultComboBoxModel<>();
88
+        final DefaultComboBoxModel<AutoCommand> model = new DefaultComboBoxModel<>();
98 89
         target = new JComboBox<>(model);
99 90
 
100 91
         add(target, "growx, pushx, wrap");
101 92
 
102
-        final Collection<PerformDescription> performList = new ArrayList<>();
93
+        final Collection<AutoCommand> performList = new ArrayList<>();
103 94
 
104
-        final PerformDescription networkPerform = new PerformDescription(
105
-                PerformType.NETWORK, connection.getNetwork());
106
-        final PerformDescription networkProfilePerform = new PerformDescription(
107
-                PerformType.NETWORK, connection.getNetwork(), connection.getProfile().getName());
108
-        final PerformDescription serverPerform = new PerformDescription(
109
-                PerformType.SERVER, connection.getAddress());
110
-        final PerformDescription serverProfilePerform = new PerformDescription(
111
-                PerformType.SERVER, connection.getAddress(), connection.getProfile().getName());
95
+        final AutoCommand networkPerform = autoCommandManager.getOrCreateAutoCommand(
96
+                Optional.of(connection.getNetwork()), Optional.empty(), Optional.empty());
97
+        final AutoCommand networkProfilePerform = autoCommandManager.getOrCreateAutoCommand(
98
+                Optional.of(connection.getNetwork()), Optional.empty(),
99
+                Optional.of(connection.getProfile().getName()));
100
+        final AutoCommand serverPerform = autoCommandManager.getOrCreateAutoCommand(
101
+                Optional.empty(), Optional.of(connection.getAddress()), Optional.empty());
102
+        final AutoCommand serverProfilePerform = autoCommandManager.getOrCreateAutoCommand(
103
+                Optional.empty(), Optional.of(connection.getAddress()),
104
+                Optional.of(connection.getProfile().getName()));
112 105
 
113 106
         model.addElement(networkPerform);
114 107
         model.addElement(networkProfilePerform);
@@ -122,8 +115,8 @@ public class PerformTab extends JPanel implements ActionListener {
122 115
         performList.add(serverPerform);
123 116
         performList.add(serverProfilePerform);
124 117
 
125
-        performPanel = new PerformPanel(iconManager, colourManagerFactory, config, wrapper,
126
-                performList);
118
+        performPanel = new PerformPanel(iconManager, colourManagerFactory, config,
119
+                autoCommandManager, performList);
127 120
         performPanel.switchPerform(networkPerform);
128 121
         add(performPanel, "grow, push");
129 122
 
@@ -141,7 +134,7 @@ public class PerformTab extends JPanel implements ActionListener {
141 134
 
142 135
     @Override
143 136
     public void actionPerformed(final ActionEvent e) {
144
-        final PerformDescription perform = (PerformDescription) ((JComboBox<?>) e.getSource()).
137
+        final AutoCommand perform = (AutoCommand) ((JComboBox<?>) e.getSource()).
145 138
                 getSelectedItem();
146 139
         performPanel.switchPerform(perform);
147 140
     }

+ 6
- 15
ui_swing/src/com/dmdirc/addons/ui_swing/dialogs/serversetting/ServerSettingsDialog.java View File

@@ -23,13 +23,13 @@
23 23
 package com.dmdirc.addons.ui_swing.dialogs.serversetting;
24 24
 
25 25
 import com.dmdirc.ServerState;
26
-import com.dmdirc.actions.wrappers.PerformWrapper;
27 26
 import com.dmdirc.addons.ui_swing.PrefsComponentFactory;
28 27
 import com.dmdirc.addons.ui_swing.UIUtilities;
29 28
 import com.dmdirc.addons.ui_swing.components.expandingsettings.SettingsPanel;
30 29
 import com.dmdirc.addons.ui_swing.components.modes.UserModesPane;
31 30
 import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
32 31
 import com.dmdirc.addons.ui_swing.dialogs.StandardQuestionDialog;
32
+import com.dmdirc.commandparser.auto.AutoCommandManager;
33 33
 import com.dmdirc.config.prefs.PreferencesManager;
34 34
 import com.dmdirc.interfaces.Connection;
35 35
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
@@ -56,8 +56,8 @@ public class ServerSettingsDialog extends StandardDialog implements ActionListen
56 56
     private static final long serialVersionUID = 2;
57 57
     /** Parent connection. */
58 58
     private final Connection connection;
59
-    /** Perform wrapper for the perform panel. */
60
-    private final PerformWrapper performWrapper;
59
+    /** Mnaager for the perform panel. */
60
+    private final AutoCommandManager autoCommandManager;
61 61
     /** Preferences manager to retrieve settings from. */
62 62
     private final PreferencesManager preferencesManager;
63 63
     /** User modes panel. */
@@ -71,25 +71,16 @@ public class ServerSettingsDialog extends StandardDialog implements ActionListen
71 71
     /** The tabbed pane. */
72 72
     private JTabbedPane tabbedPane;
73 73
 
74
-    /**
75
-     * Creates a new instance of ServerSettingsDialog.
76
-     *
77
-     * @param preferencesManager Preferences manager to retrieve settings from
78
-     * @param compFactory        Preferences setting component factory
79
-     * @param performWrapper     Wrapper for the perform tab.
80
-     * @param connection         The server object that we're editing settings for
81
-     * @param parentWindow       Parent window
82
-     */
83 74
     public ServerSettingsDialog(
84 75
             final PreferencesManager preferencesManager,
85 76
             final PrefsComponentFactory compFactory,
86
-            final PerformWrapper performWrapper,
77
+            final AutoCommandManager autoCommandManager,
87 78
             final Connection connection,
88 79
             final Window parentWindow,
89 80
             final ColourManagerFactory colourManagerFactory) {
90 81
         super(parentWindow, ModalityType.MODELESS);
91 82
         this.connection = connection;
92
-        this.performWrapper = performWrapper;
83
+        this.autoCommandManager = autoCommandManager;
93 84
         this.preferencesManager = preferencesManager;
94 85
 
95 86
         setTitle("Server settings");
@@ -122,7 +113,7 @@ public class ServerSettingsDialog extends StandardDialog implements ActionListen
122 113
                 connection, parentWindow);
123 114
 
124 115
         performPanel = new PerformTab(connection.getWindowModel().getIconManager(),
125
-                colourManagerFactory, config, performWrapper, connection);
116
+                colourManagerFactory, config, autoCommandManager, connection);
126 117
 
127 118
         settingsPanel = new SettingsPanel(connection.getWindowModel().getIconManager(), compFactory,
128 119
                 "These settings are specific to this network, any settings specified here will "

+ 3
- 3
ui_swing/src/com/dmdirc/addons/ui_swing/injection/DialogModule.java View File

@@ -25,7 +25,6 @@ package com.dmdirc.addons.ui_swing.injection;
25 25
 import com.dmdirc.Channel;
26 26
 import com.dmdirc.ClientModule.UserConfig;
27 27
 import com.dmdirc.DMDircMBassador;
28
-import com.dmdirc.actions.wrappers.PerformWrapper;
29 28
 import com.dmdirc.addons.ui_swing.MainFrame;
30 29
 import com.dmdirc.addons.ui_swing.PrefsComponentFactory;
31 30
 import com.dmdirc.addons.ui_swing.SwingWindowFactory;
@@ -41,6 +40,7 @@ import com.dmdirc.addons.ui_swing.dialogs.profile.ProfileManagerDialog;
41 40
 import com.dmdirc.addons.ui_swing.dialogs.serversetting.ServerSettingsDialog;
42 41
 import com.dmdirc.addons.ui_swing.dialogs.updater.SwingRestartDialog;
43 42
 import com.dmdirc.addons.ui_swing.dialogs.updater.SwingUpdaterDialog;
43
+import com.dmdirc.commandparser.auto.AutoCommandManager;
44 44
 import com.dmdirc.config.prefs.PreferencesManager;
45 45
 import com.dmdirc.interfaces.CommandController;
46 46
 import com.dmdirc.interfaces.Connection;
@@ -163,13 +163,13 @@ public class DialogModule {
163 163
     public KeyedDialogProvider<Connection, ServerSettingsDialog> getServerSettingsDialogProvider(
164 164
             final PreferencesManager preferencesManager,
165 165
             final PrefsComponentFactory compFactory,
166
-            final PerformWrapper performWrapper,
166
+            final AutoCommandManager autoCommandManager,
167 167
             @MainWindow final Window parentWindow,
168 168
             final ColourManagerFactory colourManagerFactory) {
169 169
         return new KeyedDialogProvider<Connection, ServerSettingsDialog>() {
170 170
             @Override
171 171
             protected ServerSettingsDialog getInstance(final Connection key) {
172
-                return new ServerSettingsDialog(preferencesManager, compFactory, performWrapper,
172
+                return new ServerSettingsDialog(preferencesManager, compFactory, autoCommandManager,
173 173
                         key, parentWindow, colourManagerFactory);
174 174
             }
175 175
         };

Loading…
Cancel
Save