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
HasDatathat 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 Description (package private) static interfaceHasDataPresenter.ElementIteratorAn iterator over DOM elements.(package private) static interfaceHasDataPresenter.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 Description (package private) static intPAGE_INCREMENTThe number of rows to jump when PAGE_UP or PAGE_DOWN is pressed and theHasKeyboardPagingPolicy.KeyboardPagingPolicyisHasKeyboardPagingPolicy.KeyboardPagingPolicy.INCREASE_RANGE.
-
Constructor Summary
Constructors Constructor Description HasDataPresenter(HasData<T> display, HasDataPresenter.View<T> view, int pageSize, ProvidesKey<T> keyProvider)Construct a newHasDataPresenter.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description HandlerRegistrationaddCellPreviewHandler(CellPreviewEvent.Handler<T> handler)Adds aCellPreviewEventhandler.HandlerRegistrationaddLoadingStateChangeHandler(LoadingStateChangeEvent.Handler handler)HandlerRegistrationaddRangeChangeHandler(RangeChangeEvent.Handler handler)Add aRangeChangeEvent.Handler.HandlerRegistrationaddRowCountChangeHandler(RowCountChangeEvent.Handler handler)Add aRowCountChangeEvent.Handler.(package private) java.util.List<Range>calculateModifiedRanges(JsArrayInteger modifiedRows, int pageStart, int pageEnd)Combine the modified row indexes into as many as twoRanges, optimizing to have the fewest unmodified rows within the ranges.voidclearKeyboardSelectedRowValue()Clear the row value associated with the keyboard selected row.voidclearSelectionModel()Clear theSelectionModelwithout updating the view.voidfireEvent(GwtEvent<?> event)Fires the given event to the handlers listening to the event's type.voidflush()Flush pending changes to the view.intgetCurrentPageSize()Get the current page size.HasKeyboardPagingPolicy.KeyboardPagingPolicygetKeyboardPagingPolicy()intgetKeyboardSelectedRow()Get the index of the keyboard selected row relative to the page start.intgetKeyboardSelectedRowInView()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.TgetKeyboardSelectedRowValue()Get the value that the user selected.HasKeyboardSelectionPolicy.KeyboardSelectionPolicygetKeyboardSelectionPolicy()ProvidesKey<T>getKeyProvider()Return the key provider.intgetRowCount()Get the overall data size.SelectionModel<? super T>getSelectionModel()Get theSelectionModelused by thisHasData.TgetVisibleItem(int indexOnPage)Get the row value at the specified visible index.intgetVisibleItemCount()Get the number of visible items being displayed.java.util.List<T>getVisibleItems()Get anIterablecomposed of all of the visible items.RangegetVisibleRange()Return the range of data being displayed.booleanhasPendingState()Check whether or not there is a pending state.booleanisEmpty()Check whether or not the data set is empty.booleanisRowCountExact()Check if the total row count is exact, or an estimate.voidredraw()Redraw the list with the current data.protected voidscheduleFinally(Scheduler.ScheduledCommand command)Schedules the command.voidsetKeyboardPagingPolicy(HasKeyboardPagingPolicy.KeyboardPagingPolicy policy)voidsetKeyboardSelectedRow(int index, boolean stealFocus, boolean forceUpdate)Set the row index of the keyboard selected element.voidsetKeyboardSelectionPolicy(HasKeyboardSelectionPolicy.KeyboardSelectionPolicy policy)voidsetRowCount(int count)Set the exact total count of all rows.voidsetRowCount(int count, boolean isExact)Set the total count of all rows, specifying whether the count is exact or an estimate.voidsetRowData(int start, java.util.List<? extends T> values)Set a values associated with the rows in the visible range.voidsetSelectionModel(SelectionModel<? super T> selectionModel)Set theSelectionModelused by thisHasData.voidsetVisibleRange(int start, int length)Set the visible range or rows.voidsetVisibleRange(Range range)Set the visible range or rows.voidsetVisibleRangeAndClearData(Range range, boolean forceRangeChangeEvent)Set the visible range and clear the current visible data.
-
-
-
Field Detail
-
PAGE_INCREMENT
static final int PAGE_INCREMENT
The number of rows to jump when PAGE_UP or PAGE_DOWN is pressed and theHasKeyboardPagingPolicy.KeyboardPagingPolicyisHasKeyboardPagingPolicy.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 newHasDataPresenter.- Parameters:
display- the display that is being presentedview- the view implementationpageSize- the default page size
-
-
Method Detail
-
addCellPreviewHandler
public HandlerRegistration addCellPreviewHandler(CellPreviewEvent.Handler<T> handler)
Description copied from interface:HasCellPreviewHandlersAdds aCellPreviewEventhandler.- Specified by:
addCellPreviewHandlerin interfaceHasCellPreviewHandlers<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:HasRowsAdd aRangeChangeEvent.Handler.- Specified by:
addRangeChangeHandlerin interfaceHasRows- Parameters:
handler- the handler- Returns:
- a
HandlerRegistrationto remove the handler
-
addRowCountChangeHandler
public HandlerRegistration addRowCountChangeHandler(RowCountChangeEvent.Handler handler)
Description copied from interface:HasRowsAdd aRowCountChangeEvent.Handler.- Specified by:
addRowCountChangeHandlerin interfaceHasRows- Parameters:
handler- the handler- Returns:
- a
HandlerRegistrationto remove the handler
-
clearKeyboardSelectedRowValue
public void clearKeyboardSelectedRowValue()
Clear the row value associated with the keyboard selected row.
-
clearSelectionModel
public void clearSelectionModel()
Clear theSelectionModelwithout updating the view.
-
fireEvent
public void fireEvent(GwtEvent<?> event)
Description copied from interface:HasHandlersFires the given event to the handlers listening to the event's type.Any exceptions thrown by handlers will be bundled into a
UmbrellaExceptionand then re-thrown after all handlers have completed. An exception thrown by a handler will not prevent other handlers from executing.- Specified by:
fireEventin interfaceHasHandlers- 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- Specified by:
getKeyboardPagingPolicyin interfaceHasKeyboardPagingPolicy- 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- Specified by:
getKeyboardSelectionPolicyin interfaceHasKeyboardSelectionPolicy- Returns:
- the selection policy
- See Also:
HasKeyboardSelectionPolicy.setKeyboardSelectionPolicy(KeyboardSelectionPolicy)
-
getKeyProvider
public ProvidesKey<T> getKeyProvider()
Description copied from interface:HasKeyProviderReturn the key provider.- Specified by:
getKeyProviderin interfaceHasKeyProvider<T>- Returns:
- the
ProvidesKeyinstance
-
getRowCount
public int getRowCount()
Get the overall data size.- Specified by:
getRowCountin interfaceHasRows- Returns:
- the data size
- See Also:
HasRows.setRowCount(int)
-
getSelectionModel
public SelectionModel<? super T> getSelectionModel()
Description copied from interface:HasDataGet theSelectionModelused by thisHasData.- Specified by:
getSelectionModelin interfaceHasData<T>- Returns:
- the
SelectionModel - See Also:
HasData.setSelectionModel(SelectionModel)
-
getVisibleItem
public T getVisibleItem(int indexOnPage)
Description copied from interface:HasDataGet the row value at the specified visible index. Index 0 corresponds to the first item on the page.- Specified by:
getVisibleItemin interfaceHasData<T>- Parameters:
indexOnPage- the index on the page- Returns:
- the row value
-
getVisibleItemCount
public int getVisibleItemCount()
Description copied from interface:HasDataGet 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:
getVisibleItemCountin interfaceHasData<T>- Returns:
- the number of visible items on the page
-
getVisibleItems
public java.util.List<T> getVisibleItems()
Description copied from interface:HasDataGet anIterablecomposed of all of the visible items.- Specified by:
getVisibleItemsin interfaceHasData<T>- Returns:
- an
Iterableinstance
-
getVisibleRange
public Range getVisibleRange()
Return the range of data being displayed.- Specified by:
getVisibleRangein interfaceHasRows- 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:HasRowsCheck if the total row count is exact, or an estimate.- Specified by:
isRowCountExactin interfaceHasRows- 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- Specified by:
setKeyboardPagingPolicyin interfaceHasKeyboardPagingPolicy- 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 indexstealFocus- true to steal focusforceUpdate- force the update even if the row didn't change
-
setKeyboardSelectionPolicy
public void setKeyboardSelectionPolicy(HasKeyboardSelectionPolicy.KeyboardSelectionPolicy policy)
Description copied from interface:HasKeyboardSelectionPolicy- Specified by:
setKeyboardSelectionPolicyin interfaceHasKeyboardSelectionPolicy- Parameters:
policy- the selection policy- See Also:
HasKeyboardSelectionPolicy.getKeyboardSelectionPolicy()
-
setRowCount
public final void setRowCount(int count)
Description copied from interface:HasRowsSet the exact total count of all rows. This method defers toHasRows.setRowCount(int, boolean).- Specified by:
setRowCountin interfaceHasRows- 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:HasRowsSet the total count of all rows, specifying whether the count is exact or an estimate.- Specified by:
setRowCountin interfaceHasRows- Parameters:
count- the total countisExact- 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:HasDataSet 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 specified values. You must call
HasRows.setRowCount(int)to set the total number of rows in the display. You should also useHasRows.setRowCount(int)to remove rows when the total number of rows decreases.- Specified by:
setRowDatain interfaceHasData<T>- Parameters:
start- the start index of the datavalues- the values within the range
-
setSelectionModel
public void setSelectionModel(SelectionModel<? super T> selectionModel)
Description copied from interface:HasDataSet theSelectionModelused by thisHasData.- Specified by:
setSelectionModelin interfaceHasData<T>- Parameters:
selectionModel- theSelectionModel- See Also:
HasData.getSelectionModel()
-
setVisibleRange
public final void setVisibleRange(int start, int length)Description copied from interface:HasRowsSet the visible range or rows. This method defers toHasRows.setVisibleRange(Range).- Specified by:
setVisibleRangein interfaceHasRows- Parameters:
start- the start indexlength- the length- Throws:
java.lang.UnsupportedOperationException- See Also:
HasRows.getVisibleRange()
-
setVisibleRange
public void setVisibleRange(Range range)
Description copied from interface:HasRowsSet the visible range or rows.- Specified by:
setVisibleRangein interfaceHasRows- Parameters:
range- the visible range- See Also:
HasRows.getVisibleRange()
-
setVisibleRangeAndClearData
public void setVisibleRangeAndClearData(Range range, boolean forceRangeChangeEvent)
Description copied from interface:HasDataSet the visible range and clear the current visible data.
If the second argument
forceRangeChangeEventis true, aRangeChangeEventwill be fired even if the range does not change. If false, aRangeChangeEventwill only be fired if the range changes.- Specified by:
setVisibleRangeAndClearDatain interfaceHasData<T>- Parameters:
range- the newRangeforceRangeChangeEvent- true to fire aRangeChangeEventeven if theRangedoesn'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 twoRanges, 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
-
-