Browse Source

Adds support for up/down buttons to re-order a reorderable list. Needs images for the buttons?

Fixes issue 3569

Change-Id: Ia1cbe3151f5cc707c8afd97c5ac5a6276283b777
Reviewed-on: http://gerrit.dmdirc.com/718
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.6.3
Gregory Holmes 14 years ago
parent
commit
2d2afa0908

+ 4
- 3
src/com/dmdirc/addons/nowplaying/ConfigPanel.java View File

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc.addons.nowplaying;
24 24
 
25
+import com.dmdirc.addons.ui_swing.components.reorderablelist.ListReorderButtonPanel;
25 26
 import com.dmdirc.config.IdentityManager;
26 27
 import com.dmdirc.config.prefs.PreferencesInterface;
27 28
 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
@@ -118,7 +119,8 @@ public class ConfigPanel extends JPanel implements PreferencesInterface,
118 119
         panel.setLayout(new MigLayout("fillx, ins 5"));
119 120
 
120 121
         panel.add(new JLabel("Drag and drop items to reorder"), "wrap");
121
-        panel.add(new JScrollPane(list), "growx");
122
+        panel.add(new JScrollPane(list), "growx, pushx");
123
+        panel.add(new ListReorderButtonPanel(list), "");
122 124
 
123 125
         add(panel, "growx, wrap");
124 126
 
@@ -136,8 +138,7 @@ public class ConfigPanel extends JPanel implements PreferencesInterface,
136 138
 
137 139
         add(new NowPlayingSubsitutionPanel(Arrays.asList(new String[]{"app",
138 140
                     "title", "artist", "album", "bitrate", "format", "length",
139
-                    "time",
140
-                    "state"})), "growx");
141
+                    "time", "state"})), "growx");
141 142
         schedulePreviewUpdate();
142 143
     }
143 144
 

+ 145
- 0
src/com/dmdirc/addons/ui_swing/components/reorderablelist/ListReorderButtonPanel.java View File

@@ -0,0 +1,145 @@
1
+/*
2
+ * 
3
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
4
+ * 
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ * 
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ * 
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+
24
+package com.dmdirc.addons.ui_swing.components.reorderablelist;
25
+
26
+import java.awt.event.ActionEvent;
27
+import java.awt.event.ActionListener;
28
+
29
+import javax.swing.DefaultListModel;
30
+import javax.swing.JButton;
31
+import javax.swing.JList;
32
+import javax.swing.JPanel;
33
+import javax.swing.event.ListSelectionEvent;
34
+import javax.swing.event.ListSelectionListener;
35
+
36
+import net.miginfocom.swing.MigLayout;
37
+
38
+/**
39
+ * Simple panel to reorder a JList using buttons.
40
+ */
41
+public class ListReorderButtonPanel extends JPanel implements
42
+        ListSelectionListener, ActionListener {
43
+
44
+    /**
45
+     * A version number for this class. It should be changed whenever the class
46
+     * structure is changed (or anything else that would prevent serialized
47
+     * objects being unserialized with the new class).
48
+     */
49
+    private static final long serialVersionUID = 1;
50
+    /** List to reoder. */
51
+    private JList list;
52
+    /** Default list model. */
53
+    private DefaultListModel model;
54
+    /** Up button. */
55
+    private JButton up;
56
+    /** Down button. */
57
+    private JButton down;
58
+
59
+    /**
60
+     * Creates a panel to reoder a jlist.
61
+     * 
62
+     * @param list JList
63
+     */
64
+    public ListReorderButtonPanel(final ReorderableJList list) {
65
+        this.list = list;
66
+        this.model = list.getModel();
67
+
68
+        list.addListSelectionListener(this);
69
+        up = new JButton("/\\");
70
+        down = new JButton("\\/");
71
+
72
+        up.addActionListener(this);
73
+        down.addActionListener(this);
74
+
75
+        setLayout(new MigLayout("fill"));
76
+
77
+        add(up, "wrap");
78
+        add(down, "");
79
+
80
+        valueChanged(null);
81
+    }
82
+
83
+    /** {@inheritDoc} */
84
+    @Override
85
+    public void valueChanged(final ListSelectionEvent e) {
86
+        up.setEnabled(list.getSelectedIndex() != -1);
87
+        down.setEnabled(list.getSelectedIndex() != -1);
88
+    }
89
+
90
+    /**
91
+     * {@inheritDoc}
92
+     * 
93
+     * @param e Action event
94
+     */
95
+    @Override
96
+    public void actionPerformed(final ActionEvent e) {
97
+        final Object object = list.getSelectedValue();
98
+        final int currentIndex = list.getSelectedIndex();
99
+        if (e.getSource() == up) {
100
+            moveUp(object, currentIndex);
101
+        } else {
102
+            moveDown(object, currentIndex);
103
+        }
104
+    }
105
+
106
+    /**
107
+     * Moves an object up in the list.
108
+     *
109
+     * @param object Object to move
110
+     * @param currentIndex Current index
111
+     */
112
+    private void moveUp(final Object object, final int currentIndex) {
113
+        int newIndex = currentIndex - 1;
114
+        if (newIndex < 0) {
115
+            newIndex = model.getSize() - 1;
116
+        }
117
+        moveElement(object, newIndex);
118
+    }
119
+
120
+    /**
121
+     * Moves an object down in the list.
122
+     *
123
+     * @param object Object to move
124
+     * @param currentIndex Current index
125
+     */
126
+    private void moveDown(final Object object, final int currentIndex) {
127
+        int newIndex = currentIndex + 1;
128
+        if (newIndex > model.getSize() - 1) {
129
+            newIndex = 0;
130
+        }
131
+        moveElement(object, newIndex);
132
+    }
133
+
134
+    /**
135
+     * Moves an object from its current positition to a new position in a list.
136
+     *
137
+     * @param object Object to move
138
+     * @param newIndex Current index
139
+     */
140
+    private void moveElement(final Object object, final int newIndex) {
141
+        model.removeElement(object);
142
+        model.add(newIndex, object);
143
+        list.setSelectedIndex(newIndex);
144
+    }
145
+}

Loading…
Cancel
Save