Class FilterRowComboBoxDataProvider<T>
- Type Parameters:
T
- The type of the objects shown within the NatTable. Needed to access the data columnwise.
- All Implemented Interfaces:
ILayerCommandHandler<UpdateDataCommand>
,IComboBoxDataProvider
,ILayerListener
- Direct Known Subclasses:
GlazedListsFilterRowComboBoxDataProvider
On creating this IComboBoxDataProvider, the possible values for all columns will be calculated taking the whole data provided by the body IDataProvider into account. Therefore you shouldn't use this one if you show huge datasets at once.
As the values are cached in here, this IComboBoxDataProvider registers itself as ILayerListener to the body DataLayer. If values are updated or rows get added/deleted, it will update the cache accordingly.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected boolean
Flag for enabling/disabling caching of filter combo box values.protected final boolean
Flag to indicate whether the combo box content should be loaded lazily. -
Constructor Summary
ConstructorsConstructorDescriptionFilterRowComboBoxDataProvider
(ILayer bodyLayer, Collection<T> baseCollection, IColumnAccessor<T> columnAccessor) FilterRowComboBoxDataProvider
(ILayer bodyLayer, Collection<T> baseCollection, IColumnAccessor<T> columnAccessor, boolean lazy) -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds the given listener to the list of listeners for value cache updates.protected FilterRowComboUpdateEvent
buildUpdateEvent
(int columnIndex, List<?> cacheBefore, List<?> cacheAfter) Creates a newFilterRowComboUpdateEvent
for the given column index.protected FilterRowComboUpdateEvent
buildUpdateEvent
(FilterRowComboUpdateEvent event, int columnIndex, List<?> cacheBefore, List<?> cacheAfter) Creates a newFilterRowComboUpdateEvent
or updates the givenFilterRowComboUpdateEvent
for the given column index.protected void
Builds the local value cache for all columns.protected void
clearCache
(boolean updateEventsFromAll) Clear the cache.protected void
clearCache
(Map<Integer, List<?>> cache, ReadWriteLock cacheLock, boolean fireUpdateEvents) Clear the cache.protected List<?>
collectValues
(int columnIndex) Iterates over all rows of the base collection and collects the distinct values for the given column index.protected List<?>
collectValues
(Collection<T> collection, int columnIndex) Iterates over all rows of the given collection and collects the distinct values for the given column index.protected List<?>
collectValuesForColumn
(int columnIndex) Collects the distinct values for the given column index.void
configureComparator
(ILayer columnHeaderDataLayer, IConfigRegistry configRegistry) Set theIConfigRegistry
that should be used to retrieve the comparator to sort the filter collection.void
Disable firing ofFilterRowComboUpdateEvent
if the filter value cache is updated.void
dispose()
Cleanup acquired resources.boolean
doCommand
(ILayer targetLayer, UpdateDataCommand command) void
Enable firing ofFilterRowComboUpdateEvent
if the filter value cache is updated.protected void
Fire the given event to all registered listeners.List<?>
getAllValues
(int columnIndex) Returns the collection of all distinct values for the given column.This method returns the column indexes of the columns for which values were cached.protected Comparator<?>
getColumnComparator
(int columnIndex) Return theComparator
that should be used to sort the filter collection of the given column.List<?>
getValues
(int columnIndex, int rowIndex) Will determine the values for the cell at the specified position.protected List<?>
getValues
(Collection<T> collection, int columnIndex, Map<Integer, List<?>> cache, ReadWriteLock cacheLock) void
handleLayerEvent
(ILayerEvent event) Handle an event notification from anILayer
boolean
boolean
protected boolean
isEventFromBodyLayer
(ILayerEvent event) Checks if the givenILayerEvent
was fired from the body layer.protected boolean
Simplified check if a filter is applied by comparing the size of the base collection with the size of the filter collection.protected boolean
isFilterChanged
(int columnIndex, Object oldValue, Object newValue) Checks if a filter was changed.boolean
void
Removes the given listener from the list of listeners for value cache updates.void
setCachingEnabled
(boolean cachingEnabled) Enable/disable the caching of filterrow combobox values.void
setContentFilter
(Predicate<T> predicate) void
setDistinctNullAndEmpty
(boolean distinctNullAndEmpty) Setting this value effects on hownull
and empty values are handled on collecting the values.void
setFilterCollection
(Collection<T> filterCollection, ILayer columnHeaderLayer) By setting a filter collection it is possible to only show the distinct values for the current available items in the table.protected void
setLastFilter
(int columnIndex, Collection<T> collection) Remember the column and the previous collection state to be able to restore the filter combobox state of the last used combobox filter.protected void
updateCache
(int columnIndex) Update the cache for the given column index.protected void
updateCache
(int columnIndex, Map<Integer, List<?>> cache, ReadWriteLock cacheLock) Update the cache for the given column index.
-
Field Details
-
lazyLoading
protected final boolean lazyLoadingFlag to indicate whether the combo box content should be loaded lazily.- Since:
- 1.4
-
cachingEnabled
protected boolean cachingEnabledFlag for enabling/disabling caching of filter combo box values.- Since:
- 1.4
-
-
Constructor Details
-
FilterRowComboBoxDataProvider
public FilterRowComboBoxDataProvider(ILayer bodyLayer, Collection<T> baseCollection, IColumnAccessor<T> columnAccessor) - Parameters:
bodyLayer
- A layer in the body region. Usually the DataLayer or a layer that is responsible for list event handling. Needed to register ourself as listener for data changes.baseCollection
- The base collection used to collect the unique values from. This need to be a collection that is not filtered, otherwise after modifications the content of the filter row combo boxes will only contain the current visible (not filtered) elements.columnAccessor
- The IColumnAccessor to be able to read the values out of the base collection objects.
-
FilterRowComboBoxDataProvider
public FilterRowComboBoxDataProvider(ILayer bodyLayer, Collection<T> baseCollection, IColumnAccessor<T> columnAccessor, boolean lazy) - Parameters:
bodyLayer
- A layer in the body region. Usually the DataLayer or a layer that is responsible for list event handling. Needed to register ourself as listener for data changes.baseCollection
- The base collection used to collect the unique values from. This need to be a collection that is not filtered, otherwise after modifications the content of the filter row combo boxes will only contain the current visible (not filtered) elements.columnAccessor
- The IColumnAccessor to be able to read the values out of the base collection objects.lazy
-true
to configure thisFilterRowComboBoxDataProvider
should load the combobox values lazily,false
to pre-build the value cache.- Since:
- 1.4
-
-
Method Details
-
getValues
Description copied from interface:IComboBoxDataProvider
Will determine the values for the cell at the specified position. It will return a list with canonical values. The registeredIDisplayConverter
will handle the conversion to display values when theComboBoxCellEditor
is filled with the selectable items. There is no need to convert within this method.- Specified by:
getValues
in interfaceIComboBoxDataProvider
- Parameters:
columnIndex
- The column index of the cell whoseComboBoxCellEditor
should be filled.rowIndex
- The row index of the cell whoseComboBoxCellEditor
should be filled.- Returns:
- List of values that should be used to fill the values of the
ComboBoxCellEditor
-
getAllValues
Returns the collection of all distinct values for the given column. Will use the non-filtered base collection, so it returns always all values, even if they are not visible.- Parameters:
columnIndex
- The column index for which the values are requested.- Returns:
- List of all distinct values for the given column.
- Since:
- 2.1
-
getValues
protected List<?> getValues(Collection<T> collection, int columnIndex, Map<Integer, List<?>> cache, ReadWriteLock cacheLock) - Parameters:
collection
- The collection out of which the distinct values should be collected.columnIndex
- The column index for which the values are requested.- Returns:
- List of all distinct values that are contained in the given collection for the given column.
- Since:
- 2.1
-
buildValueCache
protected void buildValueCache()Builds the local value cache for all columns. -
getCachedColumnIndexes
This method returns the column indexes of the columns for which values were cached. Usually it will return all column indexes that are available in the table.- Returns:
- The column indexes of the columns for which values were cached.
-
collectValues
Iterates over all rows of the base collection and collects the distinct values for the given column index.- Parameters:
columnIndex
- The column index for which the values should be collected.- Returns:
- List of all distinct values that are contained in the base collection for the given column.
-
collectValuesForColumn
Collects the distinct values for the given column index. Determines the collection to iterate over based on the information whether a filter list is configured and a filter is applied.- Parameters:
columnIndex
- The column index for which the values should be collected.- Returns:
- List of all distinct values that are contained in the determined collection for the given column.
- Since:
- 2.1
-
collectValues
Iterates over all rows of the given collection and collects the distinct values for the given column index.- Parameters:
collection
- The collection out of which the distinct values should be collected.columnIndex
- The column index for which the values should be collected- Returns:
- List of all distinct values that are contained in the given collection for the given column.
- Since:
- 2.1
-
handleLayerEvent
Description copied from interface:ILayerListener
Handle an event notification from anILayer
- Specified by:
handleLayerEvent
in interfaceILayerListener
- Parameters:
event
- the event
-
updateCache
protected void updateCache(int columnIndex) Update the cache for the given column index.- Parameters:
columnIndex
- The column index for which the cache should be updated.- Since:
- 2.1
-
updateCache
Update the cache for the given column index.- Parameters:
columnIndex
- The column index for which the cache should be updated.cache
- The cache to update (cache for current values or cache for all values).cacheLock
- The lock that matches the given cache.- Since:
- 2.1
-
clearCache
protected void clearCache(boolean updateEventsFromAll) Clear the cache.- Parameters:
updateEventsFromAll
-true
if the filter update events should be fired for the all value cache,false
if the events should be fired for the value cache.- Since:
- 2.1
-
clearCache
protected void clearCache(Map<Integer, List<?>> cache, ReadWriteLock cacheLock, boolean fireUpdateEvents) Clear the cache.- Parameters:
cache
- The cache to clear (cache for current values or cache for all values).cacheLock
- The lock that matches the given cache.fireUpdateEvents
-true
ifFilterRowComboUpdateEvent
s should be fired,false
if not.- Since:
- 2.1
-
isEventFromBodyLayer
Checks if the givenILayerEvent
was fired from the body layer.- Parameters:
event
- The event to check.- Returns:
true
if the event was fired from the body layer,false
if not.- Since:
- 2.1
-
isFilterChanged
Checks if a filter was changed. Also handlesEditConstants.SELECT_ALL_ITEMS_VALUE
and a collection of values on testing.- Parameters:
columnIndex
- The column index to check.oldValue
- The old value.newValue
- The new value.- Returns:
true
if a value was changed,false
if not.- Since:
- 2.1
-
buildUpdateEvent
protected FilterRowComboUpdateEvent buildUpdateEvent(int columnIndex, List<?> cacheBefore, List<?> cacheAfter) Creates a newFilterRowComboUpdateEvent
for the given column index. Calculates the diffs of the value cache for that column based on the given lists.- Parameters:
columnIndex
- The column index for which the value cache was updated.cacheBefore
- The value cache for the column before the change. Needed to determine which values where removed by the update.cacheAfter
- The value cache for the column after the change. Needed to determine which values where added by the update.- Returns:
- Event to tell about value cache updates for the given column or
null
if nothing has changed.
-
buildUpdateEvent
protected FilterRowComboUpdateEvent buildUpdateEvent(FilterRowComboUpdateEvent event, int columnIndex, List<?> cacheBefore, List<?> cacheAfter) Creates a newFilterRowComboUpdateEvent
or updates the givenFilterRowComboUpdateEvent
for the given column index. Calculates the diffs of the value cache for that column based on the given lists.- Parameters:
event
- theFilterRowComboUpdateEvent
to update, ornull
if a new instance should be created.columnIndex
- The column index for which the value cache was updated.cacheBefore
- The value cache for the column before the change. Needed to determine which values where removed by the update.cacheAfter
- The value cache for the column after the change. Needed to determine which values where added by the update.- Returns:
- Event to tell about value cache updates for the given column or
null
if nothing has changed. - Since:
- 2.1
-
fireCacheUpdateEvent
Fire the given event to all registered listeners.- Parameters:
event
- The event to handle.
-
addCacheUpdateListener
Adds the given listener to the list of listeners for value cache updates.- Parameters:
listener
- The listener to add.
-
removeCacheUpdateListener
Removes the given listener from the list of listeners for value cache updates.- Parameters:
listener
- The listener to remove.- Since:
- 1.6
-
getValueCache
- Returns:
- The local cache for the values to show in the filter row combobox. This is needed because otherwise the calculation of the necessary values would happen everytime the combobox is opened and if a filter is applied using GlazedLists for example, the combobox would only contain the value which is currently used for filtering.
-
isCachingEnabled
public boolean isCachingEnabled()- Returns:
true
if caching of filterrow combobox values is enabled,false
if the combobox values should be calculated on request.- Since:
- 1.4
-
setCachingEnabled
public void setCachingEnabled(boolean cachingEnabled) Enable/disable the caching of filterrow combobox values. By default the caching is enabled.You should disable caching if the base collection that is used to determine the filterrow combobox values changes its contents dynamically, e.g. if the base collection is a GlazedLists FilterList that returns only the current non-filtered items.
- Parameters:
cachingEnabled
-true
to enable caching of filter row combobox values,false
if the combobox values should be calculated on request.- Since:
- 1.4
-
dispose
public void dispose()Cleanup acquired resources.- Since:
- 1.5
-
isUpdateEventsEnabled
public boolean isUpdateEventsEnabled()- Returns:
true
if aFilterRowComboUpdateEvent
is fired in case of filter value cache updates,false
if not.- Since:
- 1.6
-
enableUpdateEvents
public void enableUpdateEvents()Enable firing ofFilterRowComboUpdateEvent
if the filter value cache is updated.By default it should be enabled to automatically update applied filters in case new values are added, otherwise the row containing the new value will be filtered directly.
Note: It is important to disable firing the events in use cases where the cache is not build up yet and the filter is restored from properties, e.g. on opening a table with stored properties.
- Since:
- 1.6
-
disableUpdateEvents
public void disableUpdateEvents()Disable firing ofFilterRowComboUpdateEvent
if the filter value cache is updated.By default it should be enabled to automatically update applied filters in case new values are added, otherwise the row containing the new value will be filtered directly.
Note: It is important to disable firing the events in use cases where the cache is not build up yet and the filter is restored from properties, e.g. on opening a table with stored properties.
- Since:
- 1.6
-
getValueCacheLock
- Returns:
- The
ReadWriteLock
that should be used for locking on accessing thevalueCache
. - Since:
- 1.6
-
isDistinctNullAndEmpty
public boolean isDistinctNullAndEmpty()- Returns:
true
ifnull
and empty values are distinct to a single "empty" value,false
if they are treated as separate values.- Since:
- 2.1
-
setDistinctNullAndEmpty
public void setDistinctNullAndEmpty(boolean distinctNullAndEmpty) Setting this value effects on hownull
and empty values are handled on collecting the values.false
meansnull
and empty are collected as two separate values,true
will distinct them into a single "empty" value.- Parameters:
distinctNullAndEmpty
-true
ifnull
and empty values are distinct to a single "empty" value,false
if they are treated as separate values.- Since:
- 2.1
-
getFilterCollection
- Returns:
- The collection that contains filtered body data. Can be
null
. - Since:
- 2.1
-
setFilterCollection
By setting a filter collection it is possible to only show the distinct values for the current available items in the table.- Parameters:
filterCollection
- The collection that contains filtered body data. Can benull
.columnHeaderLayer
- A layer in the column header region in which the filter row is included. Needed to handle theFilterAppliedEvent
.- Throws:
IllegalArgumentException
- if one of the parameters isnull
while the other isn't- Since:
- 2.1
-
setLastFilter
Remember the column and the previous collection state to be able to restore the filter combobox state of the last used combobox filter.- Parameters:
columnIndex
- The column index of the column that was used for filtering.collection
- The previous collection state to be able to restore the combobox contents.- Since:
- 2.1
-
doCommand
- Specified by:
doCommand
in interfaceILayerCommandHandler<T>
- Parameters:
targetLayer
- The targetILayer
.command
- TheUpdateDataCommand
to process.- Returns:
false
as thisILayerCommandHandler
does not consume the command, it only modifies the command to avoid incorrect processing.- Since:
- 2.1
-
isFilterApplied
protected boolean isFilterApplied()Simplified check if a filter is applied by comparing the size of the base collection with the size of the filter collection.- Returns:
true
if a filter collection is set and the size is less than the size of the base collection,false
if no filter collection is set or the size is equal.- Since:
- 2.2
-
configureComparator
Set theIConfigRegistry
that should be used to retrieve the comparator to sort the filter collection. If one of the parameters isnull
the filter collection will always be sorted viaComparator.naturalOrder()
.- Parameters:
columnHeaderDataLayer
- The DataLayer of the column header region. Needed to be able to get the cell which is needed to get the comparator that should be used to sort the filter collection.configRegistry
- TheIConfigRegistry
of the underlying NatTable.- Since:
- 2.3
-
getColumnComparator
Return theComparator
that should be used to sort the filter collection of the given column.- Parameters:
columnIndex
- The column for which theComparator
should be returned.- Returns:
- The
Comparator
that should be used to sort the filter collection of the given column. The default isComparator.naturalOrder()
. - Since:
- 2.3
-
setContentFilter
- Parameters:
predicate
- ThePredicate
to define which values should not be added to the filter combobox. Settingnull
will result in no filtering, which is the default.- Since:
- 2.3
-
getCommandClass
- Specified by:
getCommandClass
in interfaceILayerCommandHandler<T>
- Returns:
- The class that is handled by this
ILayerCommandHandler
- Since:
- 2.1
-