Class ColumnControlButton

All Implemented Interfaces:
ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible, SwingConstants

public class ColumnControlButton extends JButton
A component to allow interactive customization of JXTable's columns. It's main purpose is to allow toggling of table columns' visibility. Additionally, arbitrary configuration actions can be exposed.

This component is installed in the JXTable's trailing corner, if enabled:


 table.setColumnControlVisible(true);
 
From the perspective of a JXTable, the component's behaviour is opaque. Typically, the button's action is to popup a component for user interaction.

This class is responsible for handling/providing/updating the lists of actions and to keep each Action's state in synch with Table-/Column state. The visible behaviour of the popup is delegated to a ColumnControlPopup.

Default support for adding table (configuration or other) Actions is informal, driven by convention:

  • the JXTable's actionMap is scanned for candidate actions, the default marker is a key of type String which starts with ColumnControlButton.COLUMN_CONTROL_MARKER
  • the actions are sorted by that key and then handed over to the ColumnControlPopup for binding and addition of appropriate menu items
  • the addition as such is control by additionalActionsVisible property, its default value is true
See Also:
  • Field Details

    • COLUMN_CONTROL_MARKER

      public static final String COLUMN_CONTROL_MARKER
      Marker to auto-recognize actions which should be added to the popup.
      See Also:
    • COLUMN_CONTROL_BUTTON_ICON_KEY

      public static final String COLUMN_CONTROL_BUTTON_ICON_KEY
      the key for looking up the control's icon in the UIManager. Typically, it's LAF dependent.
      See Also:
    • COLUMN_CONTROL_BUTTON_MARGIN_KEY

      public static final String COLUMN_CONTROL_BUTTON_MARGIN_KEY
      the key for looking up the control's margin in the UIManager. Typically, it's LAF dependent.
      See Also:
  • Constructor Details

    • ColumnControlButton

      public ColumnControlButton(JXTable table)
      Creates a column control button for the table. Uses the default icon as provided by the addon.
      Parameters:
      table - the JXTable controlled by this component
    • ColumnControlButton

      public ColumnControlButton(JXTable table, Icon icon)
      Creates a column control button for the table. The button uses the given icon and has no text.
      Parameters:
      table - the JXTable controlled by this component
      icon - the Icon to show
  • Method Details

    • updateUI

      public void updateUI()
      Overrides:
      updateUI in class JButton
    • updateButtonUI

      protected void updateButtonUI()
      Updates this button's properties provided by the LAF. Here: overwrites the action's small_icon with the icon from the ui if the current icon is null or a UIResource.
    • updateActionUI

      protected void updateActionUI()
      Updates the action properties provided by the LAF. Here: overwrites the action's small_icon with the icon from the ui if the current icon is null or a UIResource.
    • togglePopup

      public void togglePopup()
      Toggles the popup component's visibility. This method is called by this control's default action.

      Here: delegates to getControlPopup().

    • getAdditionalActionsVisible

      public boolean getAdditionalActionsVisible()
      Returns the actionsVisible property which controls whether or not additional table Actions should be included into the popup.
      Returns:
      a boolean indicating whether or not additional table Actions are visible
    • setAdditionalActionsVisible

      public void setAdditionalActionsVisible(boolean additionalActionsVisible)
      Sets the additonalActionsVisible property. It controls whether or not additional table actions should be included into the popup.

      The default value is true.

      Parameters:
      additionalActionsVisible - the additionalActionsVisible to set
    • setActionGrouper

      public void setActionGrouper(ColumnControlPopup.ActionGrouper grouper)
      Sets the grouper to use for grouping the additional actions. Maybe null to have no additional grouping. Has no effect if the ColumnControlPopup doesn't implement Groupable. The default ColumnControlPopup supports Groupable, but is instantiated without a Grouper.
      Parameters:
      grouper - ActionGrouper
    • applyComponentOrientation

      public void applyComponentOrientation(ComponentOrientation o)
      Overrides:
      applyComponentOrientation in class Container
    • getColumnControlPopup

      protected ColumnControlPopup getColumnControlPopup()
      Returns to popup component for user interaction. Lazily creates the component if necessary.
      Returns:
      the ColumnControlPopup for showing the items, guaranteed to be not null.
      See Also:
    • createColumnControlPopup

      protected ColumnControlPopup createColumnControlPopup()
      Factory method to return a ColumnControlPopup. Subclasses can override to hook custom implementations.
      Returns:
      the ColumnControlPopup used.
    • updateFromColumnModelChange

      protected void updateFromColumnModelChange(TableColumnModel oldModel)
      Adjusts internal state after table's column model property has changed. Handles cleanup of listeners to the old/new columnModel (Note, that it listens to the column model only if it can control column visibility). Updates content of popup.
      Parameters:
      oldModel - the old TableColumnModel we had been listening to.
    • updateFromTableEnabledChanged

      protected void updateFromTableEnabledChanged()
      Synchs this button's enabled with table's enabled.
    • canControl

      protected boolean canControl()
      Method to check if we can control column visibility POST: if true we can be sure to have an extended TableColumnModel
      Returns:
      boolean to indicate if controlling the visibility state is possible.
    • populatePopup

      protected void populatePopup()
      Populates the popup from scratch. If applicable, creates and adds column visibility actions. Always adds additional actions.
    • clearAll

      protected void clearAll()
      removes all components from the popup, making sure to release all columnVisibility actions.
    • clearColumnVisibilityActions

      protected void clearColumnVisibilityActions()
      Releases actions and clears list of actions.
    • addVisibilityActionItems

      protected void addVisibilityActionItems()
      Adds visibility actions into the popup view. Here: delegates the list of actions to the DefaultColumnControlPopup.

      PRE: columnVisibilityActions populated before calling this.

    • addAdditionalActionItems

      protected void addAdditionalActionItems()
      Adds additional actions to the popup, if additionalActionsVisible is true, does nothing otherwise.

      Here: delegates the list of actions as returned by #getAdditionalActions() to the DefaultColumnControlPopup. Does nothing if #getColumnActions() is empty.

    • createVisibilityActions

      protected void createVisibilityActions()
      Creates and adds a ColumnVisiblityAction for every column that should be togglable via the column control.

      Here: all table columns contained in the TableColumnModel - visible and invisible columns - to createColumnVisibilityAction and adds all not null return values.

      PRE: canControl()

      See Also:
    • createColumnVisibilityAction

      protected ColumnControlButton.ColumnVisibilityAction createColumnVisibilityAction(TableColumn column)
      Creates and returns a ColumnVisibilityAction for the given TableColumn. The return value might be null, f.i. if the column should not be allowed to be toggled.
      Parameters:
      column - the TableColumn to use for the action
      Returns:
      a ColumnVisibilityAction to use for the given column, may be null.
    • getColumnVisibilityActions

      protected List<ColumnControlButton.ColumnVisibilityAction> getColumnVisibilityActions()
      Lazyly creates and returns the List of visibility actions.
      Returns:
      the list of visibility actions, guaranteed to be != null.
    • getAdditionalActions

      protected List<Action> getAdditionalActions()
      creates and returns a list of additional Actions to add to the popup. Here: the actions are looked up in the table's actionMap according to the keys as returned from #getColumnControlActionKeys();
      Returns:
      a list containing all additional actions to include into the popup.
    • getColumnControlActionKeys

      protected List getColumnControlActionKeys()
      Looks up and returns action keys to access actions in the table's actionMap which should be included into the popup. Here: all keys with isColumnControlActionKey(key). The list is sorted by those keys.
      Returns:
      the action keys of table's actionMap entries whose action should be included into the popup.
    • isColumnControlActionKey

      protected boolean isColumnControlActionKey(Object actionKey)
      Here: true if a String key starts with #COLUMN_CONTROL_MARKER.
      Parameters:
      actionKey - a key in the table's actionMap.
      Returns:
      a boolean to indicate whether the given actionKey maps to an action which should be included into the popup.
    • getTablePropertyChangeListener

      protected PropertyChangeListener getTablePropertyChangeListener()
      Returns the listener to table's property changes. The listener is lazily created if necessary.
      Returns:
      the PropertyChangeListener for use with the table, guaranteed to be not null.
    • createTablePropertyChangeListener

      protected PropertyChangeListener createTablePropertyChangeListener()
      Creates the listener to table's property changes. Subclasses are free to roll their own.

      Implementation note: this listener reacts to table's enabled and columnModel properties and calls the respective updateFromXX methodes.

      Returns:
      the PropertyChangeListener for use with the table.
    • getColumnModelListener

      protected TableColumnModelListener getColumnModelListener()
      Returns the listener to table's column model. The listener is lazily created if necessary.
      Returns:
      the TableColumnModelListener for use with the table's column model, guaranteed to be not null.
    • createColumnModelListener

      protected TableColumnModelListener createColumnModelListener()
      Creates the listener to columnModel. Subclasses are free to roll their own.

      Implementation note: this listener reacts to "real" columnRemoved/-Added by populating the popups content from scratch.

      Returns:
      the TableColumnModelListener for use with the table's columnModel.