Abstract
Highlighter
implementation which manages change notification and supports conditional highlighting. Subclasses are required to fire ChangeEvents on internal changes which might effect the highlight. The HighlightPredicate controls whether or not a highlight should be applied for the given ComponentAdapter, subclasses must guarantee to respect its decision.
Concrete custom implementations should focus on a single (or few) visual attribute to highlight. This allows easy re-use by composition. F.i. a custom FontHighlighter:
public static class FontHighlighter extends AbstractHighlighter { private Font font; public FontHighlighter(HighlightPredicate predicate, Font font) { super(predicate); setFont(font); } @Override protected Component doHighlight(Component component, ComponentAdapter adapter) { component.setFont(font); return component; } public final void setFont(Font font) { if (equals(font, this.font)) return; this.font = font; fireStateChanged(); } }
Client code can combine the effect with a f.i. Color decoration, and use a shared HighlightPredicate to apply both for the same condition.
HighlightPredicate predicate = new HighlightPredicate() { public boolean isHighlighted(Component renderer, ComponentAdapter adapter) { Object value = adapter.getFilteredValueAt(adapter.row, adapter.column); return (value instanceof Number) && ((Number) value).intValue() < 0; } }; table.setHighlighters( new ColorHighlighter(predicate, Color.RED, null), new FontHighlighter(predicate, myBoldFont));
@author Jeanette Winzenburg
@see HighlightPredicate
@see org.jdesktop.swingx.renderer.ComponentProvider