Package org.jdesktop.swingx.decorator
Class AbstractHighlighter
java.lang.Object
org.jdesktop.swingx.decorator.AbstractHighlighter
- All Implemented Interfaces:
Highlighter
- Direct Known Subclasses:
AlignmentHighlighter
,BorderHighlighter
,ColorHighlighter
,ComponentOrientationHighlighter
,CompoundHighlighter
,EnabledHighlighter
,FontHighlighter
,IconHighlighter
,PainterHighlighter
,ToolTipHighlighter
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 Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected WeakEventListenerList
The listeners waiting for model changes. -
Constructor Summary
ConstructorsConstructorDescriptionInstantiates a Highlighter with default HighlightPredicate.AbstractHighlighter
(HighlightPredicate predicate) Instantiates a Highlighter with the given HighlightPredicate. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
Adds aChangeListener
.protected boolean
Returns true if the to objects are either both null or equal each other.protected boolean
canHighlight
(Component component, ComponentAdapter adapter) Subclasses may override to further limit the highlighting based on Highlighter state, f.i. a PainterHighlighter can only be applied to PainterAware components.protected abstract Component
doHighlight
(Component component, ComponentAdapter adapter) Apply the highlights.protected final void
Notifies registeredChangeListener
s about state changes.final ChangeListener[]
Returns an array of all the change listeners registered on thisHighlighter
.Returns the HighlightPredicate used to decide whether a cell should be highlighted.highlight
(Component component, ComponentAdapter adapter) Decorates the specified component for the given component adapter.final void
Removes aChangeListener
e.void
setHighlightPredicate
(HighlightPredicate predicate) Set the HighlightPredicate used to decide whether a cell should be highlighted.
-
Field Details
-
listenerList
The listeners waiting for model changes.
-
-
Constructor Details
-
AbstractHighlighter
public AbstractHighlighter()Instantiates a Highlighter with default HighlightPredicate. -
AbstractHighlighter
Instantiates a Highlighter with the given HighlightPredicate.- Parameters:
predicate
- the HighlightPredicate to use.- See Also:
-
-
Method Details
-
setHighlightPredicate
Set the HighlightPredicate used to decide whether a cell should be highlighted. If null, sets the predicate to HighlightPredicate.ALWAYS. The default value is HighlightPredicate.ALWAYS.- Parameters:
predicate
- the HighlightPredicate to use.
-
getHighlightPredicate
Returns the HighlightPredicate used to decide whether a cell should be highlighted. Guaranteed to be never null.- Returns:
- the HighlightPredicate to use, never null.
-
highlight
Decorates the specified component for the given component adapter. This calls doHighlight to apply the decoration if both HighlightPredicate isHighlighted and canHighlight return true. Returns the undecorated component otherwise.- Specified by:
highlight
in interfaceHighlighter
- Parameters:
component
- the cell renderer component that is to be decoratedadapter
- the ComponentAdapter for this decorate operation- Returns:
- the decorated cell rendering component
- See Also:
-
canHighlight
Subclasses may override to further limit the highlighting based on Highlighter state, f.i. a PainterHighlighter can only be applied to PainterAware components.This implementation returns true always.
- Parameters:
component
- the cell renderer component that is to be decoratedadapter
- a Component Adapter- Returns:
- a boolean indication if the adapter can be highlighted based general state. This implementation returns true always.
-
doHighlight
Apply the highlights.- Parameters:
component
- the cell renderer component that is to be decoratedadapter
- the ComponentAdapter for this decorate operation- Returns:
- Component
component
- See Also:
-
areEqual
Returns true if the to objects are either both null or equal each other.- Parameters:
oneItem
- one itemanotherItem
- another item- Returns:
- true if both are null or equal other, false otherwise.
-
addChangeListener
Adds aChangeListener
. ChangeListeners are notified after changes of any attribute.- Specified by:
addChangeListener
in interfaceHighlighter
- Parameters:
l
- the ChangeListener to add- See Also:
-
removeChangeListener
Removes aChangeListener
e.- Specified by:
removeChangeListener
in interfaceHighlighter
- Parameters:
l
- theChangeListener
to remove- See Also:
-
getChangeListeners
Returns an array of all the change listeners registered on thisHighlighter
.- Specified by:
getChangeListeners
in interfaceHighlighter
- Returns:
- all of this model's
ChangeListener
s or an empty array if no change listeners are currently registered - Since:
- 1.4
- See Also:
-
fireStateChanged
protected final void fireStateChanged()Notifies registeredChangeListener
s about state changes.Note: subclasses should be polite and implement any property setters to fire only if the property is really changed.
-