KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > JXTreeTableUnitTest


1 /*
2  * $Id: JXTreeTableUnitTest.java,v 1.6 2005/02/10 15:49:38 kleopatra Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing;
9
10 import java.util.regex.Pattern JavaDoc;
11
12 import java.awt.Color JavaDoc;
13 import java.awt.Dimension JavaDoc;
14 import java.awt.Point JavaDoc;
15 import javax.swing.JComponent JavaDoc;
16 import javax.swing.JFrame JavaDoc;
17 import javax.swing.JScrollPane JavaDoc;
18
19 import org.jdesktop.swing.decorator.AlternateRowHighlighter;
20 import org.jdesktop.swing.decorator.HierarchicalColumnHighlighter;
21 import org.jdesktop.swing.decorator.Highlighter;
22 import org.jdesktop.swing.decorator.HighlighterPipeline;
23 import org.jdesktop.swing.JXTreeTable;
24 import org.jdesktop.swing.decorator.PatternHighlighter;
25 import org.jdesktop.swing.treetable.DefaultTreeTableModel;
26 import org.jdesktop.swing.treetable.FileSystemModel;
27 import org.jdesktop.swing.treetable.TreeTableModel;
28
29 // import de.kleopatra.view.LFSwitcher;
30

31 public class JXTreeTableUnitTest extends InteractiveTestCase {
32
33     private TreeTableModel treeTableModel;
34
35     public JXTreeTableUnitTest() {
36         super("JXTreeTable Unit Test");
37     }
38
39     /**
40      * Issue #151: renderer properties ignored after setting treeTableModel.
41      *
42      */

43     public void testRendererProperties() {
44         JXTreeTable treeTable = new JXTreeTable(treeTableModel);
45         // storing negates of properties
46
boolean expandsSelected = !treeTable.getExpandsSelectedPaths();
47         boolean scrollsOnExpand = !treeTable.getScrollsOnExpand();
48         boolean showRootHandles = !treeTable.getShowsRootHandles();
49         boolean rootVisible = !treeTable.isRootVisible();
50         // setting negates properties
51
treeTable.setExpandsSelectedPaths(expandsSelected);
52         treeTable.setScrollsOnExpand(scrollsOnExpand);
53         treeTable.setShowsRootHandles(showRootHandles);
54         treeTable.setRootVisible(rootVisible);
55         // assert negates are set - sanity assert
56
assertEquals("expand selected", expandsSelected, treeTable
57                 .getExpandsSelectedPaths());
58         assertEquals("scrolls expand", scrollsOnExpand, treeTable
59                 .getScrollsOnExpand());
60         assertEquals("shows handles", showRootHandles, treeTable
61                 .getShowsRootHandles());
62         assertEquals("root visible", rootVisible, treeTable.isRootVisible());
63         // setting a new model
64
treeTable.setTreeTableModel(new DefaultTreeTableModel());
65         // assert negates are set
66
assertEquals("expand selected", expandsSelected, treeTable
67                 .getExpandsSelectedPaths());
68         assertEquals("scrolls expand", scrollsOnExpand, treeTable
69                 .getScrollsOnExpand());
70         assertEquals("shows handles", showRootHandles, treeTable
71                 .getShowsRootHandles());
72         assertEquals("root visible", rootVisible, treeTable.isRootVisible());
73
74     }
75
76     /**
77      * Issue #148: line style client property not respected by renderer.
78      *
79      */

80     public void testLineStyle() {
81         JXTreeTable treeTable = new JXTreeTable(treeTableModel);
82         String JavaDoc propertyName = "JTree.lineStyle";
83         treeTable.putClientProperty(propertyName, "Horizontal");
84         JXTree renderer = (JXTree) treeTable.getCellRenderer(0, 0);
85         assertEquals(propertyName + " set on renderer", "Horizontal", renderer
86                 .getClientProperty(propertyName));
87     }
88
89     /**
90      * sanity test: arbitrary client properties not passed to renderer.
91      *
92      */

93     public void testArbitraryClientProperty() {
94         JXTreeTable treeTable = new JXTreeTable(treeTableModel);
95         String JavaDoc propertyName = "someproperty";
96         treeTable.putClientProperty(propertyName, "Horizontal");
97         JXTree renderer = (JXTree) treeTable.getCellRenderer(0, 0);
98         assertNull(propertyName + " not set on renderer", renderer
99                 .getClientProperty(propertyName));
100
101     }
102
103     // ---------------------------- interactive tests
104

105     public void interactiveTestFilterAndLineStyle() {
106         JXTreeTable treeTable = new JXTreeTable(treeTableModel);
107         // issue #148
108
// did not work on LFs which normally respect lineStyle
109
// winLF does not respect it anyway...
110
treeTable.putClientProperty("JTree.lineStyle", "Angled");
111         treeTable.setRowHeight(22);
112         treeTable.setRowMargin(1);
113         treeTable.setHighlighters(new HighlighterPipeline(new Highlighter[] {
114                 AlternateRowHighlighter.quickSilver,
115                 new HierarchicalColumnHighlighter(),
116                 new PatternHighlighter(null, Color.red, "s.*",
117                         Pattern.CASE_INSENSITIVE, 0, -1), }));
118         JFrame JavaDoc frame = wrapWithScrollingInFrame(treeTable,
119                 "QuickSilver-, Column-, PatternHighligher and LineStyle");
120         frame.setVisible(true);
121     }
122
123     public void interactiveTestFiltersAndRowHeight() {
124         JXTreeTable treeTable = new JXTreeTable(treeTableModel);
125         treeTable.setRowHeight(22);
126         treeTable.setRowMargin(1);
127         treeTable.setHighlighters(new HighlighterPipeline(new Highlighter[] {
128                 AlternateRowHighlighter.linePrinter,
129                 new HierarchicalColumnHighlighter(), }));
130         JFrame JavaDoc frame = wrapWithScrollingInFrame(treeTable,
131                 "LinePrinter-, ColumnHighlighter and RowHeight");
132         frame.setVisible(true);
133     }
134
135     public void interactiveTestAlternateRowHighlighter() {
136         JXTreeTable treeTable = new JXTreeTable(treeTableModel);
137         treeTable
138                 .setHighlighters(new HighlighterPipeline(
139                         new Highlighter[] { AlternateRowHighlighter.classicLinePrinter, }));
140         treeTable.setRowHeight(22);
141         treeTable.setRowMargin(1);
142         JFrame JavaDoc frame = wrapWithScrollingInFrame(treeTable,
143                 "ClassicLinePrinter and RowHeight");
144         frame.setVisible(true);
145     }
146
147     public void interactiveTestBackgroundHighlighter() {
148         JXTreeTable treeTable = new JXTreeTable(treeTableModel);
149         treeTable.setHighlighters(new HighlighterPipeline(new Highlighter[] {
150                 AlternateRowHighlighter.notePadBackground,
151                 new HierarchicalColumnHighlighter(), }));
152         treeTable.setBackground(new Color JavaDoc(0xFF, 0xFF, 0xCC)); // notepad
153
treeTable.setGridColor(Color.cyan.darker());
154         treeTable.setRowHeight(22);
155         treeTable.setRowMargin(1);
156         treeTable.setShowHorizontalLines(true);
157         JFrame JavaDoc frame = wrapWithScrollingInFrame(treeTable,
158                 "NotePadBackground- HierarchicalColumnHighlighter and horiz lines");
159         frame.setVisible(true);
160     }
161
162     public void interactiveTestLedgerBackground() {
163         JXTreeTable treeTable = new JXTreeTable(treeTableModel);
164         treeTable.setBackground(new Color JavaDoc(0xF5, 0xFF, 0xF5)); // ledger
165
treeTable.setGridColor(Color.cyan.darker());
166         treeTable.setRowHeight(22);
167         treeTable.setRowMargin(1);
168         treeTable.setShowHorizontalLines(true);
169         JFrame JavaDoc frame = wrapWithScrollingInFrame(treeTable, "LedgerBackground");
170         frame.setVisible(true);
171     }
172
173     public void interactiveTestHierarchicalColumn() {
174         JXTreeTable treeTable = new JXTreeTable(treeTableModel);
175         treeTable.setHighlighters(new HighlighterPipeline(
176                 new Highlighter[] { new HierarchicalColumnHighlighter(), }));
177         JFrame JavaDoc frame = wrapWithScrollingInFrame(treeTable,
178                 "HierarchicalColumnHigh");
179         frame.setVisible(true);
180     }
181
182     public void interactiveTestIntercellSpacing1() {
183         JXTreeTable treeTable = new JXTreeTable(treeTableModel);
184         treeTable.setIntercellSpacing(new Dimension JavaDoc(1, 1));
185         treeTable.setShowGrid(true);
186         JFrame JavaDoc frame = wrapWithScrollingInFrame(treeTable, "Intercellspacing 1");
187         frame.setVisible(true);
188     }
189
190     public void interactiveTestIntercellSpacing2() {
191         JXTreeTable treeTable = new JXTreeTable(treeTableModel);
192         treeTable.setIntercellSpacing(new Dimension JavaDoc(2, 2));
193         treeTable.setShowGrid(true);
194         JFrame JavaDoc frame = wrapWithScrollingInFrame(treeTable, "Intercellspacing 2");
195         frame.setVisible(true);
196     }
197
198     public void interactiveTestIntercellSpacing3() {
199         JXTreeTable treeTable = new JXTreeTable(treeTableModel);
200         treeTable.setIntercellSpacing(new Dimension JavaDoc(3, 3));
201         treeTable.setShowGrid(true);
202         JFrame JavaDoc frame = wrapWithScrollingInFrame(treeTable, "Intercellspacing 3");
203         frame.setVisible(true);
204     }
205
206     public void interactiveTestHighlighterRowHeight() {
207         JXTreeTable treeTable = new JXTreeTable(treeTableModel);
208         treeTable.setHighlighters(new HighlighterPipeline(
209                 new Highlighter[] { new Highlighter(Color.orange, null), }));
210         treeTable.setIntercellSpacing(new Dimension JavaDoc(15, 15));
211         treeTable.setRowHeight(48);
212         JFrame JavaDoc frame = wrapWithScrollingInFrame(treeTable,
213                 "Orange, IntercellSpacing15, big rowheight");
214         frame.setVisible(true);
215     }
216
217     public void interactiveTestHighLighters() {
218         JXTreeTable treeTable = new JXTreeTable(treeTableModel);
219         treeTable.setIntercellSpacing(new Dimension JavaDoc(15, 15));
220         treeTable.setRowHeight(48);
221         // not supported in JXTreeTable
222
// treeTable.setRowHeight(0, 96);
223
treeTable.setShowGrid(true);
224         treeTable.setHighlighters(new HighlighterPipeline(
225                 new Highlighter[] {
226                         new Highlighter(Color.orange, null),
227                         new HierarchicalColumnHighlighter(),
228                         new PatternHighlighter(null, Color.red,
229                                 ".*TreeTable.*", 0, 0), }));
230         JFrame JavaDoc frame = wrapWithScrollingInFrame(treeTable, "Highlighters");
231         frame.setVisible(true);
232     }
233
234     // public void interactiveTest() {
235
// JXTreeTable treeTable = new JXTreeTable(treeTableModel);
236
// JFrame frame = wrapWithScrollingInFrame(treeTable, "");
237
// frame.setVisible(true);
238
// }
239

240     // ------------------ init
241
protected void setUp() throws Exception JavaDoc {
242         super.setUp();
243         treeTableModel = new FileSystemModel();
244     }
245
246     public static void main(String JavaDoc[] args) {
247         JXTreeTableUnitTest test = new JXTreeTableUnitTest();
248         try {
249          // test.runInteractiveTests();
250
test.runInteractiveTests("interactive.*HighLighters");
251         } catch (Exception JavaDoc ex) {
252
253         }
254         // LFSwitcher.metalLF();
255

256         // final TestCase[] testCases = createTestCases();
257
// if (testCases.length > 0) {
258
// // Automatically exit after last window is closed.
259
// testCases[testCases.length - 1].frame
260
// .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
261
//
262
// Point location = testCases[0].frame.getLocation();
263
//
264
// for (int i = testCases.length - 1; i >= 0; i--) {
265
// location.translate(30, 30); // stagger frames
266
// testCases[i].frame.setTitle("JXTreeTable Unit Test " + (i + 1));
267
// testCases[i].frame.setLocation(location);
268
// testCases[i].frame.setVisible(true);
269
// }
270
// }
271
}
272
273     /**
274      * For unit testing only
275      *
276      * @return
277      */

278     // private static TestCase[] createTestCases() {
279
//
280
// final TreeTableModel treeTableModel = new FileSystemModel(); // shared
281
//
282
// final TestCase[] testCases = new TestCase[] {
283
// new TestCase(treeTableModel) {
284
// public JComponent define() {
285
// JXTreeTable treeTable = new JXTreeTable(model);
286
// // issue #148
287
// // doesn't work on LFs which normally respect lineStyle
288
// // winLF does not respect it anyway...
289
// treeTable
290
// .putClientProperty("JTree.lineStyle", "Angled");
291
// treeTable.setRowHeight(22);
292
// treeTable.setRowMargin(1);
293
// treeTable
294
// .setHighlighters(new HighlighterPipeline(
295
// new Highlighter[] {
296
// AlternateRowHighlighter.quickSilver,
297
// new HierarchicalColumnHighlighter(),
298
// new PatternHighlighter(
299
// null,
300
// Color.red,
301
// "s.*",
302
// Pattern.CASE_INSENSITIVE,
303
// 0, -1), }));
304
// return treeTable;
305
// }
306
// },
307
// new TestCase(treeTableModel) {
308
// public JComponent define() {
309
// JXTreeTable treeTable = new JXTreeTable(model);
310
// treeTable.setRowHeight(22);
311
// treeTable.setRowMargin(1);
312
// treeTable
313
// .setHighlighters(new HighlighterPipeline(
314
// new Highlighter[] {
315
// AlternateRowHighlighter.linePrinter,
316
// new HierarchicalColumnHighlighter(), }));
317
// return treeTable;
318
// }
319
// },
320
// new TestCase(treeTableModel) {
321
// public JComponent define() {
322
// JXTreeTable treeTable = new JXTreeTable(model);
323
// treeTable
324
// .setHighlighters(new HighlighterPipeline(
325
// new Highlighter[] { AlternateRowHighlighter.classicLinePrinter, }));
326
// treeTable.setRowHeight(22);
327
// treeTable.setRowMargin(1);
328
// return treeTable;
329
// }
330
// },
331
// new TestCase(treeTableModel) {
332
// public JComponent define() {
333
// JXTreeTable treeTable = new JXTreeTable(model);
334
// treeTable
335
// .setHighlighters(new HighlighterPipeline(
336
// new Highlighter[] {
337
// AlternateRowHighlighter.notePadBackground,
338
// new HierarchicalColumnHighlighter(), }));
339
// treeTable.setBackground(new Color(0xFF, 0xFF, 0xCC)); // notepad
340
// treeTable.setGridColor(Color.cyan.darker());
341
// treeTable.setRowHeight(22);
342
// treeTable.setRowMargin(1);
343
// treeTable.setShowHorizontalLines(true);
344
// return treeTable;
345
// }
346
// },
347
// new TestCase(treeTableModel) {
348
// public JComponent define() {
349
// JXTreeTable treeTable = new JXTreeTable(model);
350
// treeTable.setBackground(new Color(0xF5, 0xFF, 0xF5)); // ledger
351
// treeTable.setGridColor(Color.cyan.darker());
352
// treeTable.setRowHeight(22);
353
// treeTable.setRowMargin(1);
354
// treeTable.setShowHorizontalLines(true);
355
// return treeTable;
356
// }
357
// },
358
// new TestCase(treeTableModel) {
359
// public JComponent define() {
360
// JXTreeTable treeTable = new JXTreeTable(model);
361
// treeTable
362
// .setHighlighters(new HighlighterPipeline(
363
// new Highlighter[] { new HierarchicalColumnHighlighter(), }));
364
// return treeTable;
365
// }
366
// },
367
// new TestCase(treeTableModel) {
368
// public JComponent define() {
369
// JXTreeTable treeTable = new JXTreeTable(model);
370
// treeTable.setShowGrid(true);
371
// return treeTable;
372
// }
373
// },
374
// new TestCase(treeTableModel) {
375
// public JComponent define() {
376
// JXTreeTable treeTable = new JXTreeTable(model);
377
// treeTable.setIntercellSpacing(new Dimension(1, 1));
378
// treeTable.setShowGrid(true);
379
// return treeTable;
380
// }
381
// },
382
//
383
// new TestCase(treeTableModel) {
384
// public JComponent define() {
385
// JXTreeTable treeTable = new JXTreeTable(model);
386
// treeTable.setIntercellSpacing(new Dimension(2, 2));
387
// treeTable.setShowGrid(true);
388
// return treeTable;
389
// }
390
// },
391
//
392
// new TestCase(treeTableModel) {
393
// public JComponent define() {
394
// JXTreeTable treeTable = new JXTreeTable(model);
395
// treeTable.setIntercellSpacing(new Dimension(3, 3));
396
// treeTable.setShowGrid(true);
397
// return treeTable;
398
// }
399
// },
400
//
401
// new TestCase(treeTableModel) {
402
// public JComponent define() {
403
// JXTreeTable treeTable = new JXTreeTable(model);
404
// treeTable.setHighlighters(new HighlighterPipeline(
405
// new Highlighter[] { new Highlighter(
406
// Color.orange, null), }));
407
// treeTable.setIntercellSpacing(new Dimension(15, 15));
408
// treeTable.setRowHeight(48);
409
// return treeTable;
410
// }
411
// },
412
// new TestCase(treeTableModel) {
413
// public JComponent define() {
414
// JXTreeTable treeTable = new JXTreeTable(model);
415
// treeTable.setIntercellSpacing(new Dimension(15, 15));
416
// treeTable.setRowHeight(48);
417
// // treeTable.setRowHeight(0, 96);
418
// treeTable.setShowGrid(true);
419
// treeTable.setHighlighters(new HighlighterPipeline(
420
// new Highlighter[] {
421
// new Highlighter(Color.orange, null),
422
// new HierarchicalColumnHighlighter(),
423
// new PatternHighlighter(null, Color.red,
424
// ".*TreeTable.*", 0, 0), }));
425
// return treeTable;
426
// }
427
// }, };
428
// return testCases;
429
// }
430
// private static abstract class TestCase {
431
//
432
// public TestCase(TreeTableModel model) {
433
// this.model = model;
434
// this.frame = wrap(define());
435
// }
436
//
437
// public abstract JComponent define();
438
//
439
// public JFrame wrap(JComponent component) {
440
// final JFrame frame = new JFrame();
441
// final JScrollPane scroller = new JScrollPane(component);
442
// scroller.setPreferredSize(new Dimension(600, 304));
443
// frame.getContentPane().add(scroller);
444
// frame.pack();
445
// frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
446
// return frame;
447
// }
448
//
449
// public final TreeTableModel model;
450
//
451
// public final JFrame frame;
452
// }
453
}
454
Popular Tags