1 7 8 package org.jdesktop.swing.table; 9 10 import javax.swing.event.ChangeEvent ; 11 import javax.swing.event.ListSelectionEvent ; 12 import javax.swing.event.TableColumnModelEvent ; 13 import javax.swing.event.TableColumnModelListener ; 14 import javax.swing.table.TableColumn ; 15 import javax.swing.table.TableColumnModel ; 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 34 public class TableColumnModelTest extends TestCase { 35 36 private static final int COLUMN_COUNT = 3; 37 38 43 public void testColumnCountOnSetInvisible() { 44 TableColumnModel 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 58 public void testTotalColumnWidth() { 59 TableColumnModel 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 73 public void testRemovedFired() { 74 TableColumnModel 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 86 public void testAddedFired() { 87 TableColumnModel 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 101 public void testAddInvisibleColumn() { 102 TableColumnModel model = createColumnModel(COLUMN_COUNT); 103 TableColumnModelListener l = new TableColumnModelListener () { 104 105 public void columnAdded(TableColumnModelEvent e) { 106 assertTrue("toIndex must be positive", e.getToIndex() >= 0); 107 ((TableColumnModel ) e.getSource()).getColumn(e.getToIndex()); 108 } 109 110 public void columnRemoved(TableColumnModelEvent e) { 111 113 } 114 115 public void columnMoved(TableColumnModelEvent e) { 116 118 } 119 120 public void columnMarginChanged(ChangeEvent e) { 121 123 } 124 125 public void columnSelectionChanged(ListSelectionEvent e) { 126 128 } 129 130 }; 131 model.addColumnModelListener(l); 132 TableColumnExt invisibleColumn = new TableColumnExt(0); 134 invisibleColumn.setVisible(false); 135 model.addColumn(invisibleColumn); 136 model.addColumn(createTableColumnExt(0)); 138 } 139 143 public void testColumnAt() { 144 TableColumnModel 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 155 private TableColumnModel createColumnModel(int columns) { 156 TableColumnModel 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 |