public abstract class ComponentAdapter extends Object
ComponentAdapter
allows the decoration collaborators like f.i.
Highlighter
to interact with a target
component through a
common API. It has two aspects:
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:
column
in column view coordinates
convertColumnIndexToModel(int)
in view coordinates
row
in row view coordinates
convertRowIndexToModel(int)
in view coordinates
getFilteredValueAt(int, int)
takes the row in view coordinates.
getFilteredStringAt(int, int)
takes the row in view coordinates.
PENDING JW: formally document that row/column coordinates must be valid in all methods taking model coordinates, that is 0<= row < getRowCount().
HighlightPredicate
,
Highlighter
Modifier and Type | Field and Description |
---|---|
int |
column
current column in view coordinates.
|
static Object |
DEFAULT_COLUMN_IDENTIFIER |
int |
row
current row in view coordinates.
|
protected JComponent |
target |
Constructor and Description |
---|
ComponentAdapter(JComponent component)
Constructs a ComponentAdapter, setting the specified component as the
target component.
|
Modifier and Type | Method and 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.
|
Rectangle |
getCellBounds()
Returns the bounds of the cell identified by this adapter.
|
Class<?> |
getColumnClass()
Returns the common class of all data in the current column.
|
Class<?> |
getColumnClass(int column)
Returns the common class of all data column identified by the given
column index in model coordinates.
|
int |
getColumnCount()
Returns the number of columns in the target's data model.
|
Object |
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.
|
String |
getColumnName(int columnIndex)
Returns the column's display name (= headerValue) of the column
at columnIndex in model coordinates.
|
JComponent |
getComponent()
Returns the component which is this adapter's target.
|
int |
getDepth()
Returns the depth of this row in the hierarchy where the root is 0.
|
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.
|
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.
|
int |
getRowCount()
Returns the number of rows in the target's data model.
|
String |
getString()
Returns the String representation of the value of the cell identified by this adapter.
|
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.
|
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.
|
Object |
getValue()
Returns the value of the cell identified by this adapter.
|
Object |
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 |
hasFocus()
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 |
isEditable()
Returns
true if the cell identified by this adapter is editable,
false otherwise. |
boolean |
isExpanded()
Returns true if the cell identified by this adapter is currently expanded.
|
boolean |
isHierarchical()
Returns true if the cell identified by this adapter displays the hierarchical node.
|
boolean |
isLeaf()
Returns true if the cell identified by this adapter is a leaf node.
|
abstract boolean |
isSelected()
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.
|
public static final Object DEFAULT_COLUMN_IDENTIFIER
public int row
public int column
protected final JComponent target
public ComponentAdapter(JComponent component)
component
- target component for this adapterpublic JComponent getComponent()
public String getColumnName(int columnIndex)
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.
columnIndex
- in model coordinatespublic Object getColumnIdentifierAt(int columnIndex)
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 ;-)
columnIndex
- in model coordinates, must be valid.ArrayIndexOutOfBoundsException
- if columnIndex < 0 or columnIndex >= getColumnCount().getColumnIndex(Object)
public int getColumnIndex(Object 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));
identifier
- the column's identifier, must not be nullNullPointerException
- if identifier is null.getColumnIdentifierAt(int)
public boolean isTestable(int column)
Here: returns true if visible (that is modelToView gives a valid view column coordinate).
column
- the column index in model coordinatespublic Class<?> getColumnClass(int column)
This implementation returns Object.class
. Subclasses should
implement as appropriate.
getColumnClass()
public Class<?> getColumnClass()
This implementation delegates to getColumnClass(int) with the current column converted to model coordinates.
getColumnClass(int)
public int getColumnCount()
public int getRowCount()
public abstract Object getValueAt(int row, int column)
row
- in model coordinatescolumn
- in model coordinatespublic abstract boolean isCellEditable(int row, int column)
row
- the row to query in model coordinatescolumn
- the column to query in model coordinatestrue
if the cell is editable, false
otherwisepublic String getString()
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.
getValueAt(int, int)
,
getFilteredValueAt(int, int)
,
getValue(int)
public String getString(int modelColumnIndex)
modelColumnIndex
- the column index in model coordinatesgetFilteredStringAt(int, int)
,
getString()
public String getFilteredStringAt(int row, int column)
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?
row
- the row of the cell in view coordinatescolumn
- the column of the cell in model coordinates.public String getStringAt(int row, int column)
This implementation messages the StringValue.TO_STRING with the valueAt, subclasses should re-implement and use the api appropriate for the target component type.
row
- in model coordinatescolumn
- in model coordinatespublic Object getValue()
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.
getValueAt(int, int)
,
getFilteredValueAt(int, int)
,
getValue(int)
public Object getValue(int modelColumnIndex)
modelColumnIndex
- the column index in model coordinatesgetValueAt(int, int)
,
getFilteredValueAt(int, int)
,
getValue(int)
public Object getFilteredValueAt(int row, int column)
row
- the row of the cell in view coordinatescolumn
- the column of the cell in model coordinates.public Rectangle getCellBounds()
public abstract boolean hasFocus()
public abstract boolean isSelected()
public abstract boolean isEditable()
true
if the cell identified by this adapter is editable,
false
otherwise.true
if the cell is editable, false
otherwisepublic boolean isExpanded()
public boolean isLeaf()
public boolean isHierarchical()
public int getDepth()
public int convertColumnIndexToView(int columnModelIndex)
columnModelIndex
- index of a column in model coordinatespublic int convertColumnIndexToModel(int columnViewIndex)
columnViewIndex
- index of a column in view coordinatespublic int convertRowIndexToView(int rowModelIndex)
rowModelIndex
- index of a row in model coordinatespublic int convertRowIndexToModel(int rowViewIndex)
rowViewIndex
- index of a row in view coordinatesWebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses