Selaa lähdekoodia

Start fixing the nick colour plugin.

pull/307/head
Greg Holmes 9 vuotta sitten
vanhempi
commit
67dee7da19

+ 6
- 31
nickcolours/src/com/dmdirc/addons/nickcolours/ColourRenderer.java Näytä tiedosto

@@ -22,14 +22,8 @@
22 22
 
23 23
 package com.dmdirc.addons.nickcolours;
24 24
 
25
-import com.dmdirc.addons.ui_swing.UIUtilities;
26
-import com.dmdirc.util.colours.Colour;
27
-import com.dmdirc.ui.messages.ColourManager;
28
-
29 25
 import java.awt.Color;
30
-import java.awt.Component;
31 26
 
32
-import javax.swing.JTable;
33 27
 import javax.swing.border.LineBorder;
34 28
 import javax.swing.table.DefaultTableCellRenderer;
35 29
 
@@ -40,45 +34,26 @@ public class ColourRenderer extends DefaultTableCellRenderer {
40 34
 
41 35
     /** A version number for this class. */
42 36
     private static final long serialVersionUID = 1;
43
-    /** The colour manager to use to parse colours. */
44
-    private final ColourManager colourManager;
45 37
 
46 38
     /**
47 39
      * Creates a new instance of ColourRenderer.
48
-     *
49
-     * @param colourManager The colour manager to use to parse colours.
50 40
      */
51
-    public ColourRenderer(final ColourManager colourManager) {
52
-        this.colourManager = colourManager;
53
-
41
+    public ColourRenderer() {
42
+        setHorizontalAlignment(CENTER);
54 43
         setOpaque(true);
55 44
     }
56 45
 
57 46
     @Override
58
-    public Component getTableCellRendererComponent(final JTable table,
59
-            final Object value, final boolean isSelected,
60
-            final boolean hasFocus, final int row, final int column) {
61
-        Color colour = null;
62
-        if (value != null && !((String) value).isEmpty()) {
63
-            final Colour temp = colourManager.getColourFromString((String) value, null);
64
-            if (temp != null) {
65
-                colour = UIUtilities.convertColour(temp);
66
-            }
67
-        }
68
-
69
-        setHorizontalAlignment(CENTER);
70
-
71
-        if (colour == null) {
47
+    protected void setValue(final Object value) {
48
+        final Color color = (Color) value;
49
+        if (color == null) {
72 50
             setBorder(new LineBorder(Color.GRAY));
73 51
             setText("Not Set");
74
-            setBackground(table.getBackground());
75 52
         } else {
76 53
             setBorder(new LineBorder(Color.BLACK));
77 54
             setText("");
78
-            setBackground(colour);
55
+            setBackground(color);
79 56
         }
80
-
81
-        return this;
82 57
     }
83 58
 
84 59
 }

+ 45
- 0
nickcolours/src/com/dmdirc/addons/nickcolours/NickColourEntry.java Näytä tiedosto

@@ -0,0 +1,45 @@
1
+/*
2
+ * Copyright (c) 2006-2015 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.addons.nickcolours;
24
+
25
+import com.google.auto.value.AutoValue;
26
+
27
+import java.awt.Color;
28
+
29
+/**
30
+ *
31
+ */
32
+@AutoValue
33
+public abstract class NickColourEntry {
34
+
35
+    NickColourEntry() {}
36
+
37
+    public abstract String getNetwork();
38
+    public abstract String getUser();
39
+    public abstract Color getColor();
40
+
41
+    public static NickColourEntry create(final String network, final String user,
42
+            final Color color) {
43
+        return new AutoValue_NickColourEntry(network, user, color);
44
+    }
45
+}

+ 9
- 19
nickcolours/src/com/dmdirc/addons/nickcolours/NickColourInputDialog.java Näytä tiedosto

@@ -22,11 +22,12 @@
22 22
 
23 23
 package com.dmdirc.addons.nickcolours;
24 24
 
25
+import com.dmdirc.addons.ui_swing.components.IconManager;
25 26
 import com.dmdirc.addons.ui_swing.components.colours.ColourChooser;
26 27
 import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
27
-import com.dmdirc.addons.ui_swing.components.IconManager;
28 28
 import com.dmdirc.ui.messages.ColourManager;
29 29
 
30
+import java.awt.Color;
30 31
 import java.awt.Window;
31 32
 import java.awt.event.ActionEvent;
32 33
 import java.awt.event.ActionListener;
@@ -41,8 +42,7 @@ import net.miginfocom.swing.MigLayout;
41 42
 /**
42 43
  * New nick colour input dialog.
43 44
  */
44
-public class NickColourInputDialog extends StandardDialog
45
-        implements ActionListener {
45
+public class NickColourInputDialog extends StandardDialog implements ActionListener {
46 46
 
47 47
     /** A version number for this class. */
48 48
     private static final long serialVersionUID = 1;
@@ -58,8 +58,6 @@ public class NickColourInputDialog extends StandardDialog
58 58
     private JTextField network;
59 59
     /** text colour input. */
60 60
     private ColourChooser textColour;
61
-    /** nicklist colour input. */
62
-    private ColourChooser nicklistColour;
63 61
 
64 62
     /**
65 63
      * Creates a new instance of NickColourInputDialog.
@@ -72,7 +70,6 @@ public class NickColourInputDialog extends StandardDialog
72 70
      * @param nickname      The nickname that's currently set
73 71
      * @param network       The network that's currently set
74 72
      * @param textcolour    The text colour that's currently set
75
-     * @param nickcolour    The nicklist colour that's currently set
76 73
      */
77 74
     public NickColourInputDialog(
78 75
             final Window parentWindow,
@@ -80,7 +77,7 @@ public class NickColourInputDialog extends StandardDialog
80 77
             final IconManager iconManager,
81 78
             final NickColourPanel panel, final int row,
82 79
             final String nickname, final String network,
83
-            final String textcolour, final String nickcolour) {
80
+            final Color textcolour) {
84 81
         super(parentWindow, ModalityType.MODELESS);
85 82
 
86 83
         this.panel = panel;
@@ -88,7 +85,7 @@ public class NickColourInputDialog extends StandardDialog
88 85
 
89 86
         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
90 87
 
91
-        initComponents(colourManager, iconManager, nickname, network, textcolour, nickcolour);
88
+        initComponents(colourManager, iconManager, nickname, network, textcolour);
92 89
         initListeners();
93 90
         layoutComponents();
94 91
 
@@ -109,7 +106,7 @@ public class NickColourInputDialog extends StandardDialog
109 106
             final ColourManager colourManager,
110 107
             final IconManager iconManager,
111 108
             final NickColourPanel panel) {
112
-        this(parentWindow, colourManager, iconManager, panel, -1, "", "", "", "");
109
+        this(parentWindow, colourManager, iconManager, panel, -1, "", "", null);
113 110
 
114 111
         isnew = true;
115 112
     }
@@ -120,21 +117,17 @@ public class NickColourInputDialog extends StandardDialog
120 117
      * @param defaultNickname   The default value for the nickname text field
121 118
      * @param defaultNetwork    The default value for the network text field
122 119
      * @param defaultTextColour The default value for the text colour option
123
-     * @param defaultNickColour The default value for the nick colour option
124 120
      */
125 121
     private void initComponents(
126 122
             final ColourManager colourManager,
127 123
             final IconManager iconManager,
128 124
             final String defaultNickname,
129
-            final String defaultNetwork, final String defaultTextColour,
130
-            final String defaultNickColour) {
125
+            final String defaultNetwork, final Color defaultTextColour) {
131 126
         orderButtons(new JButton(), new JButton());
132 127
 
133 128
         nickname = new JTextField(defaultNickname);
134 129
         network = new JTextField(defaultNetwork);
135
-        textColour = new ColourChooser(colourManager, iconManager, defaultTextColour, true, true);
136
-        nicklistColour = new ColourChooser(colourManager, iconManager, defaultNickColour,
137
-                true, true);
130
+        textColour = new ColourChooser(colourManager, iconManager, "", true, true);
138 131
     }
139 132
 
140 133
     /** Initialises the listeners. */
@@ -156,9 +149,6 @@ public class NickColourInputDialog extends StandardDialog
156 149
         add(new JLabel("Text colour: "));
157 150
         add(textColour, "growx");
158 151
 
159
-        add(new JLabel("Nicklist colour: "));
160
-        add(nicklistColour, "growx");
161
-
162 152
         add(getLeftButton(), "right");
163 153
         add(getRightButton(), "right");
164 154
 
@@ -181,7 +171,7 @@ public class NickColourInputDialog extends StandardDialog
181 171
 
182 172
         panel.addRow(network.getText().toLowerCase(),
183 173
                 nickname.getText().toLowerCase(),
184
-                nickname.getText().toLowerCase(), nicklistColour.getColour());
174
+                textColour.getColour());
185 175
     }
186 176
 
187 177
 }

+ 47
- 110
nickcolours/src/com/dmdirc/addons/nickcolours/NickColourManager.java Näytä tiedosto

@@ -26,7 +26,10 @@ import com.dmdirc.ClientModule.GlobalConfig;
26 26
 import com.dmdirc.DMDircMBassador;
27 27
 import com.dmdirc.addons.ui_swing.EDTInvocation;
28 28
 import com.dmdirc.addons.ui_swing.UIUtilities;
29
+import com.dmdirc.addons.ui_swing.components.IconManager;
29 30
 import com.dmdirc.addons.ui_swing.injection.MainWindow;
31
+import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
32
+import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
30 33
 import com.dmdirc.config.ConfigBinder;
31 34
 import com.dmdirc.config.ConfigBinding;
32 35
 import com.dmdirc.config.prefs.PluginPreferencesCategory;
@@ -40,18 +43,17 @@ import com.dmdirc.events.DisplayProperty;
40 43
 import com.dmdirc.interfaces.GroupChatUser;
41 44
 import com.dmdirc.interfaces.User;
42 45
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
43
-import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
44 46
 import com.dmdirc.parser.interfaces.StringConverter;
45 47
 import com.dmdirc.plugins.PluginDomain;
46 48
 import com.dmdirc.plugins.PluginInfo;
47
-import com.dmdirc.addons.ui_swing.components.IconManager;
48 49
 import com.dmdirc.ui.messages.ColourManager;
49 50
 import com.dmdirc.util.colours.Colour;
50 51
 
52
+import java.awt.Color;
51 53
 import java.awt.Window;
52
-import java.util.ArrayList;
53
-import java.util.Collection;
54
+import java.nio.file.Path;
54 55
 import java.util.List;
56
+import java.util.Map;
55 57
 
56 58
 import javax.inject.Inject;
57 59
 import javax.inject.Provider;
@@ -67,24 +69,19 @@ public class NickColourManager {
67 69
 
68 70
     /** Manager to parse colours with. */
69 71
     private final ColourManager colourManager;
70
-    /** Config to read settings from. */
71
-    private final AggregateConfigProvider globalConfig;
72
-    /** Config binder. */
73 72
     private final ConfigBinder configBinder;
74
-    /** Plugin's setting domain. */
75
-    private final String domain;
76 73
     private final IconManager iconManager;
77 74
     private final Provider<Window> mainWindowProvider;
78
-    /** Event bus to subscribe to events on . */
79 75
     private final DMDircMBassador eventBus;
80
-    /** The plugin's info. */
76
+    private final NickColourYamlStore nickColourYamlStore;
77
+    private final Path path;
81 78
     private final PluginInfo pluginInfo;
82
-    /** "Random" colours to use to colour nicknames. */
83 79
     private String[] randColours = {
84 80
         "E90E7F", "8E55E9", "B30E0E", "18B33C", "58ADB3", "9E54B3", "B39875", "3176B3",};
85 81
     private boolean useowncolour;
86 82
     private String owncolour;
87 83
     private boolean userandomcolour;
84
+    private Map<String, Color> nickColours;
88 85
 
89 86
     @Inject
90 87
     public NickColourManager(
@@ -94,14 +91,16 @@ public class NickColourManager {
94 91
             final IconManager iconManager,
95 92
             @MainWindow final Provider<Window> mainWindowProvider,
96 93
             @PluginDomain(NickColourPlugin.class) final PluginInfo pluginInfo,
97
-            final DMDircMBassador eventBus) {
98
-        this.domain = domain;
99
-        this.globalConfig = globalConfig;
94
+            final DMDircMBassador eventBus,
95
+            final NickColourYamlStore nickColourYamlStore,
96
+            @Directory(DirectoryType.BASE) final Path path) {
100 97
         this.colourManager = colourManager;
101 98
         this.iconManager = iconManager;
102 99
         this.mainWindowProvider = mainWindowProvider;
103 100
         this.pluginInfo = pluginInfo;
104 101
         this.eventBus = eventBus;
102
+        this.nickColourYamlStore = nickColourYamlStore;
103
+        this.path = path;
105 104
         configBinder = globalConfig.getBinder().withDefaultDomain(domain);
106 105
     }
107 106
 
@@ -127,8 +126,8 @@ public class NickColourManager {
127 126
         final StringConverter sc = client.getUser().getConnection().getParser().get()
128 127
                 .getStringConverter();
129 128
         final User myself = client.getUser();
130
-        final String nickOption1 = "color:" + sc.toLowerCase(network + ':' + client.getNickname());
131
-        final String nickOption2 = "color:" + sc.toLowerCase("*:" + client.getNickname());
129
+        final String nickOption1 = sc.toLowerCase(network + ':' + client.getNickname());
130
+        final String nickOption2 = sc.toLowerCase("*:" + client.getNickname());
132 131
 
133 132
         if (useowncolour && client.getUser().equals(myself)) {
134 133
             final Colour color = colourManager.getColourFromString(owncolour, null);
@@ -137,22 +136,14 @@ public class NickColourManager {
137 136
             putColour(client, getColour(client.getNickname()));
138 137
         }
139 138
 
140
-        String[] parts = null;
141
-
142
-        if (globalConfig.hasOptionString(domain, nickOption1)) {
143
-            parts = getParts(globalConfig, domain, nickOption1);
144
-        } else if (globalConfig.hasOptionString(domain, nickOption2)) {
145
-            parts = getParts(globalConfig, domain, nickOption2);
139
+        Color color = null;
140
+        if (nickColours.containsKey(nickOption1)) {
141
+            color = nickColours.get(nickOption1);
142
+        } else if (nickColours.containsKey(nickOption2)) {
143
+            color = nickColours.get(nickOption2);
146 144
         }
147
-
148
-        if (parts != null) {
149
-            Colour textColor = null;
150
-
151
-            if (parts[0] != null) {
152
-                textColor = colourManager.getColourFromString(parts[0], null);
153
-            }
154
-
155
-            putColour(client, textColor);
145
+        if (color != null) {
146
+            putColour(client, new Colour(color.getRed(), color.getGreen(), color.getBlue()));
156 147
         }
157 148
     }
158 149
 
@@ -185,69 +176,13 @@ public class NickColourManager {
185 176
         return colourManager.getColourFromString(randColours[count], null);
186 177
     }
187 178
 
188
-    /**
189
-     * Reads the nick colour data from the config.
190
-     *
191
-     * @param config Config to read settings from
192
-     * @param domain Config domain
193
-     *
194
-     * @return A multi-dimensional array of nick colour info.
195
-     */
196
-    public static Object[][] getData(final ReadOnlyConfigProvider config, final String domain) {
197
-        final Collection<Object[]> data = new ArrayList<>();
198
-
199
-        config.getOptions(domain).keySet().stream().filter(key -> key.startsWith("color:"))
200
-                .forEach(key -> {
201
-                    final String network = key.substring(6, key.indexOf(':', 6));
202
-                    final String user = key.substring(1 + key.indexOf(':', 6));
203
-                    final String[] parts = getParts(config, domain, key);
204
-
205
-                    data.add(new Object[]{network, user, parts[0], parts[1]});
206
-                });
207
-
208
-        final Object[][] res = new Object[data.size()][4];
209
-
210
-        int i = 0;
211
-        for (Object[] row : data) {
212
-            res[i] = row;
213
-
214
-            i++;
215
-        }
216
-
217
-        return res;
218
-    }
219
-
220
-    /**
221
-     * Retrieves the config option with the specified key, and returns an array of the colours that
222
-     * should be used for it.
223
-     *
224
-     * @param config Config to read settings from
225
-     * @param domain Config domain
226
-     * @param key    The config key to look up
227
-     *
228
-     * @return The colours specified by the given key
229
-     */
230
-    private static String[] getParts(final ReadOnlyConfigProvider config, final String domain,
231
-            final String key) {
232
-        String[] parts = config.getOption(domain, key).split(":");
233
-
234
-        if (parts.length == 0) {
235
-            parts = new String[]{null, null};
236
-        } else if (parts.length == 1) {
237
-            parts = new String[]{parts[0], null};
238
-        } else if (parts.length == 2) {
239
-            parts = new String[]{parts[0], parts[1]};
240
-        }
241
-
242
-        return parts;
243
-    }
244
-
245 179
     /**
246 180
      * Loads this plugin.
247 181
      */
248 182
     public void onLoad() {
249 183
         eventBus.subscribe(this);
250 184
         configBinder.bind(this, NickColourManager.class);
185
+        nickColours = nickColourYamlStore.readNickColourEntries(path.resolve("nickcolours.yml"));
251 186
     }
252 187
 
253 188
     /**
@@ -256,6 +191,11 @@ public class NickColourManager {
256 191
     public void onUnload() {
257 192
         eventBus.unsubscribe(this);
258 193
         configBinder.unbind(this);
194
+        saveNickColourStore(nickColours);
195
+    }
196
+
197
+    public void saveNickColourStore(final Map<String, Color> savingNickColours) {
198
+        nickColourYamlStore.writeNickColourEntries(path.resolve("nickcolours.yml"), savingNickColours);
259 199
     }
260 200
 
261 201
     @ConfigBinding(key = "useowncolour", invocation = EDTInvocation.class)
@@ -280,29 +220,26 @@ public class NickColourManager {
280 220
 
281 221
     @Handler
282 222
     public void handlePrefsOpened(final ClientPrefsOpenedEvent event) {
283
-        final PreferencesCategory general = new PluginPreferencesCategory(
284
-                pluginInfo, "Nick Colours",
285
-                "General configuration for NickColour plugin.");
286
-        final PreferencesCategory colours = new PluginPreferencesCategory(
287
-                pluginInfo, "Colours",
223
+        final PreferencesCategory general =
224
+                new PluginPreferencesCategory(pluginInfo, "Nick Colours",
225
+                        "General configuration for NickColour plugin.");
226
+        final PreferencesCategory colours = new PluginPreferencesCategory(pluginInfo, "Colours",
288 227
                 "Set colours for specific nicknames.", UIUtilities.invokeAndWait(
289
-                () -> new NickColourPanel(mainWindowProvider.get(), iconManager, colourManager,
290
-                        event.getModel().getIdentity(), event.getModel().getConfigManager(),
291
-                        pluginInfo.getDomain()
292
-                )));
293
-
294
-        general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
295
-                pluginInfo.getDomain(), "userandomcolour", "Use random colour",
296
-                "Use a pseudo-random colour for each person?",
297
-                event.getModel().getConfigManager(), event.getModel().getIdentity()));
298
-        general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
299
-                pluginInfo.getDomain(), "useowncolour", "Use colour for own nick",
300
-                "Always use the same colour for our own nickname?",
301
-                event.getModel().getConfigManager(), event.getModel().getIdentity()));
228
+                () -> new NickColourPanel(mainWindowProvider.get(), iconManager,
229
+                        colourManager, this, nickColours)));
230
+
231
+        general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, pluginInfo.getDomain(),
232
+                        "userandomcolour", "Use random colour",
233
+                        "Use a pseudo-random colour for each person?",
234
+                        event.getModel().getConfigManager(), event.getModel().getIdentity()));
235
+        general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN, pluginInfo.getDomain(),
236
+                        "useowncolour", "Use colour for own nick",
237
+                        "Always use the same colour for our own nickname?",
238
+                        event.getModel().getConfigManager(), event.getModel().getIdentity()));
302 239
         general.addSetting(new PreferencesSetting(PreferencesType.COLOUR, pluginInfo.getDomain(),
303
-                "owncolour", "Colour to use for own nick",
304
-                "Colour used for our own nickname, if above setting is enabled.",
305
-                event.getModel().getConfigManager(), event.getModel().getIdentity()));
240
+                        "owncolour", "Colour to use for own nick",
241
+                        "Colour used for our own nickname, if above setting is enabled.",
242
+                        event.getModel().getConfigManager(), event.getModel().getIdentity()));
306 243
 
307 244
         general.addSubCategory(colours);
308 245
         event.getModel().getCategory("Plugins").addSubCategory(general);

+ 35
- 99
nickcolours/src/com/dmdirc/addons/nickcolours/NickColourPanel.java Näytä tiedosto

@@ -22,18 +22,18 @@
22 22
 
23 23
 package com.dmdirc.addons.nickcolours;
24 24
 
25
-import com.dmdirc.config.prefs.PreferencesInterface;
26
-import com.dmdirc.interfaces.config.AggregateConfigProvider;
27
-import com.dmdirc.interfaces.config.ConfigProvider;
25
+import com.dmdirc.addons.ui_swing.components.GenericTableModel;
28 26
 import com.dmdirc.addons.ui_swing.components.IconManager;
27
+import com.dmdirc.config.prefs.PreferencesInterface;
29 28
 import com.dmdirc.ui.messages.ColourManager;
29
+import com.dmdirc.util.colours.Colour;
30 30
 
31 31
 import java.awt.Color;
32 32
 import java.awt.Window;
33 33
 import java.awt.event.ActionEvent;
34 34
 import java.awt.event.ActionListener;
35
-import java.util.ArrayList;
36
-import java.util.List;
35
+import java.util.HashMap;
36
+import java.util.Map;
37 37
 
38 38
 import javax.swing.JButton;
39 39
 import javax.swing.JPanel;
@@ -41,8 +41,6 @@ import javax.swing.JScrollPane;
41 41
 import javax.swing.JTable;
42 42
 import javax.swing.event.ListSelectionEvent;
43 43
 import javax.swing.event.ListSelectionListener;
44
-import javax.swing.table.DefaultTableModel;
45
-import javax.swing.table.TableCellRenderer;
46 44
 
47 45
 import net.miginfocom.swing.MigLayout;
48 46
 
@@ -54,13 +52,8 @@ public class NickColourPanel extends JPanel implements ActionListener,
54 52
 
55 53
     /** A version number for this class. */
56 54
     private static final long serialVersionUID = 1;
57
-    /** The table headings. */
58
-    private static final String[] HEADERS
59
-            = {"Network", "Nickname", "Text colour", "Nicklist colour"};
60 55
     /** The table used for displaying the options. */
61 56
     private final JTable table;
62
-    /** The identity to write settings to. */
63
-    private final ConfigProvider configIdentity;
64 57
     /** Edit button. */
65 58
     private final JButton editButton;
66 59
     /** Delete button. */
@@ -71,10 +64,8 @@ public class NickColourPanel extends JPanel implements ActionListener,
71 64
     private final IconManager iconManager;
72 65
     /** Colour manage to use to parse colours. */
73 66
     private final ColourManager colourManager;
74
-    /** Config provider to read settings from. */
75
-    private final AggregateConfigProvider config;
76
-    /** The plugin's config domain. */
77
-    private final String domain;
67
+    private final NickColourManager manager;
68
+    private final GenericTableModel<NickColourEntry> model;
78 69
 
79 70
     /**
80 71
      * Creates a new instance of NickColourPanel.
@@ -82,51 +73,25 @@ public class NickColourPanel extends JPanel implements ActionListener,
82 73
      * @param parentWindow  Parent window that will own any new dialogs.
83 74
      * @param iconManager   Icon manager to load icons from.
84 75
      * @param colourManager The colour manager to use to parse colours.
85
-     * @param userSettings  The provider to write user settings to.
86
-     * @param config        The config provider to read settings from.
87
-     * @param domain        The plugin's config domain
76
+     * @param nickColours
88 77
      */
89
-    public NickColourPanel(
90
-            final Window parentWindow, final IconManager iconManager,
91
-            final ColourManager colourManager,
92
-            final ConfigProvider userSettings,
93
-            final AggregateConfigProvider config, final String domain) {
78
+    public NickColourPanel(final Window parentWindow, final IconManager iconManager,
79
+            final ColourManager colourManager, final NickColourManager manager,
80
+            Map<String, Color> nickColours) {
94 81
         this.parentWindow = parentWindow;
95 82
         this.iconManager = iconManager;
96 83
         this.colourManager = colourManager;
97
-        this.configIdentity = userSettings;
98
-        this.config = config;
99
-        this.domain = domain;
100
-
101
-        final Object[][] data = NickColourManager.getData(config, domain);
102
-
103
-        table = new JTable(new DefaultTableModel(data, HEADERS)) {
104
-            /** A version number for this class. */
105
-            private static final long serialVersionUID = 1;
106
-            /** The colour renderer we're using for colour cells. */
107
-            private final ColourRenderer colourRenderer = new ColourRenderer(colourManager);
108
-
109
-            @Override
110
-            public TableCellRenderer getCellRenderer(final int row,
111
-                    final int column) {
112
-                if (column == 2 || column == 3) {
113
-                    return colourRenderer;
114
-                } else {
115
-                    return super.getCellRenderer(row, column);
116
-                }
117
-            }
118
-
119
-            @Override
120
-            public boolean isCellEditable(final int row, final int column) {
121
-                return false;
122
-            }
123
-        };
124
-
84
+        this.manager = manager;
85
+        model = new GenericTableModel<>(NickColourEntry.class,
86
+                "getUser", "getNetwork", "getColor");
87
+        nickColours.forEach((description, colour) -> model.addValue(NickColourEntry
88
+                .create(description.split(":")[0], description.split(":")[1], colour)));
89
+        model.setHeaderNames("User", "Network", "Colour");
90
+        table = new JTable(model);
91
+        table.setDefaultRenderer(Color.class, new ColourRenderer());
125 92
         final JScrollPane scrollPane = new JScrollPane(table);
126
-
127 93
         table.getSelectionModel().addListSelectionListener(this);
128 94
         table.setFillsViewportHeight(true);
129
-        table.setDefaultRenderer(Color.class, new ColourRenderer(colourManager));
130 95
 
131 96
         setLayout(new MigLayout("ins 0, fillx, hmax 500"));
132 97
         add(scrollPane, "grow, push, wrap, spanx");
@@ -155,23 +120,16 @@ public class NickColourPanel extends JPanel implements ActionListener,
155 120
                 new NickColourInputDialog(parentWindow, colourManager, iconManager, this);
156 121
                 break;
157 122
             case "Edit":
158
-                final DefaultTableModel model = ((DefaultTableModel) table.getModel());
159
-                final String network = (String) model.getValueAt(row, 0);
160
-                final String nickname = (String) model.getValueAt(row, 1);
161
-                String textcolour = (String) model.getValueAt(row, 2);
162
-                String nickcolour = (String) model.getValueAt(row, 3);
163
-                if (textcolour == null) {
164
-                    textcolour = "";
165
-                }
166
-                if (nickcolour == null) {
167
-                    nickcolour = "";
168
-                }
123
+                final NickColourEntry entry = model.getValue(row);
124
+                final String network = entry.getNetwork();
125
+                final String nickname = entry.getUser();
126
+                final Color textcolour = entry.getColor();
169 127
                 new NickColourInputDialog(parentWindow, colourManager, iconManager, this,
170
-                        row, nickname, network, textcolour, nickcolour);
128
+                        row, nickname, network, textcolour);
171 129
                 break;
172 130
             case "Delete":
173 131
                 if (row > -1) {
174
-                    ((DefaultTableModel) table.getModel()).removeRow(row);
132
+                    model.removeValue(model.getValue(row));
175 133
                 }
176 134
                 break;
177 135
         }
@@ -183,7 +141,7 @@ public class NickColourPanel extends JPanel implements ActionListener,
183 141
      * @param row The row to be removed
184 142
      */
185 143
     public void removeRow(final int row) {
186
-        ((DefaultTableModel) table.getModel()).removeRow(row);
144
+        model.removeValue(model.getValue(row));
187 145
     }
188 146
 
189 147
     /**
@@ -192,44 +150,22 @@ public class NickColourPanel extends JPanel implements ActionListener,
192 150
      * @param network    The network setting
193 151
      * @param nickname   The nickname setting
194 152
      * @param textcolour The textpane colour setting
195
-     * @param nickcolour The nick list colour setting
196 153
      */
197 154
     public void addRow(final String network, final String nickname,
198
-            final String textcolour, final String nickcolour) {
199
-        final DefaultTableModel model = ((DefaultTableModel) table.getModel());
200
-        model.addRow(new Object[]{network, nickname, textcolour, nickcolour});
201
-    }
202
-
203
-    /**
204
-     * Retrieves the current data in use by this panel.
205
-     *
206
-     * @return This panel's current data.
207
-     */
208
-    private List<Object[]> getData() {
209
-        final List<Object[]> res = new ArrayList<>();
210
-        final DefaultTableModel model = ((DefaultTableModel) table.getModel());
211
-
212
-        @SuppressWarnings("unchecked")
213
-        final List<List<?>> rows = (List<List<?>>) model.getDataVector();
214
-        for (List<?> row : rows) {
215
-            res.add(new Object[]{row.get(0), row.get(1), row.get(2), row.get(3)});
216
-        }
217
-
218
-        return res;
155
+            final String textcolour) {
156
+        final Colour colour = colourManager.getColourFromString(textcolour, null);
157
+        model.addValue(NickColourEntry.create(network, nickname, new Color(colour.getRed(),
158
+                colour.getGreen(), colour.getBlue())));
219 159
     }
220 160
 
221 161
     @Override
222 162
     public void save() {
223
-        // Remove all old config entries
224
-        for (Object[] parts : NickColourManager.getData(config, domain)) {
225
-            configIdentity.unsetOption(domain, "color:" + parts[0] + ":" + parts[1]);
226
-        }
227
-
228
-        // And write the new ones
229
-        for (Object[] row : getData()) {
230
-            configIdentity.
231
-                    setOption(domain, "color:" + row[0] + ":" + row[1], row[2] + ":" + row[3]);
163
+        final Map<String, Color> values = new HashMap<>();
164
+        for (int i =0; i < model.getRowCount(); i++) {
165
+            final NickColourEntry entry = model.getValue(i);
166
+            values.put(entry.getNetwork()+ ':' +entry.getUser(), entry.getColor());
232 167
         }
168
+        manager.saveNickColourStore(values);
233 169
     }
234 170
 
235 171
     @Override

+ 106
- 0
nickcolours/src/com/dmdirc/addons/nickcolours/NickColourYamlStore.java Näytä tiedosto

@@ -0,0 +1,106 @@
1
+/*
2
+ * Copyright (c) 2006-2015 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.addons.nickcolours;
24
+
25
+import com.dmdirc.ClientModule.GlobalConfig;
26
+import com.dmdirc.ui.messages.ColourManager;
27
+import com.dmdirc.util.BaseYamlStore;
28
+import com.dmdirc.util.colours.Colour;
29
+import com.dmdirc.util.colours.ColourUtils;
30
+
31
+import java.awt.Color;
32
+import java.nio.file.Path;
33
+import java.util.ArrayList;
34
+import java.util.Collection;
35
+import java.util.HashMap;
36
+import java.util.Map;
37
+import java.util.Optional;
38
+
39
+import javax.inject.Inject;
40
+import javax.inject.Singleton;
41
+
42
+import org.slf4j.Logger;
43
+import org.slf4j.LoggerFactory;
44
+
45
+import static com.dmdirc.util.YamlReaderUtils.asMap;
46
+import static com.dmdirc.util.YamlReaderUtils.requiredString;
47
+
48
+@Singleton
49
+public class NickColourYamlStore extends BaseYamlStore<NickColourEntry> {
50
+
51
+    private static final Logger LOG = LoggerFactory.getLogger(NickColourYamlStore.class);
52
+    private final ColourManager colourManager;
53
+
54
+    @Inject
55
+    public NickColourYamlStore(@GlobalConfig final ColourManager colourManager) {
56
+        this.colourManager = colourManager;
57
+    }
58
+
59
+    public Map<String, Color> readNickColourEntries(final Path path) {
60
+        final Map<String, Color> nickColours = new HashMap<>();
61
+        final Collection<NickColourEntry> nickColourEntries = read(path);
62
+        nickColourEntries.stream().forEach(
63
+                e -> nickColours.put(e.getNetwork() + ':' + e.getUser(), e.getColor())
64
+        );
65
+        return nickColours;
66
+    }
67
+
68
+    public void writeNickColourEntries(final Path path, final Map<String, Color> nickColours) {
69
+        final Collection<NickColourEntry> nickColourEntries = new ArrayList<>();
70
+        nickColours.forEach((description, colour) -> nickColourEntries.add(NickColourEntry
71
+                .create(description.split(":")[0], description.split(":")[1], colour)));
72
+        write(path, nickColourEntries);
73
+    }
74
+
75
+    @Override
76
+    protected Optional<NickColourEntry> convertFromYaml(final Object object) {
77
+        try {
78
+            final Map<Object, Object> map = asMap(object);
79
+            final String network = requiredString(map, "network");
80
+            final String user = requiredString(map, "user");
81
+            final String colour = requiredString(map, "colour");
82
+            return Optional.of(NickColourEntry.create(network, user, getColourFromString(colour)));
83
+        } catch (IllegalArgumentException ex) {
84
+            LOG.info("Unable to read profile", ex);
85
+            return Optional.empty();
86
+        }
87
+    }
88
+
89
+    @Override
90
+    protected Object convertToYaml(final NickColourEntry object) {
91
+        final Map<Object, Object> map = new HashMap<>();
92
+        map.put("network", object.getNetwork());
93
+        map.put("user", object.getUser());
94
+        map.put("colour", getStringFromColor(object.getColor()));
95
+        return map;
96
+    }
97
+
98
+    private Color getColourFromString(final String value) {
99
+        final Colour colour = colourManager.getColourFromString(value, null);
100
+        return new Color(colour.getRed(), colour.getGreen(), colour.getBlue());
101
+    }
102
+
103
+    private String getStringFromColor(final Color color) {
104
+        return ColourUtils.getHex(new Colour(color.getRed(), color.getGreen(), color.getBlue()));
105
+    }
106
+}

Loading…
Peruuta
Tallenna