Class GroupedSingleSelectBoxDisplay<K,T,G>

java.lang.Object
name.matthewgreet.strutscommons.view.GroupedSingleSelectBoxDisplay<K,T,G>
Type Parameters:
K - Type of unique identifier of T.
T - Record type of list used to generate dropdown.
G - Type of describing group of T, which can be String.

public abstract class GroupedSingleSelectBoxDisplay<K,T,G> extends Object
Template that aids creation of OPTION and OPTGROUP tags in JSPs for single selection SELECT elements by formatting list for display. Concrete subclasses override getValue(T) and getText(T) to extract item values and display text from a raw list. See also SelectBoxGroupDisplay and SelectBoxItemDisplay2.

Example JSP code:
<SELECT SIZE="1" NAME="category" >
  <s:iterator value="categoryDisplay.list" var="categoryGroupDisplay">
    <OPTGROUP LABEL="<s:property value="#categoryGroupDisplay.text"/>">
    <s:iterator value="#categoryGroupDisplay.children" var="categoryItemDisplay">
      <OPTION VALUE="<s:property value="#categoryItemDisplay.value"/>" <s:property value="#categoryItemDisplay.selectedAttribute"/>><s:property value="#categoryItemDisplay.text"/></OPTION>
    </s:iterator>
    </OPTGROUP>
  </s:iterator>
</SELECT>

  • Constructor Details

    • GroupedSingleSelectBoxDisplay

      public GroupedSingleSelectBoxDisplay()
  • Method Details

    • addItems

      protected void addItems(List<SelectBoxGroupDisplay<K,T,G>> formattedModel)
      May be overridden by subclasses to add additional, formatted items to the display list. This is typically used to add a blank item to represent no value or text that demands a real value is selected. This is called after sorting.
      Parameters:
      formattedModel - List of SelectBoxGroupDisplay containing child SelectBoxItemDisplay.
    • getBlankText

      protected String getBlankText()
      If hasBlankValue() returns true or addBlankValue() is called, returns text to display for blank value. Defaults to blank.
    • getGroup

      protected abstract G getGroup(T item)
      Overridden by subclasses to return item's group.
    • getGroupLabel

      protected abstract String getGroupLabel(G group)
      Overridden by subclass to return label for a group, which becomes the LABEL attribute for OPTGROUP tags.
    • getGroupSortComparator

      protected Comparator<SelectBoxGroupDisplay<K,T,G>> getGroupSortComparator()
      May be overridden by subclasses to return a comparator that defines display order of groups. Defaults to comparator that sorts by ascending display text.
    • getInitialList

      protected List<SelectBoxGroupDisplay<K,T,G>> getInitialList()
      May be overridden by subclasses to return initial selection, especially if the list is hardcoded and setModel will not be used. Can return null for no initial list.
    • getKey

      public abstract K getKey(T item)
      Overridden by subclasses to return unformatted identifier of item record. This is the value used to match the parameter of setSelectedValue(K).
      Parameters:
      item - Member of raw data list to be displayed.
    • getSortComparator

      protected Comparator<SelectBoxItemDisplay2<K,T>> getSortComparator()
      May be overridden by subclasses to return a comparator for defining display order of formatted items. Comparator compares instances of SelectBoxItemDisplay that are allowed by subclass. Defaults to comparator that sorts by ascending display text.
    • getText

      protected abstract String getText(T item)
      Overridden by subclasses to return formatted text to be displayed to user as part of OPTION element.
      Parameters:
      item - Member of raw data list to be displayed.
    • getValue

      protected abstract String getValue(T item)
      Overridden by subclasses to return string to be used in VALUE attribute of OPTION element. This is the value return in form data.
      Parameters:
      item - Member of raw data list to be displayed.
    • hasBlankValue

      protected boolean hasBlankValue()
      May be overridden by subclasses to return true, inserting a blank value at the beginning of the SELECT list. Defaults to false.
    • isAllowed

      protected boolean isAllowed(T item)
      May be overridden by subclasses to filter items in lookup list from display. Defaults to allow all items.
      Parameters:
      item - Member of raw data list to be displayed.
    • makeSelectBoxItemDisplay

      protected SelectBoxItemDisplay2<K,T> makeSelectBoxItemDisplay(String value, String text, K key, T data)
      May be overridden by subclasses to return formatted text of an enumerated value for display in a select box. data will be null for a blank entry.
    • addBlankValue

      public void addBlankValue()
      If called after setModel(java.util.Collection<T>), inserts blank value to beginning of list. This is useful for search fields where a blank value means no search.
    • getList

      public List<SelectBoxGroupDisplay<K,T,G>> getList()
      Returns formatted version of account list for human display.
    • getSelectedItem

      public SelectBoxItemDisplay2<K,T> getSelectedItem()
      Returns selected, formatted item or null if none selected.
    • setList

      public void setList(List<SelectBoxGroupDisplay<K,T,G>> value)
      Directly sets formatted version of account list for human display. The value must already be sorted.
    • setModel

      public void setModel(Collection<T> model)
      Formats lookup list for use in single selection HTML SELECT element. This replaces any initial list.
    • setSelectedFormattedValueWithConversionError

      public void setSelectedFormattedValueWithConversionError(String value, String conversionErrorName)
      Selects item matching named Struts 2 conversion error (rejected form field value), or formatted identifier if conversion error not found. Only appropriate in a Struts 2 context.
      Parameters:
      value - Formatted identifier of item..
      conversionErrorName - Name of Struts 2 conversion error, which will have the "<form name>.<field name>" format.
    • setSelectedValueWithConversionError

      public void setSelectedValueWithConversionError(K key, String conversionErrorName)
      Selects item matching named Struts 2 conversion error (rejected form field value), or unformatted identifier if conversion error not found. Only appropriate in a Struts 2 context.
      Parameters:
      key - Unformatted identifier of item..
      conversionErrorName - Name of Struts 2 conversion error, which will have the "<form name>.<field name>" format.
    • setSelectedFormattedValue

      public void setSelectedFormattedValue(String value)
      Sets selected item of list identified by its value.
      Parameters:
      value - id of selected item or null or empty string to select none.
    • setSelectedValue

      public void setSelectedValue(K key)
      Sets selected item of list identified by its value.
      Parameters:
      key - unformatted id of selected item or null to select none.