Enhanced List component with support for general SwingX sorting/filtering,
rendering, highlighting, rollover and search functionality. List specific
enhancements include ?? PENDING JW ...
Sorting and Filtering
JXList supports sorting and filtering.
It provides api to apply a specific sort order, to toggle the sort order and to reset a sort.
Sort sequence can be configured by setting a custom comparator.
list.setFilterEnabled(true);
list.setComparator(myComparator);
list.setSortOrder(SortOrder.DESCENDING);
list.toggleSortOder();
list.resetSortOrder();
Rows can be filtered from a JXList using a Filter class and a
FilterPipeline. One assigns a FilterPipeline to the table using
#setFilters(FilterPipeline). Filtering hides, but does not delete nor
permanently remove rows from a JXList.
JXList provides api to access items of the underlying model in view coordinates
and to convert from/to model coordinates.
Note: List sorting/filtering is disabled by
default because it has side-effects which might break "normal" expectations
when using a JList: if enabled all row coordinates (including those returned
by the selection) are in view coordinates. Furthermore, the model returned
from getModel() is a wrapper around the actual data.
Note: SwingX sorting/filtering is incompatible with core sorting/filtering in
JDK 6+. Will be replaced by core functionality after switching the target jdk
version from 5 to 6.
Rendering and Highlighting
As all SwingX collection views, a JXList is a HighlighterClient (PENDING JW:
formally define and implement, like in AbstractTestHighlighter), that is it
provides consistent api to add and remove Highlighters which can visually
decorate the rendering component.
JXList list = new JXList(new Contributors());
// implement a custom string representation, concated from first-, lastName
StringValue sv = new StringValue() {
public String getString(Object value) {
if (value instanceof Contributor) {
Contributor contributor = (Contributor) value;
return contributor.lastName() + ", " + contributor.firstName();
}
return StringValues.TO_STRING(value);
}
};
list.setCellRenderer(new DefaultListRenderer(sv);
// highlight condition: gold merits
HighlightPredicate predicate = new HighlightPredicate() {
public boolean isHighlighted(Component renderer,
ComponentAdapter adapter) {
if (!(value instanceof Contributor)) return false;
return ((Contributor) value).hasGold();
}
};
// highlight with foreground color
list.addHighlighter(new PainterHighlighter(predicate, goldStarPainter);
Note: to support the highlighting this implementation wraps the
ListCellRenderer set by client code with a DelegatingRenderer which applies
the Highlighter after delegating the default configuration to the wrappee. As
a side-effect, getCellRenderer does return the wrapper instead of the custom
renderer. To access the latter, client code must call getWrappedCellRenderer.
Rollover
As all SwingX collection views, a JXList supports per-cell rollover. If
enabled, the component fires rollover events on enter/exit of a cell which by
default is promoted to the renderer if it implements RolloverRenderer, that
is simulates live behaviour. The rollover events can be used by client code
as well, f.i. to decorate the rollover row using a Highlighter.
JXList list = new JXList();
list.setRolloverEnabled(true);
list.setCellRenderer(new DefaultListRenderer());
list.addHighlighter(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW,
null, Color.RED);
Search
As all SwingX collection views, a JXList is searchable. A search action is
registered in its ActionMap under the key "find". The default behaviour is to
ask the SearchFactory to open a search component on this component. The
default keybinding is retrieved from the SearchFactory, typically ctrl-f (or
cmd-f for Mac). Client code can register custom actions and/or bindings as
appropriate.
JXList provides api to vend a renderer-controlled String representation of
cell content. This allows the Searchable and Highlighters to use WYSIWYM
(What-You-See-Is-What-You-Match), that is pattern matching against the actual
string as seen by the user.