]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/CombinedListAndModel.java
Version 19, May 2018
[GpsPrune.git] / tim / prune / gui / CombinedListAndModel.java
1 package tim.prune.gui;
2
3 import javax.swing.DefaultListModel;
4 import javax.swing.JList;
5 import javax.swing.ListSelectionModel;
6
7 import tim.prune.I18nManager;
8
9
10 /**
11  * Listbox class which also contains its own string model.
12  * Also has the ability to limit its size and show a single
13  * text instead of a huge list
14  */
15 public class CombinedListAndModel extends JList<String>
16 {
17         private DefaultListModel<String> _model = null;
18         private final int _key;
19         private int _maxNumEntries = 0;
20         private boolean _tooManyEntries = false;
21         private boolean _unlimited = false;
22
23
24         /**
25          * Constructor
26          */
27         public CombinedListAndModel(int inKey)
28         {
29                 setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
30                 _model = new DefaultListModel<String>();
31                 setModel(_model);
32                 _key = inKey;
33         }
34
35         /**
36          * @param inMaxNum maximum number of entries to allow
37          */
38         public void setMaxNumEntries(int inMaxNum)
39         {
40                 _maxNumEntries = inMaxNum;
41         }
42
43         /**
44          * @param inUnlimited true if list is temporarily unlimited
45          */
46         public void setUnlimited(boolean inUnlimited)
47         {
48                 _unlimited = inUnlimited;
49         }
50
51         /**
52          * @return key
53          */
54         public int getKey()
55         {
56                 return _key;
57         }
58
59         /**
60          * @param inItem String to add to the list
61          */
62         public void addItem(String inItem)
63         {
64                 if (!_tooManyEntries)
65                 {
66                         _model.addElement(inItem);
67                         if (_maxNumEntries > 0 && !_unlimited
68                                 && _model.getSize() > _maxNumEntries)
69                         {
70                                 _tooManyEntries = true;
71                                 _model.clear();
72                                 _model.addElement(I18nManager.getText("dialog.settimezone.list.toomany"));
73                         }
74                 }
75         }
76
77         /**
78          * @return the selected String, or null
79          */
80         public String getSelectedItem()
81         {
82                 final int selectedIndex = getSelectedIndex();
83                 if (_tooManyEntries || selectedIndex < 0)
84                 {
85                         return null;
86                 }
87                 return _model.getElementAt(selectedIndex);
88         }
89
90         /**
91          * Clear the list
92          */
93         public void clear()
94         {
95                 _model.clear();
96                 _tooManyEntries = false;
97                 _unlimited = false;
98         }
99
100         /**
101          * @param inItem item to select
102          */
103         public void selectItem(String inItem)
104         {
105                 if (!_tooManyEntries && inItem != null)
106                 {
107                         this.setSelectedValue(inItem, true);
108                 }
109         }
110 }