Class ListModelUtils
- java.lang.Object
-
- net.sf.jguiraffe.gui.builder.components.tags.ListModelUtils
-
public final class ListModelUtils extends Object
A helper class for dealing with model objects.
This class provides some functionality that can be used by concrete
ComponentHandler
implementations dealing with list box or combo box components. Especially the conversion from selected indices to corresponding value objects or vice versa is covered.- Version:
- $Id: ListModelUtils.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
- See Also:
ComponentHandler
-
-
Field Summary
Fields Modifier and Type Field Description static int
IDX_UNDEFINED
Constant for an undefined index.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static EditableComboBoxModel
fetchEditableComboBoxModel(ListModel model)
Obtains aEditableComboBoxModel
object from the passed inListModel
.static int
getDisplayIndex(ListModel listModel, Object display)
Returns the index of the specified display object in the specified list model.static int
getIndex(ListModel listModel, Object value)
Returns the index of the given value object in the specified list model.static int[]
getIndices(ListModel listModel, Object[] values)
Returns the indices of all given value objects in the specified list model.static Object
getValue(ListModel listModel, int index)
Returns the value object with the given index from the specified list model.static Object[]
getValues(ListModel listModel, int[] indices)
Returns the values from the specified list model for the given indices.static void
initializeListModel(ListModelSupport modelSupport)
Initializes theListModel
for the givenListModelSupport
object.
-
-
-
Field Detail
-
IDX_UNDEFINED
public static final int IDX_UNDEFINED
Constant for an undefined index. This value is returned by some methods which calculate an index in aListModel
if the index cannot be determined.- Since:
- 1.2
- See Also:
- Constant Field Values
-
-
Method Detail
-
getValue
public static Object getValue(ListModel listModel, int index)
Returns the value object with the given index from the specified list model. If this object is null , this method will return the index as anInteger
.- Parameters:
listModel
- the affected list modelindex
- the index- Returns:
- the value object (can be null for an invalid index)
-
getIndex
public static int getIndex(ListModel listModel, Object value)
Returns the index of the given value object in the specified list model. This method can be used for setting the data of a list box or combo box component from the property of the form bean. This implementation tries a linear search in the list model's value objects to determine the index. If thegetValueObject()
method of the list model returns null , it is tried to cast the value to anInteger
.- Parameters:
listModel
- the list modelvalue
- the value whose index is to be determined- Returns:
- the index of this value object or -1 if it cannot be determined
-
getDisplayIndex
public static int getDisplayIndex(ListModel listModel, Object display)
Returns the index of the specified display object in the specified list model. This is useful for mapping between display objects and value objects. TheListModel
is searched for the given display object and its index is returned. If the display object cannot be found, result is -1.- Parameters:
listModel
- theListModel
to be searcheddisplay
- the display object in question- Returns:
- the index of this display object or -1
- Since:
- 1.2
-
getValues
public static Object[] getValues(ListModel listModel, int[] indices)
Returns the values from the specified list model for the given indices. This method acts likegetValue()
, but can be used for lists with multi selection. Note that the resulting array is of the type of the list model.- Parameters:
listModel
- the list modelindices
- the indices- Returns:
- an object array with the selected objects
-
getIndices
public static int[] getIndices(ListModel listModel, Object[] values)
Returns the indices of all given value objects in the specified list model. This method is the equivalent ofgetIndex()
for lists with multi selection. Note that the returned array will not contain components with a value of -1. If a value object cannot be found in the list model, no component is added to the resulting array.- Parameters:
listModel
- the list modelvalues
- an array with the value objects- Returns:
- an array with the indices of the value objects (never null )
-
initializeListModel
public static void initializeListModel(ListModelSupport modelSupport) throws org.apache.commons.jelly.MissingAttributeException, FormBuilderException
Initializes theListModel
for the givenListModelSupport
object. This method implements the default algorithm for list model initialization used by tags that allow the definition of a model. It performs the following steps:- If a list model is already set, it does nothing.
- Otherwise, the list model is resolved in the following way:
- The name of the model object is obtained. If it is not set, an exception is thrown.
- A bean with this name is obtained from the current
BeanContext
. - If this bean implements the
ListModel
interface, it is set as the list model. Otherwise, an exception is thrown.
- Parameters:
modelSupport
- the object to be initialized (must not be null)- Throws:
org.apache.commons.jelly.MissingAttributeException
- if the name for the model is not setFormBuilderException
- if the model bean cannot be resolvedIllegalArgumentException
- if null is passed in
-
fetchEditableComboBoxModel
public static EditableComboBoxModel fetchEditableComboBoxModel(ListModel model)
Obtains aEditableComboBoxModel
object from the passed inListModel
. If theListModel
already implements theEditableComboBoxModel
interface, it is directly returned. Otherwise, a dummyEditableComboBoxModel
implementation is returned which does not perform any transformations.- Parameters:
model
- theListModel
in question- Returns:
- a
EditableComboBoxModel
object from this model - Since:
- 1.2
-
-