KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > table > TableColumnModelTest


1 /*
2  * $Id: TableColumnModelTest.java,v 1.1 2005/02/01 16:49:46 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.table;
9
10 import javax.swing.event.ChangeEvent JavaDoc;
11 import javax.swing.event.ListSelectionEvent JavaDoc;
12 import javax.swing.event.TableColumnModelEvent JavaDoc;
13 import javax.swing.event.TableColumnModelListener JavaDoc;
14 import javax.swing.table.TableColumn JavaDoc;
15 import javax.swing.table.TableColumnModel JavaDoc;
16
17 import org.jdesktop.swing.table.DefaultTableColumnModelExt;
18 import org.jdesktop.swing.table.TableColumnExt;
19 import org.jdesktop.swing.util.ColumnModelReport;
20
21 import junit.framework.TestCase;
22
23 /**
24  * Skeleton to unit test DefaultTableColumnExt.
25  *
26  * Incomplete list of issues to test:
27  * fired added after setVisible(true)
28  * behaviour when adding/removing invisible columns
29  * selection state
30  *
31  *
32  * @author Jeanette Winzenburg
33  */

34 public class TableColumnModelTest extends TestCase {
35
36     private static final int COLUMN_COUNT = 3;
37     
38     /**
39      * column count must be changed on changing
40      * column visibility.
41      *
42      */

43     public void testColumnCountOnSetInvisible() {
44         TableColumnModel JavaDoc model = createColumnModel(COLUMN_COUNT);
45         int columnCount = model.getColumnCount();
46         TableColumnExt column = (TableColumnExt) model.getColumn(columnCount - 1);
47         assertTrue(column.isVisible());
48         column.setVisible(false);
49         assertEquals("columnCount must be decremented", columnCount - 1, model.getColumnCount());
50     }
51     
52     /**
53      * Issue #156: must update internal state after setting invisible.
54      * Here: the cached totalWidth. Expect similar inconsistency
55      * with selection.
56      *
57      */

58     public void testTotalColumnWidth() {
59         TableColumnModel JavaDoc model = createColumnModel(COLUMN_COUNT);
60         int totalWidth = model.getTotalColumnWidth();
61         TableColumnExt column = (TableColumnExt) model.getColumn(0);
62         int columnWidth = column.getWidth();
63         column.setVisible(false);
64         assertEquals("new total width must be old minus invisible column width " + columnWidth,
65                 totalWidth - columnWidth, model.getTotalColumnWidth());
66         
67     }
68     
69     /**
70      * Issue #157: must fire columnRemoved after setting to invisible.
71      *
72      */

73     public void testRemovedFired() {
74         TableColumnModel JavaDoc model = createColumnModel(COLUMN_COUNT);
75         ColumnModelReport l = new ColumnModelReport();
76         model.addColumnModelListener(l);
77         TableColumnExt column = (TableColumnExt) model.getColumn(0);
78         column.setVisible(false);
79         assertTrue("must have fired columnRemoved", l.hasRemovedEvent());
80     }
81
82     /**
83      * Issue #157: must fire columnAdded after setting to invisible.
84      *
85      */

86     public void testAddedFired() {
87         TableColumnModel JavaDoc model = createColumnModel(COLUMN_COUNT);
88         ColumnModelReport l = new ColumnModelReport();
89         TableColumnExt column = (TableColumnExt) model.getColumn(0);
90         column.setVisible(false);
91         model.addColumnModelListener(l);
92         column.setVisible(true);
93         assertTrue("must have fired columnRemoved", l.hasAddedEvent());
94     }
95
96     /**
97      * columnAdded: event.getToIndex must be valid columnIndex.
98      *
99      *
100      */

101      public void testAddInvisibleColumn() {
102          TableColumnModel JavaDoc model = createColumnModel(COLUMN_COUNT);
103          TableColumnModelListener JavaDoc l = new TableColumnModelListener JavaDoc() {
104
105             public void columnAdded(TableColumnModelEvent JavaDoc e) {
106                 assertTrue("toIndex must be positive", e.getToIndex() >= 0);
107                 ((TableColumnModel JavaDoc) e.getSource()).getColumn(e.getToIndex());
108             }
109
110             public void columnRemoved(TableColumnModelEvent JavaDoc e) {
111                 // TODO Auto-generated method stub
112

113             }
114
115             public void columnMoved(TableColumnModelEvent JavaDoc e) {
116                 // TODO Auto-generated method stub
117

118             }
119
120             public void columnMarginChanged(ChangeEvent JavaDoc e) {
121                 // TODO Auto-generated method stub
122

123             }
124
125             public void columnSelectionChanged(ListSelectionEvent JavaDoc e) {
126                 // TODO Auto-generated method stub
127

128             }
129              
130          };
131          model.addColumnModelListener(l);
132          // add invisible column
133
TableColumnExt invisibleColumn = new TableColumnExt(0);
134          invisibleColumn.setVisible(false);
135          model.addColumn(invisibleColumn);
136          // sanity check: add visible column
137
model.addColumn(createTableColumnExt(0));
138     }
139     /**
140      * columnAt must work on visible columns.
141      *
142      */

143     public void testColumnAt() {
144         TableColumnModel JavaDoc model = createColumnModel(COLUMN_COUNT);
145         int totalWidth = model.getTotalColumnWidth();
146         int lastColumn = model.getColumnIndexAtX(totalWidth - 10);
147         assertEquals("lastColumn detected", model.getColumnCount() - 1, lastColumn);
148         TableColumnExt column = (TableColumnExt) model.getColumn(lastColumn);
149         column.setVisible(false);
150         assertEquals("out of range", -1, model.getColumnIndexAtX(totalWidth - 10));
151     }
152
153 //------------------ private factory methods
154

155     private TableColumnModel JavaDoc createColumnModel(int columns) {
156         TableColumnModel JavaDoc model = new DefaultTableColumnModelExt();
157         for (int i = 0; i < columns; i++) {
158             model.addColumn(createTableColumnExt(i));
159       
160         }
161         return model;
162     }
163
164     private TableColumnExt createTableColumnExt(int i) {
165         TableColumnExt column = new TableColumnExt(i);
166         column.setIdentifier("" + i);
167         return column;
168     }
169     
170
171 }
172
Popular Tags