Class KeyedComboBoxModel<K,V>
- java.lang.Object
-
- org.pentaho.reporting.libraries.designtime.swing.KeyedComboBoxModel<K,V>
-
- All Implemented Interfaces:
ComboBoxModel
,ListModel
public class KeyedComboBoxModel<K,V> extends Object implements ComboBoxModel
The KeyedComboBox model allows to define an internal key (the data element) for every entry in the model. This class is usefull in all cases, where the public text differs from the internal view on the data. A separation between presentation data and processing data is a prequesite for localizing combobox entries. This model does not allow selected elements, which are not in the list of valid elements.- Author:
- Thomas Morgner
-
-
Constructor Summary
Constructors Constructor Description KeyedComboBoxModel()
Creates a new keyed combobox model.KeyedComboBoxModel(K[] keys, V[] values)
Creates a new keyed combobox model for the given keys and values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(K key, V cbitem)
Adds a new entry to the model.void
addListDataListener(ListDataListener l)
Adds a listener to the list that's notified each time a change to the data model occurs.void
clear()
Removes all entries from the model.int
findElementIndex(V anItem)
Tries to find the index of element with the given value.protected void
fireListDataEvent(ListDataEvent evt)
Notifies all registered list data listener of the given event.V
getElementAt(int index)
Returns the value at the specified index.K
getKeyAt(int index)
Returns the key from the given index.V
getSelectedItem()
Returns the selected item.int
getSelectedItemIndex()
K
getSelectedKey()
Returns the selected data element or null if none is set.int
getSize()
Returns the length of the list.void
remove(int index)
void
removeDataElement(K key)
Removes an entry from the model.void
removeListDataListener(ListDataListener l)
Removes a listener from the list that's notified each time a change to the data model occurs.void
setAllowOtherValue(boolean allowOtherValue)
void
setData(K[] keys, V[] values)
Replaces the data in this combobox model.void
setSelectedItem(Object anItem)
Set the selected item.void
setSelectedKey(K anItem)
Defines the selected key.void
setSelectedValue(V anItem)
void
update(int index, K key, V cbitem)
-
-
-
Method Detail
-
setData
public void setData(K[] keys, V[] values)
Replaces the data in this combobox model. The number of keys must be equals to the number of values.- Parameters:
keys
- the keysvalues
- the values
-
fireListDataEvent
protected void fireListDataEvent(ListDataEvent evt)
Notifies all registered list data listener of the given event.- Parameters:
evt
- the event.
-
getSelectedItem
public V getSelectedItem()
Returns the selected item.- Specified by:
getSelectedItem
in interfaceComboBoxModel<K>
- Returns:
- The selected item or
null
if there is no selection
-
setSelectedKey
public void setSelectedKey(K anItem)
Defines the selected key. If the object is not in the list of values, no item gets selected.- Parameters:
anItem
- the new selected item.
-
setSelectedItem
public final void setSelectedItem(Object anItem)
Set the selected item. The implementation of this method should notify all registeredListDataListener
s that the contents have changed.- Specified by:
setSelectedItem
in interfaceComboBoxModel<K>
- Parameters:
anItem
- the list object to select ornull
to clear the selection
-
setSelectedValue
public void setSelectedValue(V anItem)
-
setAllowOtherValue
public void setAllowOtherValue(boolean allowOtherValue)
-
addListDataListener
public void addListDataListener(ListDataListener l)
Adds a listener to the list that's notified each time a change to the data model occurs.- Specified by:
addListDataListener
in interfaceListModel<K>
- Parameters:
l
- theListDataListener
to be added
-
getElementAt
public V getElementAt(int index)
Returns the value at the specified index.- Specified by:
getElementAt
in interfaceListModel<K>
- Parameters:
index
- the requested index- Returns:
- the value at
index
-
getKeyAt
public K getKeyAt(int index)
Returns the key from the given index.- Parameters:
index
- the index of the key.- Returns:
- the the key at the specified index.
-
getSelectedKey
public K getSelectedKey()
Returns the selected data element or null if none is set.- Returns:
- the selected data element.
-
getSize
public int getSize()
Returns the length of the list.
-
removeListDataListener
public void removeListDataListener(ListDataListener l)
Removes a listener from the list that's notified each time a change to the data model occurs.- Specified by:
removeListDataListener
in interfaceListModel<K>
- Parameters:
l
- theListDataListener
to be removed
-
findElementIndex
public int findElementIndex(V anItem)
Tries to find the index of element with the given value. This method is called by the setSelectedItem method and returns the first occurence of the element.- Parameters:
anItem
- the key for the element to be searched.- Returns:
- the index of the key, or -1 if not found.
-
removeDataElement
public void removeDataElement(K key)
Removes an entry from the model.- Parameters:
key
- the key
-
add
public void add(K key, V cbitem)
Adds a new entry to the model.- Parameters:
key
- the keycbitem
- the display value.
-
clear
public void clear()
Removes all entries from the model.
-
getSelectedItemIndex
public int getSelectedItemIndex()
-
remove
public void remove(int index)
-
-