1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.component;
21
22 import java.awt.BorderLayout;
23 import java.awt.Insets;
24 import java.awt.event.ActionEvent;
25
26 import javax.swing.AbstractAction;
27 import javax.swing.Action;
28 import javax.swing.Box;
29 import javax.swing.DefaultListModel;
30 import javax.swing.JButton;
31 import javax.swing.JList;
32 import javax.swing.JPanel;
33 import javax.swing.JScrollPane;
34 import javax.swing.event.ListSelectionEvent;
35 import javax.swing.event.ListSelectionListener;
36
37 import org.xnap.XNap;
38 import org.xnap.gui.util.IconHelper;
39
40 /***
41 * This class provides a {@link JList} of Strings and a add and remove
42 * button.
43 */
44 public abstract class AbstractListPanel extends JPanel
45 implements ListSelectionListener {
46
47
48
49 private JList jlItems;
50 private DefaultListModel dlmItems;
51 private Action acAdd = new AddAction();
52 private Action acRemove = new RemoveAction();
53 private Box bxInput;
54
55
56
57 public AbstractListPanel(int visibleRows)
58 {
59 setLayout(new BorderLayout());
60
61 dlmItems = new DefaultListModel();
62 jlItems = new JList(dlmItems);
63 jlItems.addListSelectionListener(this);
64 jlItems.setVisibleRowCount(visibleRows);
65 JScrollPane jsp = new JScrollPane(jlItems);
66 add(jsp, BorderLayout.CENTER);
67
68 bxInput = Box.createHorizontalBox();
69 add(bxInput, BorderLayout.NORTH);
70
71 JButton jbAdd = new XNapButton(acAdd);
72 jbAdd.setMargin(new Insets(1, 1, 1, 1));
73 bxInput.add(jbAdd);
74
75 JButton jbRemove = new XNapButton(acRemove);
76 jbRemove.setMargin(new Insets(1, 1, 1, 1));
77 bxInput.add(jbRemove);
78 }
79
80
81
82 /***
83 * Invoked when the add action is pressed.
84 */
85 protected abstract Object getInput();
86
87 /***
88 * Inovked when an item is selected from the list.
89 */
90 protected abstract void setInput(Object item);
91
92 /***
93 * Adds the current input to the list.
94 */
95 public void addInput()
96 {
97 Object item = getInput();
98 if (item != null) {
99 if (!dlmItems.contains(item)) {
100 dlmItems.addElement(item);
101 jlItems.ensureIndexIsVisible(dlmItems.getSize() - 1);
102 }
103 setInput(null);
104 }
105 }
106
107 /***
108 * Adds items to the list.
109 */
110 public void addItems(Object[] items)
111 {
112 for (int i = 0; i < items.length; i++) {
113 dlmItems.addElement(items[i]);
114 }
115 }
116
117 /***
118 * Returns the add Action object.
119 */
120 protected Action getAddAction()
121 {
122 return acAdd;
123 }
124
125 /***
126 * Returns the box that is used to display the buttons.
127 */
128 public Box getInputBox()
129 {
130 return bxInput;
131 }
132
133 /***
134 * Returns the list items.
135 */
136 public Object[] getItems()
137 {
138 return dlmItems.toArray();
139 }
140
141 /***
142 * Returns the list items as strings.
143 */
144 public String[] getStringItems()
145 {
146 String[] array = new String[dlmItems.size()];
147 System.arraycopy(dlmItems.toArray(), 0, array, 0, array.length);
148 return array;
149 }
150
151 /***
152 * Returns the remove Action object.
153 */
154 protected Action getRemoveAction()
155 {
156 return acRemove;
157 }
158
159 /***
160 * Invoked when an item from the list is selected. Invokes {@link
161 * #setInput(Object)}.
162 */
163 public void valueChanged(ListSelectionEvent e)
164 {
165 if (jlItems.getSelectedValue() != null) {
166 setInput(jlItems.getSelectedValue());
167 }
168 }
169
170
171
172 /***
173 * Provides an action that adds the current input as an item to
174 * the list.
175 */
176 private class AddAction extends AbstractAction {
177
178 public AddAction()
179 {
180 putValue(IconHelper.XNAP_ICON, "edit_add.png");
181 putValue(Action.SHORT_DESCRIPTION, XNap.tr("Add item."));
182 }
183
184 public void actionPerformed(ActionEvent event)
185 {
186 addInput();
187 }
188
189 }
190
191 /***
192 * Provides an action that removes the currently selected item
193 * from the list.
194 */
195 private class RemoveAction extends AbstractAction {
196
197 public RemoveAction()
198 {
199 putValue(IconHelper.XNAP_ICON, "edit_remove.png");
200 putValue(Action.SHORT_DESCRIPTION, "Remove selected item.");
201 }
202
203 public void actionPerformed(ActionEvent event)
204 {
205 Object items[] = jlItems.getSelectedValues();
206 for (int i = 0; i < items.length; i++) {
207 dlmItems.removeElement(items[i]);
208 }
209 }
210
211 }
212
213 }