Class 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 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 a ListModel 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 an Integer.
        Parameters:
        listModel - the affected list model
        index - 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 the getValueObject() method of the list model returns null , it is tried to cast the value to an Integer.
        Parameters:
        listModel - the list model
        value - 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. The ListModel is searched for the given display object and its index is returned. If the display object cannot be found, result is -1.
        Parameters:
        listModel - the ListModel to be searched
        display - 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 like getValue(), 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 model
        indices - 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 of getIndex() 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 model
        values - 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 the ListModel for the given ListModelSupport 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 set
        FormBuilderException - if the model bean cannot be resolved
        IllegalArgumentException - if null is passed in
      • fetchEditableComboBoxModel

        public static EditableComboBoxModel fetchEditableComboBoxModel​(ListModel model)
        Obtains a EditableComboBoxModel object from the passed in ListModel. If the ListModel already implements the EditableComboBoxModel interface, it is directly returned. Otherwise, a dummy EditableComboBoxModel implementation is returned which does not perform any transformations.
        Parameters:
        model - the ListModel in question
        Returns:
        a EditableComboBoxModel object from this model
        Since:
        1.2