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.Component;
023: import java.awt.Dimension;
024: import javax.swing.BasicSwingTestCase;
025: import javax.swing.DefaultCellEditor;
026: import javax.swing.JCheckBox;
027:
028: public class TableColumnTest extends BasicSwingTestCase {
029: private TableColumn column;
030:
031: public TableColumnTest(final String name) {
032: super (name);
033: }
034:
035: @Override
036: protected void setUp() throws Exception {
037: column = new TableColumn();
038: propertyChangeController = new PropertyChangeController();
039: column.addPropertyChangeListener(propertyChangeController);
040: }
041:
042: @Override
043: protected void tearDown() throws Exception {
044: column = null;
045: propertyChangeController = null;
046: }
047:
048: @SuppressWarnings("deprecation")
049: public void testTableColumn() throws Exception {
050: assertEquals(0, column.modelIndex);
051: assertEquals(75, column.width);
052: assertEquals(75, column.getPreferredWidth());
053: assertEquals(15, column.minWidth);
054: assertEquals(Integer.MAX_VALUE, column.maxWidth);
055: assertEquals(0, column.resizedPostingDisableCount);
056: assertNull(column.cellRenderer);
057: assertNull(column.cellEditor);
058: assertNull(column.headerValue);
059: assertNull(column.headerRenderer);
060: assertNull(column.identifier);
061: column = new TableColumn(10);
062: assertEquals(10, column.modelIndex);
063: column = new TableColumn(10, 20);
064: assertEquals(10, column.modelIndex);
065: assertEquals(20, column.width);
066: assertEquals(20, column.getPreferredWidth());
067: TableCellRenderer renderer = new DefaultTableCellRenderer();
068: TableCellEditor editor = new DefaultCellEditor(new JCheckBox());
069: column = new TableColumn(10, 20, renderer, editor);
070: assertEquals(10, column.modelIndex);
071: assertEquals(20, column.width);
072: assertEquals(20, column.getPreferredWidth());
073: assertEquals(renderer, column.cellRenderer);
074: assertEquals(editor, column.cellEditor);
075: }
076:
077: public void testGetSetModelIndex() throws Exception {
078: assertEquals(0, column.getModelIndex());
079: column.setModelIndex(5);
080: assertEquals(5, column.getModelIndex());
081: assertTrue(propertyChangeController.isChanged("modelIndex"));
082: }
083:
084: public void testGetSetIdentifier() throws Exception {
085: assertNull(column.getIdentifier());
086: column.setHeaderValue("Header");
087: assertEquals("Header", column.getIdentifier());
088: assertNull(column.identifier);
089: propertyChangeController.reset();
090: column.setIdentifier("Any");
091: assertEquals("Any", column.getIdentifier());
092: assertTrue(propertyChangeController.isChanged("identifier"));
093: }
094:
095: public void testGetSetHeaderValue() throws Exception {
096: assertNull(column.getHeaderValue());
097: column.setHeaderValue("Any");
098: assertEquals("Any", column.getHeaderValue());
099: assertTrue(propertyChangeController.isChanged("headerValue"));
100: }
101:
102: public void testGetSetHeaderRenderer() throws Exception {
103: assertNull(column.getHeaderRenderer());
104: TableCellRenderer renderer = new DefaultTableCellRenderer();
105: column.setHeaderRenderer(renderer);
106: assertEquals(renderer, column.getHeaderRenderer());
107: assertTrue(propertyChangeController.isChanged("headerRenderer"));
108: }
109:
110: public void testGetSetCellRenderer() throws Exception {
111: assertNull(column.getCellRenderer());
112: TableCellRenderer renderer = new DefaultTableCellRenderer();
113: column.setCellRenderer(renderer);
114: assertEquals(renderer, column.getCellRenderer());
115: assertTrue(propertyChangeController.isChanged("cellRenderer"));
116: }
117:
118: public void testGetSetCellEditor() throws Exception {
119: assertNull(column.getCellEditor());
120: TableCellEditor editor = new DefaultCellEditor(new JCheckBox());
121: column.setCellEditor(editor);
122: assertEquals(editor, column.getCellEditor());
123: assertTrue(propertyChangeController.isChanged("cellEditor"));
124: }
125:
126: public void testGetSetWidth() throws Exception {
127: assertEquals(75, column.getWidth());
128: column.setWidth(20);
129: assertEquals(20, column.getWidth());
130: assertTrue(propertyChangeController.isChanged("width"));
131: column.setMaxWidth(100);
132: column.setWidth(10);
133: assertEquals(15, column.getWidth());
134: column.setWidth(150);
135: assertEquals(100, column.getWidth());
136: assertEquals(75, column.getPreferredWidth());
137: }
138:
139: public void testGetSetPreferredWidth() throws Exception {
140: assertEquals(75, column.getPreferredWidth());
141: column.setPreferredWidth(50);
142: assertEquals(50, column.getPreferredWidth());
143: assertTrue(propertyChangeController.isChanged("preferredWidth"));
144: column.setMaxWidth(100);
145: column.setPreferredWidth(10);
146: assertEquals(15, column.getPreferredWidth());
147: column.setPreferredWidth(150);
148: assertEquals(100, column.getPreferredWidth());
149: assertEquals(75, column.getWidth());
150: }
151:
152: public void testGetSetMinWidth() throws Exception {
153: assertEquals(15, column.getMinWidth());
154: column.setMinWidth(50);
155: assertEquals(50, column.getMinWidth());
156: assertTrue(propertyChangeController.isChanged("minWidth"));
157: column.setMinWidth(100);
158: assertEquals(100, column.getWidth());
159: assertEquals(100, column.getPreferredWidth());
160: column.setMinWidth(10);
161: assertEquals(100, column.getWidth());
162: assertEquals(100, column.getPreferredWidth());
163: }
164:
165: public void testGetSetMaxWidth() throws Exception {
166: assertEquals(Integer.MAX_VALUE, column.getMaxWidth());
167: column.setMaxWidth(100);
168: assertEquals(100, column.getMaxWidth());
169: assertTrue(propertyChangeController.isChanged("maxWidth"));
170: column.setMaxWidth(50);
171: assertEquals(50, column.getWidth());
172: assertEquals(50, column.getPreferredWidth());
173: column.setMinWidth(100);
174: assertEquals(100, column.getMinWidth());
175: assertEquals(50, column.getWidth());
176: assertEquals(50, column.getPreferredWidth());
177: }
178:
179: public void testGetSetResizable() throws Exception {
180: assertTrue(column.getResizable());
181: column.setResizable(false);
182: assertFalse(column.getResizable());
183: assertTrue(propertyChangeController.isChanged("isResizable"));
184: }
185:
186: public void testSizeWidthToFit() throws Exception {
187: column.sizeWidthToFit();
188: assertEquals(15, column.getMinWidth());
189: assertEquals(75, column.getWidth());
190: assertEquals(75, column.getPreferredWidth());
191: assertEquals(Integer.MAX_VALUE, column.getMaxWidth());
192: DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) column
193: .createDefaultHeaderRenderer();
194: renderer.setMaximumSize(new Dimension(600, 200));
195: renderer.setPreferredSize(new Dimension(400, 100));
196: renderer.setMinimumSize(new Dimension(200, 50));
197: column.setHeaderRenderer(renderer);
198: column.sizeWidthToFit();
199: assertEquals(200, column.getMinWidth());
200: assertEquals(400, column.getWidth());
201: assertEquals(400, column.getPreferredWidth());
202: assertEquals(600, column.getMaxWidth());
203: column.setHeaderRenderer(column.createDefaultHeaderRenderer());
204: column.sizeWidthToFit();
205: // specifics of our JLabel's size calculation processing: we calculate component size including
206: // their border insets for all the components in the same manner.
207: // Phenomena caused by inconsistent definition of border insets so that
208: // different Border.getBorderInsets() methods return different results.
209: if (isHarmony()) {
210: assertEquals(2, column.getMinWidth());
211: assertEquals(2, column.getWidth());
212: assertEquals(2, column.getPreferredWidth());
213: assertEquals(2, column.getMaxWidth());
214: } else {
215: assertEquals(0, column.getMinWidth());
216: assertEquals(0, column.getWidth());
217: assertEquals(0, column.getPreferredWidth());
218: assertEquals(0, column.getMaxWidth());
219: }
220: column.setHeaderValue("Any");
221: column.sizeWidthToFit();
222: Component defaultRenderingComponent = column
223: .createDefaultHeaderRenderer()
224: .getTableCellRendererComponent(null, "Any", false,
225: false, 0, 0);
226: assertEquals(defaultRenderingComponent.getMinimumSize().width,
227: column.getMinWidth());
228: assertEquals(
229: defaultRenderingComponent.getPreferredSize().width,
230: column.getWidth());
231: assertEquals(
232: defaultRenderingComponent.getPreferredSize().width,
233: column.getPreferredWidth());
234: assertEquals(defaultRenderingComponent.getMaximumSize().width,
235: column.getMaxWidth());
236: column.setHeaderRenderer(column.createDefaultHeaderRenderer());
237: column.setHeaderValue(null);
238: defaultRenderingComponent = column
239: .createDefaultHeaderRenderer()
240: .getTableCellRendererComponent(null, "Any", false,
241: false, 0, 0);
242: assertEquals(defaultRenderingComponent.getMinimumSize().width,
243: column.getMinWidth());
244: assertEquals(
245: defaultRenderingComponent.getPreferredSize().width,
246: column.getWidth());
247: assertEquals(
248: defaultRenderingComponent.getPreferredSize().width,
249: column.getPreferredWidth());
250: assertEquals(defaultRenderingComponent.getMaximumSize().width,
251: column.getMaxWidth());
252: }
253:
254: @SuppressWarnings("deprecation")
255: public void testEnableDisableResizedPosting() throws Exception {
256: assertEquals(0, column.resizedPostingDisableCount);
257: column.disableResizedPosting();
258: assertEquals(1, column.resizedPostingDisableCount);
259: assertFalse(propertyChangeController.isChanged());
260: column.disableResizedPosting();
261: assertEquals(2, column.resizedPostingDisableCount);
262: column.disableResizedPosting();
263: assertEquals(3, column.resizedPostingDisableCount);
264: column.enableResizedPosting();
265: assertEquals(2, column.resizedPostingDisableCount);
266: assertFalse(propertyChangeController.isChanged());
267: column.enableResizedPosting();
268: assertEquals(1, column.resizedPostingDisableCount);
269: column.enableResizedPosting();
270: assertEquals(0, column.resizedPostingDisableCount);
271: column.enableResizedPosting();
272: assertEquals(-1, column.resizedPostingDisableCount);
273: }
274:
275: public void testAddRemoveGetPropertyChangeListener()
276: throws Exception {
277: assertEquals(1, column.getPropertyChangeListeners().length);
278: assertEquals(propertyChangeController, column
279: .getPropertyChangeListeners()[0]);
280: column.removePropertyChangeListener(propertyChangeController);
281: assertEquals(0, column.getPropertyChangeListeners().length);
282: }
283:
284: public void testCreateDefaultHeaderRenderer() throws Exception {
285: assertTrue(column.createDefaultHeaderRenderer() instanceof DefaultTableCellRenderer);
286: assertNotSame(column.createDefaultHeaderRenderer(), column
287: .createDefaultHeaderRenderer());
288: }
289: }
|