com.google.gwt.user.cellview.client

## Class HasDataPresenter<T>

- java.lang.Object
- - com.google.gwt.user.cellview.client.HasDataPresenter<T>

- Type Parameters:
    `T` - the data type of items in the list
  All Implemented Interfaces:
    HasHandlers, HasKeyboardPagingPolicy, HasKeyboardSelectionPolicy, HasCellPreviewHandlers<T>, HasData<T>, HasKeyProvider<T>, HasRows

       class HasDataPresenter<T>
      extends java.lang.Object
      implements HasData<T>, HasKeyProvider<T>, HasKeyboardPagingPolicy

  Presenter implementation of `HasData` that presents data for various cell based widgets. This class contains most of the shared logic used by these widgets, making it easier to test the common code.

  In proper MVP design, user code would interact with the presenter. However, that would complicate the widget code. Instead, each widget owns its own presenter and contains its own View. The widget forwards commands through to the presenter, which then updates the widget via the view. This keeps the user facing API simpler.

  Updates are not pushed to the view immediately. Instead, the presenter collects updates and resolves them all in a finally command. This reduces the total number of DOM manipulations, and makes it easier to handle side effects in user code triggered by the rendering pass. The view is responsible for called `flush()` to force the presenter to synchronize the view when needed.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `(package private) static interface` | `HasDataPresenter.ElementIterator` An iterator over DOM elements.
    `(package private) static interface` | `HasDataPresenter.View<T>` The view that this presenter presents.

    - ### Nested classes/interfaces inherited from interface com.google.gwt.user.cellview.client.HasKeyboardPagingPolicy

      `HasKeyboardPagingPolicy.KeyboardPagingPolicy`

    - ### Nested classes/interfaces inherited from interface com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy

      `HasKeyboardSelectionPolicy.KeyboardSelectionPolicy`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `(package private) static int` | `PAGE_INCREMENT` The number of rows to jump when PAGE_UP or PAGE_DOWN is pressed and the `HasKeyboardPagingPolicy.KeyboardPagingPolicy` is `HasKeyboardPagingPolicy.KeyboardPagingPolicy.INCREASE_RANGE`.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `HasDataPresenter(HasData<T> display, HasDataPresenter.View<T> view, int pageSize, ProvidesKey<T> keyProvider)` Construct a new `HasDataPresenter`.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `HandlerRegistration` | `addCellPreviewHandler(CellPreviewEvent.Handler<T> handler)` Adds a `CellPreviewEvent` handler.
    `HandlerRegistration` | `addLoadingStateChangeHandler(LoadingStateChangeEvent.Handler handler)`
    `HandlerRegistration` | `addRangeChangeHandler(RangeChangeEvent.Handler handler)` Add a `RangeChangeEvent.Handler`.
    `HandlerRegistration` | `addRowCountChangeHandler(RowCountChangeEvent.Handler handler)` Add a `RowCountChangeEvent.Handler`.
    `(package private) java.util.List<Range>` | `calculateModifiedRanges(JsArrayInteger modifiedRows, int pageStart, int pageEnd)` Combine the modified row indexes into as many as two `Range`s, optimizing to have the fewest unmodified rows within the ranges.
    `void` | `clearKeyboardSelectedRowValue()` Clear the row value associated with the keyboard selected row.
    `void` | `clearSelectionModel()` Clear the `SelectionModel` without updating the view.
    `void` | `fireEvent(GwtEvent<?> event)` Fires the given event to the handlers listening to the event's type.
    `void` | `flush()` Flush pending changes to the view.
    `int` | `getCurrentPageSize()` Get the current page size.
    `HasKeyboardPagingPolicy.KeyboardPagingPolicy` | `getKeyboardPagingPolicy()` Get the `HasKeyboardPagingPolicy.KeyboardPagingPolicy`.
    `int` | `getKeyboardSelectedRow()` Get the index of the keyboard selected row relative to the page start.
    `int` | `getKeyboardSelectedRowInView()` Get the index of the keyboard selected row relative to the page start as it appears in the view, regardless of whether or not there is a pending change.
    `T` | `getKeyboardSelectedRowValue()` Get the value that the user selected.
    `HasKeyboardSelectionPolicy.KeyboardSelectionPolicy` | `getKeyboardSelectionPolicy()` Get the `HasKeyboardSelectionPolicy.KeyboardSelectionPolicy`.
    `ProvidesKey<T>` | `getKeyProvider()` Return the key provider.
    `int` | `getRowCount()` Get the overall data size.
    `SelectionModel<? super T>` | `getSelectionModel()` Get the `SelectionModel` used by this `HasData`.
    `T` | `getVisibleItem(int indexOnPage)` Get the row value at the specified visible index.
    `int` | `getVisibleItemCount()` Get the number of visible items being displayed.
    `java.util.List<T>` | `getVisibleItems()` Get an `Iterable` composed of all of the visible items.
    `Range` | `getVisibleRange()` Return the range of data being displayed.
    `boolean` | `hasPendingState()` Check whether or not there is a pending state.
    `boolean` | `isEmpty()` Check whether or not the data set is empty.
    `boolean` | `isRowCountExact()` Check if the total row count is exact, or an estimate.
    `void` | `redraw()` Redraw the list with the current data.
    `protected void` | `scheduleFinally(Scheduler.ScheduledCommand command)` Schedules the command.
    `void` | `setKeyboardPagingPolicy(HasKeyboardPagingPolicy.KeyboardPagingPolicy policy)` Set the `HasKeyboardPagingPolicy.KeyboardPagingPolicy`.
    `void` | `setKeyboardSelectedRow(int index, boolean stealFocus, boolean forceUpdate)` Set the row index of the keyboard selected element.
    `void` | `setKeyboardSelectionPolicy(HasKeyboardSelectionPolicy.KeyboardSelectionPolicy policy)` Set the `HasKeyboardSelectionPolicy.KeyboardSelectionPolicy`.
    `void` | `setRowCount(int count)` Set the exact total count of all rows.
    `void` | `setRowCount(int count, boolean isExact)` Set the total count of all rows, specifying whether the count is exact or an estimate.
    `void` | `setRowData(int start, java.util.List<? extends T> values)` Set a values associated with the rows in the visible range.
    `void` | `setSelectionModel(SelectionModel<? super T> selectionModel)` Set the `SelectionModel` used by this `HasData`.
    `void` | `setVisibleRange(int start, int length)` Set the visible range or rows.
    `void` | `setVisibleRange(Range range)` Set the visible range or rows.
    `void` | `setVisibleRangeAndClearData(Range range, boolean forceRangeChangeEvent)` Set the visible range and clear the current visible data.

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

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

- - ### Field Detail

    - #### PAGE_INCREMENT

          static final int PAGE_INCREMENT

      The number of rows to jump when PAGE_UP or PAGE_DOWN is pressed and the `HasKeyboardPagingPolicy.KeyboardPagingPolicy` is `HasKeyboardPagingPolicy.KeyboardPagingPolicy.INCREASE_RANGE`.
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### HasDataPresenter

          public HasDataPresenter(HasData<T> display,
                          HasDataPresenter.View<T> view,
                          int pageSize,
                          ProvidesKey<T> keyProvider)

      Construct a new `HasDataPresenter`.
      Parameters:
        `display` - the display that is being presented
        `view` - the view implementation
        `pageSize` - the default page size

  - ### Method Detail

    - #### addCellPreviewHandler

          public HandlerRegistration addCellPreviewHandler(CellPreviewEvent.Handler<T> handler)

      Description copied from interface: `HasCellPreviewHandlers`
      Adds a `CellPreviewEvent` handler.
      Specified by:
        `addCellPreviewHandler` in interface `HasCellPreviewHandlers<T>`
      Parameters:
        `handler` - the handler
      Returns:
        the registration for the event

    - #### addLoadingStateChangeHandler

          public HandlerRegistration addLoadingStateChangeHandler(LoadingStateChangeEvent.Handler handler)

    - #### addRangeChangeHandler

          public HandlerRegistration addRangeChangeHandler(RangeChangeEvent.Handler handler)

      Description copied from interface: `HasRows`
      Add a `RangeChangeEvent.Handler`.
      Specified by:
        `addRangeChangeHandler` in interface `HasRows`
      Parameters:
        `handler` - the handler
      Returns:
        a `HandlerRegistration` to remove the handler

    - #### addRowCountChangeHandler

          public HandlerRegistration addRowCountChangeHandler(RowCountChangeEvent.Handler handler)

      Description copied from interface: `HasRows`
      Add a `RowCountChangeEvent.Handler`.
      Specified by:
        `addRowCountChangeHandler` in interface `HasRows`
      Parameters:
        `handler` - the handler
      Returns:
        a `HandlerRegistration` to remove the handler

    - #### clearKeyboardSelectedRowValue

          public void clearKeyboardSelectedRowValue()

      Clear the row value associated with the keyboard selected row.

    - #### clearSelectionModel

          public void clearSelectionModel()

      Clear the `SelectionModel` without updating the view.

    - #### fireEvent

          public void fireEvent(GwtEvent<?> event)

      Description copied from interface: `HasHandlers`
      Fires the given event to the handlers listening to the event's type.

      Any exceptions thrown by handlers will be bundled into a `UmbrellaException` and then re-thrown after all handlers have completed. An exception thrown by a handler will not prevent other handlers from executing.

      Specified by:
        `fireEvent` in interface `HasHandlers`
      Parameters:
        `event` - the event
      Throws:
        `java.lang.UnsupportedOperationException`

    - #### flush

          public void flush()

      Flush pending changes to the view.

    - #### getCurrentPageSize

          public int getCurrentPageSize()

      Get the current page size. This is usually the page size, but can be less if the data size cannot fill the current page.
      Returns:
        the size of the current page

    - #### getKeyboardPagingPolicy

          public HasKeyboardPagingPolicy.KeyboardPagingPolicy getKeyboardPagingPolicy()

      Description copied from interface: `HasKeyboardPagingPolicy`
      Get the `HasKeyboardPagingPolicy.KeyboardPagingPolicy`.
      Specified by:
        `getKeyboardPagingPolicy` in interface `HasKeyboardPagingPolicy`
      Returns:
        the paging policy
      See Also:
        `HasKeyboardPagingPolicy.setKeyboardPagingPolicy(KeyboardPagingPolicy)`

    - #### getKeyboardSelectedRow

          public int getKeyboardSelectedRow()

      Get the index of the keyboard selected row relative to the page start.
      Returns:
        the row index, or -1 if disabled

    - #### getKeyboardSelectedRowInView

          public int getKeyboardSelectedRowInView()

      Get the index of the keyboard selected row relative to the page start as it appears in the view, regardless of whether or not there is a pending change.
      Returns:
        the row index, or -1 if disabled

    - #### getKeyboardSelectedRowValue

          public T getKeyboardSelectedRowValue()

      Get the value that the user selected.
      Returns:
        the value, or null if a value was not selected

    - #### getKeyboardSelectionPolicy

          public HasKeyboardSelectionPolicy.KeyboardSelectionPolicy getKeyboardSelectionPolicy()

      Description copied from interface: `HasKeyboardSelectionPolicy`
      Get the `HasKeyboardSelectionPolicy.KeyboardSelectionPolicy`.
      Specified by:
        `getKeyboardSelectionPolicy` in interface `HasKeyboardSelectionPolicy`
      Returns:
        the selection policy
      See Also:
        `HasKeyboardSelectionPolicy.setKeyboardSelectionPolicy(KeyboardSelectionPolicy)`

    - #### getKeyProvider

          public ProvidesKey<T> getKeyProvider()

      Description copied from interface: `HasKeyProvider`
      Return the key provider.
      Specified by:
        `getKeyProvider` in interface `HasKeyProvider<T>`
      Returns:
        the `ProvidesKey` instance

    - #### getRowCount

          public int getRowCount()

      Get the overall data size.
      Specified by:
        `getRowCount` in interface `HasRows`
      Returns:
        the data size
      See Also:
        `HasRows.setRowCount(int)`

    - #### getSelectionModel

          public SelectionModel<? super T> getSelectionModel()

      Description copied from interface: `HasData`
      Get the `SelectionModel` used by this `HasData`.
      Specified by:
        `getSelectionModel` in interface `HasData<T>`
      Returns:
        the `SelectionModel`
      See Also:
        `HasData.setSelectionModel(SelectionModel)`

    - #### getVisibleItem

          public T getVisibleItem(int indexOnPage)

      Description copied from interface: `HasData`
      Get the row value at the specified visible index. Index 0 corresponds to the first item on the page.
      Specified by:
        `getVisibleItem` in interface `HasData<T>`
      Parameters:
        `indexOnPage` - the index on the page
      Returns:
        the row value

    - #### getVisibleItemCount

          public int getVisibleItemCount()

      Description copied from interface: `HasData`
      Get the number of visible items being displayed. Note that this value might be less than the page size if there is not enough data to fill the page.
      Specified by:
        `getVisibleItemCount` in interface `HasData<T>`
      Returns:
        the number of visible items on the page

    - #### getVisibleItems

          public java.util.List<T> getVisibleItems()

      Description copied from interface: `HasData`
      Get an `Iterable` composed of all of the visible items.
      Specified by:
        `getVisibleItems` in interface `HasData<T>`
      Returns:
        an `Iterable` instance

    - #### getVisibleRange

          public Range getVisibleRange()

      Return the range of data being displayed.
      Specified by:
        `getVisibleRange` in interface `HasRows`
      Returns:
        the visible range
      See Also:
        `HasRows.setVisibleRange(Range)`, `HasRows.setVisibleRange(int, int)`

    - #### hasPendingState

          public boolean hasPendingState()

      Check whether or not there is a pending state. If there is a pending state, views might skip DOM updates and wait for the new data to be rendered when the pending state is resolved.
      Returns:
        true if there is a pending state, false if not

    - #### isEmpty

          public boolean isEmpty()

      Check whether or not the data set is empty. That is, the row count is exactly 0.
      Returns:
        true if data set is empty

    - #### isRowCountExact

          public boolean isRowCountExact()

      Description copied from interface: `HasRows`
      Check if the total row count is exact, or an estimate.
      Specified by:
        `isRowCountExact` in interface `HasRows`
      Returns:
        true if exact, false if an estimate

    - #### redraw

          public void redraw()

      Redraw the list with the current data.

    - #### setKeyboardPagingPolicy

          public void setKeyboardPagingPolicy(HasKeyboardPagingPolicy.KeyboardPagingPolicy policy)

      Description copied from interface: `HasKeyboardPagingPolicy`
      Set the `HasKeyboardPagingPolicy.KeyboardPagingPolicy`.
      Specified by:
        `setKeyboardPagingPolicy` in interface `HasKeyboardPagingPolicy`
      Parameters:
        `policy` - the paging policy
      See Also:
        `HasKeyboardPagingPolicy.getKeyboardPagingPolicy()`

    - #### setKeyboardSelectedRow

          public void setKeyboardSelectedRow(int index,
                                    boolean stealFocus,
                                    boolean forceUpdate)

      Set the row index of the keyboard selected element.
      Parameters:
        `index` - the row index
        `stealFocus` - true to steal focus
        `forceUpdate` - force the update even if the row didn't change

    - #### setKeyboardSelectionPolicy

          public void setKeyboardSelectionPolicy(HasKeyboardSelectionPolicy.KeyboardSelectionPolicy policy)

      Description copied from interface: `HasKeyboardSelectionPolicy`
      Set the `HasKeyboardSelectionPolicy.KeyboardSelectionPolicy`.
      Specified by:
        `setKeyboardSelectionPolicy` in interface `HasKeyboardSelectionPolicy`
      Parameters:
        `policy` - the selection policy
      See Also:
        `HasKeyboardSelectionPolicy.getKeyboardSelectionPolicy()`

    - #### setRowCount

          public final void setRowCount(int count)

      Description copied from interface: `HasRows`
      Set the exact total count of all rows. This method defers to `HasRows.setRowCount(int, boolean)`.
      Specified by:
        `setRowCount` in interface `HasRows`
      Parameters:
        `count` - the exact total count
      Throws:
        `java.lang.UnsupportedOperationException`
      See Also:
        `HasRows.getRowCount()`

    - #### setRowCount

          public void setRowCount(int count,
                         boolean isExact)

      Description copied from interface: `HasRows`
      Set the total count of all rows, specifying whether the count is exact or an estimate.
      Specified by:
        `setRowCount` in interface `HasRows`
      Parameters:
        `count` - the total count
        `isExact` - true if the count is exact, false if an estimate
      See Also:
        `HasRows.getRowCount()`

    - #### setRowData

          public void setRowData(int start,
                        java.util.List<? extends T> values)

      Description copied from interface: `HasData`

      Set a values associated with the rows in the visible range.

      This method does not replace all rows in the display; it replaces the row values starting at the specified start index through the length of the the specified values. You must call `HasRows.setRowCount(int)` to set the total number of rows in the display. You should also use `HasRows.setRowCount(int)` to remove rows when the total number of rows decreases.

      Specified by:
        `setRowData` in interface `HasData<T>`
      Parameters:
        `start` - the start index of the data
        `values` - the values within the range

    - #### setSelectionModel

          public void setSelectionModel(SelectionModel<? super T> selectionModel)

      Description copied from interface: `HasData`
      Set the `SelectionModel` used by this `HasData`.
      Specified by:
        `setSelectionModel` in interface `HasData<T>`
      Parameters:
        `selectionModel` - the `SelectionModel`
      See Also:
        `HasData.getSelectionModel()`

    - #### setVisibleRange

          public final void setVisibleRange(int start,
                             int length)

      Description copied from interface: `HasRows`
      Set the visible range or rows. This method defers to `HasRows.setVisibleRange(Range)`.
      Specified by:
        `setVisibleRange` in interface `HasRows`
      Parameters:
        `start` - the start index
        `length` - the length
      Throws:
        `java.lang.UnsupportedOperationException`
      See Also:
        `HasRows.getVisibleRange()`

    - #### setVisibleRange

          public void setVisibleRange(Range range)

      Description copied from interface: `HasRows`
      Set the visible range or rows.
      Specified by:
        `setVisibleRange` in interface `HasRows`
      Parameters:
        `range` - the visible range
      See Also:
        `HasRows.getVisibleRange()`

    - #### setVisibleRangeAndClearData

          public void setVisibleRangeAndClearData(Range range,
                                         boolean forceRangeChangeEvent)

      Description copied from interface: `HasData`

      Set the visible range and clear the current visible data.

      If the second argument `forceRangeChangeEvent` is true, a `RangeChangeEvent` will be fired even if the range does not change. If false, a `RangeChangeEvent` will only be fired if the range changes.

      Specified by:
        `setVisibleRangeAndClearData` in interface `HasData<T>`
      Parameters:
        `range` - the new `Range`
        `forceRangeChangeEvent` - true to fire a `RangeChangeEvent` even if the `Range` doesn't change

    - #### scheduleFinally

          protected void scheduleFinally(Scheduler.ScheduledCommand command)

      Schedules the command.

      Protected so that subclasses can override to use an alternative scheduler.

      Parameters:
        `command` - the command to execute

    - #### calculateModifiedRanges

          java.util.List<Range> calculateModifiedRanges(JsArrayInteger modifiedRows,
                                                      int pageStart,
                                                      int pageEnd)

      Combine the modified row indexes into as many as two `Range`s, optimizing to have the fewest unmodified rows within the ranges. Using two ranges covers the most common use cases of selecting one row, selecting a range, moving selection from one row to another, or moving keyboard selection.

      Visible for testing.

      This method has the side effect of sorting the modified rows.

      Parameters:
        `modifiedRows` - the unordered indexes of modified rows
      Returns:
        up to two ranges that encompass the modified rows