Class GTableFilterPanel<ROW_OBJECT>

  • Type Parameters:
    ROW_OBJECT - the row object type for this given table and model
    All Implemented Interfaces:
    java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible
    Direct Known Subclasses:
    GhidraTableFilterPanel

    public class GTableFilterPanel<ROW_OBJECT>
    extends javax.swing.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:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class javax.swing.JPanel

        javax.swing.JPanel.AccessibleJPanel
      • Nested classes/interfaces inherited from class javax.swing.JComponent

        javax.swing.JComponent.AccessibleJComponent
      • Nested classes/interfaces inherited from class java.awt.Container

        java.awt.Container.AccessibleAWTContainer
      • Nested classes/interfaces inherited from class java.awt.Component

        java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String FILTER_TEXTFIELD_NAME  
      • Fields inherited from class javax.swing.JComponent

        listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
      • Fields inherited from class java.awt.Component

        accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
      • Fields inherited from interface java.awt.image.ImageObserver

        ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addEnterListener​(Callback callback)
      Adds a listener to this widget that is called when the user presses enter in the filtering area.
      void addFilterChagnedListener​(FilterListener l)
      Adds a listener that gets notified when the filter is changed
      protected RowObjectFilterModel<ROW_OBJECT> createTextFilterModel​(RowObjectTableModel<ROW_OBJECT> model)  
      java.lang.String createUniqueFilterPreferenceKey​(javax.swing.JTable jTable)
      Generates a key used to store user filter configuration state.
      void dispose()  
      ColumnBasedTableFilter<ROW_OBJECT> getColumnTableFilter()
      Returns the ColumnTableFilter that has been set on this GTableFilterPanel or null if there is none.
      protected TableFilter<ROW_OBJECT> getCombinedTableFilter​(TableFilter<ROW_OBJECT> filter1, TableFilter<ROW_OBJECT> filter2, TableFilter<ROW_OBJECT> filter3)  
      java.lang.String getFilterText()
      Gets the contents of the filter's text field.
      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.
      java.lang.String getPreferenceKey()
      Return a unique key that can be used to store preferences for this table.
      int getRowCount()  
      ROW_OBJECT getRowObject​(int viewRow)
      Returns the row object for the given view row index.
      ROW_OBJECT getSelectedItem()
      Returns the currently selected row object or null if there is no table selection.
      java.util.List<ROW_OBJECT> getSelectedItems()
      Returns the currently selected row objects or an empty list if there is no selection.
      protected javax.swing.JTable getTable()  
      RowObjectFilterModel<ROW_OBJECT> getTableFilterModel()  
      int getUnfilteredRowCount()  
      int getViewRow​(int modelRow)
      Returns a row number in the table (the view) for the given table model row number (the model).
      protected RowObjectFilterModel<ROW_OBJECT> installTableModel​(RowObjectTableModel<ROW_OBJECT> currentModel)  
      boolean isFiltered()  
      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.
      void requestFocus()
      Overridden to focus the text field if requestFocus() is called on this panel
      void scrollToSelectedRow()
      Scrolls the view to the currently selected item.
      void setColumnTableFilter​(ColumnBasedTableFilter<ROW_OBJECT> newFilter)
      Sets a ColumnTableFilter on this panel.
      void setFilterOptions​(FilterOptions filterOptions)
      Sets the filter options used by the filter factory.
      void setFilterRowTransformer​(RowFilterTransformer<ROW_OBJECT> transformer)
      Sets a custom RowFilterTransformer.
      void setFilterText​(java.lang.String text)
      Sets the contents of the filter's text field to the given text.
      void setFocusComponent​(java.awt.Component component)
      Setting this component will trigger the filter field to flash when the component gains focus.
      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.
      void setSelectedItem​(ROW_OBJECT t)
      Select the given row object.
      void setToolTipText​(java.lang.String text)
      Allows the caller to set tooltip text on the filter's search label.
      void updateSavedFilters​(ColumnBasedTableFilter<ROW_OBJECT> filter, boolean add)
      Updates the "quick filter" multistate button.
      • Methods inherited from class javax.swing.JPanel

        getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
      • Methods inherited from class javax.swing.JComponent

        addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
      • Methods inherited from class java.awt.Container

        add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
      • Methods inherited from class java.awt.Component

        action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • FILTER_TEXTFIELD_NAME

        public static final java.lang.String FILTER_TEXTFIELD_NAME
        See Also:
        Constant Field Values
    • Constructor Detail

      • GTableFilterPanel

        public GTableFilterPanel​(javax.swing.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​(javax.swing.JTable table,
                                 RowObjectTableModel<ROW_OBJECT> tableModel,
                                 java.lang.String filterLabel)
    • Method Detail

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

        protected javax.swing.JTable getTable()
      • dispose

        public void dispose()
      • setFocusComponent

        public void setFocusComponent​(java.awt.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 javax.swing.JComponent
      • setToolTipText

        public void setToolTipText​(java.lang.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 javax.swing.JComponent
        Parameters:
        text - The tooltip text.
      • setFilterText

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

        public java.lang.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:
        getSelectedItem(), getSelectedItems()
      • 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.
        Parameters:
        t - the row object 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 java.util.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 java.lang.String createUniqueFilterPreferenceKey​(javax.swing.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 java.lang.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.