001: /*
002: * ListSelectionPanel.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing;
023:
024: import java.awt.Dimension;
025: import java.awt.GridBagConstraints;
026: import java.awt.GridBagLayout;
027: import java.awt.Insets;
028: import java.util.Vector;
029:
030: import javax.swing.JButton;
031: import javax.swing.JLabel;
032: import javax.swing.JList;
033: import javax.swing.JPanel;
034: import javax.swing.JScrollPane;
035: import javax.swing.UIManager;
036: import org.underworldlabs.swing.actions.ActionUtilities;
037: import org.underworldlabs.swing.util.IconUtilities;
038:
039: /* ----------------------------------------------------------
040: * CVS NOTE: Changes to the CVS repository prior to the
041: * release of version 3.0.0beta1 has meant a
042: * resetting of CVS revision numbers.
043: * ----------------------------------------------------------
044: */
045:
046: /**
047: * List selection panel base.
048: *
049: * @author Takis Diakoumis
050: * @version $Revision: 1.6 $
051: * @date $Date: 2006/06/12 11:21:36 $
052: */
053: public class ListSelectionPanel extends ActionPanel implements
054: ListSelection {
055:
056: /** the available object list */
057: private JList availableList;
058:
059: /** the selected object list */
060: private JList selectedList;
061:
062: /** the selections made collection */
063: private Vector selections;
064:
065: /** the available objects collection */
066: private Vector available;
067:
068: /** label above the available object list */
069: private JLabel availableLabel;
070:
071: /** label above the selected object list */
072: private JLabel selectedLabel;
073:
074: /** <p>Constructs a new instance. */
075: public ListSelectionPanel() {
076: this (null);
077: }
078:
079: public ListSelectionPanel(Vector v) {
080: this ("Available Columns:", "Selected Columns:", v);
081: }
082:
083: public ListSelectionPanel(String availLabel, String selectLabel) {
084: this (availLabel, selectLabel, null);
085: }
086:
087: public ListSelectionPanel(String availLabel, String selectLabel,
088: Vector v) {
089: super (new GridBagLayout());
090: try {
091: jbInit();
092: selections = new Vector();
093: createAvailableList(v);
094: setLabelText(availLabel, selectLabel);
095: } catch (Exception e) {
096: e.printStackTrace();
097: }
098: }
099:
100: /**
101: * Initializes the state of this instance.
102: */
103: private void jbInit() throws Exception {
104: // create the labels
105: availableLabel = new JLabel();
106: selectedLabel = new JLabel();
107:
108: // initialise the buttons
109: JButton selectOneButton = ActionUtilities.createButton(this ,
110: "selectOneAction", IconUtilities
111: .loadDefaultIconResource("SelectOne16.gif",
112: true), "Select one");
113:
114: JButton selectAllButton = ActionUtilities.createButton(this ,
115: "selectAllAction", IconUtilities
116: .loadDefaultIconResource("SelectAll16.gif",
117: true), "Select all");
118:
119: JButton removeOneButton = ActionUtilities.createButton(this ,
120: "removeOneAction", IconUtilities
121: .loadDefaultIconResource("RemoveOne16.gif",
122: true), "Remove one");
123:
124: JButton removeAllButton = ActionUtilities.createButton(this ,
125: "removeAllAction", IconUtilities
126: .loadDefaultIconResource("RemoveAll16.gif",
127: true), "Remove all");
128:
129: // reset the button insets
130: Insets buttonInsets = UIManager.getInsets("Button.margin");
131: if (buttonInsets != null) {
132: selectOneButton.setMargin(buttonInsets);
133: selectAllButton.setMargin(buttonInsets);
134: removeOneButton.setMargin(buttonInsets);
135: removeAllButton.setMargin(buttonInsets);
136: }
137:
138: JButton moveUpButton = ActionUtilities.createButton(this ,
139: "Up16.gif", "Move selection up", "moveSelectionUp");
140:
141: JButton moveDownButton = ActionUtilities.createButton(this ,
142: "Down16.gif", "Move selection down",
143: "moveSelectionDown");
144:
145: // initialise the lists
146: availableList = new JList();
147: selectedList = new JList();
148:
149: // create the list scroll panes
150: JScrollPane availableScrollPane = new JScrollPane(availableList);
151: JScrollPane selectedScrollPane = new JScrollPane(selectedList);
152:
153: Dimension listDim = new Dimension(180, 185);
154: availableScrollPane.setPreferredSize(listDim);
155: selectedScrollPane.setPreferredSize(listDim);
156:
157: GridBagConstraints gbc = new GridBagConstraints();
158:
159: // first column - available list
160: gbc.gridx = 0;
161: gbc.gridy = 0;
162: gbc.anchor = GridBagConstraints.NORTHWEST;
163: gbc.fill = GridBagConstraints.HORIZONTAL;
164: add(availableLabel, gbc);
165: gbc.fill = GridBagConstraints.BOTH;
166: gbc.weightx = 1.0;
167: gbc.weighty = 1.0;
168: gbc.gridy++;
169: gbc.insets.top = 2;
170: add(availableScrollPane, gbc);
171:
172: // second column - selection buttons
173: JPanel buttonPanel = new JPanel(new GridBagLayout());
174: gbc.gridy = 0;
175: gbc.gridx = 0;
176: gbc.weightx = 0;
177: gbc.weighty = 0;
178: gbc.insets.top = 10;
179: gbc.insets.bottom = 5;
180: gbc.fill = GridBagConstraints.NONE;
181: gbc.anchor = GridBagConstraints.CENTER;
182: buttonPanel.add(selectOneButton, gbc);
183: gbc.gridy++;
184: gbc.insets.top = 0;
185: buttonPanel.add(selectAllButton, gbc);
186: gbc.gridy++;
187: buttonPanel.add(removeOneButton, gbc);
188: gbc.gridy++;
189: buttonPanel.add(removeAllButton, gbc);
190:
191: gbc.gridy = 0;
192: gbc.gridx = 1;
193: gbc.insets.left = 5;
194: gbc.fill = GridBagConstraints.BOTH;
195: gbc.gridheight = GridBagConstraints.REMAINDER;
196: add(buttonPanel, gbc);
197:
198: // third column - selected list
199: gbc.gridx = 2;
200: gbc.gridy = 0;
201: gbc.gridheight = 1;
202: gbc.anchor = GridBagConstraints.NORTHWEST;
203: gbc.fill = GridBagConstraints.HORIZONTAL;
204: add(selectedLabel, gbc);
205: gbc.fill = GridBagConstraints.BOTH;
206: gbc.weightx = 1.0;
207: gbc.weighty = 1.0;
208: gbc.gridy++;
209: gbc.insets.top = 2;
210: gbc.insets.bottom = 0;
211: add(selectedScrollPane, gbc);
212:
213: // fourth column - move buttons
214: JPanel buttonMovePanel = new JPanel(new GridBagLayout());
215: gbc.insets.top = 10;
216: gbc.insets.left = 0;
217: gbc.insets.bottom = 5;
218: gbc.gridy = 0;
219: gbc.gridx = 0;
220: gbc.weightx = 0;
221: gbc.weighty = 0;
222: gbc.fill = GridBagConstraints.NONE;
223: gbc.anchor = GridBagConstraints.CENTER;
224: buttonMovePanel.add(moveUpButton, gbc);
225: gbc.gridy++;
226: gbc.insets.top = 0;
227: buttonMovePanel.add(new JLabel("Move"), gbc);
228: gbc.gridy++;
229: buttonMovePanel.add(moveDownButton, gbc);
230:
231: gbc.gridy = 0;
232: gbc.gridx = 3;
233: gbc.insets.left = 5;
234: gbc.fill = GridBagConstraints.BOTH;
235: gbc.gridheight = GridBagConstraints.REMAINDER;
236: add(buttonMovePanel, gbc);
237:
238: }
239:
240: public void setLabelText(String avail, String select) {
241: availableLabel.setText(avail);
242: selectedLabel.setText(select);
243: }
244:
245: public void clear() {
246: if (available != null) {
247: available.clear();
248: availableList.setListData(available);
249: }
250: if (selections != null) {
251: selections.clear();
252: selectedList.setListData(selections);
253: }
254: }
255:
256: public void createAvailableList(String[] values) {
257: available = new Vector(values.length);
258: for (int i = 0; i < values.length; i++) {
259: available.add(values[i]);
260: }
261:
262: availableList.setListData(available);
263: selections.clear();
264: selectedList.setListData(selections);
265: }
266:
267: public void createAvailableList(Vector v) {
268: if (v == null) {
269: return;
270: }
271:
272: available = v;
273: availableList.setListData(available);
274: selections.clear();
275: selectedList.setListData(selections);
276: }
277:
278: public void removeAllAction() {
279: if (selections == null || selections.size() == 0) {
280: return;
281: }
282: for (int i = 0, n = selections.size(); i < n; i++) {
283: available.add(selections.elementAt(i));
284: }
285:
286: availableList.setListData(available);
287: selections.clear();
288: selectedList.setListData(selections);
289: }
290:
291: public void removeOneAction() {
292: if (selectedList.isSelectionEmpty()) {
293: return;
294: }
295:
296: int index = selectedList.getSelectedIndex();
297: Object[] selectedObjects = selectedList.getSelectedValues();
298: for (int i = 0; i < selectedObjects.length; i++) {
299: available.add(selectedObjects[i]);
300: selections.remove(selectedObjects[i]);
301: }
302:
303: selectedList.setListData(selections);
304: availableList.setListData(available);
305: selectedList.setSelectedIndex(index);
306: }
307:
308: public void selectAllAction() {
309: if (available == null) {
310: return;
311: }
312: for (int i = 0, n = available.size(); i < n; i++) {
313: selections.add(available.elementAt(i));
314: }
315: selectedList.setListData(selections);
316: available.clear();
317: availableList.setListData(available);
318: }
319:
320: public void selectOneAction() {
321: if (availableList.isSelectionEmpty()) {
322: return;
323: }
324:
325: Object[] selectedObjects = availableList.getSelectedValues();
326:
327: int index = availableList.getSelectedIndex();
328:
329: for (int i = 0; i < selectedObjects.length; i++) {
330: selections.add(selectedObjects[i]);
331: available.remove(selectedObjects[i]);
332: }
333:
334: availableList.setListData(available);
335: selectedList.setListData(selections);
336: availableList.setSelectedIndex(index);
337: }
338:
339: public Vector getSelectedValues() {
340: return selections;
341: }
342:
343: public boolean hasSelections() {
344: return selections.size() > 0;
345: }
346:
347: public void moveSelectionDown() {
348: if (selectedList.isSelectionEmpty()
349: || selectedList.getSelectedIndex() == selections.size() - 1) {
350: return;
351: }
352:
353: int index = selectedList.getSelectedIndex();
354: Object move = selectedList.getSelectedValue();
355: selections.removeElementAt(index);
356: selections.add(index + 1, move);
357: selectedList.setListData(selections);
358: selectedList.setSelectedIndex(index + 1);
359: }
360:
361: public void moveSelectionUp() {
362: if (selectedList.isSelectionEmpty()
363: || selectedList.getSelectedIndex() == 0) {
364: return;
365: }
366:
367: int index = selectedList.getSelectedIndex();
368: Object move = selectedList.getSelectedValue();
369: selections.removeElementAt(index);
370: selections.add(index - 1, move);
371: selectedList.setListData(selections);
372: selectedList.setSelectedIndex(index - 1);
373: }
374:
375: }
|