Selaa lähdekoodia

Remove URLHandlerTableModel.

pull/348/head
Greg Holmes 9 vuotta sitten
vanhempi
commit
9ec82a82da

+ 11
- 0
ui_swing/src/com/dmdirc/addons/ui_swing/components/GenericTableModel.java Näytä tiedosto

@@ -24,6 +24,8 @@ package com.dmdirc.addons.ui_swing.components;
24 24
 
25 25
 import java.lang.reflect.Method;
26 26
 import java.util.ArrayList;
27
+import java.util.Collection;
28
+import java.util.Collections;
27 29
 import java.util.List;
28 30
 import java.util.Optional;
29 31
 
@@ -240,6 +242,15 @@ public class GenericTableModel<T> extends AbstractTableModel {
240 242
         fireTableRowsInserted(index, index);
241 243
     }
242 244
 
245
+    /**
246
+     * Returns a collection of all the elements in this model.
247
+     *
248
+     * @return All elements in the model
249
+     */
250
+    public Collection<T> elements() {
251
+        return Collections.unmodifiableCollection(values);
252
+    }
253
+
243 254
     @SuppressWarnings("unchecked")
244 255
     private T castObjectToType(final Object value) {
245 256
         return (T) value;

+ 26
- 27
ui_swing/src/com/dmdirc/addons/ui_swing/dialogs/prefs/URLConfigPanel.java Näytä tiedosto

@@ -24,6 +24,8 @@ package com.dmdirc.addons.ui_swing.dialogs.prefs;
24 24
 
25 25
 import com.dmdirc.ClientModule.GlobalConfig;
26 26
 import com.dmdirc.ClientModule.UserConfig;
27
+import com.dmdirc.addons.ui_swing.components.GenericTableModel;
28
+import com.dmdirc.addons.ui_swing.components.IconManager;
27 29
 import com.dmdirc.addons.ui_swing.components.PackingTable;
28 30
 import com.dmdirc.addons.ui_swing.components.URLProtocolPanel;
29 31
 import com.dmdirc.addons.ui_swing.dialogs.StandardInputDialog;
@@ -32,7 +34,6 @@ import com.dmdirc.config.prefs.PreferencesInterface;
32 34
 import com.dmdirc.config.validators.URLProtocolValidator;
33 35
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
34 36
 import com.dmdirc.interfaces.config.ConfigProvider;
35
-import com.dmdirc.addons.ui_swing.components.IconManager;
36 37
 
37 38
 import java.awt.Dialog.ModalityType;
38 39
 import java.awt.Window;
@@ -40,6 +41,7 @@ import java.awt.event.ActionEvent;
40 41
 import java.awt.event.ActionListener;
41 42
 import java.net.URI;
42 43
 import java.net.URISyntaxException;
44
+import java.util.Collection;
43 45
 import java.util.HashMap;
44 46
 import java.util.Map;
45 47
 import java.util.Map.Entry;
@@ -52,7 +54,6 @@ import javax.swing.JScrollPane;
52 54
 import javax.swing.ListSelectionModel;
53 55
 import javax.swing.event.ListSelectionEvent;
54 56
 import javax.swing.event.ListSelectionListener;
55
-import javax.swing.table.TableCellRenderer;
56 57
 
57 58
 import net.miginfocom.swing.MigLayout;
58 59
 
@@ -73,7 +74,7 @@ public class URLConfigPanel extends JPanel implements
73 74
     /** Protocol list. */
74 75
     private PackingTable table;
75 76
     /** Table mode. */
76
-    private URLHandlerTableModel model;
77
+    private GenericTableModel<URLHandlerHolder> model;
77 78
     /** Table scrollpane. */
78 79
     private JScrollPane tableScrollPane;
79 80
     /** Protocol config panel. */
@@ -122,23 +123,10 @@ public class URLConfigPanel extends JPanel implements
122 123
      */
123 124
     private void initComponents() {
124 125
         tableScrollPane = new JScrollPane();
125
-        model = new URLHandlerTableModel(globalConfig);
126
-        table = new PackingTable(model, tableScrollPane) {
127
-            private static final long serialVersionUID = 1;
128
-
129
-            @Override
130
-            public TableCellRenderer getCellRenderer(final int row,
131
-                    final int column) {
132
-                switch (column) {
133
-                    case 0:
134
-                        return new URISchemeCellRenderer();
135
-                    case 1:
136
-                        return new URIHandlerCellRenderer();
137
-                    default:
138
-                        return super.getCellRenderer(row, column);
139
-                }
140
-            }
141
-        };
126
+        model = new GenericTableModel<>(URLHandlerHolder.class, "getUri", "getHandler");
127
+        table = new PackingTable(model, tableScrollPane);
128
+        table.setDefaultRenderer(URISchemeCellRenderer.class, new URISchemeCellRenderer());
129
+        table.setDefaultRenderer(URIHandlerCellRenderer.class, new URIHandlerCellRenderer());
142 130
         table.setAutoCreateRowSorter(true);
143 131
         table.setAutoCreateColumnsFromModel(true);
144 132
         table.setColumnSelectionAllowed(false);
@@ -161,7 +149,7 @@ public class URLConfigPanel extends JPanel implements
161 149
         for (final String option : options) {
162 150
             try {
163 151
                 final URI uri = new URI(option + "://example.test.com");
164
-                model.addURI(uri);
152
+                model.addValue(new URLHandlerHolder(uri, getURLHandler(uri)));
165 153
                 details.put(uri, new URLProtocolPanel(globalConfig, userConfig, uri, true));
166 154
             } catch (final URISyntaxException ex) {
167 155
                 //Ignore wont happen
@@ -169,6 +157,14 @@ public class URLConfigPanel extends JPanel implements
169 157
         }
170 158
     }
171 159
 
160
+    private String getURLHandler(final URI uri) {
161
+        if (globalConfig.hasOptionString("protocol", uri.getScheme())) {
162
+            return globalConfig.getOption("protocol", uri.getScheme());
163
+        } else {
164
+            return "";
165
+        }
166
+    }
167
+
172 168
     /**
173 169
      * Adds listeners.
174 170
      */
@@ -194,7 +190,9 @@ public class URLConfigPanel extends JPanel implements
194 190
     @Override
195 191
     public void save() {
196 192
         valueChanged(null);
197
-        final Map<URI, String> handlers = model.getURLHandlers();
193
+        final Collection<URLHandlerHolder> elements = model.elements();
194
+        final Map<URI, String> handlers = new HashMap<>();
195
+        elements.forEach(u -> handlers.put(u.getUri(), u.getHandler()));
198 196
         final Set<String> protocols = globalConfig.getOptions("protocol").keySet();
199 197
         for (final String protocol : protocols) {
200 198
             URI uri;
@@ -237,7 +235,7 @@ public class URLConfigPanel extends JPanel implements
237 235
             setVisible(false);
238 236
             if (selectedRow != -1 && selectedRow < model.getRowCount()) {
239 237
                 final URLProtocolPanel panel = details.get(model.getValueAt(selectedRow, 0));
240
-                model.setValueAt(panel.getSelection(), selectedRow, 1);
238
+                model.getValue(selectedRow).setHandler(panel.getSelection());
241 239
             }
242 240
             if (table.getSelectedRow() == -1) {
243 241
                 activeComponent = empty;
@@ -246,8 +244,8 @@ public class URLConfigPanel extends JPanel implements
246 244
                 remove.setEnabled(false);
247 245
                 selectedRow = -1;
248 246
             } else {
249
-                activeComponent = details.get(model.getValueAt(table.getRowSorter().
250
-                        convertRowIndexToModel(table.getSelectedRow()), 0));
247
+                activeComponent = details.get(model.getValue(table.getRowSorter().
248
+                        convertRowIndexToModel(table.getSelectedRow())).getUri());
251 249
                 layoutComponents();
252 250
                 add.setEnabled(true);
253 251
                 remove.setEnabled(true);
@@ -267,14 +265,15 @@ public class URLConfigPanel extends JPanel implements
267 265
                     new URLProtocolValidator(globalConfig), this::saveAddNewURLHandler).display();
268 266
 
269 267
         } else if (e.getSource() == remove) {
270
-            model.removeURI(table.getRowSorter().convertRowIndexToModel(table.getSelectedRow()));
268
+            model.removeValue(model.getValue(table.getRowSorter().convertRowIndexToModel(
269
+                    table.getSelectedRow())));
271 270
         }
272 271
     }
273 272
 
274 273
     private boolean saveAddNewURLHandler(final String text) {
275 274
         try {
276 275
             final URI uri = new URI(text + "://example.test.com");
277
-            model.addURI(uri);
276
+            model.addValue(new URLHandlerHolder(uri, getURLHandler(uri)));
278 277
             details.put(uri, new URLProtocolPanel(globalConfig, userConfig, uri, true));
279 278
             return true;
280 279
         } catch (final URISyntaxException ex) {

+ 55
- 0
ui_swing/src/com/dmdirc/addons/ui_swing/dialogs/prefs/URLHandlerHolder.java Näytä tiedosto

@@ -0,0 +1,55 @@
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.ui_swing.dialogs.prefs;
24
+
25
+import java.net.URI;
26
+
27
+/**
28
+ *
29
+ */
30
+public class URLHandlerHolder {
31
+
32
+    private URI uri;
33
+    private String handler;
34
+
35
+    public URLHandlerHolder(final URI uri, final String handler) {
36
+        this.uri = uri;
37
+        this.handler = handler;
38
+    }
39
+
40
+    public URI getUri() {
41
+        return uri;
42
+    }
43
+
44
+    public String getHandler() {
45
+        return handler;
46
+    }
47
+
48
+    public void setUri(URI uri) {
49
+        this.uri = uri;
50
+    }
51
+
52
+    public void setHandler(String handler) {
53
+        this.handler = handler;
54
+    }
55
+}

+ 0
- 215
ui_swing/src/com/dmdirc/addons/ui_swing/dialogs/prefs/URLHandlerTableModel.java Näytä tiedosto

@@ -1,215 +0,0 @@
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.ui_swing.dialogs.prefs;
24
-
25
-import com.dmdirc.interfaces.config.AggregateConfigProvider;
26
-
27
-import java.net.URI;
28
-import java.util.ArrayList;
29
-import java.util.HashMap;
30
-import java.util.List;
31
-import java.util.Map;
32
-
33
-import javax.swing.table.AbstractTableModel;
34
-
35
-/**
36
- * URL Handler table model.
37
- */
38
-public class URLHandlerTableModel extends AbstractTableModel {
39
-
40
-    /** Serial version UID. */
41
-    private static final long serialVersionUID = 3;
42
-    /** Config Manager. */
43
-    private final AggregateConfigProvider configManager;
44
-    /** Data list. */
45
-    private final List<URI> uris;
46
-    /** Handlers list. */
47
-    private final List<String> handlers;
48
-
49
-    /**
50
-     * Instantiates a new table model.
51
-     *
52
-     * @param configManager Config manager
53
-     */
54
-    public URLHandlerTableModel(final AggregateConfigProvider configManager) {
55
-        this(configManager, new ArrayList<>(), new ArrayList<>());
56
-    }
57
-
58
-    /**
59
-     * Instantiates a new table model.
60
-     *
61
-     * @param configManager Config manager
62
-     * @param uris          URIs to show
63
-     * @param handlers      Handlers to show
64
-     */
65
-    public URLHandlerTableModel(final AggregateConfigProvider configManager,
66
-            final List<URI> uris, final List<String> handlers) {
67
-
68
-        this.configManager = configManager;
69
-        this.uris = uris;
70
-        this.handlers = handlers;
71
-    }
72
-
73
-    @Override
74
-    public int getRowCount() {
75
-        return uris.size();
76
-    }
77
-
78
-    @Override
79
-    public int getColumnCount() {
80
-        return 2;
81
-    }
82
-
83
-    @Override
84
-    public String getColumnName(final int columnIndex) {
85
-        switch (columnIndex) {
86
-            case 0:
87
-                return "Protocol";
88
-            case 1:
89
-                return "Handler";
90
-            default:
91
-                throw new IllegalArgumentException("Unknown column: "
92
-                        + columnIndex);
93
-        }
94
-    }
95
-
96
-    @Override
97
-    public Class<?> getColumnClass(final int columnIndex) {
98
-        switch (columnIndex) {
99
-            case 0:
100
-                return URI.class;
101
-            case 1:
102
-                return String.class;
103
-            default:
104
-                throw new IllegalArgumentException("Unknown column: " + columnIndex);
105
-        }
106
-    }
107
-
108
-    @Override
109
-    public Object getValueAt(final int rowIndex, final int columnIndex) {
110
-        if (uris.size() <= rowIndex) {
111
-            throw new IndexOutOfBoundsException(rowIndex + " >= "
112
-                    + uris.size());
113
-        }
114
-        if (rowIndex < 0) {
115
-            throw new IllegalArgumentException(
116
-                    "Must specify a positive integer");
117
-        }
118
-        switch (columnIndex) {
119
-            case 0:
120
-                return uris.get(rowIndex);
121
-            case 1:
122
-                return handlers.get(rowIndex);
123
-            default:
124
-                throw new IllegalArgumentException("Unknown column: "
125
-                        + columnIndex);
126
-        }
127
-    }
128
-
129
-    @Override
130
-    public void setValueAt(final Object aValue, final int rowIndex,
131
-            final int columnIndex) {
132
-        if (uris.size() <= rowIndex) {
133
-            throw new IndexOutOfBoundsException(rowIndex + " >= "
134
-                    + uris.size());
135
-        }
136
-        if (rowIndex < 0) {
137
-            throw new IllegalArgumentException(
138
-                    "Must specify a positive integer");
139
-        }
140
-        switch (columnIndex) {
141
-            case 0:
142
-                if (!(aValue instanceof URI)) {
143
-                    throw new IllegalArgumentException("Value must be a URI");
144
-                }
145
-                uris.set(rowIndex, (URI) aValue);
146
-                break;
147
-            case 1:
148
-                if (!(aValue instanceof String)) {
149
-                    throw new IllegalArgumentException(
150
-                            "Value must be a String");
151
-                }
152
-                handlers.set(rowIndex, (String) aValue);
153
-                break;
154
-            default:
155
-                throw new IllegalArgumentException("Unknown column: "
156
-                        + columnIndex);
157
-        }
158
-        fireTableCellUpdated(rowIndex, columnIndex);
159
-    }
160
-
161
-    /**
162
-     * Adds a URI to the model.
163
-     *
164
-     * @param uri URI to add
165
-     */
166
-    public void addURI(final URI uri) {
167
-        final String handler;
168
-        if (configManager.hasOptionString("protocol", uri.getScheme())) {
169
-            handler = configManager.getOption("protocol", uri.getScheme());
170
-        } else {
171
-            handler = "";
172
-        }
173
-        uris.add(uri);
174
-        handlers.add(handler);
175
-        fireTableRowsInserted(uris.size() - 1, uris.size() - 1);
176
-    }
177
-
178
-    /**
179
-     * Removes a URI to the model.
180
-     *
181
-     * @param uri URI to remove
182
-     */
183
-    public void removeURI(final URI uri) {
184
-        removeURI(uris.indexOf(uri));
185
-    }
186
-
187
-    /**
188
-     * Removes a URI to the model.
189
-     *
190
-     * @param index Index of the URI to remove
191
-     */
192
-    public void removeURI(final int index) {
193
-        if (index != -1) {
194
-            uris.remove(index);
195
-            handlers.remove(index);
196
-            fireTableRowsDeleted(index, index);
197
-        }
198
-    }
199
-
200
-    /**
201
-     * Returns a map of the URL handlers in this model.
202
-     *
203
-     * @return URL Handler map
204
-     */
205
-    public Map<URI, String> getURLHandlers() {
206
-        final Map<URI, String> urlHandlers = new HashMap<>();
207
-
208
-        for (int i = 0; i < uris.size(); i++) {
209
-            urlHandlers.put(uris.get(i), handlers.get(i));
210
-        }
211
-
212
-        return urlHandlers;
213
-    }
214
-
215
-}

Loading…
Peruuta
Tallenna