|
@@ -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
|
+}
|