File: CombinedListAndModel.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (110 lines) | stat: -rw-r--r-- 2,074 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package tim.prune.gui;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListSelectionModel;

import tim.prune.I18nManager;


/**
 * Listbox class which also contains its own string model.
 * Also has the ability to limit its size and show a single
 * text instead of a huge list
 */
public class CombinedListAndModel extends JList<String>
{
	private DefaultListModel<String> _model = null;
	private final int _key;
	private int _maxNumEntries = 0;
	private boolean _tooManyEntries = false;
	private boolean _unlimited = false;


	/**
	 * Constructor
	 */
	public CombinedListAndModel(int inKey)
	{
		setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		_model = new DefaultListModel<String>();
		setModel(_model);
		_key = inKey;
	}

	/**
	 * @param inMaxNum maximum number of entries to allow
	 */
	public void setMaxNumEntries(int inMaxNum)
	{
		_maxNumEntries = inMaxNum;
	}

	/**
	 * @param inUnlimited true if list is temporarily unlimited
	 */
	public void setUnlimited(boolean inUnlimited)
	{
		_unlimited = inUnlimited;
	}

	/**
	 * @return key
	 */
	public int getKey()
	{
		return _key;
	}

	/**
	 * @param inItem String to add to the list
	 */
	public void addItem(String inItem)
	{
		if (!_tooManyEntries)
		{
			_model.addElement(inItem);
			if (_maxNumEntries > 0 && !_unlimited
				&& _model.getSize() > _maxNumEntries)
			{
				_tooManyEntries = true;
				_model.clear();
				_model.addElement(I18nManager.getText("dialog.settimezone.list.toomany"));
			}
		}
	}

	/**
	 * @return the selected String, or null
	 */
	public String getSelectedItem()
	{
		final int selectedIndex = getSelectedIndex();
		if (_tooManyEntries || selectedIndex < 0)
		{
			return null;
		}
		return _model.getElementAt(selectedIndex);
	}

	/**
	 * Clear the list
	 */
	public void clear()
	{
		_model.clear();
		_tooManyEntries = false;
		_unlimited = false;
	}

	/**
	 * @param inItem item to select
	 */
	public void selectItem(String inItem)
	{
		if (!_tooManyEntries && inItem != null)
		{
			this.setSelectedValue(inItem, true);
		}
	}
}