Explorar el Código

Switch ReorderableJList to use GenericListModel.

pull/331/head
Greg Holmes hace 9 años
padre
commit
227d85abbd

+ 2
- 11
notifications/src/com/dmdirc/addons/notifications/NotificationConfig.java Ver fichero

@@ -27,7 +27,6 @@ import com.dmdirc.addons.ui_swing.components.reorderablelist.ReorderableJList;
27 27
 import com.dmdirc.config.prefs.PreferencesInterface;
28 28
 import com.dmdirc.interfaces.config.ConfigProvider;
29 29
 
30
-import java.util.Enumeration;
31 30
 import java.util.LinkedList;
32 31
 import java.util.List;
33 32
 
@@ -85,7 +84,7 @@ public class NotificationConfig extends JPanel implements PreferencesInterface {
85 84
         list = new ReorderableJList<>();
86 85
 
87 86
         for (String method : methods) {
88
-            list.getModel().addElement(method);
87
+            list.getModel().add(method);
89 88
         }
90 89
 
91 90
         setLayout(new MigLayout("fillx, ins 0"));
@@ -117,15 +116,7 @@ public class NotificationConfig extends JPanel implements PreferencesInterface {
117 116
      * @return An ordered list of methods
118 117
      */
119 118
     public List<String> getMethods() {
120
-        final List<String> newMethods = new LinkedList<>();
121
-
122
-        final Enumeration<?> values = list.getModel().elements();
123
-
124
-        while (values.hasMoreElements()) {
125
-            newMethods.add((String) values.nextElement());
126
-        }
127
-
128
-        return newMethods;
119
+        return new LinkedList<>(list.getModel().elements());
129 120
     }
130 121
 
131 122
     @Override

+ 2
- 11
nowplaying/src/com/dmdirc/addons/nowplaying/ConfigPanel.java Ver fichero

@@ -33,7 +33,6 @@ import com.dmdirc.interfaces.config.ConfigProvider;
33 33
 import java.awt.event.KeyEvent;
34 34
 import java.awt.event.KeyListener;
35 35
 import java.util.Arrays;
36
-import java.util.Enumeration;
37 36
 import java.util.LinkedList;
38 37
 import java.util.List;
39 38
 import java.util.Timer;
@@ -112,7 +111,7 @@ public class ConfigPanel extends JPanel implements PreferencesInterface, KeyList
112 111
         list = new ReorderableJList<>();
113 112
 
114 113
         for (String source : sources) {
115
-            list.getModel().addElement(source);
114
+            list.getModel().add(source);
116 115
         }
117 116
 
118 117
         textfield = new JTextField(globalConfig.getOption(domain, "format"));
@@ -178,15 +177,7 @@ public class ConfigPanel extends JPanel implements PreferencesInterface, KeyList
178 177
      * @return An ordered list of sources
179 178
      */
180 179
     public List<String> getSources() {
181
-        final List<String> newSources = new LinkedList<>();
182
-
183
-        final Enumeration<String> values = list.getModel().elements();
184
-
185
-        while (values.hasMoreElements()) {
186
-            newSources.add(values.nextElement());
187
-        }
188
-
189
-        return newSources;
180
+        return new LinkedList<>(list.getModel().elements());
190 181
     }
191 182
 
192 183
     @Override

+ 10
- 1
ui_swing/src/com/dmdirc/addons/ui_swing/components/GenericListModel.java Ver fichero

@@ -54,7 +54,7 @@ public class GenericListModel<T> extends AbstractListModel<T> {
54 54
      *
55 55
      * @param list Data to be included in the model
56 56
      */
57
-    public GenericListModel(final List<T> list) {
57
+    public GenericListModel(final Collection<T> list) {
58 58
 
59 59
         this.list = Collections.synchronizedList(new ArrayList<>(list));
60 60
     }
@@ -221,6 +221,15 @@ public class GenericListModel<T> extends AbstractListModel<T> {
221 221
         fireIntervalRemoved(this, start, end);
222 222
     }
223 223
 
224
+    /**
225
+     * Returns a list of items in this model
226
+     *
227
+     * @return List available in this model
228
+     */
229
+    public List<T> elements() {
230
+        return Collections.unmodifiableList(list);
231
+    }
232
+
224 233
     /**
225 234
      * Adds all the objects in the specified collection to this list.
226 235
      *

+ 4
- 3
ui_swing/src/com/dmdirc/addons/ui_swing/components/reorderablelist/ListReorderButtonPanel.java Ver fichero

@@ -22,10 +22,11 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.components.reorderablelist;
24 24
 
25
+import com.dmdirc.addons.ui_swing.components.GenericListModel;
26
+
25 27
 import java.awt.event.ActionEvent;
26 28
 import java.awt.event.ActionListener;
27 29
 
28
-import javax.swing.DefaultListModel;
29 30
 import javax.swing.JButton;
30 31
 import javax.swing.JList;
31 32
 import javax.swing.JPanel;
@@ -47,7 +48,7 @@ public class ListReorderButtonPanel<T> extends JPanel implements
47 48
     /** List to reorder. */
48 49
     private final JList<T> list;
49 50
     /** Default list model. */
50
-    private final DefaultListModel<T> model;
51
+    private final GenericListModel<T> model;
51 52
     /** Up button. */
52 53
     private final JButton up;
53 54
     /** Down button. */
@@ -129,7 +130,7 @@ public class ListReorderButtonPanel<T> extends JPanel implements
129 130
      * @param newIndex Current index
130 131
      */
131 132
     private void moveElement(final T object, final int newIndex) {
132
-        model.removeElement(object);
133
+        model.remove(object);
133 134
         model.add(newIndex, object);
134 135
         list.setSelectedIndex(newIndex);
135 136
     }

+ 9
- 7
ui_swing/src/com/dmdirc/addons/ui_swing/components/reorderablelist/ReorderableJList.java Ver fichero

@@ -22,6 +22,8 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_swing.components.reorderablelist;
24 24
 
25
+import com.dmdirc.addons.ui_swing.components.GenericListModel;
26
+
25 27
 import java.awt.Cursor;
26 28
 import java.awt.Point;
27 29
 import java.awt.Rectangle;
@@ -75,7 +77,7 @@ public class ReorderableJList<T> extends JList<T> implements DragSourceListener,
75 77
 
76 78
     /** Instantiate new ReorderableJList. */
77 79
     public ReorderableJList() {
78
-        this(new DefaultListModel<>());
80
+        this(new GenericListModel<>());
79 81
     }
80 82
 
81 83
     /**
@@ -83,7 +85,7 @@ public class ReorderableJList<T> extends JList<T> implements DragSourceListener,
83 85
      *
84 86
      * @param model Model
85 87
      */
86
-    public ReorderableJList(final DefaultListModel<T> model) {
88
+    public ReorderableJList(final GenericListModel<T> model) {
87 89
         super(model);
88 90
 
89 91
         setCellRenderer(new ReorderableJListCellRenderer<>(this));
@@ -104,8 +106,8 @@ public class ReorderableJList<T> extends JList<T> implements DragSourceListener,
104 106
     }
105 107
 
106 108
     @Override
107
-    public DefaultListModel<T> getModel() {
108
-        return (DefaultListModel<T>) super.getModel();
109
+    public GenericListModel<T> getModel() {
110
+        return (GenericListModel<T>) super.getModel();
109 111
     }
110 112
 
111 113
     @Override
@@ -113,7 +115,7 @@ public class ReorderableJList<T> extends JList<T> implements DragSourceListener,
113 115
         if (model instanceof DefaultListModel) {
114 116
             super.setModel(model);
115 117
         } else {
116
-            throw new IllegalArgumentException("model needs to be an instance of DefaultListModel");
118
+            throw new IllegalArgumentException("model needs to be an instance of GenericListModel");
117 119
         }
118 120
     }
119 121
 
@@ -242,10 +244,10 @@ public class ReorderableJList<T> extends JList<T> implements DragSourceListener,
242 244
 
243 245
         //move items
244 246
         final boolean sourceBeforeTarget = draggedIndex < index;
245
-        final DefaultListModel<T> mod = getModel();
247
+        final GenericListModel<T> mod = getModel();
246 248
         final int newIndex = sourceBeforeTarget ? index - 1 : index;
247 249
         mod.remove(draggedIndex);
248
-        for (Object item : (ArrayList<?>) dragged) {
250
+        for (Object item : (Iterable<?>) dragged) {
249 251
             @SuppressWarnings("unchecked")
250 252
             final T genericItem = (T) item;
251 253
             mod.add(newIndex, genericItem);

Loading…
Cancelar
Guardar