KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: JXTableUnitTest.java,v 1.17 2005/02/28 11:52:26 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.awt.Color JavaDoc;
11 import java.awt.Component JavaDoc;
12 import java.awt.Dimension JavaDoc;
13 import java.awt.Font JavaDoc;
14 import java.awt.Graphics JavaDoc;
15 import java.awt.event.ActionEvent JavaDoc;
16 import java.net.MalformedURLException JavaDoc;
17 import java.net.URL JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.regex.Pattern JavaDoc;
22
23 import javax.swing.AbstractAction JavaDoc;
24 import javax.swing.AbstractButton JavaDoc;
25 import javax.swing.Action JavaDoc;
26 import javax.swing.Icon JavaDoc;
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.JFrame JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import javax.swing.JTable JavaDoc;
31 import javax.swing.KeyStroke JavaDoc;
32 import javax.swing.SwingUtilities JavaDoc;
33 import javax.swing.table.AbstractTableModel JavaDoc;
34 import javax.swing.table.DefaultTableModel JavaDoc;
35 import javax.swing.table.TableCellRenderer JavaDoc;
36 import javax.swing.table.TableColumn JavaDoc;
37 import javax.swing.table.TableModel JavaDoc;
38
39 import org.jdesktop.swing.data.DataLoader;
40 import org.jdesktop.swing.data.DefaultTableModelExt;
41 import org.jdesktop.swing.data.Link;
42 import org.jdesktop.swing.data.TableModelExtTextLoader;
43 import org.jdesktop.swing.decorator.AlternateRowHighlighter;
44 import org.jdesktop.swing.decorator.ComponentAdapter;
45 import org.jdesktop.swing.decorator.Filter;
46 import org.jdesktop.swing.decorator.FilterPipeline;
47 import org.jdesktop.swing.decorator.Highlighter;
48 import org.jdesktop.swing.decorator.HighlighterPipeline;
49 import org.jdesktop.swing.decorator.PatternFilter;
50 import org.jdesktop.swing.decorator.PatternHighlighter;
51 import org.jdesktop.swing.decorator.PipelineListener;
52 import org.jdesktop.swing.decorator.ShuttleSorter;
53 import org.jdesktop.swing.table.ColumnHeaderRenderer;
54 import org.jdesktop.swing.table.TableColumnExt;
55 import org.jdesktop.swing.util.AncientSwingTeam;
56
57 import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
58
59 public class JXTableUnitTest extends InteractiveTestCase {
60
61     private DynamicTableModel tableModel = null;
62     private TableModel JavaDoc sortableTableModel;
63     
64     public JXTableUnitTest() {
65         super("JXTable unit test");
66     }
67
68     protected void setUp() throws Exception JavaDoc {
69        super.setUp();
70         // set loader priority to normal
71
System.setProperty(DataLoader.READER_PRIORITY_KEY,
72                 String.valueOf(Thread.NORM_PRIORITY));
73         if (tableModel == null) {
74             tableModel = new DynamicTableModel();
75         }
76         sortableTableModel = new AncientSwingTeam();
77     }
78     
79     protected void tearDown() throws Exception JavaDoc {
80         // reset loader priority
81
System.getProperties().remove(DataLoader.READER_PRIORITY_KEY);
82         super.tearDown();
83     }
84
85     
86     /**
87      * Issue #175: multiple registration as PipelineListener.
88      *
89      *
90      */

91     public void testRegisterUniquePipelineListener() {
92         JXTable table = new JXTable();
93         PatternFilter noFilter = new PatternFilter(".*", 0, 1);
94         table.setFilters(new FilterPipeline(new Filter[] {noFilter}));
95         int listenerCount = table.getFilters().getPipelineListeners().length;
96         PipelineListener l = table.getFilters().getPipelineListeners()[listenerCount - 1];
97         // sanity assert
98
assertEquals(1, listenerCount);
99         assertEquals(table, l);
100         table.setModel(createModel(0, 20));
101         assertEquals("pipeline listener count must not change after setModel", listenerCount, table.getFilters().getPipelineListeners().length);
102         
103     }
104     /**
105      * Issue #174: componentAdapter.hasFocus() looks for anchor instead of lead.
106      *
107      */

108     public void testLeadFocusCell() {
109         final JXTable table = new JXTable();
110         table.setModel(createModel(0, 10));
111         // sort first column
112
// table.setSorter(0);
113
// select last rows
114
table.addRowSelectionInterval(table.getRowCount() - 2, table.getRowCount() - 1);
115         final int leadRow = table.getSelectionModel().getLeadSelectionIndex();
116         int anchorRow = table.getSelectionModel().getAnchorSelectionIndex();
117         table.addColumnSelectionInterval(0, 0);
118         final int leadColumn = table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
119         int anchorColumn = table.getColumnModel().getSelectionModel().getAnchorSelectionIndex();
120         assertEquals("lead must be last row", table.getRowCount() - 1, leadRow);
121         assertEquals("anchor must be second last row", table.getRowCount() - 2, anchorRow);
122         assertEquals("lead must be first column", 0, leadColumn);
123         assertEquals("anchor must be first column", 0, anchorColumn);
124         final JFrame JavaDoc frame = new JFrame JavaDoc();
125         frame.add(table);
126         frame.pack();
127         frame.setVisible(true);
128         table.requestFocus();
129         SwingUtilities.invokeLater(new Runnable JavaDoc() {
130             public void run() {
131                 ComponentAdapter adapter = table.getComponentAdapter();
132                 adapter.row = leadRow;
133                 adapter.column = leadColumn;
134                 // difficult to test - hasFocus() implies that the table isFocusOwner()
135
assertTrue("adapter must have focus for leadRow/Column: " + adapter.row + "/" + adapter.column,
136                         adapter.hasFocus());
137                 frame.dispose();
138
139             }
140         });
141     }
142     
143
144     /**
145      * Issue #173:
146      * ArrayIndexOOB if replacing model with one containing
147      * fewer rows and the "excess" is selected.
148      *
149      */

150     public void testSelectionAndToggleModel() {
151         JXTable table = new JXTable();
152         table.setModel(createModel(0, 10));
153         // sort first column
154
table.setSorter(0);
155         // select last rows
156
table.addRowSelectionInterval(table.getRowCount() - 2, table.getRowCount() - 1);
157         // invert sort
158
table.setSorter(0);
159         // set model with less rows
160
table.setModel(createModel(0, 8));
161         
162     }
163     
164     /**
165      * testing selection and adding rows.
166      *
167      *
168      */

169     public void testSelectionAndAddRows() {
170         JXTable table = new JXTable();
171         DefaultTableModel JavaDoc model = createModel(0, 10);
172         table.setModel(model);
173         // sort first column
174
table.setSorter(0);
175         // select last rows
176
table.addRowSelectionInterval(table.getRowCount() - 2, table.getRowCount() - 1);
177         // invert sort
178
table.setSorter(0);
179         
180         Integer JavaDoc highestValue = new Integer JavaDoc(100);
181         model.addRow(new Object JavaDoc[] { highestValue });
182         assertEquals(highestValue, table.getValueAt(0, 0));
183     }
184     
185     /**
186      * Issue #16: removing row throws ArrayIndexOOB if
187      * last row was selected
188      *
189      */

190     public void testSelectionAndRemoveRows() {
191         JXTable table = new JXTable();
192         DefaultTableModel JavaDoc model = createModel(0, 10);
193         table.setModel(model);
194         // sort first column
195
table.setSorter(0);
196         // select last rows
197
table.addRowSelectionInterval(table.getRowCount() - 2, table.getRowCount() - 1);
198         // invert sort
199
table.setSorter(0);
200         model.removeRow(0);
201     }
202     
203     private DefaultTableModel JavaDoc createModel(int startRow, int count) {
204         DefaultTableModel JavaDoc model = new DefaultTableModel JavaDoc(count, 5);
205         for (int i = 0; i < model.getRowCount(); i++) {
206             model.setValueAt(new Integer JavaDoc(startRow++), i, 0);
207         }
208         return model;
209     }
210     
211     
212     /**
213      * Issue #171: row-coordinate not transformed in isCellEditable (sorting)
214      *
215      */

216     public void testSortedEditability() {
217         int rows = 2;
218         RowObjectTableModel model = createRowObjectTableModel(rows);
219         JXTable table = new JXTable(model);
220         RowObject firstInModel = model.getRowObject(0);
221         assertEquals("rowObject data must be equal", firstInModel.getData1(), table.getValueAt(0, 0));
222         assertEquals("rowObject editability must be equal", firstInModel.isEditable(), table.isCellEditable(0, 0));
223         // nothing changed
224
table.setSorter(0);
225         Object JavaDoc firstDataValueInTable = table.getValueAt(0,0);
226         boolean firstEditableValueInTable = table.isCellEditable(0, 0);
227         assertEquals("rowObject data must be equal", firstInModel.getData1(), table.getValueAt(0, 0));
228         assertEquals("rowObject editability must be equal", firstInModel.isEditable(), table.isCellEditable(0, 0));
229         // sanity assert: first and last have different values/editability
230
assertTrue("lastValue different from first", firstDataValueInTable !=
231                 table.getValueAt(rows - 1, 0));
232         assertTrue("lastEditability different from first", firstEditableValueInTable !=
233             table.isCellEditable(rows - 1, 0));
234         // reverse order
235
table.setSorter(0);
236         assertEquals("last row data must be equal to former first", firstDataValueInTable,
237                 table.getValueAt(rows - 1, 0));
238         assertEquals("last row editability must be equal to former first", firstEditableValueInTable,
239                 table.isCellEditable(rows - 1, 0));
240     }
241
242     /**
243      * Issue #171: row-coordinate not transformed in isCellEditable (filtering)
244      *
245      */

246     public void testFilteredEditability() {
247         int rows = 2;
248         RowObjectTableModel model = createRowObjectTableModel(rows);
249         JXTable table = new JXTable(model);
250         // sanity assert
251
for (int i = 0; i < table.getRowCount(); i++) {
252             assertEquals("even/uneven rows must be editable/notEditable " + i,
253                     i % 2 == 0, table.isCellEditable(i, 0));
254         }
255         // need to chain two filters (to reach the "else" block in
256
// filter.isCellEditable()
257
PatternFilter filter = new PatternFilter("NOT.*", 0, 1);
258         PatternFilter noFilter = new PatternFilter(".*", 0, 1);
259
260         table.setFilters(new FilterPipeline(new Filter[] {noFilter, filter}));
261         assertEquals("row count is half", rows / 2, table.getRowCount());
262         for (int i = 0; i < table.getRowCount(); i++) {
263             assertFalse("all rows must be not-editable " + i, table.isCellEditable(i, 0));
264             
265         }
266     }
267
268 //----------------------- test data for exposing #171 (Tim Dilks)
269

270     /**
271      * create test model - all cells in even rows are editable,
272      * in odd rows are not editable.
273      * @param rows the number of rows to create
274      * @return
275      */

276     private RowObjectTableModel createRowObjectTableModel(int rows) {
277         List JavaDoc rowObjects = new ArrayList JavaDoc();
278         for (int i = 0; i < rows; i++) {
279             rowObjects.add(new RowObject("somedata" + i, i % 2 == 0));
280         }
281         return new RowObjectTableModel(rowObjects);
282     }
283
284     /**
285      * test object to map in test table model.
286      */

287     static class RowObject {
288
289         private String JavaDoc data1;
290         private boolean editable;
291
292         public RowObject(String JavaDoc data1, boolean editable) {
293             this.data1 = data1;
294             this.editable = editable;
295         }
296
297         public String JavaDoc getData1() {
298             return data1;
299         }
300       
301         public boolean isEditable() {
302             return editable;
303         }
304
305     }
306
307     /**
308      * test TableModel wrapping RowObject.
309      */

310     static class RowObjectTableModel extends AbstractTableModel JavaDoc {
311
312         List JavaDoc data;
313
314         public RowObjectTableModel(List JavaDoc data) {
315             this.data = data;
316         }
317
318         public RowObject getRowObject(int row) {
319             return (RowObject) data.get(row);
320         }
321         public int getColumnCount() {
322             return 2;
323         }
324
325         public int getRowCount() {
326             return data.size();
327         }
328
329         public Object JavaDoc getValueAt(int row, int col) {
330             RowObject object = getRowObject(row);
331             switch (col) {
332                 case 0 :
333                     return object.getData1();
334                 case 1 :
335                     return object.isEditable() ? "EDITABLE" : "NOT EDITABLE";
336                 default :
337                     return null;
338             }
339         }
340
341         public boolean isCellEditable(int row, int col) {
342             return getRowObject(row).isEditable();
343         }
344     }
345
346     
347     public void testLinkCellType() {
348         JXTable table = new JXTable(tableModel);
349
350         assertEquals(Link.class,
351                      table.getColumnClass(DynamicTableModel.IDX_COL_LINK));
352
353         TableCellRenderer JavaDoc renderer = table.getCellRenderer(0,
354             DynamicTableModel.IDX_COL_LINK);
355
356         // XXX note: this should be a LinkCellRenderer LinkRenderer is not public.
357
// assertEquals(renderer.getClass(),
358
// org.jdesktop.swing.table.TableCellRenderers$LinkRenderer.class);
359
}
360
361     /**
362      * Issue #167: IllegalStateException if re-setting filter while
363      * sorting.
364      *
365      */

366     public void testToggleFiltersWhileSorting() {
367         Object JavaDoc[][] rowData = new Object JavaDoc[][] {
368                 new Object JavaDoc[] { Boolean.TRUE, "AA" },
369                 new Object JavaDoc[] { Boolean.FALSE, "AB" },
370                 new Object JavaDoc[] { Boolean.FALSE, "AC" },
371                 new Object JavaDoc[] { Boolean.TRUE, "BA" },
372                 new Object JavaDoc[] { Boolean.FALSE, "BB" },
373                 new Object JavaDoc[] { Boolean.TRUE, "BC" } };
374         String JavaDoc[] columnNames = new String JavaDoc[] { "Critical", "Task" };
375         final JXTable table = new JXTable(rowData, columnNames);
376         Filter filterA = new PatternFilter("A.*", Pattern.CASE_INSENSITIVE, 1);
377         // simulates the sequence of user interaction as described in
378
// the original bug report in
379
// http://www.javadesktop.org/forums/thread.jspa?messageID=56285
380
table.setFilters(new FilterPipeline(new Filter[] {filterA}));
381         table.setSorter(1);
382         Filter filterB = new PatternFilter(".*", Pattern.CASE_INSENSITIVE, 1);
383         table.setFilters(new FilterPipeline(new Filter[] {filterB}));
384         table.setSorter(1);
385     }
386
387     /**
388      * Issue #167: IllegalStateException if re-setting filter while
389      * sorting.
390      * Another variant ...
391      *
392      */

393     public void testToggleFiltersWhileSortingLonger() {
394         Object JavaDoc[][] rowData = new Object JavaDoc[][] {
395                 new Object JavaDoc[] { Boolean.TRUE, "AA" },
396                 new Object JavaDoc[] { Boolean.FALSE, "AB" },
397                 new Object JavaDoc[] { Boolean.FALSE, "AC" },
398                 new Object JavaDoc[] { Boolean.TRUE, "BA" },
399                 new Object JavaDoc[] { Boolean.FALSE, "BB" },
400                 new Object JavaDoc[] { Boolean.TRUE, "BC" } };
401         String JavaDoc[] columnNames = new String JavaDoc[] { "Critical", "Task" };
402         final JXTable table = new JXTable(rowData, columnNames);
403         // simulates the sequence of user interaction as described in
404
// the follow-up bug report in
405
// http://www.javadesktop.org/forums/thread.jspa?messageID=56285
406
table.setFilters(createFilterPipeline(false, 1));
407         table.setSorter(0);
408         table.setSorter(1);
409         table.setFilters(createFilterPipeline(true, 1));
410         table.setFilters(createFilterPipeline(false, 1));
411         table.setSorter(0);
412     }
413     
414     private FilterPipeline createFilterPipeline(boolean matchAll, int col) {
415         Filter filter;
416         if (matchAll) {
417             filter = new PatternFilter(".*", Pattern.CASE_INSENSITIVE, col);
418         } else {
419            filter = new PatternFilter("A.*", Pattern.CASE_INSENSITIVE, col);
420         }
421         return new FilterPipeline(new Filter[] {filter});
422         
423     }
424     /**
425      * Issue #125: setting filter to null doesn't clean up.
426      *
427      * A visual consequence is that the hidden (by the old
428      * filters) rows don't appear. A not-so visual consequence
429      * is that the sorter is out of synch and accessing a row in
430      * the region outside of the formerly filtered.
431      *
432      */

433     public void testRemoveFilterWhileSorting() {
434         Object JavaDoc[][] rowData = new Object JavaDoc[][] {
435                 new Object JavaDoc[] { Boolean.TRUE, "AA" },
436                 new Object JavaDoc[] { Boolean.FALSE, "AB" },
437                 new Object JavaDoc[] { Boolean.FALSE, "AC" },
438                 new Object JavaDoc[] { Boolean.TRUE, "BA" },
439                 new Object JavaDoc[] { Boolean.FALSE, "BB" },
440                 new Object JavaDoc[] { Boolean.TRUE, "BC" } };
441         String JavaDoc[] columnNames = new String JavaDoc[] { "Critical", "Task" };
442         final JXTable table = new JXTable(rowData, columnNames);
443         int rows = table.getRowCount();
444         Filter filterA = new PatternFilter("A.*", Pattern.CASE_INSENSITIVE, 1);
445         table.setFilters(new FilterPipeline(new Filter[] {filterA}));
446         table.setSorter(1);
447         table.setFilters(null);
448         assertEquals("rowCount must be original", rows, table.getRowCount());
449         table.getValueAt(rows - 1, 0);
450
451     
452     }
453
454     /**
455      * Issue #134: JXTable - default renderers not loaded.
456      * To fix the issue the JXTable internal renderers' access scope
457      * was changed to public. Note: if the JTable internal renderers
458      * access scope were to be widened then this test has to be changed
459      * (the comparing class are hardcoded).
460      *
461      */

462     public void testLazyRenderersByClass() {
463         JXTable table = new JXTable();
464         assertEquals("default Boolean renderer", JXTable.BooleanRenderer.class, table.getDefaultRenderer(Boolean JavaDoc.class).getClass());
465         assertEquals("default Number renderer", JXTable.NumberRenderer.class, table.getDefaultRenderer(Number JavaDoc.class).getClass());
466         assertEquals("default Double renderer", JXTable.DoubleRenderer.class, table.getDefaultRenderer(Double JavaDoc.class).getClass());
467         assertEquals("default Date renderer", JXTable.DateRenderer.class, table.getDefaultRenderer(Date JavaDoc.class).getClass());
468         assertEquals("default Link renderer", JXTable.LinkRenderer.class, table.getDefaultRenderer(Link.class).getClass());
469         assertEquals("default Icon renderer", JXTable.IconRenderer.class, table.getDefaultRenderer(Icon JavaDoc.class).getClass());
470     }
471
472     /**
473      * Issue #150: setting filters must not re-create columns.
474      *
475      */

476     public void testTableColumnsWithFilters() {
477         JXTable table = new JXTable(tableModel);
478         assertEquals("table columns are equal to columns of model",
479                 tableModel.getColumnCount(), table.getColumnCount());
480         TableColumn JavaDoc column = table.getColumnExt(0);
481         table.removeColumn(column);
482         int columnCountAfterRemove = table.getColumnCount();
483         assertEquals("table columns must be one less than columns of model",
484                 tableModel.getColumnCount() - 1, columnCountAfterRemove);
485         table.setFilters(new FilterPipeline(new Filter[] {
486                 new ShuttleSorter(1, false), // column 1, descending
487
}));
488         assertEquals("table columns must be unchanged after setting filter",
489                 columnCountAfterRemove, table.getColumnCount());
490         
491     }
492     
493     public void testDynamicColumns() {
494         URL JavaDoc url = JXTableUnitTest.class.getResource("resources/bugdata.txt");
495         DefaultTableModelExt data = new DefaultTableModelExt(url);
496         System.out.println("loader prio" + System.getProperty(DataLoader.READER_PRIORITY_KEY));
497         JXTable table = new JXTable(data);
498         try {
499             data.startLoading();
500         }
501         catch (Exception JavaDoc ex) {
502             fail("Exception in data load: " + ex.getMessage());
503         }
504
505         // JW: this hangs occasionally
506
while(data.isLoading());
507
508         assertEquals(8, data.getColumnCount());
509         assertEquals(8, table.getColumnCount());
510     }
511
512     public void interactiveTestTableLoading() {
513         URL JavaDoc url = JXTableUnitTest.class.getResource("resources/stringdata.csv");
514         System.out.println("url="+url);
515         DefaultTableModelExt data = new DefaultTableModelExt(url);
516         TableModelExtTextLoader loader = (TableModelExtTextLoader)data.getLoader();
517         loader.setFirstRowHeader(true);
518         loader.setColumnDelimiter(",");
519         JXTable table = new JXTable(data);
520         try {
521             data.startLoading();
522         }
523         catch (Exception JavaDoc ex) {
524             fail("Exception in data load: " + ex.getMessage());
525         }
526         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TableLoading Test");
527         frame.setVisible(true); // RG: Changed from deprecated method show();
528
}
529
530     public void interactiveTestLinkCell() {
531         JXTable table = new JXTable(tableModel);
532         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TableLinkCell Test");
533         frame.setVisible(true); // RG: Changed from deprecated method show();
534
}
535
536     public void interactiveTestToggleSortingEnabled() {
537 // final JXTable table = new JXTable(tableModel);
538
URL JavaDoc url = JXTableUnitTest.class.getResource("resources/stringdata.csv");
539         System.out.println("url="+url);
540         DefaultTableModelExt data = new DefaultTableModelExt(url);
541         TableModelExtTextLoader loader = (TableModelExtTextLoader)data.getLoader();
542         loader.setFirstRowHeader(true);
543         loader.setColumnDelimiter(",");
544         final JXTable table = new JXTable(data);
545         try {
546             data.startLoading();
547         }
548         catch (Exception JavaDoc ex) {
549             fail("Exception in data load: " + ex.getMessage());
550         }
551         Action JavaDoc toggleSortableAction = new AbstractAction JavaDoc("Toggle Sortable") {
552
553             public void actionPerformed(ActionEvent JavaDoc e) {
554                 table.setSortable(!table.isSortable());
555                 
556             }
557             
558         };
559         table.getActionMap().put("toggleSortable", toggleSortableAction);
560         table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
561                 KeyStroke.getKeyStroke("F6"), "toggleSortable");
562         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "ToggleSortingEnabled Test (press F6 to toggle)");
563         frame.setVisible(true); // RG: Changed from deprecated method show();
564

565     }
566     public void interactiveTestTableSizing1() {
567         JXTable table = new JXTable();
568         table.setAutoCreateColumnsFromModel(false);
569         table.setModel(tableModel);
570
571         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
572
573         TableColumnExt columns[] = new TableColumnExt[tableModel.getColumnCount()];
574         for (int i = 0; i < columns.length; i++) {
575             columns[i] = new TableColumnExt(i);
576             table.addColumn(columns[i]);
577         }
578         columns[0].setPrototypeValue(new Integer JavaDoc(0));
579         columns[1].setPrototypeValue("Simple String Value");
580         columns[2].setPrototypeValue(new Integer JavaDoc(1000));
581         columns[3].setPrototypeValue(Boolean.TRUE);
582         columns[4].setPrototypeValue(new Date JavaDoc(100));
583         columns[5].setPrototypeValue(new Float JavaDoc(1.5));
584         columns[6].setPrototypeValue(new Link("Sun Micro", "_blank",
585                                               tableModel.linkURL));
586         columns[7].setPrototypeValue(new Integer JavaDoc(3023));
587         columns[8].setPrototypeValue("John Doh");
588         columns[9].setPrototypeValue("23434 Testcase St");
589         columns[10].setPrototypeValue(new Integer JavaDoc(33333));
590         columns[11].setPrototypeValue(Boolean.FALSE);
591
592         table.setVisibleRowCount(12);
593
594         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TableSizing1 Test");
595         frame.setVisible(true);
596     }
597
598     public void interactiveTestTableSizing2() {
599         JXTable table = new JXTable();
600         table.setAutoCreateColumnsFromModel(false);
601         table.setModel(tableModel);
602
603         TableColumnExt columns[] = new TableColumnExt[6];
604         int viewIndex = 0;
605         for (int i = columns.length - 1; i >= 0; i--) {
606             columns[viewIndex] = new TableColumnExt(i);
607             table.addColumn(columns[viewIndex++]);
608         }
609         columns[5].setHeaderValue("String Value");
610         columns[5].setPrototypeValue("9999");
611         columns[4].setHeaderValue("String Value");
612         columns[4].setPrototypeValue("Simple String Value");
613         columns[3].setHeaderValue("Int Value");
614         columns[3].setPrototypeValue(new Integer JavaDoc(1000));
615         columns[2].setHeaderValue("Bool");
616         columns[2].setPrototypeValue(Boolean.FALSE);
617         //columns[2].setSortable(false);
618
columns[1].setHeaderValue("Date");
619         columns[1].setPrototypeValue(new Date JavaDoc(0));
620         //columns[1].setSortable(false);
621
columns[0].setHeaderValue("Float");
622         columns[0].setPrototypeValue(new Float JavaDoc(5.5));
623
624         table.setRowHeight(24);
625         table.setRowMargin(2);
626         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TableSizing2 Test");
627         frame.setVisible(true);
628     }
629
630     public void interactiveTestTableAlternateHighlighter1() {
631         JXTable table = new JXTable(tableModel);
632         table.setRowHeight(22);
633         table.setRowMargin(1);
634
635         table.setFilters(new FilterPipeline(new Filter[] {
636                                             new ShuttleSorter(0, true) // column 0, ascending
637
}));
638
639         table.setHighlighters(new HighlighterPipeline(new Highlighter[] {
640             AlternateRowHighlighter.
641             linePrinter,
642         }));
643
644         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TableAlternateRowHighlighter1 Test");
645         frame.setVisible(true);
646     }
647
648     public void interactiveTestTableAlternateRowHighlighter2() {
649         JXTable table = new JXTable(tableModel);
650         table.setRowHeight(22);
651         table.setRowMargin(1);
652         table.setFilters(new FilterPipeline(new Filter[] {
653                                             new ShuttleSorter(1, false), // column 1, descending
654
}));
655
656         table.setHighlighters(new HighlighterPipeline(new Highlighter[] {
657             AlternateRowHighlighter.classicLinePrinter,
658         }));
659
660         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TableAlternateRowHighlighter2 Test");
661         frame.setVisible(true);
662     }
663
664     public void interactiveTestTableSorter1() {
665         JXTable table = new JXTable(sortableTableModel);
666         table.setHighlighters(new HighlighterPipeline(new Highlighter[] {
667             AlternateRowHighlighter.notePadBackground,
668         }));
669         table.setBackground(new Color JavaDoc(0xFF, 0xFF, 0xCC)); // notepad
670
table.setGridColor(Color.cyan.darker());
671         table.setRowHeight(22);
672         table.setRowMargin(1);
673         table.setShowHorizontalLines(true);
674         table.setFilters(new FilterPipeline(new Filter[] {
675                                             new ShuttleSorter(0, true), // column 0, ascending
676
new ShuttleSorter(1, true), // column 1, ascending
677
}));
678
679         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TableSorter1 col 0= asc, col 1 = asc");
680         frame.setVisible(true);
681
682     }
683
684     public void interactiveTestTableSorter2() {
685         JXTable table = new JXTable(sortableTableModel);
686         table.setBackground(new Color JavaDoc(0xF5, 0xFF, 0xF5)); // ledger
687
table.setGridColor(Color.cyan.darker());
688         table.setRowHeight(22);
689         table.setRowMargin(1);
690         table.setShowHorizontalLines(true);
691         table.setFilters(new FilterPipeline(new Filter[] {
692                                             new ShuttleSorter(0, true), // column 0, ascending
693
new ShuttleSorter(1, false), // column 1, descending
694
}));
695         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TableSorter2 col 0 = asc, col 1 = desc");
696         frame.setVisible(true);
697     }
698
699     public void interactiveTestTableSorter3() {
700         JXTable table = new JXTable(sortableTableModel);
701         table.setHighlighters(new HighlighterPipeline(new Highlighter[] {
702             new Highlighter(Color.orange, null),
703         }));
704         table.setFilters(new FilterPipeline(new Filter[] {
705                                             new ShuttleSorter(1, true), // column 1, ascending
706
new ShuttleSorter(0, false), // column 0, descending
707
}));
708         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TableSorter3 col 1 = asc, col 0 = desc");
709         frame.setVisible(true);
710     }
711
712     public void interactiveTestTableSorter4() {
713         JXTable table = new JXTable(sortableTableModel);
714         table.setHighlighters(new HighlighterPipeline(new Highlighter[] {
715             new Highlighter(Color.orange, null),
716         }));
717         table.setFilters(new FilterPipeline(new Filter[] {
718                 new ShuttleSorter(0, false), // column 0, descending
719
new ShuttleSorter(1, true), // column 1, ascending
720
}));
721         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TableSorter4 col 0 = des, col 1 = asc");
722         frame.setVisible(true);
723     }
724     public void interactiveTestTablePatternFilter1() {
725         JXTable table = new JXTable(tableModel);
726         table.setIntercellSpacing(new Dimension JavaDoc(1, 1));
727         table.setShowGrid(true);
728         table.setFilters(new FilterPipeline(new Filter[] {
729                                             new PatternFilter("A.*", 0, 1)
730         }));
731         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TablePatternFilter1 Test");
732         frame.setVisible(true);
733     }
734
735     public void interactiveTestTablePatternFilter2() {
736         JXTable table = new JXTable(tableModel);
737         table.setIntercellSpacing(new Dimension JavaDoc(2, 2));
738         table.setShowGrid(true);
739         table.setFilters(new FilterPipeline(new Filter[] {
740                                             new PatternFilter("S.*", 0, 1),
741                                             new ShuttleSorter(0, false), // column 0, descending
742
}));
743         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TablePatternFilter2 Test");
744         frame.setVisible(true);
745     }
746
747     public void interactiveTestTablePatternFilter3() {
748         JXTable table = new JXTable(tableModel);
749         table.setShowGrid(true);
750         table.setFilters(new FilterPipeline(new Filter[] {
751                                             new PatternFilter("S.*", 0, 1),
752                                             new ShuttleSorter(1, false), // column 1, descending
753
new ShuttleSorter(0, false), // column 0, descending
754
}));
755         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TablePatternFilter3 Test");
756         frame.setVisible(true);
757     }
758
759     public void interactiveTestTablePatternFilter4() {
760         JXTable table = new JXTable(tableModel);
761         table.setIntercellSpacing(new Dimension JavaDoc(3, 3));
762         table.setShowGrid(true);
763         table.setFilters(new FilterPipeline(new Filter[] {
764                                             new PatternFilter("A.*", 0, 1),
765                                             new ShuttleSorter(0, false), // column 0, descending
766
}));
767         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TablePatternFilter4 Test");
768         frame.setVisible(true);
769     }
770
771     public void interactiveTestTablePatternFilter5() {
772         // **** IMPORTANT TEST CASE for interaction between ****
773
// **** PatternFilter and PatternHighlighter!!! ****
774
JXTable table = new JXTable(tableModel);
775         table.setFilters(new FilterPipeline(new Filter[] {
776                                             new PatternFilter("S.*", 0, 1),
777                                             new ShuttleSorter(0, false), // column 0, descending
778
new ShuttleSorter(1, true), // column 1, ascending
779
new ShuttleSorter(3, false), // column 3, descending
780
}));
781         table.setHighlighters(new HighlighterPipeline(new Highlighter[] {
782             new PatternHighlighter(null, Color.red, "S.*", 0, 1),
783         }));
784         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TablePatternFilter5 Test");
785         frame.setVisible(true);
786     }
787
788     public void interactiveTestTableViewProperties() {
789         JXTable table = new JXTable(tableModel);
790         table.setIntercellSpacing(new Dimension JavaDoc(15, 15));
791         table.setRowHeight(48);
792         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TableViewProperties Test");
793         frame.setVisible(true);
794     }
795
796     public void interactiveTestTablePatternHighlighter() {
797         JXTable table = new JXTable(tableModel);
798         table.setIntercellSpacing(new Dimension JavaDoc(15, 15));
799         table.setRowHeight(48);
800         table.setRowHeight(0, 96);
801         table.setShowGrid(true);
802         table.setHighlighters(new HighlighterPipeline(new Highlighter[] {
803             new PatternHighlighter(null, Color.red, "A.*", 0, 1),
804         }));
805         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TablePatternHighlighter Test");
806         frame.setVisible(true);
807     }
808
809     public void interactiveTestTableColumnProperties() {
810         JXTable table = new JXTable();
811         table.setModel(tableModel);
812
813         table.getTableHeader().setBackground(Color.green);
814         table.getTableHeader().setForeground(Color.magenta);
815         table.getTableHeader().setFont(new Font JavaDoc("Serif", Font.PLAIN, 10));
816
817         ColumnHeaderRenderer headerRenderer = new ColumnHeaderRenderer();
818         headerRenderer.setHorizontalAlignment(JLabel.CENTER);
819         headerRenderer.setBackground(Color.blue);
820         headerRenderer.setForeground(Color.yellow);
821         headerRenderer.setIcon(new Icon JavaDoc() {
822             public int getIconWidth() {
823                 return 12;
824             }
825
826             public int getIconHeight() {
827                 return 12;
828             }
829
830             public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
831                 g.setColor(Color.red);
832                 g.fillOval(0, 0, 10, 10);
833             }
834         });
835         headerRenderer.setIconTextGap(20);
836         headerRenderer.setFont(new Font JavaDoc("Serif", Font.BOLD, 18));
837
838         for (int i = 0; i < table.getColumnCount(); i++) {
839             TableColumnExt column = table.getColumnExt(i);
840             if (i % 3 > 0) {
841                 column.setHeaderRenderer(headerRenderer);
842             }
843             if (i % 2 > 0) {
844                 TableCellRenderer JavaDoc cellRenderer =
845                     table.getNewDefaultRenderer(table.getColumnClass(i));
846                 if (cellRenderer instanceof JLabel JavaDoc || cellRenderer instanceof AbstractButton JavaDoc) {
847                     JLabel JavaDoc labelCellRenderer = (JLabel JavaDoc)cellRenderer;
848                     labelCellRenderer.setBackground(Color.gray);
849                     labelCellRenderer.setForeground(Color.red);
850                     labelCellRenderer.setHorizontalAlignment(JLabel.CENTER);
851                     column.setCellRenderer(cellRenderer);
852                 }
853             }
854         }
855
856         JFrame JavaDoc frame = wrapWithScrollingInFrame(table, "TableColumnProperties Test");
857         frame.setVisible(true);
858     }
859
860     public static class DynamicTableModel extends AbstractTableModel JavaDoc {
861         private Object JavaDoc columnSamples[];
862         private Object JavaDoc columnSamples2[];
863         public URL JavaDoc linkURL;
864
865         public static final int IDX_COL_LINK = 6;
866
867         public DynamicTableModel() {
868             try {
869                 linkURL = new URL JavaDoc("http://www.sun.com");
870             }
871             catch (MalformedURLException JavaDoc ex) {
872                 throw new RuntimeException JavaDoc(ex);
873             }
874
875             columnSamples = new Object JavaDoc[12];
876             columnSamples[0] = new Integer JavaDoc(0);
877             columnSamples[1] = "Simple String Value";
878             columnSamples[2] = new Integer JavaDoc(1000);
879             columnSamples[3] = Boolean.TRUE;
880             columnSamples[4] = new Date JavaDoc(100);
881             columnSamples[5] = new Float JavaDoc(1.5);
882             columnSamples[IDX_COL_LINK] = new Link("Sun Micro", "_blank", linkURL);
883             columnSamples[7] = new Integer JavaDoc(3023);
884             columnSamples[8] = "John Doh";
885             columnSamples[9] = "23434 Testcase St";
886             columnSamples[10] = new Integer JavaDoc(33333);
887             columnSamples[11] = Boolean.FALSE;
888
889             columnSamples2 = new Object JavaDoc[12];
890             columnSamples2[0] = new Integer JavaDoc(0);
891             columnSamples2[1] = "Another String Value";
892             columnSamples2[2] = new Integer JavaDoc(999);
893             columnSamples2[3] = Boolean.FALSE;
894             columnSamples2[4] = new Date JavaDoc(333);
895             columnSamples2[5] = new Float JavaDoc(22.22);
896             columnSamples2[IDX_COL_LINK] = new Link("Sun Web", "new_frame", linkURL);
897             columnSamples[7] = new Integer JavaDoc(5503);
898             columnSamples[8] = "Jane Smith";
899             columnSamples[9] = "2343 Table Blvd.";
900             columnSamples[10] = new Integer JavaDoc(2);
901             columnSamples[11] = Boolean.TRUE;
902
903         }
904
905         public DynamicTableModel(Object JavaDoc columnSamples[]) {
906             this.columnSamples = columnSamples;
907         }
908
909         public Class JavaDoc getColumnClass(int column) {
910             return columnSamples[column].getClass();
911         }
912
913         public int getRowCount() {
914             return 1000;
915         }
916
917         public int getColumnCount() {
918             return columnSamples.length;
919         }
920
921         public Object JavaDoc getValueAt(int row, int column) {
922             Object JavaDoc value;
923             if (row % 3 == 0) {
924                 value = columnSamples[column];
925             }
926             else {
927                 value = columnSamples2[column];
928             }
929             return column == 0 ? new Integer JavaDoc(row >> 3) :
930                 column == 3 ? new Boolean JavaDoc(row % 2 == 0) : value;
931         }
932
933         public boolean isCellEditable(int row, int column) {
934             return (column == 1);
935         }
936
937         public void setValueAt(Object JavaDoc aValue, int row, int column) {
938             if (column == 1) {
939                 if (row % 3 == 0) {
940                     columnSamples[column] = aValue;
941                 }
942                 else {
943                     columnSamples2[column] = aValue;
944                 }
945             }
946             this.fireTableDataChanged();
947         }
948     }
949
950     public static void main(String JavaDoc args[]) {
951         JXTableUnitTest test = new JXTableUnitTest();
952         try {
953          // test.runInteractiveTests();
954
test.runInteractiveTests("interactive.*Sort.*");
955          // test.runInteractiveTests("interactive.*Toggle.*");
956
} catch (Exception JavaDoc e) {
957             System.err.println("exception when executing interactive tests:");
958             e.printStackTrace();
959         }
960     }
961 }
962
Popular Tags