|
swingx Version 2005-08-19 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.jdesktop.swingx.decorator.Highlighter
public class Highlighter
Highlighter
is a lightweight mechanism to modify the behavior
and attributes of cell renderers such as ListCellRenderer
,
TableCellRenderer
, and
TreeCellRenderer
in a simple layered fashion.
While cell renderers are split along component lines, highlighters provide a
common interface for decorating cell renderers.
Highlighter
achieves this by vectoring access to all component-specific
state and functionality through a ComponentAdapter
object.
The primary purpose of Highlighter
is to decorate a cell
renderer in controlled ways, such as by applying a different color
or font to it. For example, AlternateRowHighlighter
highlights cell
renderers with alternating background colors. In data visualization components
that support multiple columns with potentially different types of data, this
highlighter imparts the same background color consistently across all
columns of the target
component
regardless of the actual cell renderer registered for any specific column.
Thus, the Highlighter
mechanism is orthogonal to the cell
rendering mechanism.
To use Highlighter
you must first set up a
HighlighterPipeline
using an array of Highlighter
objects,
and then call setHighlighters() on a data visualization component, passing in
the highligher pipeline. If the array of highlighters is not null and is not
empty, the highlighters are applied to the selected renderer for each cell in
the order they appear in the array.
When it is time to render a cell, the cell renderer is primed as usual, after
which, the highlight
method of the first
highlighter in the HighlighterPipeline
is invoked. The prepared
renderer, and a suitable ComponentAdapter
object is passed to the
highlight
method. The highlighter is expected to modify the
renderer in controlled ways, and return the modified renderer (or a substitute)
that is passed to the next highlighter, if any, in the pipeline. The renderer
returned by the highlight
method of the last highlighter in the
pipeline is ultimately used to render the cell.
The Highlighter
mechanism enables multiple degrees of
freedom. In addition to specifying the actual cell renderer class, now you
can also specify the number, order, and class of highlighter objects. Using
highlighters is really simple, as shown by the following example:
Highlighter[] highlighters = new Highlighter[] { new AlternateRowHighlighter(Color.white, new Color(0xF0, 0xF0, 0xE0), null), new PatternHighlighter(null, Color.red, "^s", 0, 0) }; HighlighterPipeline highlighterPipeline = new HighlighterPipeline(highlighters); JXTable table = new JXTable(); table.setHighlighters(highlighterPipeline);
The above example allocates an array of Highlighter
and populates
it with a new AlternateRowHighlighter
and PatternHighlighter
.
The first one in this example highlights all cells in odd rows with a white
background, and all cells in even rows with a silver background, but it does
not specify a foreground color explicitly. The second highlighter does not
specify a background color explicitly, but sets the foreground color to red
if certain conditions are met (see PatternHighlighter
for
more details). In this example, if the cells in the first column of any
row start with the letter 's', then all cells in that row are highlighted with
a red foreground. Also, as mentioned earlier, the highlighters are applied in
the order they appear in the list.
Highlighters are mutable by default, that is all there properties can be changed dynamically. If so they fire changeEvents to registered ChangeListeners. They can be marked as immutable at instantiation time - if so, trying to mutate all properties will not have any effect, ChangeListeners are not registered and no events are fired.
This base class has properties background/foreground and corresponding selectionBackground/selectionForeground. It will apply those colors "absolutely", that is without actually computing any derived color. It's up to subclasses to implement color computation, if desired.
ComponentAdapter
,
ListCellRenderer
,
TableCellRenderer
,
TreeCellRenderer
Nested Class Summary | |
---|---|
static interface |
Highlighter.UIHighlighter
|
Field Summary | |
---|---|
protected ChangeEvent |
changeEvent
Only one ChangeEvent is needed per model instance since the
event's only (read-only) state is the source property. |
protected boolean |
immutable
flag to indicate whether the Highlighter is mutable in every respect. |
static Highlighter |
ledgerBackground
Deprecated. set the component's background color instead! |
protected EventListenerList |
listenerList
The listeners waiting for model changes. |
static Highlighter |
notePadBackground
Deprecated. set the component's background color instead! |
Constructor Summary | |
---|---|
Highlighter()
Default constructor for mutable Highlighter. |
|
Highlighter(Color cellBackground,
Color cellForeground)
Constructs a mutable Highlighter with the specified
background and foreground colors, selectedBackground and
selectedForeground to null. |
|
Highlighter(Color cellBackground,
Color cellForeground,
boolean immutable)
|
|
Highlighter(Color cellBackground,
Color cellForeground,
Color selectedBackground,
Color selectedForeground)
Constructs a mutable Highlighter with the specified
background and foreground colors. |
|
Highlighter(Color cellBackground,
Color cellForeground,
Color selectedBackground,
Color selectedForeground,
boolean immutable)
Constructs a Highlighter with the specified
background and foreground colors with mutability depending on
given flag. |
Method Summary | |
---|---|
void |
addChangeListener(ChangeListener l)
Adds a ChangeListener if this is mutable. |
protected void |
applyBackground(Component renderer,
ComponentAdapter adapter)
Applies a suitable background for the renderer component within the specified adapter. |
protected void |
applyForeground(Component renderer,
ComponentAdapter adapter)
Applies a suitable foreground for the renderer component within the specified adapter. |
protected Color |
computeBackground(Component renderer,
ComponentAdapter adapter)
Computes a suitable background for the renderer component within the specified adapter and returns the computed color. |
protected Color |
computeForeground(Component renderer,
ComponentAdapter adapter)
Computes a suitable foreground for the renderer component within the specified adapter and returns the computed color. |
protected Color |
computeSelectedBackground(Color seed)
Deprecated. this is no longer used by this implementation |
protected Color |
computeSelectedBackground(Component renderer,
ComponentAdapter adapter)
Computes a suitable selected background for the renderer component within the specified adapter and returns the computed color. |
protected Color |
computeSelectedForeground(Color seed)
Deprecated. this method is longer called by this implementation |
protected Color |
computeSelectedForeground(Component renderer,
ComponentAdapter adapter)
Computes a suitable selected foreground for the renderer component within the specified adapter and returns the computed color. |
protected Color |
computeUnselectedBackground(Component renderer,
ComponentAdapter adapter)
Computes a suitable unselected background for the renderer component within the specified adapter and returns the computed color. |
protected Color |
computeUnselectedForeground(Component renderer,
ComponentAdapter adapter)
Computes a suitable unselected foreground for the renderer component within the specified adapter and returns the computed color. |
protected Component |
doHighlight(Component renderer,
ComponentAdapter adapter)
This is the bottleneck decorate method that all highlighters must invoke to decorate the cell renderer. |
protected void |
fireStateChanged()
Runs each ChangeListener 's stateChanged method. |
Color |
getBackground()
Returns the background color of this Highlighter . |
ChangeListener[] |
getChangeListeners()
Returns an array of all the change listeners registered on this Highlighter . |
Color |
getForeground()
Returns the foreground color of this Highlighter . |
Color |
getSelectedBackground()
Returns the selected background color of this Highlighter . |
Color |
getSelectedForeground()
Returns the selected foreground color of this Highlighter . |
Component |
highlight(Component renderer,
ComponentAdapter adapter)
Decorates the specified cell renderer component for the given component data adapter using highlighters that were previously set for the component. |
boolean |
isImmutable()
Returns immutable flag: if true, none of the setXX methods have any effects, there are no listeners added and no change events fired. |
void |
removeChangeListener(ChangeListener l)
Removes a ChangeListener if this is mutable. |
void |
setBackground(Color color)
Sets the background color of this Highlighter and
notifies registered ChangeListeners if this
is mutable. |
void |
setForeground(Color color)
Sets the foreground color of this Highlighter and notifies
registered ChangeListeners if this is mutable. |
void |
setSelectedBackground(Color color)
Sets the selected background color of this Highlighter
and notifies registered ChangeListeners if this is mutable. |
void |
setSelectedForeground(Color color)
Sets the selected foreground color of this Highlighter and
notifies registered ChangeListeners if this is mutable. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected 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 EventListenerList listenerList
protected final boolean immutable
public static final Highlighter ledgerBackground
Highlighter
that highlights the background of
each cell with a pastel green "ledger" background color, and is most
effective when the ComponentAdapter.target
component has
horizontal gridlines in Color.cyan.darker()
color.
public static final Highlighter notePadBackground
Highlighter
that decorates the background of
each cell with a pastel yellow "notepad" background color, and is most
effective when the ComponentAdapter.target
component has
horizontal gridlines in Color.cyan.darker()
color.
Constructor Detail |
---|
public Highlighter()
public Highlighter(Color cellBackground, Color cellForeground)
Highlighter
with the specified
background and foreground colors, selectedBackground and
selectedForeground to null.
cellBackground
- background color for the renderer, or null,
to compute a suitable backgroundcellForeground
- foreground color for the renderer, or null,
to compute a suitable foregroundpublic Highlighter(Color cellBackground, Color cellForeground, boolean immutable)
public Highlighter(Color cellBackground, Color cellForeground, Color selectedBackground, Color selectedForeground)
Highlighter
with the specified
background and foreground colors.
cellBackground
- background color for the renderer, or null,
to compute a suitable backgroundcellForeground
- foreground color for the renderer, or null,
to compute a suitable foregroundpublic Highlighter(Color cellBackground, Color cellForeground, Color selectedBackground, Color selectedForeground, boolean immutable)
Highlighter
with the specified
background and foreground colors with mutability depending on
given flag.
cellBackground
- background color for the renderer, or null,
to compute a suitable backgroundcellForeground
- foreground color for the renderer, or null,
to compute a suitable foregroundMethod Detail |
---|
public Component highlight(Component renderer, ComponentAdapter adapter)
doHighlight
with
the same arguments as were passed in.
renderer
- the cell renderer component that is to be decoratedadapter
- the ComponentAdapter
for this decorate operation
protected Component doHighlight(Component renderer, ComponentAdapter adapter)
applyBackground
, applyForeground
,
to decorate the corresponding
attributes of the specified component within the given adapter. Subclasses which want to decorate additional properties must override this and additionally call custom applyXX methods.
renderer
- the cell renderer component that is to be decoratedadapter
- the ComponentAdapter
for this decorate operation
protected void applyBackground(Component renderer, ComponentAdapter adapter)
This implementation calls computeBackground
and applies the computed color to the component if the returned value is
!= null. Otherwise it does nothing.
renderer
- the cell renderer component that is to be decoratedadapter
- the ComponentAdapter
for this decorate operationprotected void applyForeground(Component renderer, ComponentAdapter adapter)
This implementation calls computeForeground
and applies the computed color to the component if the returned value
is != null. Otherwise it does nothing.
renderer
- the cell renderer component that is to be decoratedadapter
- the ComponentAdapter
for this decorate operationprotected Color computeBackground(Component renderer, ComponentAdapter adapter)
Computes a suitable background for the renderer component within the specified adapter and returns the computed color.
In this implementation the returned color depends
on isSelected
: it will
return computSelected/-UnselectedBackground, respectively.
renderer
- the cell renderer component that is to be decoratedadapter
- the ComponentAdapter
for this decorate operation
protected Color computeUnselectedBackground(Component renderer, ComponentAdapter adapter)
Computes a suitable unselected background for the renderer component within the specified adapter and returns the computed color. This implementation returns getBackground().
renderer
- adapter
-
protected Color computeSelectedBackground(Component renderer, ComponentAdapter adapter)
Computes a suitable selected background for the renderer component within the specified adapter and returns the computed color. This implementation returns getSelectedBackground().
renderer
- adapter
-
protected Color computeForeground(Component renderer, ComponentAdapter adapter)
Computes a suitable foreground for the renderer component within the
specified adapter and returns the computed color.
In this implementation the returned color depends
on isSelected
: it will
return computSelected/-UnselectedForeground, respectively.
renderer
- the cell renderer component that is to be decoratedadapter
- the ComponentAdapter
for this decorate operation
protected Color computeUnselectedForeground(Component renderer, ComponentAdapter adapter)
Computes a suitable unselected foreground for the renderer component within the specified adapter and returns the computed color. This implementation returns getForeground().
renderer
- adapter
-
protected Color computeSelectedForeground(Component renderer, ComponentAdapter adapter)
Computes a suitable selected foreground for the renderer component within the specified adapter and returns the computed color. This implementation returns getSelectedForeground().
renderer
- adapter
-
protected Color computeSelectedBackground(Color seed)
seed
- initial background color; must cope with null!
protected Color computeSelectedForeground(Color seed)
seed
- initial foreground color; must cope with null!
public boolean isImmutable()
public Color getBackground()
Highlighter
.
Highlighter
,
or null, if no background color has been setpublic void setBackground(Color color)
Highlighter
and
notifies registered ChangeListeners if this
is mutable. Does nothing if immutable.
color
- the background color of this Highlighter
,
or null, to clear any existing background colorpublic Color getForeground()
Highlighter
.
Highlighter
,
or null, if no foreground color has been setpublic void setForeground(Color color)
Highlighter
and notifies
registered ChangeListeners if this is mutable. Does nothing if
immutable.
color
- the foreground color of this Highlighter
,
or null, to clear any existing foreground colorpublic Color getSelectedBackground()
Highlighter
.
Highlighter
,
or null, if no selected background color has been setpublic void setSelectedBackground(Color color)
Highlighter
and notifies registered ChangeListeners if this is mutable. Does nothing
if immutable.
color
- the selected background color of this Highlighter
,
or null, to clear any existing selected background colorpublic Color getSelectedForeground()
Highlighter
.
Highlighter
,
or null, if no selected foreground color has been setpublic void setSelectedForeground(Color color)
Highlighter
and
notifies registered ChangeListeners if this is mutable. Does nothing if
immutable.
color
- the selected foreground color of this Highlighter
,
or null, to clear any existing selected foreground colorpublic void addChangeListener(ChangeListener l)
ChangeListener
if this is mutable. ChangeListeners are
notified after changes of any attribute. Does nothing if immutable.
l
- the ChangeListener to addremoveChangeListener(javax.swing.event.ChangeListener)
public void removeChangeListener(ChangeListener l)
ChangeListener
if this is mutable.
Does nothis if immutable.
l
- the ChangeListener
to removeaddChangeListener(javax.swing.event.ChangeListener)
public ChangeListener[] getChangeListeners()
Highlighter
.
ChangeListener
s
or an empty
array if no change listeners are currently registeredaddChangeListener(javax.swing.event.ChangeListener)
,
removeChangeListener(javax.swing.event.ChangeListener)
protected void fireStateChanged()
ChangeListener
's stateChanged
method.
|
swingx Version 2005-08-19 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |