Class GTableFilterPanel<ROW_OBJECT>

Type Parameters:
ROW_OBJECT - the row object type for this given table and model
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible
Direct Known Subclasses:
GhidraTableFilterPanel

public class GTableFilterPanel<ROW_OBJECT> extends JPanel
This class is a panel that provides a label and text field that allows users to input text that filters the contents of the table.

This class also handles restoring selection for the client when the table has been filtered. See below for a caveat.

Filter Reminder
The filter text will flash as the table (by default) gains focus. This is done to remind the user that the data has been filtered. To change the component that triggers the flashing use setFocusComponent(Component), where the Component parameter is the component that will trigger focus flashing when it gains focus. To disable focus flashing, pass in null to setFocusComponent(Component).

Filtering
The filtering behavior is controlled by the filter button displayed to the right of this panel's text field.

Important Usage Notes

  • You must translate row values retrieved from the table using this panel.

    Since this class wraps the given table with a new model, you must use this class to translate row number values. For example, when getting the selected row, the normal Java code snippet below will give the incorrect value:

             JTable table = ...
             int selectedRowNumber = table.getSelectedRow();
         
    Instead, you must translate the returned value from above, as in the following snippet:
             JTable table = ...
             
             int selectedRowNumber = table.getSelectedRow();
             int modelRowNumber = tableFilterPanel.getModelRow( selectedRowNumber );  // see getModelRow(int)
             
         
  • This class may set a new model on the given table, which can affect how tables are sized.

    If JTable.getAutoCreateColumnsFromModel() returns true, then the columns will be recreated and resized when this class is constructed.

  • The TableFilter used by this class will be passed the empty string ("") when TableFilter.acceptsRow(Object) is called.
  • You cannot rely on JTable.getRowCount() to access all of the table data, since the data may be filtered.

    To get a row count that is always all of the model's data, call getUnfilteredRowCount().

See Also:
  • Field Details

  • Constructor Details

    • GTableFilterPanel

      public GTableFilterPanel(JTable table, RowObjectTableModel<ROW_OBJECT> tableModel)
      Creates a table filter panel that filters the contents of the given table.
      Parameters:
      table - The table whose contents will be filtered.
      tableModel - The table model used by the table--passed in by the type that we require
    • GTableFilterPanel

      public GTableFilterPanel(JTable table, RowObjectTableModel<ROW_OBJECT> tableModel, String filterLabel)
  • Method Details

    • setAccessibleNamePrefix

      public void setAccessibleNamePrefix(String namePrefix)
      Sets an accessible name on the filter component. This prefix will be used to assign meaningful accessible names to the filter text field and the filter options button such that screen readers will properly describe them.

      This prefix should be the base name that describes the type of items in the table. For example if the table contains fruits, then "Fruits" would be an appropriate prefix name. This method will then append the necessary information to name the text field and the button.

      Parameters:
      namePrefix - the accessible name prefix to assign to the filter component.
    • getCombinedTableFilter

      protected TableFilter<ROW_OBJECT> getCombinedTableFilter(TableFilter<ROW_OBJECT> filter1, TableFilter<ROW_OBJECT> filter2, TableFilter<ROW_OBJECT> filter3)
    • addFilterChagnedListener

      public void addFilterChagnedListener(FilterListener l)
      Adds a listener that gets notified when the filter is changed

      Note: this listener cannot be anonymous, as the underlying storage mechanism may be using a weak data structure. This means that you will need to store the listener in a field inside of your class.

      Parameters:
      l - the listener
    • addEnterListener

      public void addEnterListener(Callback callback)
      Adds a listener to this widget that is called when the user presses enter in the filtering area.

      Note: this listener cannot be anonymous, as the underlying storage mechanism may be using a weak data structure. This means that you will need to store the listener in a field inside of your class.

      Parameters:
      callback - the listener
    • setColumnTableFilter

      public void setColumnTableFilter(ColumnBasedTableFilter<ROW_OBJECT> newFilter)
      Sets a ColumnTableFilter on this panel.
      Parameters:
      newFilter - the ColumnTableFilter to use for filtering this table.
    • setFilterRowTransformer

      public void setFilterRowTransformer(RowFilterTransformer<ROW_OBJECT> transformer)
      Sets a custom RowFilterTransformer. The default row transformer will gather strings for each column in the table and use those strings for filtering. This method allows the user to have complete control on generating the strings used to filter a table row; for example, to only filter on some columns but not others.
      Parameters:
      transformer - the custom row to string transformer used to generate strings from a row to be used for filtering.
    • setSecondaryFilter

      public void setSecondaryFilter(TableFilter<ROW_OBJECT> tableFilter)
      Sets a secondary filter that users can use to filter table rows by other criteria other than the text typed in at the bottom of a table. This filter is an additional filter that will be applied with the typed text filter.
      Parameters:
      tableFilter - the additional filter to use for the table.
    • setFilterOptions

      public void setFilterOptions(FilterOptions filterOptions)
      Sets the filter options used by the filter factory. The options are items like "starts with", "contains", "regex", etc.
      Parameters:
      filterOptions - the filter options to be used by the filter factory.
    • installTableModel

      protected RowObjectFilterModel<ROW_OBJECT> installTableModel(RowObjectTableModel<ROW_OBJECT> currentModel)
    • createTextFilterModel

      protected RowObjectFilterModel<ROW_OBJECT> createTextFilterModel(RowObjectTableModel<ROW_OBJECT> model)
    • getTable

      protected JTable getTable()
    • getTableFilterModel

      public RowObjectFilterModel<ROW_OBJECT> getTableFilterModel()
    • dispose

      public void dispose()
    • setFocusComponent

      public void setFocusComponent(Component component)
      Setting this component will trigger the filter field to flash when the component gains focus. If you do not want the filter field to flash as focus returns to the client, then pass in null.
      Parameters:
      component - The component that will trigger the filter field to flash when it gains focus.
    • requestFocus

      public void requestFocus()
      Overridden to focus the text field if requestFocus() is called on this panel
      Overrides:
      requestFocus in class JComponent
    • setToolTipText

      public void setToolTipText(String text)
      Allows the caller to set tooltip text on the filter's search label. This can be used to provide an indication as to exactly how the filter text field will filter the table.
      Overrides:
      setToolTipText in class JComponent
      Parameters:
      text - The tooltip text.
    • setFilterText

      public void setFilterText(String text)
      Sets the contents of the filter's text field to the given text.
      Parameters:
      text - The text to set.
    • getFilterText

      public String getFilterText()
      Gets the contents of the filter's text field.
      Returns:
      The filter text field text.
    • getModelRow

      public int getModelRow(int viewRow)
      Returns a row number for this panel's underlying table model that is tied to the given row number that represents a row in a table's display. For example, if a user clicks a table row in a filtered table, then this method can be used to return the table's underlying TableModel row index for that row. Click here for more information.

      Update: The simpler way of getting the selected object is to call the newly added getSelectedItem() method(s), which saves the client from having to get the index and then lookup the data. Further, it handles differences in filtering across different model implementations.

      This method is used as a means for models to translate user actions on a table to the underlying data model, since table models maintain a complete list of data, some of which may not be displayed, due to user filtering.

      This is the companion method to getViewRow(int)

      Parameters:
      viewRow - The table's row, as seen in the display.
      Returns:
      the corresponding model row, based upon the table's row.
      See Also:
    • getViewRow

      public int getViewRow(int modelRow)
      Returns a row number in the table (the view) for the given table model row number (the model). The given value is the unfiltered row value and the returned value is the filtered value.

      This is the companion method to getModelRow(int)

      Parameters:
      modelRow - the row number in the unfiltered model.
      Returns:
      the row in the table for the given model row.
    • getRowObject

      public ROW_OBJECT getRowObject(int viewRow)
      Returns the row object for the given view row index.
      Parameters:
      viewRow - the desired row in terms of the UI (e.g., the table's row index)
      Returns:
      the row object matching the given index
    • setSelectedItem

      public void setSelectedItem(ROW_OBJECT t)
      Select the given row object. No selection will be made if the object is filtered out of view. Passing null will clear the selection.
      Parameters:
      t - the row object to select
    • setSelectedItems

      public void setSelectedItems(List<ROW_OBJECT> items)
      Select the given row objects. No selection will be made if the objects are filtered out of view. Passing a null list or an empty list will clear the selection.
      Parameters:
      items - the row objects to select
    • scrollToSelectedRow

      public void scrollToSelectedRow()
      Scrolls the view to the currently selected item.
    • getSelectedItem

      public ROW_OBJECT getSelectedItem()
      Returns the currently selected row object or null if there is no table selection.
      Returns:
      the currently selected row object or null if there is no table selection.
    • getSelectedItems

      public List<ROW_OBJECT> getSelectedItems()
      Returns the currently selected row objects or an empty list if there is no selection.
      Returns:
      the currently selected row objects or an empty list if there is no selection.
    • isInView

      public boolean isInView(ROW_OBJECT o)
      Returns true if the given row object is currently in the view of the table; false implies the object has been filtered out of view.
      Parameters:
      o - the row object
      Returns:
      true if in the view
    • isFiltered

      public boolean isFiltered()
    • getRowCount

      public int getRowCount()
    • getUnfilteredRowCount

      public int getUnfilteredRowCount()
    • createUniqueFilterPreferenceKey

      public String createUniqueFilterPreferenceKey(JTable jTable)
      Generates a key used to store user filter configuration state. You can override this method to generate unique keys yourself. You are required to override this method if you create multiple versions of a filter panel from the same place in your code, as multiple instances created in the same place will cause them all to share the same key and thus to have the same filter settings when they are created initially.

      As an example, consider a plugin that creates n providers. If each provider uses a filter panel, then each provider will share the same filter settings when that provider is created. If this is not what you want, then you need to override this method to generate a unique key for each provider.

      Parameters:
      jTable - the table
      Returns:
      a key used to store user filter configuration state.
    • getColumnTableFilter

      public ColumnBasedTableFilter<ROW_OBJECT> getColumnTableFilter()
      Returns the ColumnTableFilter that has been set on this GTableFilterPanel or null if there is none.
      Returns:
      the ColumnTableFilter that has been set.
    • getPreferenceKey

      public String getPreferenceKey()
      Return a unique key that can be used to store preferences for this table.
      Returns:
      a unique key that can be used to store preferences for this table.
    • updateSavedFilters

      public void updateSavedFilters(ColumnBasedTableFilter<ROW_OBJECT> filter, boolean add)
      Updates the "quick filter" multistate button.
      Parameters:
      filter - the filter to add or remove.
      add - if true, the filter is added to the quick list. Otherwise, it is removed.