KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdnc > JNTableUnitTest


1 /*
2  * $Id: JNTableUnitTest.java,v 1.13 2005/02/22 12:11:37 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.jdnc;
9
10 import java.awt.Color JavaDoc;
11 import java.awt.Font JavaDoc;
12 import java.awt.Point JavaDoc;
13 import java.awt.event.ActionEvent JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.text.SimpleDateFormat JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.Locale JavaDoc;
19
20 import javax.swing.AbstractAction JavaDoc;
21 import javax.swing.Action JavaDoc;
22 import javax.swing.ImageIcon JavaDoc;
23 import javax.swing.JFrame JavaDoc;
24 import javax.swing.JLabel JavaDoc;
25 import javax.swing.table.DefaultTableModel JavaDoc;
26 import javax.swing.table.TableModel JavaDoc;
27
28 import org.jdesktop.jdnc.actions.FindAction;
29 import org.jdesktop.jdnc.actions.PrintAction;
30 import org.jdesktop.swing.InteractiveTestCase;
31 import org.jdesktop.swing.JXSearchPanel;
32 import org.jdesktop.swing.actions.TargetManager;
33 import org.jdesktop.swing.data.DefaultTableModelExt;
34 import org.jdesktop.swing.data.EnumeratedMetaData;
35 import org.jdesktop.swing.data.MetaData;
36 import org.jdesktop.swing.data.NumberMetaData;
37 import org.jdesktop.swing.decorator.AlternateRowHighlighter;
38 import org.jdesktop.swing.decorator.Filter;
39 import org.jdesktop.swing.decorator.FilterPipeline;
40 import org.jdesktop.swing.decorator.Highlighter;
41 import org.jdesktop.swing.decorator.HighlighterPipeline;
42 import org.jdesktop.swing.decorator.PatternFilter;
43 import org.jdesktop.swing.decorator.PatternHighlighter;
44
45
46 public class JNTableUnitTest extends InteractiveTestCase {
47
48     final static String JavaDoc DATA_URL = "resources/bugdata.txt";
49
50     final static String JavaDoc bugstates[] = {
51         "dispatched", "accepted", "evaluated", "committed",
52         "fixed", "integrated", "verified", "closed"};
53
54     public static DefaultTableModelExt createRawData(URL JavaDoc dataURL) {
55         DefaultTableModelExt rawData = new DefaultTableModelExt(dataURL);
56         try {
57             rawData.startLoading();
58         }
59         catch (IOException JavaDoc e) {
60             e.printStackTrace();
61         }
62         return rawData;
63     }
64
65     public static DefaultTableModelExt createRichData(URL JavaDoc dataURL) {
66         DefaultTableModelExt richdata = new DefaultTableModelExt(dataURL);
67         richdata.setColumnCount(8);
68
69         MetaData metaData = richdata.getColumnMetaData(0);
70         metaData.setName("bugid");
71         metaData.setLabel("Bug ID");
72         metaData.setElementClass(java.lang.Integer JavaDoc.class);
73
74         NumberMetaData numberMetaData = new NumberMetaData("priority");
75         numberMetaData.setLabel("Pri");
76         numberMetaData.setMinimum(new Integer JavaDoc(1));
77         numberMetaData.setMaximum(new Integer JavaDoc(5));
78         richdata.setColumnMetaData(1, numberMetaData);
79
80         numberMetaData = new NumberMetaData("severity");
81         numberMetaData.setLabel("Sev");
82         numberMetaData.setReadOnly(true);
83         richdata.setColumnMetaData(2, numberMetaData);
84
85         metaData = richdata.getColumnMetaData(3);
86         metaData.setName("engineer");
87         metaData.setLabel("Engineer");
88
89         metaData = richdata.getColumnMetaData(4);
90         metaData.setName("dispatchdate");
91         metaData.setLabel("Dispatched");
92         metaData.setElementClass(java.util.Date JavaDoc.class);
93         metaData.setDecodeFormat(new SimpleDateFormat JavaDoc(
94             "EEE MMM dd HH:mm:ss zzz yyyy"));
95
96         numberMetaData = new NumberMetaData("jdcvotes");
97         numberMetaData.setLabel("JDC Votes");
98         numberMetaData.setReadOnly(true);
99         richdata.setColumnMetaData(5, numberMetaData);
100
101         metaData = richdata.getColumnMetaData(6);
102         metaData.setName("synopsis");
103         metaData.setLabel("Synopsis");
104
105         EnumeratedMetaData enumMetaData = new EnumeratedMetaData("state");
106         enumMetaData.setLabel("State");
107         enumMetaData.setEnumeration(bugstates);
108         richdata.setColumnMetaData(7, enumMetaData);
109
110         try {
111             richdata.startLoading();
112         }
113         catch (IOException JavaDoc e) {
114             e.printStackTrace();
115         }
116         return richdata;
117     }
118
119     private URL JavaDoc dataURL;
120     private DefaultTableModelExt rawdata;
121     private DefaultTableModelExt richdata;
122
123     private Point JavaDoc frameLocation = new Point JavaDoc(0,0);
124
125     public JNTableUnitTest() {
126         super("JNTable unit test");
127     }
128
129     protected void setUp() {
130         // needed because of local probs when loading resources
131
// see issue #??
132
Locale.setDefault(Locale.US);
133         dataURL = JNTableUnitTest.class.getResource(DATA_URL);
134
135         // Create two flavors of data for test purposes:
136
// rawdata: no metadata, all values interpretted as raw strings
137
// richdata: with metadata, including type, edit constraints, etc.
138
rawdata = createRawData(dataURL);
139         richdata = createRichData(dataURL);
140     }
141
142     // Dummmy test
143
public void testInstantiation() {
144         JNTable table = new JNTable();
145     }
146
147     /**
148      * Issue #102: ??.
149      * fixed.
150      *
151      */

152     public void testHighlighers() {
153        Highlighter[] highlighters = new Highlighter[] {
154            new AlternateRowHighlighter(Color.white, new Color JavaDoc(0xF0, 0xF0, 0xE0), null),
155            new PatternHighlighter(null, Color.red, "s.*", 0, 0)
156        };
157
158        HighlighterPipeline highlighterPipeline = new HighlighterPipeline(highlighters);
159        JNTable table = new JNTable();
160        table.setHighlighters(highlighterPipeline);
161        table.setColumnForeground("bugid", Color.green);
162     }
163
164     public void interactiveTestJNTableActions() {
165         JNTable table = new JNTable(rawdata);
166         table.setRowHeaderLocked(true);
167
168         PatternFilter patternFilter = new PatternFilter(null, 0, 3);
169         JXSearchPanel searchPanel = new JXSearchPanel();
170         searchPanel.setPatternFilter(patternFilter);
171         table.setFilters(new FilterPipeline(new Filter[] {
172                                             patternFilter,
173         }));
174
175         PrintAction print = new PrintAction();
176         table.addAction(print);
177
178         FindAction search = new FindAction();
179         table.addAction(search);
180         // this is the old way of doing things
181
// use ActionContainerFactory instead.
182
TargetManager.getInstance().setTarget(table);
183         table.addToolBarComponent(searchPanel);
184         JFrame JavaDoc frame = wrapInFrame(table, "JNTable Actions");
185         frame.setVisible(true);
186     }
187
188     public void interactiveTestJNTableAlternatingRowColors() {
189         JNTable table = new JNTable(rawdata);
190         table.setOddRowBackground(new Color JavaDoc(0xCC, 0xCC, 0xFF));
191         table.setEvenRowBackground(Color.white);
192         table.setShowHorizontalLines(false);
193         JFrame JavaDoc frame = wrapInFrame(table, "JNTable Alternating Row Colors");
194         frame.setVisible(true);
195     }
196
197     /**
198      * Reported problem with setModel not updating correctly.
199      * Reproducible if there is any (row) selection when
200      * toggling the model.
201      * Probably related to Issue #16: Exception thrown when
202      * deleting last row.
203      *
204      */

205     public void interactiveTestToggleModel() {
206         final JNTable table = new JNTable();
207         Filter filter = new PatternFilter(".*4.*", 0, 0);
208         table.setFilters(new FilterPipeline(new Filter[] {filter}));
209         Action JavaDoc toggleAction = new AbstractAction JavaDoc("Toggle Model") {
210             int startRow = 0;
211             public void actionPerformed(ActionEvent JavaDoc e) {
212                 int count = new Date JavaDoc().getSeconds();
213                 table.setModel(createModel(startRow, count));
214                 startRow +=count;
215                 
216             }
217
218             private TableModel JavaDoc createModel(int startRow, int count) {
219                 DefaultTableModel JavaDoc model = new DefaultTableModel JavaDoc(count, 5);
220                 for (int i = 0; i < model.getRowCount(); i++) {
221                     model.setValueAt(new Integer JavaDoc(startRow++), i, 0);
222                 }
223                 return model;
224             }
225             
226         };
227         table.addAction(toggleAction);
228         table.setModel(new DefaultTableModel JavaDoc(10, 5));
229         table.setHasColumnControl(true);
230         JFrame JavaDoc frame = wrapInFrame(table, "JNTable Toggle Model");
231         frame.setVisible(true);
232     }
233  
234     /**
235      * testing sorting/filtering when adding rows.
236      * Looks okay.
237      *
238      */

239     public void interactiveTestAddRow() {
240         final JNTable table = new JNTable();
241         Filter filter = new PatternFilter(".*4.*", 0, 0);
242         table.setFilters(new FilterPipeline(new Filter[] {filter}));
243         Action JavaDoc toggleAction = new AbstractAction JavaDoc("Add Row") {
244             int count = -100;
245             public void actionPerformed(ActionEvent JavaDoc e) {
246                 ((DefaultTableModel JavaDoc) table.getModel()).addRow(new Object JavaDoc[] { new Integer JavaDoc(count--) });
247                 
248             }
249
250             
251         };
252         table.addAction(toggleAction);
253         table.setModel(createModel(40, 10));
254         table.setHasColumnControl(true);
255         JFrame JavaDoc frame = wrapInFrame(table, "JNTable Add Row");
256         frame.setVisible(true);
257     }
258     
259     /**
260      * Issue #16
261      * testing sorting/filtering when removing row
262      * if last row (in model coordinates) is selected
263      *
264      */

265     public void interactiveTestRemoveRow() {
266         final JNTable table = new JNTable();
267         Filter filter = new PatternFilter(".*4.*", 0, 0);
268         table.setFilters(new FilterPipeline(new Filter[] {filter}));
269         Action JavaDoc toggleAction = new AbstractAction JavaDoc("Remove Row") {
270             public void actionPerformed(ActionEvent JavaDoc e) {
271                 if (table.getModel().getRowCount() > 0)
272                     ((DefaultTableModel JavaDoc) table.getModel()).removeRow(0);
273                 
274             }
275
276             
277         };
278         table.addAction(toggleAction);
279         table.setModel(createModel(40, 10));
280         table.setHasColumnControl(true);
281         JFrame JavaDoc frame = wrapInFrame(table, "JNTable Remove Row");
282         frame.setVisible(true);
283     }
284     
285     private DefaultTableModel JavaDoc createModel(int startRow, int count) {
286         DefaultTableModel JavaDoc model = new DefaultTableModel JavaDoc(count, 5);
287         for (int i = 0; i < model.getRowCount(); i++) {
288             model.setValueAt(new Integer JavaDoc(startRow++), i, 0);
289         }
290         return model;
291     }
292  
293     /**
294      * Issue #11: Column control not showing with few rows.
295      *
296      */

297     public void interactiveTestJNTableFewRows() {
298         final JNTable table = new JNTable();
299         Action JavaDoc toggleAction = new AbstractAction JavaDoc("Toggle Control") {
300
301             public void actionPerformed(ActionEvent JavaDoc e) {
302                 table.setHasColumnControl(!table.getHasColumnControl());
303                 
304             }
305             
306         };
307         table.addAction(toggleAction);
308         table.setModel(new DefaultTableModel JavaDoc(10, 5));
309         table.setHasColumnControl(true);
310         JFrame JavaDoc frame = wrapInFrame(table, "JNTable ColumnControl with few rows");
311         frame.setVisible(true);
312     }
313     /**
314      * hmm... sporadic ArrayIndexOOB after sequence:
315      *
316      * filter(column), sort(column), hide(column), setFilter(null)
317      *
318      */

319     public void testColumnControlAndFilters() {
320         final JNTable table = new JNTable(richdata);
321         table.setHasColumnControl(true);
322         Filter filter = new PatternFilter("41.*", 0, 0);
323         table.setFilters(new FilterPipeline(new Filter[] {filter}));
324         // needed to make public in JXTable for testing
325
// table.getTable().setSorter(0);
326
table.getColumn(0).setVisible(false);
327         table.setFilters(null);
328
329     }
330     /**
331      * Issue #??: Problem with filters and ColumnControl
332      *
333      */

334     public void interactiveTestJNTableColumnControlAndFilters() {
335         final JNTable table = new JNTable(richdata);
336         Action JavaDoc toggleFilter = new AbstractAction JavaDoc("Toggle Filter") {
337             
338             public void actionPerformed(ActionEvent JavaDoc e) {
339                 if (table.getFilters() != null) {
340                     table.setFilters(null);
341                 } else {
342                     Filter filter = new PatternFilter("41.*", 0, 0);
343                     table.setFilters(new FilterPipeline(new Filter[] {filter}));
344
345                 }
346                 
347             }
348             
349         };
350         table.addAction(toggleFilter);
351         table.setHasColumnControl(true);
352         JFrame JavaDoc frame = wrapInFrame(table, "JNTable ColumnControl and Filters");
353         frame.setVisible(true);
354     }
355     /**
356      * Issue #144 : odd/evenRowBackground ignored if set before
357      * Highlighter.
358      * it's already marked as a Todo in JNTable.setHighlighters().
359      *
360      * HACK: setHighlighters before odd/even background.
361      *
362      *
363      */

364     public void interactiveTestJNTableAlternatingRowColorsWithHighlighters() {
365         JNTable table = new JNTable(rawdata);
366         Highlighter[] highlighters = new Highlighter[] {
367           // new AlternateRowHighlighter(Color.white, new Color(0xF0, 0xF0, 0xE0), null),
368
new PatternHighlighter(null, Color.red, "41.*", 0, 0)
369             };
370
371         HighlighterPipeline highlighterPipeline = new HighlighterPipeline(highlighters);
372
373         //table.setHighlighters(highlighterPipeline);
374
table.setOddRowBackground(new Color JavaDoc(0xCC, 0xCC, 0xFF));
375         table.setEvenRowBackground(Color.white);
376         table.setShowHorizontalLines(false);
377         // setting the highlighter here will reset
378
table.setHighlighters(highlighterPipeline);
379         JFrame JavaDoc frame = wrapInFrame(table, "JNTable Alternating Row Colors with Highlighters");
380         frame.setVisible(true);
381     }
382     
383     /**
384      * Issues #141, #142 :
385      * Rowheader appearence and function not properly synched with main.
386      *
387      *
388      */

389     public void interactiveTestJNTableRowHeaderLocked() {
390         JNTable table = new JNTable(rawdata);
391         table.setRowHeaderLocked(true);
392         JFrame JavaDoc frame = wrapInFrame(table, "JNTable RowHeader Locked");
393         frame.setVisible(true);
394     }
395
396     public void interactiveTestJNTableColumnControl() {
397         JNTable table = new JNTable(rawdata);
398         table.setHasColumnControl(true);
399         JFrame JavaDoc frame = wrapInFrame(table, "JNTable ColumnControl");
400         frame.setVisible(true);
401     }
402
403     /**
404      * Issue #143: RowHeader column appears in columnControlButton.
405      *
406      * HACK: set lock before columnControl.
407      *
408      */

409     public void interactiveTestJNTableColumnControlAndRowHeader() {
410         JNTable table = new JNTable(rawdata);
411         table.setHasColumnControl(true);
412         table.setRowHeaderLocked(true);
413         JFrame JavaDoc frame = wrapInFrame(table, "JNTable ColumnControl and RowHeader");
414         frame.setVisible(true);
415     }
416
417     public void interactiveTestJNTableColumnProperties() {
418         JNTable table = new JNTable(richdata);
419         table.setOddRowBackground(Color.cyan);
420
421         table.setHeaderBackground(Color.blue);
422         table.setHeaderForeground(Color.white);
423         table.setHeaderFont(new Font JavaDoc("sanserif", Font.BOLD, 18));
424         table.setHeaderSortDownIcon(new ImageIcon JavaDoc(
425             JNTableUnitTest.class.getResource("resources/down.png")));
426         table.setHeaderSortUpIcon(new ImageIcon JavaDoc(
427             JNTableUnitTest.class.getResource("resources/up.png")));
428
429         table.setColumnHorizontalAlignment("bugid", JLabel.CENTER);
430         table.setColumnPrototypeValue("bugid", "888888");
431         table.setColumnBackground("dispatchdate", Color.green);
432         table.setColumnBackground("priority", Color.red);
433         table.setColumnForeground("priority", Color.blue);
434         table.setColumnPrototypeValue("priority", new Integer JavaDoc(5));
435         table.setColumnFont("priority", new Font JavaDoc("sanserif", Font.ITALIC, 20));
436         table.setColumnBackground("severity", Color.black);
437         table.setColumnForeground("severity", Color.white);
438         table.setColumnPrototypeValue("synopsis", "JTable Horizontal Scrollbar doesn't work");
439
440         JFrame JavaDoc frame = wrapInFrame(table, "JNTable Column Properties");
441         frame.setVisible(true);
442     }
443
444     public void interactiveTestJNTableFiltering() {
445         final JNTable table = new JNTable(richdata);
446         table.setHasColumnControl(true);
447
448         Filter[] filters = new Filter[] {
449             new PatternFilter(".*", 0, 0)};
450
451         FilterPipeline fp = new FilterPipeline(filters) {
452             public void flush() {
453                 super.flush();
454                 // Would be better if these were invisible rather than removed.
455
// JW: invalid usage? Filters are about rows, not columns?
456
// at the very least, must not try to remove
457
// more than once!
458
// table.removeColumn(table.getColumn("jdcvotes"));
459
// table.removeColumn(table.getColumn("dispatchdate"));
460
// table.removeColumn(table.getColumn("state"));
461
}
462         };
463
464         table.setFilters(fp);
465         table.setOddRowBackground(new Color JavaDoc(255, 0, 0));
466
467         JFrame JavaDoc frame = wrapInFrame(table, "JNTable Filtering");
468         frame.setVisible(true);
469
470     }
471
472     public static void main(String JavaDoc args[]) {
473 // LFSwitcher.windowsLF();
474
JNTableUnitTest test = new JNTableUnitTest();
475         try {
476             //test.runInteractiveTests();
477
test.runInteractiveTests("interactive.*Toggle.*");
478       // test.runInteractiveTests("interactive.*Column.*");
479
// test.runInteractiveTests("interactive.*Action.*");
480
} catch (Exception JavaDoc e) {
481             System.err.println("exception when executing interactive tests:");
482             e.printStackTrace();
483         }
484     }
485 }
486
487
Popular Tags