Browse Source

Tidy the nick colours plugin some more.

pull/308/head
Greg Holmes 9 years ago
parent
commit
da83e44353

+ 23
- 27
nickcolours/src/com/dmdirc/addons/nickcolours/NickColourInputDialog.java View File

22
 
22
 
23
 package com.dmdirc.addons.nickcolours;
23
 package com.dmdirc.addons.nickcolours;
24
 
24
 
25
+import com.dmdirc.addons.ui_swing.components.GenericTableModel;
25
 import com.dmdirc.addons.ui_swing.components.IconManager;
26
 import com.dmdirc.addons.ui_swing.components.IconManager;
26
 import com.dmdirc.addons.ui_swing.components.colours.ColourChooser;
27
 import com.dmdirc.addons.ui_swing.components.colours.ColourChooser;
27
 import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
28
 import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
29
 
30
 
30
 import java.awt.Color;
31
 import java.awt.Color;
31
 import java.awt.Window;
32
 import java.awt.Window;
32
-import java.awt.event.ActionEvent;
33
-import java.awt.event.ActionListener;
34
 
33
 
35
 import javax.swing.JButton;
34
 import javax.swing.JButton;
36
 import javax.swing.JLabel;
35
 import javax.swing.JLabel;
42
 /**
41
 /**
43
  * New nick colour input dialog.
42
  * New nick colour input dialog.
44
  */
43
  */
45
-public class NickColourInputDialog extends StandardDialog implements ActionListener {
44
+public class NickColourInputDialog extends StandardDialog {
46
 
45
 
47
     /** A version number for this class. */
46
     /** A version number for this class. */
48
     private static final long serialVersionUID = 1;
47
     private static final long serialVersionUID = 1;
50
     private boolean isnew;
49
     private boolean isnew;
51
     /** The row we're editing, if this isn't a new entry. */
50
     /** The row we're editing, if this isn't a new entry. */
52
     private final int row;
51
     private final int row;
53
-    /** The NickColourPanel we're reporting to. */
54
-    private final NickColourPanel panel;
52
+    private final ColourManager colourManager;
53
+    /** The table model to modify entries in. */
54
+    private final GenericTableModel<NickColourEntry> model;
55
     /** nickname textfield. */
55
     /** nickname textfield. */
56
     private JTextField nickname;
56
     private JTextField nickname;
57
     /** network textfield. */
57
     /** network textfield. */
65
      * @param parentWindow  The window that owns this dialog.
65
      * @param parentWindow  The window that owns this dialog.
66
      * @param colourManager The colour manager to use to retrieve colours.
66
      * @param colourManager The colour manager to use to retrieve colours.
67
      * @param iconManager   The icon manager to use for the dialog icon.
67
      * @param iconManager   The icon manager to use for the dialog icon.
68
-     * @param panel         The panel that's opening this dialog
68
+     * @param model         The table model to modify entries in
69
      * @param row           The row of the table we're editing
69
      * @param row           The row of the table we're editing
70
      * @param nickname      The nickname that's currently set
70
      * @param nickname      The nickname that's currently set
71
      * @param network       The network that's currently set
71
      * @param network       The network that's currently set
75
             final Window parentWindow,
75
             final Window parentWindow,
76
             final ColourManager colourManager,
76
             final ColourManager colourManager,
77
             final IconManager iconManager,
77
             final IconManager iconManager,
78
-            final NickColourPanel panel, final int row,
78
+            final GenericTableModel<NickColourEntry> model, final int row,
79
             final String nickname, final String network,
79
             final String nickname, final String network,
80
             final Color textcolour) {
80
             final Color textcolour) {
81
         super(parentWindow, ModalityType.MODELESS);
81
         super(parentWindow, ModalityType.MODELESS);
82
+        this.colourManager = colourManager;
82
 
83
 
83
-        this.panel = panel;
84
+        this.model = model;
84
         this.row = row;
85
         this.row = row;
85
 
86
 
86
         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
87
         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
99
      * @param parentWindow  The window that owns this dialog.
100
      * @param parentWindow  The window that owns this dialog.
100
      * @param colourManager The colour manager to use to retrieve colours.
101
      * @param colourManager The colour manager to use to retrieve colours.
101
      * @param iconManager   The icon manager to use for the dialog icon.
102
      * @param iconManager   The icon manager to use for the dialog icon.
102
-     * @param panel         The panel that's opening this dialog
103
+     * @param model         The table model to modify entries in
103
      */
104
      */
104
     public NickColourInputDialog(
105
     public NickColourInputDialog(
105
             final Window parentWindow,
106
             final Window parentWindow,
106
             final ColourManager colourManager,
107
             final ColourManager colourManager,
107
             final IconManager iconManager,
108
             final IconManager iconManager,
108
-            final NickColourPanel panel) {
109
-        this(parentWindow, colourManager, iconManager, panel, -1, "", "", null);
109
+            final GenericTableModel<NickColourEntry> model) {
110
+        this(parentWindow, colourManager, iconManager, model, -1, "", "", null);
110
 
111
 
111
         isnew = true;
112
         isnew = true;
112
     }
113
     }
132
 
133
 
133
     /** Initialises the listeners. */
134
     /** Initialises the listeners. */
134
     private void initListeners() {
135
     private void initListeners() {
135
-        getOkButton().addActionListener(this);
136
-        getCancelButton().addActionListener(this);
136
+        getOkButton().addActionListener(e -> saveSettings());
137
+        getCancelButton().addActionListener(e -> dispose());
137
     }
138
     }
138
 
139
 
139
     /** Lays out the components. */
140
     /** Lays out the components. */
155
         pack();
156
         pack();
156
     }
157
     }
157
 
158
 
158
-    @Override
159
-    public void actionPerformed(final ActionEvent e) {
160
-        if (e.getSource() == getOkButton()) {
161
-            saveSettings();
162
-        }
163
-        dispose();
164
-    }
165
-
166
     /** Saves settings. */
159
     /** Saves settings. */
167
     public void saveSettings() {
160
     public void saveSettings() {
168
-        if (!isnew) {
169
-            panel.removeRow(row);
170
-        }
171
-
172
-        panel.addRow(network.getText().toLowerCase(),
173
-                nickname.getText().toLowerCase(),
161
+        final Color colour = NickColourUtils.getColourFromString(colourManager,
174
                 textColour.getColour());
162
                 textColour.getColour());
163
+        final NickColourEntry entry = NickColourEntry.create(network.getText().toLowerCase(),
164
+                nickname.getText().toLowerCase(),
165
+                new Color(colour.getRed(), colour.getGreen(), colour.getBlue()));
166
+        if (isnew) {
167
+            model.replaceValueAt(entry, row);
168
+        } else {
169
+            model.addValue(entry);
170
+        }
175
     }
171
     }
176
 
172
 
177
 }
173
 }

+ 1
- 1
nickcolours/src/com/dmdirc/addons/nickcolours/NickColourManager.java View File

143
             color = nickColours.get(nickOption2);
143
             color = nickColours.get(nickOption2);
144
         }
144
         }
145
         if (color != null) {
145
         if (color != null) {
146
-            putColour(client, new Colour(color.getRed(), color.getGreen(), color.getBlue()));
146
+            putColour(client, NickColourUtils.getColourfromColor(color));
147
         }
147
         }
148
     }
148
     }
149
 
149
 

+ 3
- 3
nickcolours/src/com/dmdirc/addons/nickcolours/NickColourPanel.java View File

77
      */
77
      */
78
     public NickColourPanel(final Window parentWindow, final IconManager iconManager,
78
     public NickColourPanel(final Window parentWindow, final IconManager iconManager,
79
             final ColourManager colourManager, final NickColourManager manager,
79
             final ColourManager colourManager, final NickColourManager manager,
80
-            Map<String, Color> nickColours) {
80
+            final Map<String, Color> nickColours) {
81
         this.parentWindow = parentWindow;
81
         this.parentWindow = parentWindow;
82
         this.iconManager = iconManager;
82
         this.iconManager = iconManager;
83
         this.colourManager = colourManager;
83
         this.colourManager = colourManager;
117
         final int row = table.getSelectedRow();
117
         final int row = table.getSelectedRow();
118
         switch (e.getActionCommand()) {
118
         switch (e.getActionCommand()) {
119
             case "Add":
119
             case "Add":
120
-                new NickColourInputDialog(parentWindow, colourManager, iconManager, this);
120
+                new NickColourInputDialog(parentWindow, colourManager, iconManager, model);
121
                 break;
121
                 break;
122
             case "Edit":
122
             case "Edit":
123
                 final NickColourEntry entry = model.getValue(row);
123
                 final NickColourEntry entry = model.getValue(row);
124
                 final String network = entry.getNetwork();
124
                 final String network = entry.getNetwork();
125
                 final String nickname = entry.getUser();
125
                 final String nickname = entry.getUser();
126
                 final Color textcolour = entry.getColor();
126
                 final Color textcolour = entry.getColor();
127
-                new NickColourInputDialog(parentWindow, colourManager, iconManager, this,
127
+                new NickColourInputDialog(parentWindow, colourManager, iconManager, model,
128
                         row, nickname, network, textcolour);
128
                         row, nickname, network, textcolour);
129
                 break;
129
                 break;
130
             case "Delete":
130
             case "Delete":

+ 49
- 0
nickcolours/src/com/dmdirc/addons/nickcolours/NickColourUtils.java View File

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.ui.messages.ColourManager;
26
+import com.dmdirc.util.colours.Colour;
27
+import com.dmdirc.util.colours.ColourUtils;
28
+
29
+import java.awt.Color;
30
+
31
+final class NickColourUtils {
32
+
33
+    private NickColourUtils() {
34
+        //Utility class
35
+    }
36
+
37
+    static Color getColourFromString(final ColourManager colourManager, final String value) {
38
+        final Colour colour = colourManager.getColourFromString(value, null);
39
+        return new Color(colour.getRed(), colour.getGreen(), colour.getBlue());
40
+    }
41
+
42
+    static String getStringFromColor(final Color color) {
43
+        return ColourUtils.getHex(new Colour(color.getRed(), color.getGreen(), color.getBlue()));
44
+    }
45
+
46
+    static Colour getColourfromColor(final Color color) {
47
+        return new Colour(color.getRed(), color.getGreen(), color.getBlue());
48
+    }
49
+}

+ 3
- 13
nickcolours/src/com/dmdirc/addons/nickcolours/NickColourYamlStore.java View File

25
 import com.dmdirc.ClientModule.GlobalConfig;
25
 import com.dmdirc.ClientModule.GlobalConfig;
26
 import com.dmdirc.ui.messages.ColourManager;
26
 import com.dmdirc.ui.messages.ColourManager;
27
 import com.dmdirc.util.BaseYamlStore;
27
 import com.dmdirc.util.BaseYamlStore;
28
-import com.dmdirc.util.colours.Colour;
29
-import com.dmdirc.util.colours.ColourUtils;
30
 
28
 
31
 import java.awt.Color;
29
 import java.awt.Color;
32
 import java.nio.file.Path;
30
 import java.nio.file.Path;
79
             final String network = requiredString(map, "network");
77
             final String network = requiredString(map, "network");
80
             final String user = requiredString(map, "user");
78
             final String user = requiredString(map, "user");
81
             final String colour = requiredString(map, "colour");
79
             final String colour = requiredString(map, "colour");
82
-            return Optional.of(NickColourEntry.create(network, user, getColourFromString(colour)));
80
+            return Optional.of(NickColourEntry.create(network, user,
81
+                    NickColourUtils.getColourFromString(colourManager, colour)));
83
         } catch (IllegalArgumentException ex) {
82
         } catch (IllegalArgumentException ex) {
84
             LOG.info("Unable to read profile", ex);
83
             LOG.info("Unable to read profile", ex);
85
             return Optional.empty();
84
             return Optional.empty();
91
         final Map<Object, Object> map = new HashMap<>();
90
         final Map<Object, Object> map = new HashMap<>();
92
         map.put("network", object.getNetwork());
91
         map.put("network", object.getNetwork());
93
         map.put("user", object.getUser());
92
         map.put("user", object.getUser());
94
-        map.put("colour", getStringFromColor(object.getColor()));
93
+        map.put("colour", NickColourUtils.getStringFromColor(object.getColor()));
95
         return map;
94
         return map;
96
     }
95
     }
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
 }
96
 }

Loading…
Cancel
Save