Class FilterRowComboBoxDataProvider<T>

java.lang.Object
org.eclipse.nebula.widgets.nattable.filterrow.combobox.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

public class FilterRowComboBoxDataProvider<T> extends Object implements IComboBoxDataProvider, ILayerListener, ILayerCommandHandler<UpdateDataCommand>
IComboBoxDataProvider that provides items for a combobox in the filter row. These items are calculated dynamically based on the content contained in the column it is connected to.

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 Details

    • lazyLoading

      protected final boolean lazyLoading
      Flag to indicate whether the combo box content should be loaded lazily.
      Since:
      1.4
    • cachingEnabled

      protected boolean cachingEnabled
      Flag 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 this FilterRowComboBoxDataProvider should load the combobox values lazily, false to pre-build the value cache.
      Since:
      1.4
  • Method Details

    • getValues

      public List<?> getValues(int columnIndex, int rowIndex)
      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 registered IDisplayConverter will handle the conversion to display values when the ComboBoxCellEditor is filled with the selectable items. There is no need to convert within this method.
      Specified by:
      getValues in interface IComboBoxDataProvider
      Parameters:
      columnIndex - The column index of the cell whose ComboBoxCellEditor should be filled.
      rowIndex - The row index of the cell whose ComboBoxCellEditor should be filled.
      Returns:
      List of values that should be used to fill the values of the ComboBoxCellEditor
    • getAllValues

      public List<?> getAllValues(int columnIndex)
      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

      public Collection<Integer> 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

      protected List<?> collectValues(int columnIndex)
      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

      protected List<?> collectValuesForColumn(int columnIndex)
      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

      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.
      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

      public void handleLayerEvent(ILayerEvent event)
      Description copied from interface: ILayerListener
      Handle an event notification from an ILayer
      Specified by:
      handleLayerEvent in interface ILayerListener
      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

      protected void updateCache(int columnIndex, Map<Integer,List<?>> cache, ReadWriteLock cacheLock)
      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 if FilterRowComboUpdateEvents should be fired, false if not.
      Since:
      2.1
    • isEventFromBodyLayer

      protected boolean isEventFromBodyLayer(ILayerEvent event)
      Checks if the given ILayerEvent 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

      protected boolean isFilterChanged(int columnIndex, Object oldValue, Object newValue)
      Checks if a filter was changed. Also handles EditConstants.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 new FilterRowComboUpdateEvent 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 new FilterRowComboUpdateEvent or updates the given FilterRowComboUpdateEvent for the given column index. Calculates the diffs of the value cache for that column based on the given lists.
      Parameters:
      event - the FilterRowComboUpdateEvent to update, or null 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

      protected void fireCacheUpdateEvent(FilterRowComboUpdateEvent event)
      Fire the given event to all registered listeners.
      Parameters:
      event - The event to handle.
    • addCacheUpdateListener

      public void addCacheUpdateListener(IFilterRowComboUpdateListener listener)
      Adds the given listener to the list of listeners for value cache updates.
      Parameters:
      listener - The listener to add.
    • removeCacheUpdateListener

      public void removeCacheUpdateListener(IFilterRowComboUpdateListener listener)
      Removes the given listener from the list of listeners for value cache updates.
      Parameters:
      listener - The listener to remove.
      Since:
      1.6
    • getValueCache

      protected Map<Integer,List<?>> 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 a FilterRowComboUpdateEvent is fired in case of filter value cache updates, false if not.
      Since:
      1.6
    • enableUpdateEvents

      public void enableUpdateEvents()
      Enable firing of FilterRowComboUpdateEvent 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 of FilterRowComboUpdateEvent 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

      public ReadWriteLock getValueCacheLock()
      Returns:
      The ReadWriteLock that should be used for locking on accessing the valueCache.
      Since:
      1.6
    • isDistinctNullAndEmpty

      public boolean isDistinctNullAndEmpty()
      Returns:
      true if null 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 how null and empty values are handled on collecting the values. false means null and empty are collected as two separate values, true will distinct them into a single "empty" value.
      Parameters:
      distinctNullAndEmpty - true if null and empty values are distinct to a single "empty" value, false if they are treated as separate values.
      Since:
      2.1
    • getFilterCollection

      public Collection<T> getFilterCollection()
      Returns:
      The collection that contains filtered body data. Can be null.
      Since:
      2.1
    • setFilterCollection

      public 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.
      Parameters:
      filterCollection - The collection that contains filtered body data. Can be null.
      columnHeaderLayer - A layer in the column header region in which the filter row is included. Needed to handle the FilterAppliedEvent.
      Throws:
      IllegalArgumentException - if one of the parameters is null while the other isn't
      Since:
      2.1
    • setLastFilter

      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.
      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

      public boolean doCommand(ILayer targetLayer, UpdateDataCommand command)
      Specified by:
      doCommand in interface ILayerCommandHandler<T>
      Parameters:
      targetLayer - The target ILayer.
      command - The UpdateDataCommand to process.
      Returns:
      false as this ILayerCommandHandler 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

      public void configureComparator(ILayer columnHeaderDataLayer, IConfigRegistry configRegistry)
      Set the IConfigRegistry that should be used to retrieve the comparator to sort the filter collection. If one of the parameters is null the filter collection will always be sorted via Comparator.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 - The IConfigRegistry of the underlying NatTable.
      Since:
      2.3
    • getColumnComparator

      protected Comparator<?> getColumnComparator(int columnIndex)
      Return the Comparator that should be used to sort the filter collection of the given column.
      Parameters:
      columnIndex - The column for which the Comparator should be returned.
      Returns:
      The Comparator that should be used to sort the filter collection of the given column. The default is Comparator.naturalOrder().
      Since:
      2.3
    • setContentFilter

      public void setContentFilter(Predicate<T> predicate)
      Parameters:
      predicate - The Predicate to define which values should not be added to the filter combobox. Setting null will result in no filtering, which is the default.
      Since:
      2.3
    • getCommandClass

      public Class<UpdateDataCommand> getCommandClass()
      Specified by:
      getCommandClass in interface ILayerCommandHandler<T>
      Returns:
      The class that is handled by this ILayerCommandHandler
      Since:
      2.1