PageRenderTime 34ms CodeModel.GetById 29ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/CommonControls/common/gui/ListPanel.java

#
Java | 95 lines | 72 code | 17 blank | 6 comment | 5 complexity | 00c476d292292bb8782e9bfacf477614 MD5 | raw file
 1package common.gui;
 2
 3import java.awt.*;
 4import java.awt.event.*;
 5import javax.swing.*;
 6
 7import common.gui.util.*;
 8
 9/**
10 * A simple list in a scroll pane, which can be reorderable or sorted.
11 *
12 * @author    mace
13 * @version   $Revision: 1347 $ modified $Date: 2004-02-09 03:49:01 +0100 (Mon, 09 Feb 2004) $ by $Author: bemace $
14 */
15public class ListPanel extends JPanel {
16	private JLabel label;
17	private JList list;
18	private JScrollPane scrollpane;
19	private boolean reorderable = true;
20	private boolean sorted = false;
21
22	public ListPanel(String title) {
23		setLayout(new GridBagLayout());
24		label = new JLabel(title);
25		list = new JList(new DefaultListModel());
26		list.setBorder(BorderFactory.createLoweredBevelBorder());
27		scrollpane = new JScrollPane(list);
28		ConstraintFactory cf = new ConstraintFactory();
29		add(label, cf.buildConstraints(0, 0, 10, 1, cf.W, cf.H, 100, 0));
30		add(scrollpane, cf.buildConstraints(0, 1, 10, 10, cf.N, cf.BOTH));
31		if (reorderable) {
32		}
33	}
34
35	public ListPanel(String title, Object[] items) {
36		this(title);
37		for (int i = 0; i < items.length; i++) {
38			addElement(items[i]);
39		}
40	}
41
42	public void clear() {
43		((DefaultListModel) list.getModel()).clear();
44	}
45
46	public void addKeyListener(KeyListener kl) {
47		super.addKeyListener(kl);
48		list.addKeyListener(kl);
49	}
50
51	public void addElement(Object element) {
52		((DefaultListModel) list.getModel()).addElement(element);
53	}
54
55	public boolean removeElement(Object element) {
56		return ((DefaultListModel) list.getModel()).removeElement(element);
57	}
58
59	public void setLabel(String label) {
60		this.label.setText(label);
61	}
62
63	public void setSorted(boolean b) {
64		sorted = b;
65		if (sorted) {
66			reorderable = false;
67		}
68	}
69
70	public void setReorderable(boolean b) {
71		reorderable = b;
72	}
73
74	public void setSelectedIndex(int i) {
75		list.setSelectedIndex(i);
76	}
77
78	public int getLastSelectedIndex() {
79		int[] selected = list.getSelectedIndices();
80		if (selected.length > 0) {
81			return selected[selected.length - 1];
82		} else {
83			return -1;
84		}
85	}
86
87	public Object[] getSelectedValues() {
88		return list.getSelectedValues();
89	}
90
91	public Object[] toArray() {
92		return ((DefaultListModel) list.getModel()).toArray();
93	}
94}
95