~ubuntu-branches/ubuntu/trusty/libswingx-java/trusty

« back to all changes in this revision

Viewing changes to src/test/org/jdesktop/swingx/renderer/HighlighterClientVisualCheck.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-03-06 00:28:45 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110306002845-escned3cbqp5qx0t
Tags: 1:1.6.2-1
* New upstream release.
* Switch to maven as build system:
  - d/control: drop ant, add maven-debian-helper
  - d/rules: use maven.mk
* d/patches/pom.diff: drop, uneeded since upstream fixed its dependencies.
* d/watch: update to use java.net directly.
* d/rules: force debian version for JARs (Closes: #603495).
* d/copyright: Update to lastest DEP-5 r166.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * $Id: HighlighterClientVisualCheck.java 3674 2010-04-26 10:15:12Z kleopatra $
3
 
 *
4
 
 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
5
 
 * Santa Clara, California 95054, U.S.A. All rights reserved.
6
 
 *
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.
11
 
 * 
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.
16
 
 * 
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
20
 
 *
21
 
 */
22
 
package org.jdesktop.swingx.renderer;
23
 
 
24
 
import java.awt.Color;
25
 
import java.awt.Component;
26
 
import java.awt.Cursor;
27
 
import java.awt.Font;
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;
38
 
import java.util.Map;
39
 
import java.util.logging.Logger;
40
 
import java.util.regex.Pattern;
41
 
 
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;
58
 
 
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;
85
 
 
86
 
/**
87
 
 * visual checks of highlighter clients. Mostly by example of JXTable.
88
 
 * 
89
 
 * @author Jeanette Winzenburg
90
 
 */
91
 
public class HighlighterClientVisualCheck extends InteractiveTestCase {
92
 
    
93
 
    @SuppressWarnings("unused")
94
 
    private static final Logger LOG = Logger
95
 
            .getLogger(HighlighterClientVisualCheck.class.getName());
96
 
    
97
 
    protected TableModel tableModel;
98
 
    protected Color background = Color.RED;
99
 
    protected Color foreground = Color.BLUE;
100
 
    
101
 
    
102
 
    public static void main(String args[]) {
103
 
//        UIManager.put("Nimbus.keepAlternateRowColor", Boolean.TRUE);
104
 
//      setSystemLF(true);
105
 
      HighlighterClientVisualCheck test = new HighlighterClientVisualCheck();
106
 
      try {
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:");
115
 
          e.printStackTrace();
116
 
      }
117
 
  }
118
 
 
119
 
    /**
120
 
     * Issue #1295-swingx: JXTable rolloverController repaint incomplete.
121
 
     * Column not repainted.
122
 
     */
123
 
    public void interactiveBorderHighlighterOnRollover() {
124
 
        JXTable table = new JXTable(tableModel) {
125
 
            int count;
126
 
            @Override
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);
132
 
            }
133
 
            
134
 
        };
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))
139
 
                , true, false );  
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") {
145
 
            
146
 
            @Override
147
 
            public void actionPerformed(ActionEvent e) {
148
 
                if (hlRow.getHighlightPredicate() == HighlightPredicate.ROLLOVER_ROW) {
149
 
                    hlRow.setHighlightPredicate(HighlightPredicate.NEVER);
150
 
                    hlColumn.setHighlightPredicate(HighlightPredicate.ROLLOVER_CELL);
151
 
                } else {
152
 
                    hlRow.setHighlightPredicate(HighlightPredicate.ROLLOVER_ROW);
153
 
                    hlColumn.setHighlightPredicate(HighlightPredicate.ROLLOVER_COLUMN);
154
 
                }
155
 
                
156
 
            }
157
 
        };
158
 
        addAction(frame, toggleCell);
159
 
        show(frame);
160
 
    }
161
 
    /**
162
 
     * Show variants of border Highlighters.
163
 
     *
164
 
     */
165
 
    public void interactiveTableBorderHighlighter() {
166
 
        JXTable table = new JXTable(tableModel);
167
 
        table.setVisibleRowCount(table.getRowCount());
168
 
        table.setVisibleColumnCount(7);
169
 
        table.packAll();
170
 
        table.setColumnControlVisible(true);
171
 
        
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");
180
 
    }
181
 
    
182
 
    /**
183
 
     * Show variants of border Highlighters.
184
 
     *
185
 
     */
186
 
    public void interactiveTreeBorderHighlighter() {
187
 
        JXTree tree = new JXTree();
188
 
        tree.expandAll();
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");
194
 
    }
195
 
 
196
 
    /**
197
 
     * Multiple Highlighters (shown as example in Javapolis 2007).
198
 
     * 
199
 
     */
200
 
    public void interactiveTablePatternHighlighterJP() {
201
 
        JXTable table = new JXTable(tableModel);
202
 
        table.setVisibleRowCount(table.getRowCount());
203
 
        table.setVisibleColumnCount(7);
204
 
        table.packAll();
205
 
        table.setColumnControlVisible(true);
206
 
 
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,
213
 
                font);
214
 
        Highlighter gradient = createRelativeGradientHighlighter(HorizontalAlignment.LEFT);
215
 
        Highlighter shading = new ShadingColorHighlighter(
216
 
                new HighlightPredicate.ColumnHighlightPredicate(1));
217
 
 
218
 
        table.setHighlighters(simpleStriping, magenta, derivedFont, shading,
219
 
                gradient);
220
 
        showWithScrollingInFrame(table, "Multiple Highlighters");
221
 
    }
222
 
 
223
 
    /**
224
 
     * Simulates table by one-list per column.
225
 
     * 
226
 
     * NOTE: the only purpose is to demonstrate the similarity
227
 
     * of highlighter usage across collection components!
228
 
     * (shown as example in Javapolis 2007)
229
 
     * 
230
 
     * @see #interactiveTablePatternHighlighterJP()
231
 
     */
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,
242
 
                font);
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>();
249
 
        // first name
250
 
        JXList list0 = new JXList(createListModel(source, 0));
251
 
        list0.setHighlighters(simpleStriping);
252
 
        lists.add(list0);
253
 
        // last name
254
 
        JXList list1 = new JXList(createListModel(source, 1));
255
 
        list1.setHighlighters(simpleStriping, magenta, derivedFont, shading);
256
 
        lists.add(list1);
257
 
 
258
 
        // color
259
 
        JXList listc = new JXList(createListModel(source, 2));
260
 
        listc.setHighlighters(simpleStriping);
261
 
        lists.add(listc);
262
 
 
263
 
        // number
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);
268
 
        lists.add(listn);
269
 
 
270
 
        // boolean
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);
276
 
        lists.add(listb);
277
 
 
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));
283
 
        }
284
 
        showInFrame(panel, "Multiple Highlighters");
285
 
    }
286
 
 
287
 
    /**
288
 
     * @param right
289
 
     * @return
290
 
     */
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));
305
 
        return p;
306
 
    }
307
 
    
308
 
    /**
309
 
     * @param tableModel2
310
 
     * @param i
311
 
     * @return
312
 
     */
313
 
    private ListModel createListModel(final JXTable tableModel, final int i) {
314
 
        ListModel listModel = new AbstractListModel(){
315
 
 
316
 
            public Object getElementAt(int index) {
317
 
                return tableModel.getValueAt(index, i);
318
 
            }
319
 
 
320
 
            public int getSize() {
321
 
                return tableModel.getRowCount();
322
 
            }};
323
 
        return listModel ;
324
 
    }
325
 
 
326
 
 
327
 
    public static class FontHighlighter extends AbstractHighlighter {
328
 
        
329
 
        private Font font;
330
 
        public FontHighlighter(HighlightPredicate predicate, Font font) {
331
 
            super(predicate);
332
 
            this.font = font;
333
 
        }
334
 
        @Override
335
 
        protected Component doHighlight(Component component,
336
 
                ComponentAdapter adapter) {
337
 
            component.setFont(font);
338
 
            return component;
339
 
        }
340
 
        
341
 
    }
342
 
    /**
343
 
     * Example from forum requirement: highlight all rows of a given group
344
 
     * if mouse if over one of them.
345
 
     * 
346
 
     * PENDING: need to track row view-model coordinates mapping after
347
 
     * filtering/sorting.
348
 
     *
349
 
     */
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) {
355
 
             rowSet.add(i);
356
 
         }
357
 
       }
358
 
       final HighlightPredicate predicate = new HighlightPredicate() {
359
 
 
360
 
        public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
361
 
            return rowSet.contains(adapter.row);
362
 
        }
363
 
           
364
 
       };
365
 
       final ColorHighlighter highlighter = new ColorHighlighter(HighlightPredicate.NEVER, Color.YELLOW, 
366
 
               null);
367
 
       JXTable table = new JXTable(model);
368
 
       table.addHighlighter(highlighter);
369
 
       PropertyChangeListener l = new PropertyChangeListener() {
370
 
 
371
 
        public void propertyChange(PropertyChangeEvent evt) {
372
 
            Point location = (Point) evt.getNewValue();
373
 
            int row = -1;
374
 
            if (location != null) {
375
 
                row = location.y;
376
 
            }
377
 
            if (rowSet.contains(row)) {
378
 
                highlighter.setHighlightPredicate(predicate);
379
 
            } else {
380
 
                highlighter.setHighlightPredicate(HighlightPredicate.NEVER);
381
 
            }
382
 
        }
383
 
           
384
 
       };
385
 
       table.addPropertyChangeListener(RolloverProducer.ROLLOVER_KEY, l);
386
 
       showWithScrollingInFrame(table, "rollover highlight of row groups");
387
 
    }
388
 
    
389
 
    /**
390
 
     * Example to highlight against a value/color map.
391
 
     * Here the control is in predicate. <p>
392
 
     * 
393
 
     */
394
 
    public void interactiveColorValueMappedHighlighterPredicate() {
395
 
        JXTable table = new JXTable(new AncientSwingTeam());
396
 
        // build a quick color lookup to simulate multi-value value-based
397
 
        // coloring
398
 
        final int numberColumn = 3;
399
 
        table.toggleSortOrder(numberColumn);
400
 
        Color[] colors = new Color[] { Color.YELLOW, Color.CYAN, Color.MAGENTA,
401
 
                Color.GREEN };
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));
408
 
            }
409
 
            map.put(colors[i], new ValueMappedHighlightPredicate(values, numberColumn));
410
 
        }
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));
416
 
        }
417
 
        table.resetSortOrder();
418
 
        table.addHighlighter(chl);
419
 
        showWithScrollingInFrame(table,
420
 
                "compound highlighter with value-based color mapping predicate");
421
 
    }
422
 
    
423
 
    /**
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?
427
 
     */
428
 
    public static class ValueMappedHighlightPredicate implements HighlightPredicate {
429
 
 
430
 
        private List<?> values;
431
 
        private int testColumn;
432
 
        public ValueMappedHighlightPredicate(List<?> values, int testColumn) {
433
 
            this.values = values;
434
 
            this.testColumn = testColumn;
435
 
        }
436
 
        public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
437
 
            return values.contains(adapter.getValue(testColumn));
438
 
        }
439
 
        
440
 
    }
441
 
    /**
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.
447
 
     */
448
 
    public void interactiveColorValueMappedHighlighter() {
449
 
        JXTable table = new JXTable(new AncientSwingTeam());
450
 
        // build a quick color lookup to simulate multi-value value-based
451
 
        // coloring
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,
456
 
                Color.GREEN };
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]);
462
 
            }
463
 
        }
464
 
        table.resetSortOrder();
465
 
        // PENDING JW: add and use HighlightPredicate.SELECTED/UN_SELECTED 
466
 
        Highlighter hl = new ColorHighlighter() {
467
 
 
468
 
            @Override
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);
474
 
                }
475
 
            }
476
 
        };
477
 
        table.addHighlighter(hl);
478
 
        showWithScrollingInFrame(table,
479
 
                "conditional highlighter with value-based color mapping");
480
 
    }
481
 
 
482
 
    /**
483
 
     * test to see searchPanel functionality in new Highlighter api
484
 
     * 
485
 
     */
486
 
    public void interactiveSearchPanel() {
487
 
        JXTable table = new JXTable(tableModel);
488
 
        final ColorHighlighter cl = new ColorHighlighter(new PatternPredicate((Pattern) null, 0), null,
489
 
                Color.RED);
490
 
        table.addHighlighter(cl);
491
 
        JXSearchPanel searchPanel = new JXSearchPanel();
492
 
        PatternMatcher patternMatcher = new PatternMatcher() {
493
 
            public Pattern getPattern() {
494
 
                return getPatternPredicate().getPattern();
495
 
            }
496
 
 
497
 
            public void setPattern(Pattern pattern) {
498
 
                PatternPredicate old = getPatternPredicate();
499
 
                cl.setHighlightPredicate(new PatternPredicate(pattern, old
500
 
                        .getTestColumn(), old.getHighlightColumn()));
501
 
            }
502
 
            
503
 
            private PatternPredicate getPatternPredicate() {
504
 
                return (PatternPredicate) cl.getHighlightPredicate();
505
 
            }
506
 
 
507
 
        };
508
 
        searchPanel.addPatternMatcher(patternMatcher);
509
 
        JXFrame frame = wrapWithScrollingInFrame(table,
510
 
                "Pattern highlighting col 0");
511
 
        addStatusComponent(frame, searchPanel);
512
 
        show(frame);
513
 
    }
514
 
    
515
 
//----------------- custom PatternMatcher
516
 
    
517
 
    /**
518
 
     * columm shading (was: hierarchicalColumnHighlighter)
519
 
     *
520
 
     */
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;
528
 
                break;
529
 
            }
530
 
        }
531
 
        treeTable.addHighlighter(new ShadingColorHighlighter(new ColumnHighlightPredicate(hierarchicalColumn)));
532
 
        showWithScrollingInFrame(treeTable, "hierarchical column");
533
 
        
534
 
    }
535
 
    
536
 
    /**
537
 
     * Classic lineprinter striping and hyperlink (LF only, no action).
538
 
     * 
539
 
     */
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);
550
 
    }
551
 
 
552
 
 
553
 
    /**
554
 
     * LinePrinter striping and rollover.
555
 
     *
556
 
     */
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");
564
 
    }
565
 
 
566
 
    /**
567
 
     * Foreground highlight on column 1 and 3.
568
 
     *
569
 
     */
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");
575
 
    }
576
 
 
577
 
 
578
 
    /**
579
 
     * ColorHighlighter with pattern predicate
580
 
     *
581
 
     */
582
 
    public void interactiveTablePatternHighlighter() {
583
 
        JXTable table = new JXTable(tableModel);
584
 
        table.setColumnControlVisible(true);
585
 
        table.addHighlighter(new ColorHighlighter(new PatternPredicate("^M", 1), null, 
586
 
                Color.red));
587
 
        showWithScrollingInFrame(table, "Pattern: highlight row if ^M col 1");
588
 
    }
589
 
 
590
 
    /**
591
 
     * Issue #258-swingx: Background LegacyHighlighter must not change custom
592
 
     * foreground.
593
 
     * <p>
594
 
     * 
595
 
     * Use SwingX extended default renderer.
596
 
     */
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");
609
 
    }
610
 
    
611
 
    /**
612
 
     * Requirement from forum: value based color and tooltip.
613
 
     * 
614
 
     * Here the tooltip is regarded as visual decoration and 
615
 
     * set in a specialized Highlighter. 
616
 
     *
617
 
     */
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()) {
624
 
 
625
 
//            @Override
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);
632
 
//                }
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();
638
 
////                return null;
639
 
//            }
640
 
//            
641
 
 
642
 
        };
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"));
648
 
        
649
 
        PropertyChangeListener l = new PropertyChangeListener() {
650
 
            
651
 
            @Override
652
 
            public void propertyChange(PropertyChangeEvent evt) {
653
 
                LOG.info("evt.getP" + evt.getPropertyName());
654
 
                ToolTipManager.sharedInstance().mousePressed(null);
655
 
            }
656
 
        };
657
 
        table.addPropertyChangeListener(RolloverProducer.ROLLOVER_KEY, l);
658
 
//        table.addMouseMotionListener(new MouseMotionAdapter() {
659
 
//            int prevRow = -1;
660
 
//            int prevCol = -1;
661
 
//          
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)
666
 
//                  return;
667
 
//               ToolTipManager.sharedInstance().mousePressed(e);
668
 
////               ToolTipManager.sharedInstance().mouseEntered(e);
669
 
//               prevRow = row;
670
 
//               prevCol = col;
671
 
//            }
672
 
//         });
673
 
        HighlightPredicate predicate = new HighlightPredicate() {
674
 
 
675
 
            public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
676
 
                if (!(adapter.getValue() instanceof Number)) return false;
677
 
                return ((Number) adapter.getValue()).intValue() < 10;
678
 
            }
679
 
            
680
 
        };
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) {
686
 
 
687
 
            @Override
688
 
            protected Component doHighlight(Component component, ComponentAdapter adapter) {
689
 
                String text = "low on luck: " + ((JLabel) component).getText();
690
 
                ((JComponent) component).setToolTipText(text);
691
 
                return component;
692
 
            }
693
 
            @Override
694
 
            protected boolean canHighlight(Component component,
695
 
                    ComponentAdapter adapter) {
696
 
                return component instanceof JLabel;
697
 
            }
698
 
            
699
 
        };
700
 
        table.setHighlighters(HighlighterFactory.createSimpleStriping(HighlighterFactory.GENERIC_GRAY),
701
 
                hl, tl);
702
 
        showWithScrollingInFrame(table, "Value-based Tooltip ... on Highlighter");
703
 
    }
704
 
 
705
 
    /**
706
 
     * Requirement from forum: value based color and tooltip.<p>
707
 
     * 
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.
712
 
     * 
713
 
     *
714
 
     */
715
 
    public void interactiveValueBasedToolTipAndColorOnProvider() {
716
 
        JXTable table = new JXTable(new AncientSwingTeam());
717
 
        HighlightPredicate predicate = new HighlightPredicate() {
718
 
 
719
 
            public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
720
 
                if (!(adapter.getValue() instanceof Number)) return false;
721
 
                return ((Number) adapter.getValue()).intValue() < 10;
722
 
            }
723
 
            
724
 
        };
725
 
        ColorHighlighter hl = new ColorHighlighter(
726
 
                predicate, null, Color.RED, null, Color.RED);
727
 
        table.setHighlighters(HighlighterFactory.createSimpleStriping(HighlighterFactory.GENERIC_GRAY),
728
 
                hl); //, tl);
729
 
        // here: set's value based .. this duplicates logic of 
730
 
        // predicate
731
 
        LabelProvider provider = new LabelProvider() {
732
 
 
733
 
            
734
 
            @Override
735
 
            protected void configureState(CellContext context) {
736
 
                super.configureState(context);
737
 
                rendererComponent.setToolTipText(getToolTipText(context));
738
 
            }
739
 
 
740
 
 
741
 
            private String getToolTipText(CellContext context) {
742
 
                if ((context.getValue() instanceof Number))  {
743
 
                    int luck = ((Number) context.getValue()).intValue();
744
 
                    if (luck < 10) {
745
 
                        return "low on luck: " + luck;
746
 
                    }
747
 
                }
748
 
                return null;
749
 
            }
750
 
            
751
 
        };
752
 
        provider.setHorizontalAlignment(JLabel.RIGHT);
753
 
        table.setDefaultRenderer(Number.class, new DefaultTableRenderer(provider));
754
 
        showWithScrollingInFrame(table, "Value-based Tooltip ... on provider");
755
 
    }
756
 
 
757
 
    /**
758
 
     * Example of custom predicate based on the component's value, 
759
 
     * (as opposed to on the value of the adapter). 
760
 
     * 
761
 
     * 
762
 
     */
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() {
768
 
 
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");
773
 
            }
774
 
            
775
 
        };
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) ");
781
 
    }
782
 
 
783
 
 
784
 
    //------------------ rollover
785
 
    
786
 
    /**
787
 
     * Issue #503-swingx: custom cursor respected when rollover?
788
 
     * Seems okay for table, 
789
 
     */
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");
795
 
    }
796
 
 
797
 
    /**
798
 
     * Plain RolloverHighlighter. 
799
 
     * Issue #513-swingx: no rollover effect for disabled table.
800
 
     *
801
 
     */
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") {
807
 
 
808
 
            public void actionPerformed(ActionEvent e) {
809
 
                table.setEnabled(!table.isEnabled());
810
 
                
811
 
            }
812
 
            
813
 
        };
814
 
        JXFrame frame = showWithScrollingInFrame(table, "rollover highlight, enabled/disabled table");
815
 
        addAction(frame, action);
816
 
        frame.pack();
817
 
    }
818
 
    
819
 
 
820
 
//--------------------- factory    
821
 
    /**
822
 
     * Simple ui-striping.
823
 
     *
824
 
     */
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");
830
 
    }
831
 
    
832
 
    /**
833
 
     * shows the effect of a simple striping highlighter on a 
834
 
     * colored table.
835
 
     *
836
 
     */
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");
843
 
    }
844
 
    /**
845
 
     * Simple ui-striping.
846
 
     *
847
 
     */
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");
853
 
    }
854
 
 
855
 
    /**
856
 
     * shows the effect of a simple striping highlighter on a 
857
 
     * colored table.
858
 
     *
859
 
     */
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) {
866
 
            
867
 
            @Override
868
 
            protected Component doHighlight(Component component,
869
 
                    ComponentAdapter adapter) {
870
 
                component.setEnabled(false);
871
 
                return component;
872
 
            }
873
 
        };
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");
879
 
    }
880
 
 
881
 
    public static class TextFieldProvider extends ComponentProvider<JTextField> {
882
 
 
883
 
        @Override
884
 
        protected void configureState(CellContext context) {
885
 
            // TODO Auto-generated method stub
886
 
            
887
 
        }
888
 
 
889
 
        @Override
890
 
        protected JTextField createRendererComponent() {
891
 
            JTextField field = new JTextField(20);
892
 
            return field;
893
 
        }
894
 
 
895
 
        @Override
896
 
        protected void format(CellContext context) {
897
 
            rendererComponent.setText(getValueAsString(context));
898
 
        }
899
 
        
900
 
    }
901
 
    /**
902
 
     * shows the effect of a simple striping highlighter on a 
903
 
     * colored table.
904
 
     *
905
 
     */
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");
912
 
    }
913
 
    /**
914
 
     * shows the effect of a simple striping highlighter on a 
915
 
     * colored table.
916
 
     *
917
 
     */
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");
924
 
    }
925
 
    
926
 
    /**
927
 
     * shows the effect of a simple striping highlighter on a 
928
 
     * colored table.
929
 
     *
930
 
     */
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");
937
 
    }
938
 
 
939
 
    
940
 
    @Override
941
 
    protected void setUp() throws Exception {
942
 
        super.setUp();
943
 
         tableModel = new AncientSwingTeam();
944
 
     }
945
 
 
946
 
    /**
947
 
     * Do nothing, make the test runner happy
948
 
     * (would output a warning without a test fixture).
949
 
     *
950
 
     */
951
 
    public void testDummy() {
952
 
        
953
 
    }
954
 
}