org.jdesktop.swingx.decorator
Class AbstractHighlighter

java.lang.Object
  extended by org.jdesktop.swingx.decorator.AbstractHighlighter
All Implemented Interfaces:
Highlighter
Direct Known Subclasses:
BorderHighlighter, ColorHighlighter, CompoundHighlighter, IconHighlighter, PainterHighlighter

public abstract class AbstractHighlighter
extends java.lang.Object
implements Highlighter

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:
HighlightPredicate, ComponentProvider

Constructor Summary
AbstractHighlighter()
          Instantiates a Highlighter with default HighlightPredicate.
AbstractHighlighter(HighlightPredicate predicate)
          Instantiates a Highlighter with the given HighlightPredicate.
 
Method Summary
 void addChangeListener(javax.swing.event.ChangeListener l)
          Adds a ChangeListener.
 javax.swing.event.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.
 java.awt.Component highlight(java.awt.Component component, ComponentAdapter adapter)
          Decorates the specified component for the given component adapter.
 void removeChangeListener(javax.swing.event.ChangeListener l)
          Removes a ChangeListenere.
 void setHighlightPredicate(HighlightPredicate predicate)
          Set the HighlightPredicate used to decide whether a cell should be highlighted.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractHighlighter

public AbstractHighlighter()
Instantiates a Highlighter with default HighlightPredicate.

See Also:
setHighlightPredicate(HighlightPredicate)

AbstractHighlighter

public AbstractHighlighter(HighlightPredicate predicate)
Instantiates a Highlighter with the given HighlightPredicate.

Parameters:
predicate - the HighlightPredicate to use.
See Also:
setHighlightPredicate(HighlightPredicate)
Method Detail

setHighlightPredicate

public void setHighlightPredicate(HighlightPredicate predicate)
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

public HighlightPredicate 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

public java.awt.Component highlight(java.awt.Component component,
                                    ComponentAdapter adapter)
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 interface Highlighter
Parameters:
component - the cell renderer component that is to be decorated
adapter - the ComponentAdapter for this decorate operation
Returns:
the decorated cell rendering component
See Also:
canHighlight(Component, ComponentAdapter), doHighlight(Component, ComponentAdapter), getHighlightPredicate()

addChangeListener

public final void addChangeListener(javax.swing.event.ChangeListener l)
Adds a ChangeListener. ChangeListeners are notified after changes of any attribute.

Specified by:
addChangeListener in interface Highlighter
Parameters:
l - the ChangeListener to add
See Also:
removeChangeListener(javax.swing.event.ChangeListener)

removeChangeListener

public final void removeChangeListener(javax.swing.event.ChangeListener l)
Removes a ChangeListenere.

Specified by:
removeChangeListener in interface Highlighter
Parameters:
l - the ChangeListener to remove
See Also:
addChangeListener(javax.swing.event.ChangeListener)

getChangeListeners

public final javax.swing.event.ChangeListener[] getChangeListeners()
Returns an array of all the change listeners registered on this Highlighter.

Specified by:
getChangeListeners in interface Highlighter
Returns:
all of this model's ChangeListeners or an empty array if no change listeners are currently registered
Since:
1.4
See Also:
addChangeListener(javax.swing.event.ChangeListener), removeChangeListener(javax.swing.event.ChangeListener)