Class ComponentAdapter

java.lang.Object
org.jdesktop.swingx.decorator.ComponentAdapter
Direct Known Subclasses:
JXComboBox.ComboBoxAdapter, JXList.ListAdapter, JXTable.TableAdapter, JXTree.TreeAdapter

public abstract class ComponentAdapter extends Object
Abstract base class for all component data adapter classes. A ComponentAdapter allows the decoration collaborators like f.i. Highlighter to interact with a target component through a common API.

It has two aspects:

  • interact with the view state for the "current" cell. The row/column fields and the parameterless methods service this aspect. The coordinates are in view coordinate system.
  • interact with the data of the component. The methods for this are those taking row/column indices as parameters. The coordinates are in model coordinate system.
Typically, application code is interested in the first aspect. An example is highlighting the background of a row in a JXTable based on the value of a cell in a specific column. The solution is to implement a custom HighlightPredicate which decides if a given cell should be highlighted and configure a ColorHighlighter with the predicate and an appropriate background color.

 HighlightPredicate feverWarning = new HighlightPredicate() {
     int temperatureColumn = 10;
 
     public boolean isHighlighted(Component component, ComponentAdapter adapter) {
         return hasFever(adapter.getValue(temperatureColumn));
     }
 
     private boolean hasFever(Object value) {
         if (!value instanceof Number)
             return false;
         return ((Number) value).intValue() > 37;
     }
 };
 
 Highlighter hl = new ColorHighlighter(feverWarning, Color.RED, null);
 
The adapter is responsible for mapping column and row coordinates. All input column indices are in model coordinates with exactly two exceptions: All input row indices are in model coordinates with exactly four exceptions: PENDING JW: anything to gain by generics here?

PENDING JW: formally document that row/column coordinates must be valid in all methods taking model coordinates, that is 0 ≤ row < getRowCount().

Author:
Ramesh Gupta, Karl Schaefer, Jeanette Winzenburg
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int
    current column in view coordinates.
    static final Object
    default
    int
    current row in view coordinates.
    protected final JComponent
    TODO doc
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a ComponentAdapter, setting the specified component as the target component.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    convertColumnIndexToModel(int columnViewIndex)
    For target components that support multiple columns in their model, along with column reordering in the view, this method transforms the specified columnIndex from view coordinates to model coordinates.
    int
    convertColumnIndexToView(int columnModelIndex)
    For target components that support multiple columns in their model, along with column reordering in the view, this method transforms the specified columnIndex from model coordinates to view coordinates.
    int
    convertRowIndexToModel(int rowViewIndex)
    Converts a row index in view coordinates to an index in model coordinates.
    int
    convertRowIndexToView(int rowModelIndex)
    Converts a row index in model coordinates to an index in view coordinates.
    Returns the bounds of the cell identified by this adapter.
    Returns the common class of all data in the current column.
    getColumnClass(int column)
    Returns the common class of all data column identified by the given column index in model coordinates.
    int
    Returns the number of columns in the target's data model.
    getColumnIdentifierAt(int columnIndex)
    Returns logical identifier of the column at columnIndex in model coordinates.
    int
    getColumnIndex(Object identifier)
    Returns the column index in model coordinates for the logical identifier.
    getColumnName(int columnIndex)
    Returns the column's display name (= headerValue) of the column at columnIndex in model coordinates.
    Returns the component which is this adapter's target.
    int
    Returns the depth of this row in the hierarchy where the root is 0.
    getFilteredStringAt(int row, int column)
    Returns the String representation of the filtered value of the cell identified by the row in view coordinate and the column in model coordinates.
    getFilteredValueAt(int row, int column)
    Returns the filtered value of the cell identified by the row in view coordinate and the column in model coordinates.
    int
    Returns the number of rows in the target's data model.
    Returns the String representation of the value of the cell identified by this adapter.
    getString(int modelColumnIndex)
    Returns the String representation of the value of the cell identified by the current adapter row and the given column index in model coordinates.
    getStringAt(int row, int column)
    Returns the String representation of the value of the cell identified by the row specified row and column in model coordinates.
    Returns the value of the cell identified by this adapter.
    getValue(int modelColumnIndex)
    Returns the value of the cell identified by the current adapter row and the given column index in model coordinates.
    abstract Object
    getValueAt(int row, int column)
    Returns the value of the target component's cell identified by the specified row and column in model coordinates.
    abstract boolean
    Returns true if the cell identified by this adapter currently has focus.
    abstract boolean
    isCellEditable(int row, int column)
    Determines whether this cell is editable.
    abstract boolean
    Returns true if the cell identified by this adapter is editable, false otherwise.
    boolean
    Returns true if the cell identified by this adapter is currently expanded.
    boolean
    Returns true if the cell identified by this adapter displays the hierarchical node.
    boolean
    Returns true if the cell identified by this adapter is a leaf node.
    abstract boolean
    Returns true if the cell identified by this adapter is currently selected.
    boolean
    isTestable(int column)
    Returns true if the column should be included in testing.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_COLUMN_IDENTIFIER

      public static final Object DEFAULT_COLUMN_IDENTIFIER
      default
    • row

      public int row
      current row in view coordinates.
    • column

      public int column
      current column in view coordinates.
    • target

      protected final JComponent target
      TODO doc
  • Constructor Details

    • ComponentAdapter

      public ComponentAdapter(JComponent component)
      Constructs a ComponentAdapter, setting the specified component as the target component.
      Parameters:
      component - target component for this adapter
  • Method Details

    • getComponent

      public JComponent getComponent()
      Returns the component which is this adapter's target.
      Returns:
      the component which is this adapter's target.
    • getColumnName

      public String getColumnName(int columnIndex)
      Returns the column's display name (= headerValue) of the column at columnIndex in model coordinates. Used f.i. in SearchPanel to fill the field with the column name.

      Note: it's up to the implementation to decide for which columns it returns a name - most will do so for the subset with isTestable = true. This implementation delegates to getColumnIdentifierAt and returns it's toString or null.

      Parameters:
      columnIndex - in model coordinates
      Returns:
      column name or null if not found
    • getColumnIdentifierAt

      public Object getColumnIdentifierAt(int columnIndex)
      Returns logical identifier of the column at columnIndex in model coordinates. Note: it's up to the implementation to decide for which columns it returns an identifier - most will do so for the subset with isTestable = true.

      This implementation returns DEFAULT_COLUMN_IDENTIFIER. PENDING JW: This method replaces the old getColumnIdentifier(int) which returned a String which is overly restrictive. The only way to gently replace this method was to add this with a different name - which makes this name suboptimal. Probably should rename again once the old has died out ;-)

      Parameters:
      columnIndex - in model coordinates, must be valid.
      Returns:
      the identifier of the column at columnIndex or null if it has none.
      Throws:
      ArrayIndexOutOfBoundsException - if columnIndex < 0 or columnIndex ≥ getColumnCount().
      See Also:
    • getColumnIndex

      public int getColumnIndex(Object identifier)
      Returns the column index in model coordinates for the logical identifier.

      This implementation returns 0 if the identifier is the same as the one known identifier returned from getColumnIdentifierAt(0), or -1 otherwise. So subclasses with one column and a customizable identifier need not override. Subclasses which support multiple columns must override this as well to keep the contract as in (assuming that the lookup succeeded):

      
        Object id = getColumnIdentifierAt(index);
        assertEquals(index, getColumnIndex(index);
        // and the reverse 
        int column = getColumnIndex(identifier);
        assertEquals(identifier, getColumnIdentifierAt(column));
       
      Parameters:
      identifier - the column's identifier, must not be null
      Returns:
      the index of the column identified by identifier in model coordinates or -1 if no column with the given identifier is found.
      Throws:
      NullPointerException - if identifier is null.
      See Also:
    • isTestable

      public boolean isTestable(int column)
      Returns true if the column should be included in testing.

      Here: returns true if visible (that is modelToView gives a valid view column coordinate).

      Parameters:
      column - the column index in model coordinates
      Returns:
      true if the column should be included in testing
    • getColumnClass

      public Class<?> getColumnClass(int column)
      Returns the common class of all data column identified by the given column index in model coordinates.

      This implementation returns Object.class. Subclasses should implement as appropriate.

      Parameters:
      column - not used here
      Returns:
      the common class of all data given column in model coordinates.
      See Also:
    • getColumnClass

      public Class<?> getColumnClass()
      Returns the common class of all data in the current column.

      This implementation delegates to getColumnClass(int) with the current column converted to model coordinates.

      Returns:
      the common class of all data in the current column.
      See Also:
    • getColumnCount

      public int getColumnCount()
      Returns the number of columns in the target's data model.
      Returns:
      the number of columns in the target's data model.
    • getRowCount

      public int getRowCount()
      Returns the number of rows in the target's data model.
      Returns:
      the number of rows in the target's data model.
    • getValueAt

      public abstract Object getValueAt(int row, int column)
      Returns the value of the target component's cell identified by the specified row and column in model coordinates.
      Parameters:
      row - in model coordinates
      column - in model coordinates
      Returns:
      the value of the target component's cell identified by the specified row and column
    • isCellEditable

      public abstract boolean isCellEditable(int row, int column)
      Determines whether this cell is editable.
      Parameters:
      row - the row to query in model coordinates
      column - the column to query in model coordinates
      Returns:
      true if the cell is editable, false otherwise
    • getString

      public String getString()
      Returns the String representation of the value of the cell identified by this adapter. That is, for the at position (adapter.row, adapter.column) in view coordinates.

      NOTE: this implementation assumes that view coordinates == model coordinates, that is simply calls getValueAt(this.row, this.column). It is up to subclasses to override appropriately is they support model/view coordinate transformation.

      This implementation messages the StringValue.TO_STRING with the getValue, subclasses should re-implement and use the API appropriate for the target component type.

      Returns:
      the String representation of value of the cell identified by this adapter
      See Also:
    • getString

      public String getString(int modelColumnIndex)
      Returns the String representation of the value of the cell identified by the current adapter row and the given column index in model coordinates.
      Parameters:
      modelColumnIndex - the column index in model coordinates
      Returns:
      the String representation of the value of the cell identified by this adapter
      See Also:
    • getFilteredStringAt

      public String getFilteredStringAt(int row, int column)
      Returns the String representation of the filtered value of the cell identified by the row in view coordinate and the column in model coordinates.

      Note: the asymetry of the coordinates is intentional - clients like Highlighters are interested in view values but might need to access non-visible columns for testing. While it is possible to access row coordinates different from the current (that is this.row) it is not safe to do so for row > this.row because the adapter doesn't allow to query the count of visible rows.

      This implementation messages the StringValue.TO_STRING with the filteredValue, subclasses should re-implement and use the API appropriate for the target component type.

      PENDING JW: what about null cell values? StringValue has a contract to return a empty string then, would that be okay here as well?

      Parameters:
      row - the row of the cell in view coordinates
      column - the column of the cell in model coordinates.
      Returns:
      the String representation of the filtered value of the cell identified by the row in view coordinate and the column in model coordinates
    • getStringAt

      public String getStringAt(int row, int column)
      Returns the String representation of the value of the cell identified by the row specified row and column in model coordinates.

      This implementation messages the StringValue.TO_STRING with the valueAt, subclasses should re-implement and use the api appropriate for the target component type.

      Parameters:
      row - in model coordinates
      column - in model coordinates
      Returns:
      the value of the target component's cell identified by the specified row and column
    • getValue

      public Object getValue()
      Returns the value of the cell identified by this adapter. That is, for the at position (adapter.row, adapter.column) in view coordinates.

      NOTE: this implementation assumes that view coordinates == model coordinates, that is simply calls getValueAt(this.row, this.column). It is up to subclasses to override appropriately is they support model/view coordinate transformation.

      Returns:
      the value of the cell identified by this adapter
      See Also:
    • getValue

      public Object getValue(int modelColumnIndex)
      Returns the value of the cell identified by the current adapter row and the given column index in model coordinates.
      Parameters:
      modelColumnIndex - the column index in model coordinates
      Returns:
      the value of the cell identified by this adapter
      See Also:
    • getFilteredValueAt

      public Object getFilteredValueAt(int row, int column)
      Returns the filtered value of the cell identified by the row in view coordinate and the column in model coordinates. Note: the asymmetry of the coordinates is intentional - clients like Highlighters are interested in view values but might need to access non-visible columns for testing. While it is possible to access row coordinates different from the current (that is this.row) it is not safe to do so for row > this.row because the adapter doesn't allow to query the count of visible rows.
      Parameters:
      row - the row of the cell in view coordinates
      column - the column of the cell in model coordinates.
      Returns:
      the filtered value of the cell identified by the row in view coordinate and the column in model coordinates
    • getCellBounds

      public Rectangle getCellBounds()
      Returns the bounds of the cell identified by this adapter.
      Returns:
      the bounds of the cell identified by this adapter
    • hasFocus

      public abstract boolean hasFocus()
      Returns true if the cell identified by this adapter currently has focus. Otherwise, it returns false.
      Returns:
      true if the cell identified by this adapter currently has focus; Otherwise, return false
    • isSelected

      public abstract boolean isSelected()
      Returns true if the cell identified by this adapter is currently selected. Otherwise, it returns false.
      Returns:
      true if the cell identified by this adapter is currently selected; Otherwise, return false
    • isEditable

      public abstract boolean isEditable()
      Returns true if the cell identified by this adapter is editable, false otherwise.
      Returns:
      true if the cell is editable, false otherwise
    • isExpanded

      public boolean isExpanded()
      Returns true if the cell identified by this adapter is currently expanded. Otherwise, it returns false. For components that do not support hierarchical data, this method always returns true because the cells in such components can never be collapsed.
      Returns:
      true if the cell identified by this adapter is currently expanded; Otherwise, return false
    • isLeaf

      public boolean isLeaf()
      Returns true if the cell identified by this adapter is a leaf node. Otherwise, it returns false. For components that do not support hierarchical data, this method always returns true because the cells in such components can never have children.
      Returns:
      true if the cell identified by this adapter is a leaf node; Otherwise, return false
    • isHierarchical

      public boolean isHierarchical()
      Returns true if the cell identified by this adapter displays the hierarchical node. Otherwise, it returns false. For components that do not support hierarchical data, this method always returns false because the cells in such components can never have children.
      Returns:
      true if the cell identified by this adapter displays the hierarchical node; Otherwise, return false
    • getDepth

      public int getDepth()
      Returns the depth of this row in the hierarchy where the root is 0. For components that do not contain hierarchical data, this method returns 1.
      Returns:
      the depth for this adapter
    • convertColumnIndexToView

      public int convertColumnIndexToView(int columnModelIndex)
      For target components that support multiple columns in their model, along with column reordering in the view, this method transforms the specified columnIndex from model coordinates to view coordinates. For all other types of target components, this method returns the columnIndex unchanged.
      Parameters:
      columnModelIndex - index of a column in model coordinates
      Returns:
      index of the specified column in view coordinates
    • convertColumnIndexToModel

      public int convertColumnIndexToModel(int columnViewIndex)
      For target components that support multiple columns in their model, along with column reordering in the view, this method transforms the specified columnIndex from view coordinates to model coordinates. For all other types of target components, this method returns the columnIndex unchanged.
      Parameters:
      columnViewIndex - index of a column in view coordinates
      Returns:
      index of the specified column in model coordinates
    • convertRowIndexToView

      public int convertRowIndexToView(int rowModelIndex)
      Converts a row index in model coordinates to an index in view coordinates.
      Parameters:
      rowModelIndex - index of a row in model coordinates
      Returns:
      index of the specified row in view coordinates
    • convertRowIndexToModel

      public int convertRowIndexToModel(int rowViewIndex)
      Converts a row index in view coordinates to an index in model coordinates.
      Parameters:
      rowViewIndex - index of a row in view coordinates
      Returns:
      index of the specified row in model coordinates