public abstract class AbstractHighlighter extends Object implements Highlighter
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));
HighlightPredicate
,
ComponentProvider
Modifier and Type | Field and Description |
---|---|
private ChangeEvent |
changeEvent
Only one
ChangeEvent is needed per model instance since the
event's only (read-only) state is the source property. |
protected WeakEventListenerList |
listenerList
The listeners waiting for model changes.
|
private HighlightPredicate |
predicate
the HighlightPredicate to use.
|
Constructor and Description |
---|
AbstractHighlighter()
Instantiates a Highlighter with default HighlightPredicate.
|
AbstractHighlighter(HighlightPredicate predicate)
Instantiates a Highlighter with the given
HighlightPredicate.
|
Modifier and Type | Method and Description |
---|---|
void |
addChangeListener(ChangeListener l)
Adds a
ChangeListener . |
protected boolean |
areEqual(Object oneItem,
Object anotherItem)
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 void |
fireStateChanged()
Notifies registered
ChangeListener s about
state changes. |
ChangeListener[] |
getChangeListeners()
Returns an array of all the change listeners
registered on this
Highlighter . |
HighlightPredicate |
getHighlightPredicate()
Returns the HighlightPredicate used to decide whether a cell
should be highlighted.
|
Component |
highlight(Component component,
ComponentAdapter adapter)
Decorates the specified component for the given component
adapter.
|
void |
removeChangeListener(ChangeListener l)
Removes a
ChangeListener e. |
void |
setHighlightPredicate(HighlightPredicate predicate)
Set the HighlightPredicate used to decide whether a cell should
be highlighted.
|
private transient ChangeEvent changeEvent
ChangeEvent
is needed per model instance since the
event's only (read-only) state is the source property. The source
of events generated here is always "this".protected WeakEventListenerList listenerList
private HighlightPredicate predicate
public AbstractHighlighter()
public AbstractHighlighter(HighlightPredicate predicate)
predicate
- the HighlightPredicate to use.setHighlightPredicate(HighlightPredicate)
public void setHighlightPredicate(HighlightPredicate predicate)
predicate
- the HighlightPredicate to use.public HighlightPredicate getHighlightPredicate()
public Component highlight(Component component, ComponentAdapter adapter)
highlight
in interface Highlighter
component
- the cell renderer component that is to be decoratedadapter
- the ComponentAdapter for this decorate operationcanHighlight(Component, ComponentAdapter)
,
doHighlight(Component, ComponentAdapter)
,
getHighlightPredicate()
protected boolean canHighlight(Component component, ComponentAdapter adapter)
This implementation returns true always.
component
- adapter
- protected abstract Component doHighlight(Component component, ComponentAdapter adapter)
component
- the cell renderer component that is to be decoratedadapter
- the ComponentAdapter for this decorate operationhighlight(Component, ComponentAdapter)
protected boolean areEqual(Object oneItem, Object anotherItem)
oneItem
- one itemanotherItem
- another itempublic final void addChangeListener(ChangeListener l)
ChangeListener
. ChangeListeners are
notified after changes of any attribute.addChangeListener
in interface Highlighter
l
- the ChangeListener to addremoveChangeListener(javax.swing.event.ChangeListener)
public final void removeChangeListener(ChangeListener l)
ChangeListener
e.removeChangeListener
in interface Highlighter
l
- the ChangeListener
to removeaddChangeListener(javax.swing.event.ChangeListener)
public final ChangeListener[] getChangeListeners()
Highlighter
.getChangeListeners
in interface Highlighter
ChangeListener
s
or an empty
array if no change listeners are currently registeredaddChangeListener(javax.swing.event.ChangeListener)
,
removeChangeListener(javax.swing.event.ChangeListener)
protected final void fireStateChanged()
ChangeListener
s about
state changes.Note: subclasses should be polite and implement any property setters to fire only if the property is really changed.
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses