X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=tim%2Fprune%2Fgui%2FCombinedListAndModel.java;fp=tim%2Fprune%2Fgui%2FCombinedListAndModel.java;h=4b94cebdcba23796c098696e935fe21d0465ef16;hp=0000000000000000000000000000000000000000;hb=92dad5df664287acb51728e9ea599f150765d34a;hpb=81843c3d8d0771bf00d0f26034a13aa515465c78 diff --git a/tim/prune/gui/CombinedListAndModel.java b/tim/prune/gui/CombinedListAndModel.java new file mode 100644 index 0000000..4b94ceb --- /dev/null +++ b/tim/prune/gui/CombinedListAndModel.java @@ -0,0 +1,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 +{ + private DefaultListModel _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(); + 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); + } + } +}