Quellcode durchsuchen

Tidy the nick colours plugin some more.

pull/308/head
Greg Holmes vor 9 Jahren
Ursprung
Commit
da83e44353

+ 23
- 27
nickcolours/src/com/dmdirc/addons/nickcolours/NickColourInputDialog.java Datei anzeigen

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.nickcolours;
24 24
 
25
+import com.dmdirc.addons.ui_swing.components.GenericTableModel;
25 26
 import com.dmdirc.addons.ui_swing.components.IconManager;
26 27
 import com.dmdirc.addons.ui_swing.components.colours.ColourChooser;
27 28
 import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
@@ -29,8 +30,6 @@ import com.dmdirc.ui.messages.ColourManager;
29 30
 
30 31
 import java.awt.Color;
31 32
 import java.awt.Window;
32
-import java.awt.event.ActionEvent;
33
-import java.awt.event.ActionListener;
34 33
 
35 34
 import javax.swing.JButton;
36 35
 import javax.swing.JLabel;
@@ -42,7 +41,7 @@ import net.miginfocom.swing.MigLayout;
42 41
 /**
43 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 46
     /** A version number for this class. */
48 47
     private static final long serialVersionUID = 1;
@@ -50,8 +49,9 @@ public class NickColourInputDialog extends StandardDialog implements ActionListe
50 49
     private boolean isnew;
51 50
     /** The row we're editing, if this isn't a new entry. */
52 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 55
     /** nickname textfield. */
56 56
     private JTextField nickname;
57 57
     /** network textfield. */
@@ -65,7 +65,7 @@ public class NickColourInputDialog extends StandardDialog implements ActionListe
65 65
      * @param parentWindow  The window that owns this dialog.
66 66
      * @param colourManager The colour manager to use to retrieve colours.
67 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 69
      * @param row           The row of the table we're editing
70 70
      * @param nickname      The nickname that's currently set
71 71
      * @param network       The network that's currently set
@@ -75,12 +75,13 @@ public class NickColourInputDialog extends StandardDialog implements ActionListe
75 75
             final Window parentWindow,
76 76
             final ColourManager colourManager,
77 77
             final IconManager iconManager,
78
-            final NickColourPanel panel, final int row,
78
+            final GenericTableModel<NickColourEntry> model, final int row,
79 79
             final String nickname, final String network,
80 80
             final Color textcolour) {
81 81
         super(parentWindow, ModalityType.MODELESS);
82
+        this.colourManager = colourManager;
82 83
 
83
-        this.panel = panel;
84
+        this.model = model;
84 85
         this.row = row;
85 86
 
86 87
         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
@@ -99,14 +100,14 @@ public class NickColourInputDialog extends StandardDialog implements ActionListe
99 100
      * @param parentWindow  The window that owns this dialog.
100 101
      * @param colourManager The colour manager to use to retrieve colours.
101 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 105
     public NickColourInputDialog(
105 106
             final Window parentWindow,
106 107
             final ColourManager colourManager,
107 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 112
         isnew = true;
112 113
     }
@@ -132,8 +133,8 @@ public class NickColourInputDialog extends StandardDialog implements ActionListe
132 133
 
133 134
     /** Initialises the listeners. */
134 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 140
     /** Lays out the components. */
@@ -155,23 +156,18 @@ public class NickColourInputDialog extends StandardDialog implements ActionListe
155 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 159
     /** Saves settings. */
167 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 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 Datei anzeigen

@@ -143,7 +143,7 @@ public class NickColourManager {
143 143
             color = nickColours.get(nickOption2);
144 144
         }
145 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 Datei anzeigen

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

+ 49
- 0
nickcolours/src/com/dmdirc/addons/nickcolours/NickColourUtils.java Datei anzeigen

@@ -0,0 +1,49 @@
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 Datei anzeigen

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

Laden…
Abbrechen
Speichern