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.
| Method from org.jfree.ui.KeyedComboBoxModel Detail: |
public void add(Object key,
Object cbitem) {
final ComboBoxItemPair con = new ComboBoxItemPair(key, cbitem);
data.add(con);
final ListDataEvent evt = new ListDataEvent
(this, ListDataEvent.INTERVAL_ADDED, data.size() - 2, data.size() - 2);
fireListDataEvent(evt);
}
Adds a new entry to the model. |
public synchronized void addListDataListener(ListDataListener l) {
if (l == null)
{
throw new NullPointerException();
}
listdatalistener.add(l);
tempListeners = null;
}
Adds a listener to the list that's notified each time a change to the data
model occurs. |
public void clear() {
final int size = getSize();
data.clear();
final ListDataEvent evt = new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, 0, size - 1);
fireListDataEvent(evt);
}
Removes all entries from the model. |
public int findElementIndex(Object key) {
if (key == null)
{
throw new NullPointerException("Item to find must not be null");
}
for (int i = 0; i < data.size(); i++)
{
final ComboBoxItemPair datacon = (ComboBoxItemPair) data.get(i);
if (key.equals(datacon.getValue()))
{
return i;
}
}
return -1;
}
Tries to find the index of element with the given key. The key must not
be null. |
protected synchronized void fireListDataEvent(ListDataEvent evt) {
if (tempListeners == null)
{
tempListeners = (ListDataListener[]) listdatalistener.toArray
(new ListDataListener[listdatalistener.size()]);
}
final ListDataListener[] listeners = tempListeners;
for (int i = 0; i < listeners.length; i++)
{
final ListDataListener l = listeners[i];
l.contentsChanged(evt);
}
}
Notifies all registered list data listener of the given event. |
public Object getElementAt(int index) {
if (index >= data.size())
{
return null;
}
final ComboBoxItemPair datacon = (ComboBoxItemPair) data.get(index);
if (datacon == null)
{
return null;
}
return datacon.getValue();
}
Returns the value at the specified index. |
public Object getKeyAt(int index) {
if (index >= data.size())
{
return null;
}
if (index < 0)
{
return null;
}
final ComboBoxItemPair datacon = (ComboBoxItemPair) data.get(index);
if (datacon == null)
{
return null;
}
return datacon.getKey();
}
Returns the key from the given index. |
public Object getSelectedItem() {
return selectedItemValue;
}
Returns the selected item. |
public Object getSelectedKey() {
return getKeyAt(selectedItemIndex);
}
Returns the selected data element or null if none is set. |
public int getSize() {
return data.size();
}
Returns the length of the list. |
public void removeDataElement(Object key) {
final int idx = findDataElementIndex(key);
if (idx == -1)
{
return;
}
data.remove(idx);
final ListDataEvent evt = new ListDataEvent
(this, ListDataEvent.INTERVAL_REMOVED, idx, idx);
fireListDataEvent(evt);
}
Removes an entry from the model. |
public void removeListDataListener(ListDataListener l) {
listdatalistener.remove(l);
tempListeners = null;
}
Removes a listener from the list that's notified each time a change to
the data model occurs. |
public void setAllowOtherValue(boolean allowOtherValue) {
this.allowOtherValue = allowOtherValue;
}
|
public void setData(Object[] keys,
Object[] values) {
if (values.length != keys.length)
{
throw new IllegalArgumentException("Values and text must have the same length.");
}
data.clear();
data.ensureCapacity(keys.length);
for (int i = 0; i < values.length; i++)
{
add(keys[i], values[i]);
}
selectedItemIndex = -1;
final ListDataEvent evt = new ListDataEvent
(this, ListDataEvent.CONTENTS_CHANGED, 0, data.size() - 1);
fireListDataEvent(evt);
}
Replaces the data in this combobox model. The number of keys must be
equals to the number of values. |
public void setSelectedItem(Object anItem) {
if (anItem == null)
{
selectedItemIndex = -1;
selectedItemValue = null;
}
else
{
final int newSelectedItem = findElementIndex(anItem);
if (newSelectedItem == -1)
{
if (isAllowOtherValue())
{
selectedItemIndex = -1;
selectedItemValue = anItem;
}
else
{
selectedItemIndex = -1;
selectedItemValue = null;
}
}
else
{
selectedItemIndex = newSelectedItem;
selectedItemValue = getElementAt(selectedItemIndex);
}
}
fireListDataEvent(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, -1, -1));
}
Set the selected item. The implementation of this method should notify
all registered ListDataListeners that the contents have
changed. |
public void setSelectedKey(Object anItem) {
if (anItem == null)
{
selectedItemIndex = -1;
selectedItemValue = null;
}
else
{
final int newSelectedItem = findDataElementIndex(anItem);
if (newSelectedItem == -1)
{
selectedItemIndex = -1;
selectedItemValue = null;
}
else
{
selectedItemIndex = newSelectedItem;
selectedItemValue = getElementAt(selectedItemIndex);
}
}
fireListDataEvent(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, -1, -1));
}
Defines the selected key. If the object is not in the list of values, no
item gets selected. |