Package org.zkoss.zul

## Class SimpleListModel<E>

- java.lang.Object
- - org.zkoss.zul.AbstractListModel<E>
  - - org.zkoss.zul.SimpleListModel<E>

- All Implemented Interfaces:
    `java.io.Serializable`, `Pageable`, `Selectable<E>`, `Sortable<E>`, `ListModel<E>`, `ListSubModel<E>`, `PageableModel`

      public class SimpleListModel<E>
      extends AbstractListModel<E>
      implements Sortable<E>, ListSubModel<E>, java.io.Serializable

  A simple implementation of `ListModel`.

  Note: It assumes the content is immutable. If not, use `ListModelList` or `ListModelArray` instead. In additions, it stores the data in the array format, so if the original data is not an array. It is better not to use this class.

  Also notice that `SimpleListModel` also implements `ListSubModel`. It means when it is used with `Combobox`, only the data that matches what the user typed will be shown.

  Author:
    tomyeh
  See Also:
    `ListModelArray`, `ListModelSet`, `ListModelList`, `ListModelMap`, `(since 3.0.2)`, Serialized Form

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class org.zkoss.zul.AbstractListModel

      `AbstractListModel.DefaultSelectionControl<E>`

  - ### Field Summary

    - ### Fields inherited from class org.zkoss.zul.AbstractListModel

      `_selection`

    - ### Fields inherited from interface org.zkoss.zul.PageableModel

      `INTERNAL_EVENT`

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `SimpleListModel​(E[] data)` | Constructor.
    `SimpleListModel​(E[] data, boolean live)` | Constructor.
    `SimpleListModel​(java.util.List<? extends E> data)` | Constructor.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `java.lang.Object` | `clone()` |
    `protected void` | `fireSelectionEvent​(E e)` | Selectable's implementor use only.
    `E` | `getElementAt​(int j)` | Returns the value at the specified index.
    `protected int` | `getMaxNumberInSubModel​(int nRows)` | Returns the maximal allowed number of matched items in the sub-model returned by `getSubModel(java.lang.Object, int)`.
    `int` | `getSize()` | Returns the length of the list.
    `java.lang.String` | `getSortDirection​(java.util.Comparator<E> cmpr)` | Returns the sort direction of this model for the given comparator.
    `ListModel<E>` | `getSubModel​(java.lang.Object value, int nRows)` | Returns the subset of the list model data that matches the specified value.
    `protected boolean` | `inSubModel​(java.lang.Object key, java.lang.Object value)` | Compares if the given value shall belong to the submodel represented by the key.
    `void` | `sort()` | Sort the data model by default or assigned comparator.
    `void` | `sort​(java.util.Comparator<E> cmpr, boolean ascending)` | Sorts the data.

    - ### Methods inherited from class org.zkoss.zul.AbstractListModel

      `addListDataListener, addPagingEventListener, addToSelection, clearSelection, fireEvent, getActivePage, getListDataListeners, getPageCount, getPageSize, getSelection, getSelectionControl, getTotalSize, isMultiple, isSelected, isSelectionEmpty, newEmptySelection, readSelection, removeAllSelection, removeFromSelection, removeListDataListener, removePagingEventListener, retainAllSelection, setActivePage, setMultiple, setPageSize, setSelection, setSelectionControl, writeSelection`

    - ### Methods inherited from class java.lang.Object

      `equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### SimpleListModel

          public SimpleListModel​(E[] data,
                                 boolean live)

      Constructor.
      Parameters:
        `data` - the array to represent
        `live` - whether to have a 'live' `ListModel` on top of the specified list. If false, the content of the specified list is copied. If true, this object is a 'facade' of the specified list, i.e., when you add or remove items from this ListModelList, the inner "live" list would be changed accordingly. However, it is not a good idea to modify `data` once it is passed to this method with live is true, since `Listbox` is not smart enough to handle it.
      Since:
        2.4.1

    - #### SimpleListModel

          public SimpleListModel​(E[] data)

      Constructor. It made a copy of the specified array (`data`).

      Notice that if the data is static or not shared, it is better to use `SimpleListModelMap(data, true)` instead, since making a copy is slower.

    - #### SimpleListModel

          public SimpleListModel​(java.util.List<? extends E> data)

      Constructor. Notice the data will be converted to an array, so the performance is not good if the data is huge. Use `ListModelList` instead if the data is huge.
      Since:
        2.4.1

  - ### Method Detail

    - #### getSize

          public int getSize()

      Description copied from interface: `ListModel`
      Returns the length of the list.
      Specified by:
        `getSize` in interface `ListModel<E>`

    - #### getElementAt

          public E getElementAt​(int j)

      Description copied from interface: `ListModel`
      Returns the value at the specified index.
      Specified by:
        `getElementAt` in interface `ListModel<E>`

    - #### sort

          public void sort​(java.util.Comparator<E> cmpr,
                           boolean ascending)

      Sorts the data.
      Specified by:
        `sort` in interface `Sortable<E>`
      Parameters:
        `cmpr` - the comparator.
        `ascending` - whether to sort in the ascending order. It is ignored since this implementation uses cmpr to compare.

    - #### sort

          public void sort()

      Description copied from interface: `Sortable`
      Sort the data model by default or assigned comparator. Notice that the default implementation does nothing, the model which implements Sortable need to implement this method.
      Specified by:
        `sort` in interface `Sortable<E>`
      See Also:
        `Sortable.sort(Comparator, boolean)`

    - #### getSortDirection

          public java.lang.String getSortDirection​(java.util.Comparator<E> cmpr)

      Description copied from interface: `Sortable`
      Returns the sort direction of this model for the given comparator. It must be one of "ascending", "descending" and "natural".

      Default: "natural".

      Specified by:
        `getSortDirection` in interface `Sortable<E>`

    - #### getSubModel

          public ListModel<E> getSubModel​(java.lang.Object value,
                                          int nRows)

      Returns the subset of the list model data that matches the specified value. It is usually used for implementation of auto-complete.

      The implementation uses `inSubModel(java.lang.Object, java.lang.Object)` to check if the returned object of `getElementAt(int)` shall be in the sub model.

      Notice the maximal allowed number of items is decided by `getMaxNumberInSubModel(int)`, which, by default, returns 15 if nRows is negative.

      Specified by:
        `getSubModel` in interface `ListSubModel<E>`
      Parameters:
        `value` - the value to retrieve the subset of the list model. It is the key argument when invoking `inSubModel(java.lang.Object, java.lang.Object)`. this string.
        `nRows` - the maximal allowed number of matched items. If negative, it means the caller allows any number, but the implementation usually limits to a certain number (for better performance).
      Since:
        3.0.2
      See Also:
        `inSubModel(java.lang.Object, java.lang.Object)`, `getMaxNumberInSubModel(int)`

    - #### getMaxNumberInSubModel

          protected int getMaxNumberInSubModel​(int nRows)

      Returns the maximal allowed number of matched items in the sub-model returned by `getSubModel(java.lang.Object, int)`.

      Default: `nRows < 0 ? 15: nRows`.

      Since:
        5.0.4

    - #### inSubModel

          protected boolean inSubModel​(java.lang.Object key,
                                       java.lang.Object value)

      Compares if the given value shall belong to the submodel represented by the key.

      Default: converts both key and value to String objects and then return true if the String object of value starts with the String object

      Parameters:
        `key` - the key representing the submodel. In autocomplete, it is the value entered by user.
        `value` - the value in this model.
      Since:
        5.0.4
      See Also:
        `getSubModel(java.lang.Object, int)`

    - #### clone

          public java.lang.Object clone()

      Overrides:
        `clone` in class `AbstractListModel<E>`

    - #### fireSelectionEvent

          protected void fireSelectionEvent​(E e)

      Description copied from class: `AbstractListModel`
      Selectable's implementor use only.

      Fires a selection event for component to scroll into view. The override subclass must put the index0 of `AbstractListModel.fireEvent(int, int, int)` as the view index to scroll. By default, the value -1 is assumed which means no scroll into view.

      The method is invoked when both methods are invoked. `AbstractListModel.addToSelection(Object)` and `AbstractListModel.setSelection(Collection)`.

      Overrides:
        `fireSelectionEvent` in class `AbstractListModel<E>`
      Parameters:
        `e` - selected object.