2
* $Id: HighlighterClientVisualCheck.java 3674 2010-04-26 10:15:12Z kleopatra $
4
* Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
5
* Santa Clara, California 95054, U.S.A. All rights reserved.
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
package org.jdesktop.swingx.renderer;
24
import java.awt.Color;
25
import java.awt.Component;
26
import java.awt.Cursor;
28
import java.awt.GradientPaint;
29
import java.awt.Graphics;
30
import java.awt.Point;
31
import java.awt.event.ActionEvent;
32
import java.awt.geom.Point2D;
33
import java.beans.PropertyChangeEvent;
34
import java.beans.PropertyChangeListener;
35
import java.util.ArrayList;
36
import java.util.HashMap;
37
import java.util.List;
39
import java.util.logging.Logger;
40
import java.util.regex.Pattern;
42
import javax.swing.AbstractAction;
43
import javax.swing.AbstractListModel;
44
import javax.swing.Action;
45
import javax.swing.BorderFactory;
46
import javax.swing.Box;
47
import javax.swing.JComponent;
48
import javax.swing.JFrame;
49
import javax.swing.JLabel;
50
import javax.swing.JScrollPane;
51
import javax.swing.JTextField;
52
import javax.swing.KeyStroke;
53
import javax.swing.ListModel;
54
import javax.swing.ToolTipManager;
55
import javax.swing.UIManager;
56
import javax.swing.border.MatteBorder;
57
import javax.swing.table.TableModel;
59
import org.jdesktop.swingx.InteractiveTestCase;
60
import org.jdesktop.swingx.JXFrame;
61
import org.jdesktop.swingx.JXList;
62
import org.jdesktop.swingx.JXSearchPanel;
63
import org.jdesktop.swingx.JXTable;
64
import org.jdesktop.swingx.JXTree;
65
import org.jdesktop.swingx.JXTreeTable;
66
import org.jdesktop.swingx.color.ColorUtil;
67
import org.jdesktop.swingx.decorator.AbstractHighlighter;
68
import org.jdesktop.swingx.decorator.BorderHighlighter;
69
import org.jdesktop.swingx.decorator.ColorHighlighter;
70
import org.jdesktop.swingx.decorator.ComponentAdapter;
71
import org.jdesktop.swingx.decorator.CompoundHighlighter;
72
import org.jdesktop.swingx.decorator.HighlightPredicate;
73
import org.jdesktop.swingx.decorator.Highlighter;
74
import org.jdesktop.swingx.decorator.HighlighterFactory;
75
import org.jdesktop.swingx.decorator.PatternPredicate;
76
import org.jdesktop.swingx.decorator.ShadingColorHighlighter;
77
import org.jdesktop.swingx.decorator.HighlightPredicate.ColumnHighlightPredicate;
78
import org.jdesktop.swingx.painter.MattePainter;
79
import org.jdesktop.swingx.painter.AbstractLayoutPainter.HorizontalAlignment;
80
import org.jdesktop.swingx.renderer.RelativePainterHighlighter.NumberRelativizer;
81
import org.jdesktop.swingx.rollover.RolloverProducer;
82
import org.jdesktop.swingx.search.PatternMatcher;
83
import org.jdesktop.swingx.treetable.FileSystemModel;
84
import org.jdesktop.test.AncientSwingTeam;
87
* visual checks of highlighter clients. Mostly by example of JXTable.
89
* @author Jeanette Winzenburg
91
public class HighlighterClientVisualCheck extends InteractiveTestCase {
93
@SuppressWarnings("unused")
94
private static final Logger LOG = Logger
95
.getLogger(HighlighterClientVisualCheck.class.getName());
97
protected TableModel tableModel;
98
protected Color background = Color.RED;
99
protected Color foreground = Color.BLUE;
102
public static void main(String args[]) {
103
// UIManager.put("Nimbus.keepAlternateRowColor", Boolean.TRUE);
104
// setSystemLF(true);
105
HighlighterClientVisualCheck test = new HighlighterClientVisualCheck();
107
// setLookAndFeel("Nimbus");
108
// test.runInteractiveTests();
109
// test.runInteractiveTests(".*Striping.*");
110
test.runInteractiveTests(".*ToolTip.*");
111
// test.runInteractiveTests("interactive.*Search.*");
112
// test.runInteractiveTests("interactive.*BorderHighlighter.*");
113
} catch (Exception e) {
114
System.err.println("exception when executing interactive tests:");
120
* Issue #1295-swingx: JXTable rolloverController repaint incomplete.
121
* Column not repainted.
123
public void interactiveBorderHighlighterOnRollover() {
124
JXTable table = new JXTable(tableModel) {
127
protected void paintComponent(Graphics g) {
128
// crude check if the logic of column/row painting
129
// in TableRolloverController is working (looks so)
130
LOG.info("paint clip: " + count++ + g.getClip());
131
super.paintComponent(g);
135
final AbstractHighlighter hlRow = new BorderHighlighter(HighlightPredicate.ROLLOVER_ROW,
136
// BorderFactory.createCompoundBorder(
137
new MatteBorder(1, 0, 1, 0, Color.lightGray)
138
// , BorderFactory.createEmptyBorder(0, 1, 0, 1))
140
final AbstractHighlighter hlColumn = new ColorHighlighter(HighlightPredicate.ROLLOVER_COLUMN,
141
Color.LIGHT_GRAY, null);
142
table.setHighlighters(hlRow, hlColumn);
143
JXFrame frame = wrapWithScrollingInFrame(table, "borderHighlgihter on rollover");
144
Action toggleCell = new AbstractAction("toggle cell-rollover") {
147
public void actionPerformed(ActionEvent e) {
148
if (hlRow.getHighlightPredicate() == HighlightPredicate.ROLLOVER_ROW) {
149
hlRow.setHighlightPredicate(HighlightPredicate.NEVER);
150
hlColumn.setHighlightPredicate(HighlightPredicate.ROLLOVER_CELL);
152
hlRow.setHighlightPredicate(HighlightPredicate.ROLLOVER_ROW);
153
hlColumn.setHighlightPredicate(HighlightPredicate.ROLLOVER_COLUMN);
158
addAction(frame, toggleCell);
162
* Show variants of border Highlighters.
165
public void interactiveTableBorderHighlighter() {
166
JXTable table = new JXTable(tableModel);
167
table.setVisibleRowCount(table.getRowCount());
168
table.setVisibleColumnCount(7);
170
table.setColumnControlVisible(true);
172
BorderHighlighter outer = new BorderHighlighter(new HighlightPredicate.ColumnHighlightPredicate(1),
173
BorderFactory.createLineBorder(Color.RED, 3));
174
BorderHighlighter inner = new BorderHighlighter(new HighlightPredicate.ColumnHighlightPredicate(2),
175
BorderFactory.createLineBorder(Color.RED, 3), true, true);
176
BorderHighlighter replace = new BorderHighlighter(new HighlightPredicate.ColumnHighlightPredicate(0),
177
BorderFactory.createLineBorder(Color.RED, 3), false, true);
178
table.setHighlighters(outer, inner, replace);
179
showWithScrollingInFrame(table, "Border Highlighters");
183
* Show variants of border Highlighters.
186
public void interactiveTreeBorderHighlighter() {
187
JXTree tree = new JXTree();
189
tree.setVisibleRowCount(tree.getRowCount());
190
// need SwingX highlighter
191
tree.setCellRenderer(new DefaultTreeRenderer());
192
tree.setHighlighters(new BorderHighlighter(BorderFactory.createLineBorder(Color.GREEN, 1)));
193
showWithScrollingInFrame(tree, "Border Highlighters");
197
* Multiple Highlighters (shown as example in Javapolis 2007).
200
public void interactiveTablePatternHighlighterJP() {
201
JXTable table = new JXTable(tableModel);
202
table.setVisibleRowCount(table.getRowCount());
203
table.setVisibleColumnCount(7);
205
table.setColumnControlVisible(true);
207
Font font = table.getFont().deriveFont(Font.BOLD | Font.ITALIC);
208
Highlighter simpleStriping = HighlighterFactory.createSimpleStriping();
209
PatternPredicate patternPredicate = new PatternPredicate("^M", 1);
210
ColorHighlighter magenta = new ColorHighlighter(patternPredicate, null,
211
Color.MAGENTA, null, Color.MAGENTA);
212
FontHighlighter derivedFont = new FontHighlighter(patternPredicate,
214
Highlighter gradient = createRelativeGradientHighlighter(HorizontalAlignment.LEFT);
215
Highlighter shading = new ShadingColorHighlighter(
216
new HighlightPredicate.ColumnHighlightPredicate(1));
218
table.setHighlighters(simpleStriping, magenta, derivedFont, shading,
220
showWithScrollingInFrame(table, "Multiple Highlighters");
224
* Simulates table by one-list per column.
226
* NOTE: the only purpose is to demonstrate the similarity
227
* of highlighter usage across collection components!
228
* (shown as example in Javapolis 2007)
230
* @see #interactiveTablePatternHighlighterJP()
232
public void interactiveListPatternHighlighterJP() {
233
JXTable source = new JXTable(tableModel);
234
source.toggleSortOrder(3);
235
Font font = source.getFont().deriveFont(Font.BOLD | Font.ITALIC);
236
Highlighter simpleStriping = HighlighterFactory.createSimpleStriping();
237
String pattern = "^M";
238
PatternPredicate patternPredicate = new PatternPredicate(pattern, 0);
239
ColorHighlighter magenta = new ColorHighlighter(patternPredicate, null,
240
Color.MAGENTA, null, Color.MAGENTA);
241
FontHighlighter derivedFont = new FontHighlighter(patternPredicate,
243
Highlighter gradient = createRelativeGradientHighlighter(
244
HorizontalAlignment.LEFT);
245
Highlighter shading = new ShadingColorHighlighter(
246
new HighlightPredicate.ColumnHighlightPredicate(0));
247
// create and configure one JXList per column.
248
List<JXList> lists = new ArrayList<JXList>();
250
JXList list0 = new JXList(createListModel(source, 0));
251
list0.setHighlighters(simpleStriping);
254
JXList list1 = new JXList(createListModel(source, 1));
255
list1.setHighlighters(simpleStriping, magenta, derivedFont, shading);
259
JXList listc = new JXList(createListModel(source, 2));
260
listc.setHighlighters(simpleStriping);
264
JXList listn = new JXList(createListModel(source, 3));
265
listn.setCellRenderer(new DefaultListRenderer(
266
StringValues.NUMBER_TO_STRING, JLabel.RIGHT));
267
listn.setHighlighters(simpleStriping, gradient);
271
JXList listb = new JXList(createListModel(source, 4));
272
listb.setCellRenderer(new DefaultListRenderer(new CheckBoxProvider()));
273
listb.setFixedCellHeight(list0.getPreferredSize().height
274
/ source.getRowCount());
275
listb.setHighlighters(simpleStriping, magenta, derivedFont, gradient);
278
JComponent panel = Box.createHorizontalBox();
279
for (JXList l : lists) {
280
listb.setVisibleRowCount(source.getRowCount());
281
l.setFont(source.getFont());
282
panel.add(new JScrollPane(l));
284
showInFrame(panel, "Multiple Highlighters");
291
private RelativePainterHighlighter createRelativeGradientHighlighter(
292
HorizontalAlignment right) {
293
Color startColor = ColorUtil.setAlpha(Color.RED, 130);
294
Color endColor = ColorUtil.setAlpha(Color.RED.brighter(), 0);
295
boolean isRightAligned = HorizontalAlignment.RIGHT == right;
296
GradientPaint paint = new GradientPaint(new Point2D.Double(0, 0),
297
isRightAligned ? endColor : startColor,
298
new Point2D.Double(100, 0),
299
isRightAligned ? startColor : endColor);
300
MattePainter painter = new MattePainter(paint);
301
painter.setPaintStretched(true);
302
RelativePainterHighlighter p = new RelativePainterHighlighter(painter);
303
p.setHorizontalAlignment(right);
304
p.setRelativizer(new NumberRelativizer(100));
313
private ListModel createListModel(final JXTable tableModel, final int i) {
314
ListModel listModel = new AbstractListModel(){
316
public Object getElementAt(int index) {
317
return tableModel.getValueAt(index, i);
320
public int getSize() {
321
return tableModel.getRowCount();
327
public static class FontHighlighter extends AbstractHighlighter {
330
public FontHighlighter(HighlightPredicate predicate, Font font) {
335
protected Component doHighlight(Component component,
336
ComponentAdapter adapter) {
337
component.setFont(font);
343
* Example from forum requirement: highlight all rows of a given group
344
* if mouse if over one of them.
346
* PENDING: need to track row view-model coordinates mapping after
350
public void interactiveRolloverRowGroup() {
351
TableModel model = new AncientSwingTeam();
352
final List<Integer> rowSet = new ArrayList<Integer>();
353
for (int i = 0; i < model.getRowCount(); i++) {
354
if ((Integer)model.getValueAt(i, 3) > 10) {
358
final HighlightPredicate predicate = new HighlightPredicate() {
360
public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
361
return rowSet.contains(adapter.row);
365
final ColorHighlighter highlighter = new ColorHighlighter(HighlightPredicate.NEVER, Color.YELLOW,
367
JXTable table = new JXTable(model);
368
table.addHighlighter(highlighter);
369
PropertyChangeListener l = new PropertyChangeListener() {
371
public void propertyChange(PropertyChangeEvent evt) {
372
Point location = (Point) evt.getNewValue();
374
if (location != null) {
377
if (rowSet.contains(row)) {
378
highlighter.setHighlightPredicate(predicate);
380
highlighter.setHighlightPredicate(HighlightPredicate.NEVER);
385
table.addPropertyChangeListener(RolloverProducer.ROLLOVER_KEY, l);
386
showWithScrollingInFrame(table, "rollover highlight of row groups");
390
* Example to highlight against a value/color map.
391
* Here the control is in predicate. <p>
394
public void interactiveColorValueMappedHighlighterPredicate() {
395
JXTable table = new JXTable(new AncientSwingTeam());
396
// build a quick color lookup to simulate multi-value value-based
398
final int numberColumn = 3;
399
table.toggleSortOrder(numberColumn);
400
Color[] colors = new Color[] { Color.YELLOW, Color.CYAN, Color.MAGENTA,
402
int rowsPerColor = (table.getRowCount() - 5) / colors.length;
403
Map<Color, HighlightPredicate> map = new HashMap<Color, HighlightPredicate>();
404
for (int i = 0; i < colors.length; i++) {
405
List<Integer> values = new ArrayList<Integer>();
406
for (int j = 0; j < rowsPerColor; j++) {
407
values.add((Integer) table.getValueAt(i * rowsPerColor + j, numberColumn));
409
map.put(colors[i], new ValueMappedHighlightPredicate(values, numberColumn));
411
// create one ColorHighlighter for each color/predicate pair and
412
// add to a compoundHighlighter
413
CompoundHighlighter chl = new CompoundHighlighter();
414
for (Color color : colors) {
415
chl.addHighlighter(new ColorHighlighter(map.get(color), color, null));
417
table.resetSortOrder();
418
table.addHighlighter(chl);
419
showWithScrollingInFrame(table,
420
"compound highlighter with value-based color mapping predicate");
424
* Custom predicate which returns true if the filtered cell value
425
* of a given testColumn is contained in a list of values.
426
* PENDING: logic similar to search/pattern, enough to abstract?
428
public static class ValueMappedHighlightPredicate implements HighlightPredicate {
430
private List<?> values;
431
private int testColumn;
432
public ValueMappedHighlightPredicate(List<?> values, int testColumn) {
433
this.values = values;
434
this.testColumn = testColumn;
436
public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
437
return values.contains(adapter.getValue(testColumn));
442
* Example to highlight against a value/color map. <p>
443
* Here the Highlighter takes full control. Which is a bit
444
* on the border line of
445
* the intended distribution of responsibility between
446
* Highlighter and HighlighterPredicate.
448
public void interactiveColorValueMappedHighlighter() {
449
JXTable table = new JXTable(new AncientSwingTeam());
450
// build a quick color lookup to simulate multi-value value-based
452
final int numberColumn = 3;
453
table.toggleSortOrder(numberColumn);
454
final Map<Integer, Color> lookup = new HashMap<Integer, Color>();
455
Color[] colors = new Color[] { Color.YELLOW, Color.CYAN, Color.MAGENTA,
457
int rowsPerColor = (table.getRowCount() - 5) / colors.length;
458
for (int i = 0; i < colors.length; i++) {
459
for (int j = 0; j < rowsPerColor; j++) {
460
lookup.put((Integer) table.getValueAt(i * rowsPerColor + j,
461
numberColumn), colors[i]);
464
table.resetSortOrder();
465
// PENDING JW: add and use HighlightPredicate.SELECTED/UN_SELECTED
466
Highlighter hl = new ColorHighlighter() {
469
protected void applyBackground(Component renderer, ComponentAdapter adapter) {
470
if (adapter.isSelected()) return;
471
Color background = lookup.get(adapter.getValue(numberColumn));
472
if (background != null) {
473
renderer.setBackground(background);
477
table.addHighlighter(hl);
478
showWithScrollingInFrame(table,
479
"conditional highlighter with value-based color mapping");
483
* test to see searchPanel functionality in new Highlighter api
486
public void interactiveSearchPanel() {
487
JXTable table = new JXTable(tableModel);
488
final ColorHighlighter cl = new ColorHighlighter(new PatternPredicate((Pattern) null, 0), null,
490
table.addHighlighter(cl);
491
JXSearchPanel searchPanel = new JXSearchPanel();
492
PatternMatcher patternMatcher = new PatternMatcher() {
493
public Pattern getPattern() {
494
return getPatternPredicate().getPattern();
497
public void setPattern(Pattern pattern) {
498
PatternPredicate old = getPatternPredicate();
499
cl.setHighlightPredicate(new PatternPredicate(pattern, old
500
.getTestColumn(), old.getHighlightColumn()));
503
private PatternPredicate getPatternPredicate() {
504
return (PatternPredicate) cl.getHighlightPredicate();
508
searchPanel.addPatternMatcher(patternMatcher);
509
JXFrame frame = wrapWithScrollingInFrame(table,
510
"Pattern highlighting col 0");
511
addStatusComponent(frame, searchPanel);
515
//----------------- custom PatternMatcher
518
* columm shading (was: hierarchicalColumnHighlighter)
521
public void interactiveColumnShading() {
522
JXTreeTable treeTable = new JXTreeTable(new FileSystemModel());
523
// simulate hierarchicalColumnHighlighter
524
int hierarchicalColumn = 0;
525
for (int i = 0; i < treeTable.getColumnCount(); i++) {
526
if (treeTable.isHierarchical(i)) {
527
hierarchicalColumn = i;
531
treeTable.addHighlighter(new ShadingColorHighlighter(new ColumnHighlightPredicate(hierarchicalColumn)));
532
showWithScrollingInFrame(treeTable, "hierarchical column");
537
* Classic lineprinter striping and hyperlink (LF only, no action).
540
public void interactiveTableAlternateAndHyperlink() {
541
JXTable table = new JXTable(tableModel);
542
table.setRowHeight(22);
543
table.getColumn(1).setCellRenderer(
544
new DefaultTableRenderer(new HyperlinkProvider()));
545
table.addHighlighter(HighlighterFactory
546
.createSimpleStriping(HighlighterFactory.CLASSIC_LINE_PRINTER));
547
JFrame frame = wrapWithScrollingInFrame(table,
548
"classic lineprinter and hyperlink on column 1");
549
frame.setVisible(true);
554
* LinePrinter striping and rollover.
557
public void interactiveTableAlternateAndRollover() {
558
JXTable table = new JXTable(tableModel);
559
table.setRowHeight(22);
560
table.setHighlighters(
561
HighlighterFactory.createSimpleStriping(HighlighterFactory.LINE_PRINTER),
562
new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW, Color.YELLOW, null));
563
showWithScrollingInFrame(table, "LinePrinter plus yellow rollover");
567
* Foreground highlight on column 1 and 3.
570
public void interactiveColumnForeground() {
571
JXTable table = new JXTable(tableModel);
572
HighlightPredicate predicate = new ColumnHighlightPredicate(1, 3);
573
table.addHighlighter(new ColorHighlighter(predicate, null, Color.BLUE));
574
showWithScrollingInFrame(table, "Foreground highlight col 1 and 3");
579
* ColorHighlighter with pattern predicate
582
public void interactiveTablePatternHighlighter() {
583
JXTable table = new JXTable(tableModel);
584
table.setColumnControlVisible(true);
585
table.addHighlighter(new ColorHighlighter(new PatternPredicate("^M", 1), null,
587
showWithScrollingInFrame(table, "Pattern: highlight row if ^M col 1");
591
* Issue #258-swingx: Background LegacyHighlighter must not change custom
595
* Use SwingX extended default renderer.
597
public void interactiveTableCustomRendererColor() {
598
TableModel model = new AncientSwingTeam();
599
JXTable table = new JXTable(model);
600
DefaultTableRenderer renderer = new DefaultTableRenderer();
601
renderer.setForeground(foreground);
602
renderer.setBackground(background);
603
table.addHighlighter(HighlighterFactory.createAlternateStriping(Color.WHITE, HighlighterFactory.GENERIC_GRAY));
604
table.setDefaultRenderer(Object.class, renderer);
605
JXTable nohighlight = new JXTable(model);
606
nohighlight.setDefaultRenderer(Object.class, renderer);
607
showWithScrollingInFrame(table, nohighlight,
608
"ext: custom colored renderer with bg highlighter <--> shared without highl");
612
* Requirement from forum: value based color and tooltip.
614
* Here the tooltip is regarded as visual decoration and
615
* set in a specialized Highlighter.
618
public void interactiveValueBasedToolTipAndColorOnHighlighter() {
619
ToolTipManager t = ToolTipManager.sharedInstance();
620
LOG.info("initial/reshow/dismiss" + t.getInitialDelay() + " / " + t.getReshowDelay()
621
+ " / " + t.getDismissDelay()
622
+ " / " + UIManager.getString("ToolTipManager.enableToolTipMode"));
623
final JXTable table = new JXTable(new AncientSwingTeam()) {
626
// public Point getToolTipLocation(MouseEvent event) {
627
// int column = columnAtPoint(event.getPoint());
628
// int row = rowAtPoint(event.getPoint());
629
// Rectangle cellRect = getCellRect(row, column, false);
630
// if (!getComponentOrientation().isLeftToRight()) {
631
// cellRect.translate(cellRect.width, 0);
633
// // PENDING JW: otherwise we get a small (borders only) tooltip for null
634
// // core issue? Yeh, the logic in tooltipManager is crooked.
635
// // but this here is ehem ... rather arbitrary.
636
//// return getValueAt(row, column) == null ? null : cellRect.getLocation();
637
// return getToolTipText(event) == null ? null : cellRect.getLocation();
643
t.unregisterComponent(table);
644
table.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke('q'), "dummy");
645
t.registerComponent(table);
646
table.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("F5"), "hideTip");
647
LOG.info("hideTip? " + table.getActionMap().get("hideTip"));
649
PropertyChangeListener l = new PropertyChangeListener() {
652
public void propertyChange(PropertyChangeEvent evt) {
653
LOG.info("evt.getP" + evt.getPropertyName());
654
ToolTipManager.sharedInstance().mousePressed(null);
657
table.addPropertyChangeListener(RolloverProducer.ROLLOVER_KEY, l);
658
// table.addMouseMotionListener(new MouseMotionAdapter() {
662
// public void mouseMoved(MouseEvent e) {
663
// int row = table.rowAtPoint(e.getPoint());
664
// int col = table.columnAtPoint(e.getPoint());
665
// if (row == prevRow && col == prevCol)
667
// ToolTipManager.sharedInstance().mousePressed(e);
668
//// ToolTipManager.sharedInstance().mouseEntered(e);
673
HighlightPredicate predicate = new HighlightPredicate() {
675
public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
676
if (!(adapter.getValue() instanceof Number)) return false;
677
return ((Number) adapter.getValue()).intValue() < 10;
681
ColorHighlighter hl = new ColorHighlighter(
682
predicate, null, Color.RED, null, Color.RED);
683
// THINK this is possible, but maybe not the correct place
684
// ... more on the what-side of "what vs. how" ?
685
Highlighter tl = new AbstractHighlighter(predicate) {
688
protected Component doHighlight(Component component, ComponentAdapter adapter) {
689
String text = "low on luck: " + ((JLabel) component).getText();
690
((JComponent) component).setToolTipText(text);
694
protected boolean canHighlight(Component component,
695
ComponentAdapter adapter) {
696
return component instanceof JLabel;
700
table.setHighlighters(HighlighterFactory.createSimpleStriping(HighlighterFactory.GENERIC_GRAY),
702
showWithScrollingInFrame(table, "Value-based Tooltip ... on Highlighter");
706
* Requirement from forum: value based color and tooltip.<p>
708
* Here the tooltip is regarded as belonging more to the "what"
709
* of rendering and set in a custom provider. The implication
710
* is that the logic (whether to show the tooltip or not) is
711
* duplicated (in the predicate and the provider.
715
public void interactiveValueBasedToolTipAndColorOnProvider() {
716
JXTable table = new JXTable(new AncientSwingTeam());
717
HighlightPredicate predicate = new HighlightPredicate() {
719
public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
720
if (!(adapter.getValue() instanceof Number)) return false;
721
return ((Number) adapter.getValue()).intValue() < 10;
725
ColorHighlighter hl = new ColorHighlighter(
726
predicate, null, Color.RED, null, Color.RED);
727
table.setHighlighters(HighlighterFactory.createSimpleStriping(HighlighterFactory.GENERIC_GRAY),
729
// here: set's value based .. this duplicates logic of
731
LabelProvider provider = new LabelProvider() {
735
protected void configureState(CellContext context) {
736
super.configureState(context);
737
rendererComponent.setToolTipText(getToolTipText(context));
741
private String getToolTipText(CellContext context) {
742
if ((context.getValue() instanceof Number)) {
743
int luck = ((Number) context.getValue()).intValue();
745
return "low on luck: " + luck;
752
provider.setHorizontalAlignment(JLabel.RIGHT);
753
table.setDefaultRenderer(Number.class, new DefaultTableRenderer(provider));
754
showWithScrollingInFrame(table, "Value-based Tooltip ... on provider");
758
* Example of custom predicate based on the component's value,
759
* (as opposed to on the value of the adapter).
763
public void interactiveTableColorBasedOnComponentValue() {
764
TableModel model = new AncientSwingTeam();
765
JXTable table = new JXTable(model);
766
table.setForeground(Color.GREEN);
767
HighlightPredicate predicate = new HighlightPredicate() {
769
public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
770
if (!(renderer instanceof JLabel)) return false;
771
String text = ((JLabel) renderer).getText();
772
return text.contains("y");
776
ColorHighlighter hl = new ColorHighlighter(predicate, null, Color.RED);
777
table.addHighlighter(HighlighterFactory.createSimpleStriping(HighlighterFactory.GENERIC_GRAY));
778
table.addHighlighter(hl);
779
showWithScrollingInFrame(table,
780
"component value-based rendering (label text contains y) ");
784
//------------------ rollover
787
* Issue #503-swingx: custom cursor respected when rollover?
788
* Seems okay for table,
790
public void interactiveRolloverHighlightCustomCursor() {
791
JXTable table = new JXTable(tableModel);
792
table.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
793
table.addHighlighter(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW, Color.YELLOW, null));
794
showWithScrollingInFrame(table, "rollover highlight, custom cursor");
798
* Plain RolloverHighlighter.
799
* Issue #513-swingx: no rollover effect for disabled table.
802
public void interactiveRolloverHighlight() {
803
final JXTable table = new JXTable(tableModel);
804
ColorHighlighter colorHighlighter = new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW, Color.YELLOW, null);
805
table.addHighlighter(colorHighlighter);
806
Action action = new AbstractAction("toggle table enabled") {
808
public void actionPerformed(ActionEvent e) {
809
table.setEnabled(!table.isEnabled());
814
JXFrame frame = showWithScrollingInFrame(table, "rollover highlight, enabled/disabled table");
815
addAction(frame, action);
820
//--------------------- factory
822
* Simple ui-striping.
825
public void interactiveTreeTableSimpleStripingUI() {
826
JXTreeTable table = new JXTreeTable(new FileSystemModel());
827
table.setVisibleRowCount(table.getRowCount() + 3);
828
table.addHighlighter(HighlighterFactory.createSimpleStriping());
829
showWithScrollingInFrame(table, "Simple ui striping");
833
* shows the effect of a simple striping highlighter on a
837
public void interactiveTreeTableSimpleStriping() {
838
JXTreeTable table = new JXTreeTable(new FileSystemModel());
839
table.setVisibleRowCount(table.getRowCount() + 3);
840
table.setBackground(new Color(0xC0FFC0));
841
table.addHighlighter(HighlighterFactory.createSimpleStriping());
842
showWithScrollingInFrame(table, "Simple gray striping");
845
* Simple ui-striping.
848
public void interactiveSimpleStripingUI() {
849
JXTable table = new JXTable(tableModel);
850
table.setVisibleRowCount(table.getRowCount() + 3);
851
table.addHighlighter(HighlighterFactory.createSimpleStriping());
852
showWithScrollingInFrame(table, "Simple ui striping");
856
* shows the effect of a simple striping highlighter on a
860
public void interactiveSimpleStriping() {
861
JXTable table = new JXTable(tableModel);
862
table.setVisibleRowCount(table.getRowCount() + 3);
863
table.setBackground(new Color(0xC0FFC0));
864
table.addHighlighter(HighlighterFactory.createSimpleStriping());
865
Highlighter disabled = new AbstractHighlighter(HighlightPredicate.READ_ONLY) {
868
protected Component doHighlight(Component component,
869
ComponentAdapter adapter) {
870
component.setEnabled(false);
874
table.getColumnExt(0).setEditable(false);
875
table.getColumnExt(2).setEditable(false);
876
table.getColumnExt(0).setCellRenderer(new DefaultTableRenderer(new TextFieldProvider()));
877
table.addHighlighter(disabled);
878
showWithScrollingInFrame(table, "Simple gray striping");
881
public static class TextFieldProvider extends ComponentProvider<JTextField> {
884
protected void configureState(CellContext context) {
885
// TODO Auto-generated method stub
890
protected JTextField createRendererComponent() {
891
JTextField field = new JTextField(20);
896
protected void format(CellContext context) {
897
rendererComponent.setText(getValueAsString(context));
902
* shows the effect of a simple striping highlighter on a
906
public void interactiveSimpleStripingGroup() {
907
JXTable table = new JXTable(tableModel);
908
table.setVisibleRowCount(table.getRowCount() + 3);
909
table.setBackground(Color.YELLOW);
910
table.addHighlighter(HighlighterFactory.createSimpleStriping(Color.LIGHT_GRAY, 3));
911
showWithScrollingInFrame(table, "Simple gray striping, grouped by 3");
914
* shows the effect of a simple striping highlighter on a
918
public void interactiveAlternateStriping() {
919
JXTable table = new JXTable(tableModel);
920
table.setVisibleRowCount(table.getRowCount() + 3);
921
table.setBackground(Color.YELLOW);
922
table.addHighlighter(HighlighterFactory.createAlternateStriping(Color.WHITE, Color.LIGHT_GRAY));
923
showWithScrollingInFrame(table, "Alternate white/gray striping");
927
* shows the effect of a simple striping highlighter on a
931
public void interactiveAlternateStripingGroup() {
932
JXTable table = new JXTable(tableModel);
933
table.setVisibleRowCount(table.getRowCount() + 3);
934
table.setBackground(Color.YELLOW);
935
table.addHighlighter(HighlighterFactory.createAlternateStriping(Color.WHITE, Color.LIGHT_GRAY, 3));
936
showWithScrollingInFrame(table, "Alternate white/gray striping");
941
protected void setUp() throws Exception {
943
tableModel = new AncientSwingTeam();
947
* Do nothing, make the test runner happy
948
* (would output a warning without a test fixture).
951
public void testDummy() {