KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > gui > ListPanel


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.gui;
22
23 import java.awt.*;
24 import java.awt.event.*;
25 import java.util.List JavaDoc;
26 import java.util.*;
27
28 import javax.swing.*;
29 import javax.swing.event.*;
30
31 /**
32  * This <code>Jpanel</code> allows the user to move and remove entries in a
33  * list and between lists. Extensions of this class should add buttons to add
34  * and possibly edit entries, and to set and get the resulting list.
35  *
36  * @author Eric Lafortune
37  */

38 abstract class ListPanel extends JPanel
39 {
40     protected DefaultListModel listModel = new DefaultListModel();
41     protected JList list = new JList(listModel);
42
43     protected int firstSelectionButton = 2;
44
45
46     protected ListPanel()
47     {
48         GridBagLayout layout = new GridBagLayout();
49         setLayout(layout);
50
51         GridBagConstraints listConstraints = new GridBagConstraints();
52         listConstraints.gridheight = GridBagConstraints.REMAINDER;
53         listConstraints.fill = GridBagConstraints.BOTH;
54         listConstraints.weightx = 1.0;
55         listConstraints.weighty = 1.0;
56         listConstraints.anchor = GridBagConstraints.NORTHWEST;
57         listConstraints.insets = new Insets(0, 2, 0, 2);
58
59         // Make sure some buttons are disabled or enabled depending on whether
60
// the selection is empty or not.
61
list.addListSelectionListener(new ListSelectionListener()
62         {
63             public void valueChanged(ListSelectionEvent e)
64             {
65                 enableSelectionButtons();
66             }
67         });
68
69         add(new JScrollPane(list), listConstraints);
70
71         // something like the following calls are up to the extending class:
72
//addAddButton();
73
//addEditButton();
74
//addRemoveButton();
75
//addUpButton();
76
//addDownButton();
77
//
78
//enableSelectionButtons();
79
}
80
81
82     protected void addRemoveButton()
83     {
84         JButton removeButton = new JButton(GUIResources.getMessage("remove"));
85         removeButton.addActionListener(new ActionListener()
86         {
87             public void actionPerformed(ActionEvent e)
88             {
89                 // Remove the selected elements.
90
removeElementsAt(list.getSelectedIndices());
91             }
92         });
93
94         addButton(removeButton);
95     }
96
97
98     protected void addUpButton()
99     {
100         JButton upButton = new JButton(GUIResources.getMessage("moveUp"));
101         upButton.addActionListener(new ActionListener()
102         {
103             public void actionPerformed(ActionEvent e)
104             {
105                 int[] selectedIndices = list.getSelectedIndices();
106                 if (selectedIndices.length > 0 &&
107                     selectedIndices[0] > 0)
108                 {
109                     // Move the selected elements up.
110
moveElementsAt(selectedIndices, -1);
111                 }
112             }
113         });
114
115         addButton(upButton);
116     }
117
118
119     protected void addDownButton()
120     {
121         JButton downButton = new JButton(GUIResources.getMessage("moveDown"));
122         downButton.addActionListener(new ActionListener()
123         {
124             public void actionPerformed(ActionEvent e)
125             {
126                 int[] selectedIndices = list.getSelectedIndices();
127                 if (selectedIndices.length > 0 &&
128                     selectedIndices[selectedIndices.length-1] < listModel.getSize()-1)
129                 {
130                     // Move the selected elements down.
131
moveElementsAt(selectedIndices, 1);
132                 }
133             }
134         });
135
136         addButton(downButton);
137     }
138
139
140     /**
141      * Adds a button that allows to copy or move entries to another ListPanel.
142      *
143      * @param buttonText the button text.
144      * @param panel the other ListPanel.
145      */

146     public void addCopyToPanelButton(String JavaDoc buttonText,
147                                      final ListPanel panel)
148     {
149         JButton moveButton = new JButton(buttonText);
150         moveButton.addActionListener(new ActionListener()
151         {
152             public void actionPerformed(ActionEvent e)
153             {
154                 int[] selectedIndices = list.getSelectedIndices();
155                 Object JavaDoc[] selectedElements = list.getSelectedValues();
156
157                 // Remove the selected elements from this panel.
158
removeElementsAt(selectedIndices);
159
160                 // Add the elements to the other panel.
161
panel.addElements(selectedElements);
162             }
163         });
164
165         addButton(moveButton);
166     }
167
168
169     protected void addButton(JButton button)
170     {
171         GridBagConstraints buttonConstraints = new GridBagConstraints();
172         buttonConstraints.gridwidth = GridBagConstraints.REMAINDER;
173         buttonConstraints.fill = GridBagConstraints.HORIZONTAL;
174         buttonConstraints.anchor = GridBagConstraints.NORTHWEST;
175         buttonConstraints.insets = new Insets(0, 2, 0, 2);
176
177         add(button, buttonConstraints);
178     }
179
180
181     /**
182      * Returns a list of all right-hand side buttons.
183      */

184     public List JavaDoc getButtons()
185     {
186         List JavaDoc list = new ArrayList(getComponentCount()-1);
187
188         // Add all buttons.
189
for (int index = 1; index < getComponentCount(); index++)
190         {
191             list.add(getComponent(index));
192         }
193
194         return list;
195     }
196
197
198     protected void addElement(Object JavaDoc element)
199     {
200         listModel.addElement(element);
201
202         // Make sure it is selected.
203
list.setSelectedIndex(listModel.size() - 1);
204     }
205
206
207     protected void addElements(Object JavaDoc[] elements)
208     {
209         // Add the elements one by one.
210
for (int index = 0; index < elements.length; index++)
211         {
212             listModel.addElement(elements[index]);
213         }
214
215         // Make sure they are selected.
216
int[] selectedIndices = new int[elements.length];
217         for (int index = 0; index < selectedIndices.length; index++)
218         {
219             selectedIndices[index] =
220                 listModel.size() - selectedIndices.length + index;
221         }
222         list.setSelectedIndices(selectedIndices);
223     }
224
225
226     protected void moveElementsAt(int[] indices, int offset)
227     {
228         // Remember the selected elements.
229
Object JavaDoc[] selectedElements = list.getSelectedValues();
230
231         // Remove the selected elements.
232
removeElementsAt(indices);
233
234         // Update the element indices.
235
for (int index = 0; index < indices.length; index++)
236         {
237             indices[index] += offset;
238         }
239
240         // Reinsert the selected elements.
241
insertElementsAt(selectedElements, indices);
242     }
243
244
245     protected void insertElementsAt(Object JavaDoc[] elements, int[] indices)
246     {
247         for (int index = 0; index < elements.length; index++)
248         {
249             listModel.insertElementAt(elements[index], indices[index]);
250         }
251
252         // Make sure they are selected.
253
list.setSelectedIndices(indices);
254     }
255
256
257     protected void setElementAt(Object JavaDoc element, int index)
258     {
259         listModel.setElementAt(element, index);
260
261         // Make sure it is selected.
262
list.setSelectedIndex(index);
263     }
264
265
266     protected void setElementsAt(Object JavaDoc[] elements, int[] indices)
267     {
268         for (int index = 0; index < elements.length; index++)
269         {
270             listModel.setElementAt(elements[index], indices[index]);
271         }
272
273         // Make sure they are selected.
274
list.setSelectedIndices(indices);
275     }
276
277
278     protected void removeElementsAt(int[] indices)
279     {
280         for (int index = indices.length - 1; index >= 0; index--)
281         {
282             listModel.removeElementAt(indices[index]);
283         }
284
285         // Make sure nothing is selected.
286
list.clearSelection();
287
288         // Make sure the selection buttons are properly enabled,
289
// since the above method doesn't seem to notify the listener.
290
enableSelectionButtons();
291     }
292
293
294     protected void removeAllElements()
295     {
296         listModel.removeAllElements();
297
298         // Make sure the selection buttons are properly enabled,
299
// since the above method doesn't seem to notify the listener.
300
enableSelectionButtons();
301     }
302
303
304     /**
305      * Enables or disables the buttons that depend on a selection.
306      */

307     protected void enableSelectionButtons()
308     {
309         boolean selected = !list.isSelectionEmpty();
310
311         // Loop over all components, except the list itself and the Add button.
312
for (int index = firstSelectionButton; index < getComponentCount(); index++)
313         {
314             getComponent(index).setEnabled(selected);
315         }
316     }
317 }
318
Popular Tags