Class JosmComboBoxModel<E>
- java.lang.Object
-
- javax.swing.AbstractListModel<E>
-
- org.openstreetmap.josm.gui.widgets.JosmComboBoxModel<E>
-
- Type Parameters:
E
- The element type.
- All Implemented Interfaces:
Serializable
,Iterable<E>
,ComboBoxModel<E>
,ListModel<E>
,MutableComboBoxModel<E>
- Direct Known Subclasses:
AbstractListMergeModel.ComparePairListModel
,AutoCompComboBoxModel
,LanguagePreference.LanguageComboBoxModel
public class JosmComboBoxModel<E> extends AbstractListModel<E> implements MutableComboBoxModel<E>, Iterable<E>
A data model for theJosmComboBox
- Since:
- 18221
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
JosmComboBoxModel.Preferences
Loads and saves the model to the JOSM preferences.
-
Field Summary
Fields Modifier and Type Field Description protected ArrayList<E>
elements
the elements shown in the dropdownprivate int
maxSize
The maximum number of elements to hold, -1 for no limit.protected Object
selected
the selected element in the dropdown or null-
Fields inherited from class javax.swing.AbstractListModel
listenerList
-
-
Constructor Summary
Constructors Constructor Description JosmComboBoxModel()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAllElements(Collection<E> elems)
Adds all elements from the collection.void
addAllElements(Collection<String> strings, Function<String,E> buildE)
Adds all elements from the collection of string representations.void
addElement(E element)
Adds an element to the end of the model.E
addTopElement(E newElement)
Adds an element to the top of the list.Collection<E>
asCollection()
Returns a copy of the element list.protected void
doAddElement(E element)
E
find(String s)
Finds the item that matches string.E
getElementAt(int index)
int
getIndexOf(E element)
Returns the index of the specified element Note: This is not part of theComboBoxModel
interface but is defined inDefaultComboBoxModel
.Object
getSelectedItem()
int
getSize()
void
insertElementAt(E element, int index)
Adds an element at a specific index.Iterator<E>
iterator()
JosmComboBoxModel.Preferences
prefs(Function<String,E> readE, Function<E,String> writeE)
Gets a preference loader and saver.void
removeAllElements()
Empties the list.void
removeElement(Object elem)
void
removeElementAt(int index)
void
setSelectedItem(Object elem)
Set the value of the selected item.void
setSize(int size)
Sets the maximum number of elements.-
Methods inherited from class javax.swing.AbstractListModel
addListDataListener, fireContentsChanged, fireIntervalAdded, fireIntervalRemoved, getListDataListeners, getListeners, removeListDataListener
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Methods inherited from interface javax.swing.ListModel
addListDataListener, removeListDataListener
-
-
-
-
Constructor Detail
-
JosmComboBoxModel
public JosmComboBoxModel()
-
-
Method Detail
-
setSize
public void setSize(int size)
Sets the maximum number of elements.- Parameters:
size
- The maximal number of elements in the model.
-
asCollection
public Collection<E> asCollection()
Returns a copy of the element list.- Returns:
- a copy of the data
-
getIndexOf
public int getIndexOf(E element)
Returns the index of the specified element Note: This is not part of theComboBoxModel
interface but is defined inDefaultComboBoxModel
.- Parameters:
element
- the element to get the index of- Returns:
- the index of the first occurrence of the specified element in this model, or -1 if this model does not contain the element
-
doAddElement
protected void doAddElement(E element)
-
addElement
public void addElement(E element)
Adds an element to the end of the model. Does nothing if max size is already reached.- Specified by:
addElement
in interfaceMutableComboBoxModel<E>
-
removeElement
public void removeElement(Object elem)
- Specified by:
removeElement
in interfaceMutableComboBoxModel<E>
-
removeElementAt
public void removeElementAt(int index)
- Specified by:
removeElementAt
in interfaceMutableComboBoxModel<E>
-
insertElementAt
public void insertElementAt(E element, int index)
Adds an element at a specific index.- Specified by:
insertElementAt
in interfaceMutableComboBoxModel<E>
- Parameters:
element
- The element to addindex
- Location to add the element
-
setSelectedItem
public void setSelectedItem(Object elem)
Set the value of the selected item. The selected item may be null.- Specified by:
setSelectedItem
in interfaceComboBoxModel<E>
- Parameters:
elem
- The combo box value or null for no selection.
-
getSelectedItem
public Object getSelectedItem()
- Specified by:
getSelectedItem
in interfaceComboBoxModel<E>
-
getElementAt
public E getElementAt(int index)
- Specified by:
getElementAt
in interfaceListModel<E>
-
addAllElements
public void addAllElements(Collection<E> elems)
Adds all elements from the collection.- Parameters:
elems
- The elements to add.
-
addAllElements
public void addAllElements(Collection<String> strings, Function<String,E> buildE)
Adds all elements from the collection of string representations.- Parameters:
strings
- The string representation of the elements to add.buildE
- AFunction
that builds an<E>
from aString
.
-
addTopElement
public E addTopElement(E newElement)
Adds an element to the top of the list.If the element is already in the model, moves it to the top. If the model gets too big, deletes the last element.
- Parameters:
newElement
- the element to add- Returns:
- The element that is at the top now.
-
removeAllElements
public void removeAllElements()
Empties the list.
-
find
public E find(String s)
Finds the item that matches string.Looks in the model for an element whose
toString()
matchess
.- Parameters:
s
- The string to match.- Returns:
- The item or null
-
prefs
public JosmComboBoxModel.Preferences prefs(Function<String,E> readE, Function<E,String> writeE)
Gets a preference loader and saver.- Parameters:
readE
- AFunction
that builds an<E>
from aString
.writeE
- AFunction
that serializes an<E>
to aString
- Returns:
- The
JosmComboBoxModel.Preferences
instance.
-
-