001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Anton Avtamonov
019: * @version $Revision$
020: */package javax.swing.table;
021:
022: import java.awt.Point;
023: import java.awt.Rectangle;
024: import java.awt.event.MouseEvent;
025: import java.util.Arrays;
026: import javax.swing.BasicSwingTestCase;
027: import javax.swing.JTable;
028: import javax.swing.plaf.TableHeaderUI;
029: import javax.swing.plaf.UIResource;
030: import javax.swing.plaf.basic.BasicTableHeaderUI;
031:
032: public class JTableHeaderTest extends BasicSwingTestCase {
033: private JTableHeader header;
034:
035: public JTableHeaderTest(final String name) {
036: super (name);
037: }
038:
039: @Override
040: protected void setUp() throws Exception {
041: header = new JTableHeader();
042: propertyChangeController = new PropertyChangeController();
043: header.addPropertyChangeListener(propertyChangeController);
044: }
045:
046: @Override
047: protected void tearDown() throws Exception {
048: header = null;
049: }
050:
051: public void testJTableHeader() throws Exception {
052: assertTrue(header.columnModel instanceof DefaultTableColumnModel);
053: header = new JTableHeader(null);
054: assertTrue(header.columnModel instanceof DefaultTableColumnModel);
055: TableColumnModel cm = new DefaultTableColumnModel();
056: header = new JTableHeader(cm);
057: assertSame(cm, header.columnModel);
058: }
059:
060: public void testGetSetTable() throws Exception {
061: assertNull(header.table);
062: assertNull(header.getTable());
063: JTable table = new JTable();
064: header.setTable(table);
065: assertSame(table, header.getTable());
066: assertTrue(propertyChangeController.isChanged("table"));
067: }
068:
069: public void testGetSetReorderingAllowed() throws Exception {
070: assertTrue(header.getReorderingAllowed());
071: header.setReorderingAllowed(false);
072: assertFalse(header.getReorderingAllowed());
073: assertTrue(propertyChangeController
074: .isChanged("reorderingAllowed"));
075: }
076:
077: public void testGetSetResizingAllowed() throws Exception {
078: assertTrue(header.getResizingAllowed());
079: header.setResizingAllowed(false);
080: assertFalse(header.getResizingAllowed());
081: assertTrue(propertyChangeController
082: .isChanged("resizingAllowed"));
083: }
084:
085: public void testGetSetDraggedColumn() throws Exception {
086: assertNull(header.getDraggedColumn());
087: TableColumn column = new TableColumn();
088: header.setDraggedColumn(column);
089: assertEquals(column, header.getDraggedColumn());
090: assertFalse(propertyChangeController.isChanged());
091: }
092:
093: public void testGetSetDraggedDistance() throws Exception {
094: assertEquals(0, header.getDraggedDistance());
095: header.setDraggedDistance(10);
096: assertEquals(10, header.getDraggedDistance());
097: header.setDraggedDistance(-10);
098: assertEquals(-10, header.getDraggedDistance());
099: assertFalse(propertyChangeController.isChanged());
100: }
101:
102: public void testGetSetResizingColumn() throws Exception {
103: assertNull(header.getResizingColumn());
104: TableColumn column = new TableColumn();
105: header.setResizingColumn(column);
106: assertEquals(column, header.getResizingColumn());
107: assertFalse(propertyChangeController.isChanged());
108: }
109:
110: public void testGetSetUpdateTableInRealTime() throws Exception {
111: assertTrue(header.getUpdateTableInRealTime());
112: header.setUpdateTableInRealTime(false);
113: assertFalse(header.getUpdateTableInRealTime());
114: assertFalse(propertyChangeController.isChanged());
115: }
116:
117: public void testGetSetDefaultRenderer() throws Exception {
118: assertTrue(header.getDefaultRenderer() instanceof DefaultTableCellRenderer);
119: assertTrue(header.getDefaultRenderer() instanceof UIResource);
120: TableCellRenderer renderer = new DefaultTableCellRenderer();
121: header.setDefaultRenderer(renderer);
122: assertSame(renderer, header.getDefaultRenderer());
123: assertFalse(propertyChangeController.isChanged());
124: header.setDefaultRenderer(null);
125: assertNull(header.getDefaultRenderer());
126: }
127:
128: public void testColumnAtPoint() throws Exception {
129: assertEquals(-1, header.columnAtPoint(new Point(10, 10)));
130: header.columnModel.addColumn(new TableColumn(0, 20));
131: header.columnModel.addColumn(new TableColumn(0, 30));
132: assertEquals(0, header.columnAtPoint(new Point(10, 1000)));
133: assertEquals(0, header.columnAtPoint(new Point(19, 1000)));
134: assertEquals(1, header.columnAtPoint(new Point(30, 1000)));
135: }
136:
137: public void testGetHeaderRect() throws Exception {
138: assertEquals(new Rectangle(), header.getHeaderRect(10));
139: header.columnModel.addColumn(new TableColumn(0, 20));
140: assertEquals(new Rectangle(0, 0, 20, 0), header
141: .getHeaderRect(0));
142: assertEquals(new Rectangle(), header.getHeaderRect(1));
143: header.columnModel.addColumn(new TableColumn(0, 30));
144: assertEquals(new Rectangle(0, 0, 20, 0), header
145: .getHeaderRect(0));
146: assertEquals(new Rectangle(20, 0, 30, 0), header
147: .getHeaderRect(1));
148: assertEquals(new Rectangle(), header.getHeaderRect(2));
149: assertNotSame(header.getHeaderRect(2), header.getHeaderRect(2));
150: header.setBounds(10, 10, 100, 30);
151: assertEquals(new Rectangle(0, 0, 20, 30), header
152: .getHeaderRect(0));
153: }
154:
155: public void testGetToolTipText() throws Exception {
156: assertNull(header.getToolTipText(new MouseEvent(header, 0, 0,
157: 0, 0, 0, 0, false)));
158: ((DefaultTableCellRenderer) header.getDefaultRenderer())
159: .setToolTipText("tooltip");
160: assertNull(header.getToolTipText(new MouseEvent(header, 0, 0,
161: 0, 0, 0, 0, false)));
162: header.columnModel.addColumn(new TableColumn(0, 20));
163: assertEquals("tooltip", header.getToolTipText(new MouseEvent(
164: header, 0, 0, 0, 0, 100, 0, false)));
165: assertNull(header.getToolTipText(new MouseEvent(header, 0, 0,
166: 0, 25, 0, 0, false)));
167: header.columnModel.addColumn(new TableColumn(0, 20));
168: assertEquals("tooltip", header.getToolTipText(new MouseEvent(
169: header, 0, 0, 0, 0, 100, 0, false)));
170: assertEquals("tooltip", header.getToolTipText(new MouseEvent(
171: header, 0, 0, 0, 25, 100, 0, false)));
172: DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) header.columnModel
173: .getColumn(0).createDefaultHeaderRenderer();
174: renderer.setToolTipText("column tooltip");
175: header.columnModel.getColumn(0).setHeaderRenderer(renderer);
176: assertEquals("column tooltip", header
177: .getToolTipText(new MouseEvent(header, 0, 0, 0, 0, 100,
178: 0, false)));
179: assertEquals("tooltip", header.getToolTipText(new MouseEvent(
180: header, 0, 0, 0, 25, 100, 0, false)));
181: }
182:
183: public void testGetSetUpdateUI() throws Exception {
184: assertNotNull(header.getUI());
185: TableHeaderUI ui = new BasicTableHeaderUI();
186: header.setUI(ui);
187: assertSame(ui, header.getUI());
188: header.updateUI();
189: assertNotNull(header.getUI());
190: assertNotSame(ui, header.getUI());
191: }
192:
193: public void testGetSetColumnModel() throws Exception {
194: assertTrue(header.getColumnModel() instanceof DefaultTableColumnModel);
195: assertTrue(Arrays.asList(
196: ((DefaultTableColumnModel) header.getColumnModel())
197: .getColumnModelListeners()).contains(header));
198: DefaultTableColumnModel model = new DefaultTableColumnModel();
199: header.setColumnModel(model);
200: assertSame(model, header.getColumnModel());
201: assertTrue(Arrays.asList(model.getColumnModelListeners())
202: .contains(header));
203: assertTrue(propertyChangeController.isChanged("columnModel"));
204: testExceptionalCase(new IllegalArgumentCase() {
205: @Override
206: public void exceptionalAction() throws Exception {
207: header.setColumnModel(null);
208: }
209: });
210: }
211:
212: public void testCreateDefaultColumnModel() throws Exception {
213: assertTrue(header.createDefaultColumnModel().getClass() == DefaultTableColumnModel.class);
214: assertNotSame(header.createDefaultColumnModel(), header
215: .createDefaultColumnModel());
216: }
217:
218: public void testCreateDefaultRenderer() throws Exception {
219: assertTrue(header.createDefaultRenderer() instanceof DefaultTableCellRenderer);
220: assertTrue(header.createDefaultRenderer() instanceof UIResource);
221: assertNotSame(header.createDefaultRenderer(), header
222: .createDefaultRenderer());
223: }
224:
225: public void testInitializeLocalVars() throws Exception {
226: assertTrue(header.getReorderingAllowed());
227: assertTrue(header.getResizingAllowed());
228: assertTrue(header.getUpdateTableInRealTime());
229: assertNotNull(header.getDefaultRenderer());
230: assertNull(header.getResizingColumn());
231: assertNull(header.getDraggedColumn());
232: assertEquals(0, header.getDraggedDistance());
233: assertNull(header.getTable());
234: header.setReorderingAllowed(false);
235: header.setResizingAllowed(false);
236: header.setUpdateTableInRealTime(false);
237: header.setDefaultRenderer(null);
238: header.setResizingColumn(new TableColumn());
239: header.setDraggedColumn(new TableColumn());
240: header.setDraggedDistance(10);
241: header.setTable(new JTable());
242: header.initializeLocalVars();
243: assertTrue(header.getReorderingAllowed());
244: assertTrue(header.getResizingAllowed());
245: assertTrue(header.getUpdateTableInRealTime());
246: assertNotNull(header.getDefaultRenderer());
247: assertNull(header.getResizingColumn());
248: assertNull(header.getDraggedColumn());
249: assertEquals(0, header.getDraggedDistance());
250: assertNull(header.getTable());
251: }
252:
253: public void testGetAccessibleContext() throws Exception {
254: assertTrue(header.getAccessibleContext() instanceof JTableHeader.AccessibleJTableHeader);
255: }
256: }
|