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.beans.PropertyChangeEvent;
023: import java.util.Enumeration;
024: import java.util.EventObject;
025: import javax.swing.BasicSwingTestCase;
026: import javax.swing.DefaultListSelectionModel;
027: import javax.swing.event.ChangeEvent;
028: import javax.swing.event.ListSelectionEvent;
029: import javax.swing.event.ListSelectionListener;
030: import javax.swing.event.TableColumnModelEvent;
031: import javax.swing.event.TableColumnModelListener;
032:
033: public class DefaultTableColumnModelTest extends BasicSwingTestCase {
034: private DefaultTableColumnModel model;
035:
036: public DefaultTableColumnModelTest(final String name) {
037: super (name);
038: }
039:
040: @Override
041: protected void setUp() throws Exception {
042: model = new DefaultTableColumnModel();
043: }
044:
045: @Override
046: protected void tearDown() throws Exception {
047: model = null;
048: }
049:
050: public void testDefaultTableColumnModel() throws Exception {
051: assertNotNull(model.tableColumns);
052: assertEquals(0, model.tableColumns.size());
053: assertTrue(model.selectionModel instanceof DefaultListSelectionModel);
054: assertEquals(1,
055: ((DefaultListSelectionModel) model.selectionModel)
056: .getListSelectionListeners().length);
057: assertEquals(model,
058: ((DefaultListSelectionModel) model.selectionModel)
059: .getListSelectionListeners()[0]);
060: assertEquals(1, model.columnMargin);
061: assertNotNull(model.listenerList);
062: assertNull(model.changeEvent);
063: assertFalse(model.columnSelectionAllowed);
064: assertEquals(-1, model.totalColumnWidth);
065: }
066:
067: public void testAddRemoveMoveColumn() throws Exception {
068: TestTableColumnModelListener listener = new TestTableColumnModelListener();
069: model.addColumnModelListener(listener);
070: TableColumn column1 = new TableColumn();
071: model.addColumn(column1);
072: assertEquals(1, model.getColumnCount());
073: assertEquals(0, column1.getModelIndex());
074: assertTrue(listener.eventOccured());
075: assertEquals(TestTableColumnModelListener.COLUMN_ADDED,
076: listener.getEventType());
077: assertEquals(model, ((TableColumnModelEvent) listener
078: .getEvent()).getSource());
079: assertEquals(0, ((TableColumnModelEvent) listener.getEvent())
080: .getFromIndex());
081: assertEquals(0, ((TableColumnModelEvent) listener.getEvent())
082: .getToIndex());
083: assertEquals(1, column1.getPropertyChangeListeners().length);
084: assertEquals(model, column1.getPropertyChangeListeners()[0]);
085: listener.reset();
086: TableColumn column2 = new TableColumn();
087: model.addColumn(column2);
088: assertEquals(2, model.getColumnCount());
089: assertEquals(0, column2.getModelIndex());
090: assertTrue(listener.eventOccured());
091: assertEquals(TestTableColumnModelListener.COLUMN_ADDED,
092: listener.getEventType());
093: assertEquals(model, ((TableColumnModelEvent) listener
094: .getEvent()).getSource());
095: assertEquals(0, ((TableColumnModelEvent) listener.getEvent())
096: .getFromIndex());
097: assertEquals(1, ((TableColumnModelEvent) listener.getEvent())
098: .getToIndex());
099: listener.reset();
100: model.removeColumn(column1);
101: assertEquals(1, model.getColumnCount());
102: assertEquals(column2, model.getColumn(0));
103: assertTrue(listener.eventOccured());
104: assertEquals(TestTableColumnModelListener.COLUMN_REMOVED,
105: listener.getEventType());
106: assertEquals(model, ((TableColumnModelEvent) listener
107: .getEvent()).getSource());
108: assertEquals(0, ((TableColumnModelEvent) listener.getEvent())
109: .getFromIndex());
110: assertEquals(0, ((TableColumnModelEvent) listener.getEvent())
111: .getToIndex());
112: assertEquals(0, column1.getPropertyChangeListeners().length);
113: listener.reset();
114: model.addColumn(column1);
115: assertEquals(column2, model.getColumn(0));
116: assertEquals(column1, model.getColumn(1));
117: assertTrue(listener.eventOccured());
118: assertEquals(TestTableColumnModelListener.COLUMN_ADDED,
119: listener.getEventType());
120: assertEquals(model, ((TableColumnModelEvent) listener
121: .getEvent()).getSource());
122: assertEquals(0, ((TableColumnModelEvent) listener.getEvent())
123: .getFromIndex());
124: assertEquals(1, ((TableColumnModelEvent) listener.getEvent())
125: .getToIndex());
126: listener.reset();
127: model.moveColumn(0, 1);
128: assertEquals(column1, model.getColumn(0));
129: assertEquals(column2, model.getColumn(1));
130: assertTrue(listener.eventOccured());
131: assertEquals(TestTableColumnModelListener.COLUMN_MOVED,
132: listener.getEventType());
133: assertEquals(model, ((TableColumnModelEvent) listener
134: .getEvent()).getSource());
135: assertEquals(0, ((TableColumnModelEvent) listener.getEvent())
136: .getFromIndex());
137: assertEquals(1, ((TableColumnModelEvent) listener.getEvent())
138: .getToIndex());
139: listener.reset();
140: model.moveColumn(1, 0);
141: assertEquals(column2, model.getColumn(0));
142: assertEquals(column1, model.getColumn(1));
143: assertTrue(listener.eventOccured());
144: assertEquals(TestTableColumnModelListener.COLUMN_MOVED,
145: listener.getEventType());
146: assertEquals(model, ((TableColumnModelEvent) listener
147: .getEvent()).getSource());
148: assertEquals(1, ((TableColumnModelEvent) listener.getEvent())
149: .getFromIndex());
150: assertEquals(0, ((TableColumnModelEvent) listener.getEvent())
151: .getToIndex());
152: listener.reset();
153: model.moveColumn(0, 0);
154: assertEquals(column2, model.getColumn(0));
155: assertEquals(column1, model.getColumn(1));
156: assertTrue(listener.eventOccured());
157: model.removeColumn(null);
158: assertEquals(2, model.getColumnCount());
159: assertTrue(listener.eventOccured());
160: testExceptionalCase(new IllegalArgumentCase() {
161: @Override
162: public void exceptionalAction() throws Exception {
163: model.addColumn(null);
164: }
165: });
166: testExceptionalCase(new IllegalArgumentCase() {
167: @Override
168: public void exceptionalAction() throws Exception {
169: model.moveColumn(5, 1);
170: }
171: });
172: testExceptionalCase(new IllegalArgumentCase() {
173: @Override
174: public void exceptionalAction() throws Exception {
175: model.moveColumn(1, -1);
176: }
177: });
178: }
179:
180: public void testGetSetColumnMargin() throws Exception {
181: TestTableColumnModelListener listener = new TestTableColumnModelListener();
182: model.addColumnModelListener(listener);
183: assertNull(model.changeEvent);
184: assertEquals(1, model.getColumnMargin());
185: model.setColumnMargin(10);
186: assertEquals(10, model.getColumnMargin());
187: assertTrue(listener.eventOccured());
188: assertEquals(TestTableColumnModelListener.MARGIN_CHANGED,
189: listener.getEventType());
190: assertEquals(model, ((ChangeEvent) listener.getEvent())
191: .getSource());
192: assertNotNull(model.changeEvent);
193: model.setColumnMargin(-1);
194: assertEquals(-1, model.getColumnMargin());
195: }
196:
197: public void testGetColumnCount() throws Exception {
198: assertEquals(0, model.getColumnCount());
199: model.addColumn(new TableColumn());
200: assertEquals(1, model.getColumnCount());
201: model.addColumn(new TableColumn());
202: assertEquals(2, model.getColumnCount());
203: }
204:
205: public void testGetColumns() throws Exception {
206: Enumeration<?> columns = model.getColumns();
207: assertNotNull(columns);
208: assertFalse(columns.hasMoreElements());
209: TableColumn column = new TableColumn();
210: model.addColumn(column);
211: columns = model.getColumns();
212: assertNotNull(columns);
213: assertTrue(columns.hasMoreElements());
214: assertEquals(column, columns.nextElement());
215: assertFalse(columns.hasMoreElements());
216: }
217:
218: public void testGetColumnIndex() throws Exception {
219: testExceptionalCase(new IllegalArgumentCase() {
220: @Override
221: public void exceptionalAction() throws Exception {
222: model.getColumnIndex(null);
223: }
224: });
225: testExceptionalCase(new IllegalArgumentCase() {
226: @Override
227: public void exceptionalAction() throws Exception {
228: model.getColumnIndex("tag1");
229: }
230: });
231: TableColumn column1 = new TableColumn();
232: TableColumn column2 = new TableColumn();
233: TableColumn column3 = new TableColumn();
234: TableColumn column4 = new TableColumn();
235: column2.setIdentifier("tag1");
236: column3.setIdentifier("tag2");
237: column4.setIdentifier("tag1");
238: model.addColumn(column1);
239: model.addColumn(column2);
240: model.addColumn(column3);
241: model.addColumn(column4);
242: assertEquals(1, model.getColumnIndex("tag1"));
243: assertEquals(2, model.getColumnIndex("tag2"));
244: testExceptionalCase(new IllegalArgumentCase() {
245: @Override
246: public void exceptionalAction() throws Exception {
247: model.getColumnIndex("tag3");
248: }
249: });
250: }
251:
252: public void testGetColumn() throws Exception {
253: testExceptionalCase(new ExceptionalCase() {
254: @Override
255: public void exceptionalAction() throws Exception {
256: model.getColumn(0);
257: }
258: });
259: TableColumn column1 = new TableColumn();
260: TableColumn column2 = new TableColumn();
261: TableColumn column3 = new TableColumn();
262: TableColumn column4 = new TableColumn();
263: model.addColumn(column1);
264: model.addColumn(column2);
265: model.addColumn(column3);
266: model.addColumn(column4);
267: assertEquals(column1, model.getColumn(0));
268: assertEquals(column2, model.getColumn(1));
269: assertEquals(column3, model.getColumn(2));
270: assertEquals(column4, model.getColumn(3));
271: }
272:
273: public void testGetColumnIndexAtX() throws Exception {
274: assertEquals(-1, model.getColumnIndexAtX(0));
275: model.addColumn(new TableColumn(0, 10));
276: model.addColumn(new TableColumn(0, 20));
277: model.addColumn(new TableColumn(0, 30));
278: model.addColumn(new TableColumn(0, 40));
279: assertEquals(0, model.getColumnIndexAtX(0));
280: assertEquals(0, model.getColumnIndexAtX(9));
281: assertEquals(1, model.getColumnIndexAtX(10));
282: assertEquals(1, model.getColumnIndexAtX(29));
283: assertEquals(2, model.getColumnIndexAtX(30));
284: assertEquals(2, model.getColumnIndexAtX(59));
285: assertEquals(3, model.getColumnIndexAtX(60));
286: assertEquals(3, model.getColumnIndexAtX(99));
287: assertEquals(-1, model.getColumnIndexAtX(100));
288: assertEquals(-1, model.getColumnIndexAtX(-5));
289: }
290:
291: public void testGetTotalColumnWidth() throws Exception {
292: assertEquals(0, model.getTotalColumnWidth());
293: model.addColumn(new TableColumn(0, 10));
294: model.addColumn(new TableColumn(0, 20));
295: model.addColumn(new TableColumn(0, 30));
296: model.addColumn(new TableColumn(0, 40));
297: assertEquals(100, model.getTotalColumnWidth());
298: }
299:
300: public void testGetSetSelectionModel() throws Exception {
301: assertTrue(model.getSelectionModel() instanceof DefaultListSelectionModel);
302: DefaultListSelectionModel oldModel = (DefaultListSelectionModel) model.selectionModel;
303: assertEquals(1, oldModel.getListSelectionListeners().length);
304: assertEquals(model, oldModel.getListSelectionListeners()[0]);
305: DefaultListSelectionModel newModel = new DefaultListSelectionModel();
306: model.setSelectionModel(newModel);
307: assertEquals(newModel, model.getSelectionModel());
308: assertEquals(1, newModel.getListSelectionListeners().length);
309: assertEquals(model, newModel.getListSelectionListeners()[0]);
310: assertEquals(0, oldModel.getListSelectionListeners().length);
311: }
312:
313: public void testGetSetColumnSelectionAllowed() throws Exception {
314: TestTableColumnModelListener listener = new TestTableColumnModelListener();
315: TestListSelectionListener selectionListener = new TestListSelectionListener();
316: model.addColumnModelListener(listener);
317: model.getSelectionModel().addListSelectionListener(
318: selectionListener);
319: assertFalse(model.getColumnSelectionAllowed());
320: model.setColumnSelectionAllowed(true);
321: assertTrue(model.getColumnSelectionAllowed());
322: assertFalse(listener.eventOccured());
323: assertFalse(selectionListener.eventOccured());
324: }
325:
326: public void testGetSelectedColumns() throws Exception {
327: assertEquals(0, model.getSelectedColumns().length);
328: model.addColumn(new TableColumn());
329: model.addColumn(new TableColumn());
330: model.addColumn(new TableColumn());
331: model.addColumn(new TableColumn());
332: model.addColumn(new TableColumn());
333: assertEquals(0, model.getSelectedColumns().length);
334: model.getSelectionModel().setSelectionInterval(1, 1);
335: model.getSelectionModel().addSelectionInterval(3, 4);
336: int[] selectedIndices = model.getSelectedColumns();
337: assertEquals(3, selectedIndices.length);
338: assertEquals(1, selectedIndices[0]);
339: assertEquals(3, selectedIndices[1]);
340: assertEquals(4, selectedIndices[2]);
341: }
342:
343: public void testGetSelectedColumnCount() throws Exception {
344: assertEquals(0, model.getSelectedColumnCount());
345: model.addColumn(new TableColumn());
346: model.addColumn(new TableColumn());
347: model.addColumn(new TableColumn());
348: model.addColumn(new TableColumn());
349: model.addColumn(new TableColumn());
350: assertEquals(0, model.getSelectedColumnCount());
351: model.getSelectionModel().setSelectionInterval(1, 1);
352: model.getSelectionModel().addSelectionInterval(3, 4);
353: assertEquals(3, model.getSelectedColumnCount());
354: }
355:
356: public void testAddRemoveGetTableColumnModelListener()
357: throws Exception {
358: assertEquals(0, model.getColumnModelListeners().length);
359: TableColumnModelListener listener = new TestTableColumnModelListener();
360: model.addColumnModelListener(listener);
361: model
362: .addColumnModelListener(new TestTableColumnModelListener());
363: assertEquals(2, model.getColumnModelListeners().length);
364: model.removeColumnModelListener(listener);
365: assertEquals(1, model.getColumnModelListeners().length);
366: }
367:
368: public void testgetListeners() throws Exception {
369: assertEquals(0, model
370: .getListeners(TableColumnModelListener.class).length);
371: model
372: .addColumnModelListener(new TestTableColumnModelListener());
373: assertEquals(1, model
374: .getListeners(TableColumnModelListener.class).length);
375: }
376:
377: public void testPropertyChange() throws Exception {
378: TableColumn column = new TableColumn();
379: model.addColumn(column);
380: assertEquals(-1, model.totalColumnWidth);
381: assertEquals(75, model.getTotalColumnWidth());
382: column.setWidth(25);
383: assertEquals(-1, model.totalColumnWidth);
384: assertEquals(25, model.getTotalColumnWidth());
385: model.totalColumnWidth = 10;
386: model.propertyChange(new PropertyChangeEvent("source",
387: TableColumn.WIDTH_PROPERTY, "a", "b"));
388: assertEquals(-1, model.totalColumnWidth);
389: model.totalColumnWidth = 10;
390: model.propertyChange(new PropertyChangeEvent("source",
391: TableColumn.PREFERRED_WIDTH_PROPERTY, "a", "b"));
392: assertEquals(-1, model.totalColumnWidth);
393: }
394:
395: public void testValueChanged() throws Exception {
396: TestTableColumnModelListener listener = new TestTableColumnModelListener();
397: model.addColumnModelListener(listener);
398: model.getSelectionModel().setSelectionInterval(1, 2);
399: assertTrue(listener.eventOccured());
400: assertEquals(TestTableColumnModelListener.SELECTION_CHANGED,
401: listener.getEventType());
402: assertEquals(model.getSelectionModel(), listener.getEvent()
403: .getSource());
404: assertEquals(1, ((ListSelectionEvent) listener.getEvent())
405: .getFirstIndex());
406: assertEquals(2, ((ListSelectionEvent) listener.getEvent())
407: .getLastIndex());
408: listener.reset();
409: ListSelectionEvent event = new ListSelectionEvent("source", 4,
410: 7, true);
411: model.valueChanged(event);
412: assertTrue(listener.eventOccured());
413: assertEquals(TestTableColumnModelListener.SELECTION_CHANGED,
414: listener.getEventType());
415: assertEquals("source", listener.getEvent().getSource());
416: assertEquals(4, ((ListSelectionEvent) listener.getEvent())
417: .getFirstIndex());
418: assertEquals(7, ((ListSelectionEvent) listener.getEvent())
419: .getLastIndex());
420: }
421:
422: public void testRecalcWidthCache() throws Exception {
423: assertEquals(-1, model.totalColumnWidth);
424: assertEquals(0, model.getTotalColumnWidth());
425: assertEquals(0, model.totalColumnWidth);
426: model.addColumn(new TableColumn());
427: assertEquals(-1, model.totalColumnWidth);
428: model.recalcWidthCache();
429: assertEquals(75, model.totalColumnWidth);
430: model.removeColumn(model.getColumn(0));
431: assertEquals(-1, model.totalColumnWidth);
432: model.addColumn(new TableColumn());
433: model.addColumn(new TableColumn());
434: model.totalColumnWidth = -1;
435: model.moveColumn(0, 1);
436: assertEquals(-1, model.totalColumnWidth);
437: }
438:
439: private class TestTableColumnModelListener implements
440: TableColumnModelListener {
441: public static final int COLUMN_ADDED = 0;
442:
443: public static final int COLUMN_REMOVED = 1;
444:
445: public static final int COLUMN_MOVED = 2;
446:
447: public static final int MARGIN_CHANGED = 3;
448:
449: public static final int SELECTION_CHANGED = 4;
450:
451: private EventObject event;
452:
453: private int eventType = -1;
454:
455: public void columnMarginChanged(final ChangeEvent e) {
456: event = e;
457: eventType = MARGIN_CHANGED;
458: }
459:
460: public void columnSelectionChanged(final ListSelectionEvent e) {
461: event = e;
462: eventType = SELECTION_CHANGED;
463: }
464:
465: public void columnAdded(final TableColumnModelEvent e) {
466: event = e;
467: eventType = COLUMN_ADDED;
468: }
469:
470: public void columnMoved(final TableColumnModelEvent e) {
471: event = e;
472: eventType = COLUMN_MOVED;
473: }
474:
475: public void columnRemoved(final TableColumnModelEvent e) {
476: event = e;
477: eventType = COLUMN_REMOVED;
478: }
479:
480: public EventObject getEvent() {
481: return event;
482: }
483:
484: public int getEventType() {
485: return eventType;
486: }
487:
488: public boolean eventOccured() {
489: return eventType != -1;
490: }
491:
492: public void reset() {
493: event = null;
494: eventType = -1;
495: }
496: }
497:
498: private class TestListSelectionListener implements
499: ListSelectionListener {
500: private ListSelectionEvent event;
501:
502: public void valueChanged(ListSelectionEvent e) {
503: event = e;
504: }
505:
506: public ListSelectionEvent getEvent() {
507: return event;
508: }
509:
510: public boolean eventOccured() {
511: return event != null;
512: }
513:
514: public void reset() {
515: event = null;
516: }
517: }
518: }
|