0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: /**
0018: * @author Anton Avtamonov
0019: * @version $Revision$
0020: */package javax.swing;
0021:
0022: import java.awt.Color;
0023: import java.awt.Dimension;
0024: import java.awt.Point;
0025: import java.awt.Rectangle;
0026: import java.awt.event.KeyEvent;
0027: import java.awt.event.MouseEvent;
0028: import javax.swing.plaf.TableUI;
0029: import javax.swing.plaf.basic.BasicTableUI;
0030: import javax.swing.table.DefaultTableCellRenderer;
0031: import javax.swing.table.DefaultTableColumnModel;
0032: import javax.swing.table.DefaultTableModel;
0033: import javax.swing.table.JTableHeader;
0034: import javax.swing.table.TableCellEditor;
0035: import javax.swing.table.TableCellRenderer;
0036: import javax.swing.table.TableColumn;
0037: import javax.swing.table.TableColumnModel;
0038:
0039: @SuppressWarnings("serial")
0040: public class JTableTest extends BasicSwingTestCase {
0041: private JTable table;
0042:
0043: public JTableTest(final String name) {
0044: super (name);
0045: }
0046:
0047: @Override
0048: protected void setUp() throws Exception {
0049: table = new JTable();
0050: propertyChangeController = new PropertyChangeController();
0051: table.addPropertyChangeListener(propertyChangeController);
0052: }
0053:
0054: @Override
0055: protected void tearDown() throws Exception {
0056: table = null;
0057: }
0058:
0059: public void testJTable() throws Exception {
0060: assertSame(DefaultTableModel.class, table.dataModel.getClass());
0061: assertSame(DefaultTableColumnModel.class, table.columnModel
0062: .getClass());
0063: assertSame(DefaultListSelectionModel.class,
0064: table.selectionModel.getClass());
0065: assertSame(JTableHeader.class, table.tableHeader.getClass());
0066: assertEquals(3, table.defaultEditorsByColumnClass.size());
0067: assertEquals(8, table.defaultRenderersByColumnClass.size());
0068: DefaultTableModel model = new DefaultTableModel();
0069: table = new JTable(model);
0070: assertSame(model, table.dataModel);
0071: assertSame(DefaultTableColumnModel.class, table.columnModel
0072: .getClass());
0073: assertSame(DefaultListSelectionModel.class,
0074: table.selectionModel.getClass());
0075: DefaultTableColumnModel columnModel = new DefaultTableColumnModel();
0076: table = new JTable(model, columnModel);
0077: assertSame(model, table.dataModel);
0078: assertSame(columnModel, table.columnModel);
0079: assertSame(DefaultListSelectionModel.class,
0080: table.selectionModel.getClass());
0081: DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
0082: table = new JTable(model, columnModel, selectionModel);
0083: assertSame(model, table.dataModel);
0084: assertSame(columnModel, table.columnModel);
0085: assertSame(selectionModel, table.selectionModel);
0086: table = new JTable(null, columnModel, null);
0087: assertSame(DefaultTableModel.class, table.dataModel.getClass());
0088: assertSame(columnModel, table.columnModel);
0089: assertSame(DefaultListSelectionModel.class,
0090: table.selectionModel.getClass());
0091: table = new JTable(3, 4);
0092: assertSame(DefaultTableModel.class, table.dataModel.getClass());
0093: assertSame(DefaultTableColumnModel.class, table.columnModel
0094: .getClass());
0095: assertSame(DefaultListSelectionModel.class,
0096: table.selectionModel.getClass());
0097: assertEquals(3, table.dataModel.getRowCount());
0098: assertEquals(4, table.dataModel.getColumnCount());
0099: assertEquals(4, table.columnModel.getColumnCount());
0100: }
0101:
0102: public void testAddNotify() throws Exception {
0103: final Marker marker = new Marker();
0104: new JTable() {
0105: @Override
0106: protected void configureEnclosingScrollPane() {
0107: marker.setOccurred();
0108: }
0109: }.addNotify();
0110: assertTrue(marker.isOccurred());
0111: }
0112:
0113: public void testRemoveNotify() throws Exception {
0114: final Marker marker = new Marker();
0115: new JTable() {
0116: @Override
0117: protected void unconfigureEnclosingScrollPane() {
0118: marker.setOccurred();
0119: }
0120: }.removeNotify();
0121: assertTrue(marker.isOccurred());
0122: }
0123:
0124: public void testConfigureUnconfigureEnclosingScrollPane()
0125: throws Exception {
0126: JScrollPane pane = new JScrollPane();
0127: pane.setViewportView(table);
0128: assertNull(pane.getColumnHeader());
0129: table.addNotify();
0130: assertNotNull(pane.getColumnHeader());
0131: assertSame(table.getTableHeader(), pane.getColumnHeader()
0132: .getView());
0133: table.removeNotify();
0134: assertNull(pane.getColumnHeader().getView());
0135: }
0136:
0137: public void testGetSetTableHeader() throws Exception {
0138: assertNotNull(table.getTableHeader());
0139: assertEquals(table.tableHeader, table.getTableHeader());
0140: assertEquals(table, table.getTableHeader().getTable());
0141: assertEquals(table.getColumnModel(), table.getTableHeader()
0142: .getColumnModel());
0143: JTableHeader oldValue = table.getTableHeader();
0144: JTableHeader header = new JTableHeader();
0145: table.setTableHeader(header);
0146: assertEquals(table.tableHeader, table.getTableHeader());
0147: assertEquals(table, table.getTableHeader().getTable());
0148: assertNull(oldValue.getTable());
0149: assertNotSame(table.getColumnModel(), table.getTableHeader()
0150: .getColumnModel());
0151: assertTrue(propertyChangeController.isChanged("tableHeader"));
0152: table.setTableHeader(null);
0153: assertNull(table.getTableHeader());
0154: }
0155:
0156: public void testGetSetRowHeight() throws Exception {
0157: assertEquals(16, table.rowHeight);
0158: assertEquals(16, table.getRowHeight());
0159: table.setRowHeight(30);
0160: assertEquals(30, table.getRowHeight());
0161: assertEquals(30, table.getRowHeight(10));
0162: assertTrue(propertyChangeController.isChanged("rowHeight"));
0163: propertyChangeController.reset();
0164: table.setRowHeight(10, 50);
0165: assertEquals(30, table.getRowHeight());
0166: if (isHarmony()) {
0167: assertEquals(30, table.getRowHeight(10));
0168: } else {
0169: assertEquals(0, table.getRowHeight(10));
0170: }
0171: assertFalse(propertyChangeController.isChanged("rowHeight"));
0172: table = new JTable(3, 4);
0173: table.setRowHeight(2, 50);
0174: assertEquals(16, table.getRowHeight());
0175: assertEquals(16, table.getRowHeight(0));
0176: assertEquals(50, table.getRowHeight(2));
0177: if (isHarmony()) {
0178: assertEquals(16, table.getRowHeight(10));
0179: } else {
0180: assertEquals(0, table.getRowHeight(10));
0181: }
0182: table.setRowHeight(20, 50);
0183: if (isHarmony()) {
0184: assertEquals(16, table.getRowHeight(20));
0185: } else {
0186: assertEquals(0, table.getRowHeight(20));
0187: }
0188: table.setRowHeight(3, 25);
0189: if (isHarmony()) {
0190: assertEquals(16, table.getRowHeight(3));
0191: } else {
0192: assertEquals(0, table.getRowHeight(3));
0193: }
0194: table.setRowHeight(40);
0195: ((DefaultTableModel) table.getModel()).addRow(new Object[] {
0196: "3", "3" });
0197: assertEquals(40, table.getRowHeight(3));
0198: table.setRowHeight(3, 25);
0199: assertEquals(25, table.getRowHeight(3));
0200: ((DefaultTableModel) table.getModel()).removeRow(3);
0201: ((DefaultTableModel) table.getModel()).addRow(new Object[] {
0202: "3", "3" });
0203: if (isHarmony()) {
0204: assertEquals(25, table.getRowHeight(3));
0205: } else {
0206: assertEquals(40, table.getRowHeight(3));
0207: }
0208: testExceptionalCase(new IllegalArgumentCase() {
0209: @Override
0210: public void exceptionalAction() throws Exception {
0211: table.setRowHeight(0);
0212: }
0213: });
0214: }
0215:
0216: public void testGetSetRowMargin() throws Exception {
0217: assertEquals(1, table.getRowMargin());
0218: assertEquals(1, table.getIntercellSpacing().height);
0219: table.setRowMargin(10);
0220: assertEquals(10, table.getRowMargin());
0221: assertEquals(10, table.getIntercellSpacing().height);
0222: assertTrue(propertyChangeController.isChanged("rowMargin"));
0223: table.setRowMargin(-5);
0224: assertEquals(-5, table.getRowMargin());
0225: assertEquals(-5, table.getIntercellSpacing().height);
0226: }
0227:
0228: public void testGetSetIntercellSpacing() throws Exception {
0229: assertEquals(new Dimension(1, 1), table.getIntercellSpacing());
0230: assertNotSame(table.getIntercellSpacing(), table
0231: .getIntercellSpacing());
0232: table.setRowMargin(10);
0233: assertEquals(new Dimension(1, 10), table.getIntercellSpacing());
0234: table.getColumnModel().setColumnMargin(5);
0235: assertEquals(new Dimension(5, 10), table.getIntercellSpacing());
0236: propertyChangeController.reset();
0237: Dimension spacing = new Dimension(3, 4);
0238: table.setIntercellSpacing(spacing);
0239: assertNotSame(spacing, table.getIntercellSpacing());
0240: assertEquals(spacing, table.getIntercellSpacing());
0241: assertEquals(4, table.getRowMargin());
0242: assertEquals(3, table.getColumnModel().getColumnMargin());
0243: assertTrue(propertyChangeController.isChanged("rowMargin"));
0244: }
0245:
0246: public void testGetSetGridColor() throws Exception {
0247: assertEquals(UIManager.getColor("Table.gridColor"), table
0248: .getGridColor());
0249: table.setGridColor(Color.RED);
0250: assertEquals(Color.RED, table.getGridColor());
0251: assertTrue(propertyChangeController.isChanged("gridColor"));
0252: }
0253:
0254: public void testSetShowGrid() throws Exception {
0255: assertTrue(table.getShowHorizontalLines());
0256: assertTrue(table.getShowVerticalLines());
0257: table.setShowGrid(false);
0258: assertFalse(table.getShowHorizontalLines());
0259: assertFalse(table.getShowVerticalLines());
0260: assertTrue(propertyChangeController
0261: .isChanged("showHorizontalLines"));
0262: assertTrue(propertyChangeController
0263: .isChanged("showVerticalLines"));
0264: table.setShowGrid(true);
0265: assertTrue(table.getShowHorizontalLines());
0266: assertTrue(table.getShowVerticalLines());
0267: }
0268:
0269: public void testGetSetShowHorizontalLines() throws Exception {
0270: assertTrue(table.getShowHorizontalLines());
0271: table.setShowHorizontalLines(false);
0272: assertFalse(table.getShowHorizontalLines());
0273: assertTrue(propertyChangeController
0274: .isChanged("showHorizontalLines"));
0275: table.setShowHorizontalLines(true);
0276: assertTrue(table.getShowHorizontalLines());
0277: }
0278:
0279: public void testGetSetShowVerticalLines() throws Exception {
0280: assertTrue(table.getShowVerticalLines());
0281: table.setShowVerticalLines(false);
0282: assertFalse(table.getShowVerticalLines());
0283: assertTrue(propertyChangeController
0284: .isChanged("showVerticalLines"));
0285: table.setShowVerticalLines(true);
0286: assertTrue(table.getShowVerticalLines());
0287: }
0288:
0289: public void testGetSetAutoResizeMode() throws Exception {
0290: assertEquals(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS, table
0291: .getAutoResizeMode());
0292: table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
0293: assertEquals(JTable.AUTO_RESIZE_ALL_COLUMNS, table
0294: .getAutoResizeMode());
0295: assertTrue(propertyChangeController.isChanged("autoResizeMode"));
0296: propertyChangeController.reset();
0297: table.setAutoResizeMode(20);
0298: assertEquals(JTable.AUTO_RESIZE_ALL_COLUMNS, table
0299: .getAutoResizeMode());
0300: assertFalse(propertyChangeController.isChanged());
0301: }
0302:
0303: public void testGetSetAutoCreateColumnsFromModel() throws Exception {
0304: assertTrue(table.getAutoCreateColumnsFromModel());
0305: table.setAutoCreateColumnsFromModel(false);
0306: assertFalse(table.getAutoCreateColumnsFromModel());
0307: assertTrue(propertyChangeController
0308: .isChanged("autoCreateColumnsFromModel"));
0309: DefaultTableModel model = (DefaultTableModel) table.getModel();
0310: model.addColumn("column1");
0311: assertEquals(0, table.getColumnModel().getColumnCount());
0312: table.setAutoCreateColumnsFromModel(true);
0313: assertEquals(1, table.getColumnModel().getColumnCount());
0314: model.addColumn("column2");
0315: assertEquals(2, table.getColumnModel().getColumnCount());
0316: table.setAutoCreateColumnsFromModel(false);
0317: model.addColumn("column3");
0318: assertEquals(2, table.getColumnModel().getColumnCount());
0319: }
0320:
0321: public void testCreateDefaultColumnsFromModel() throws Exception {
0322: TableColumnModel columnModel = table.getColumnModel();
0323: assertEquals(0, columnModel.getColumnCount());
0324: TableColumn modelColumn1 = new TableColumn();
0325: modelColumn1.setIdentifier("modelColumn1");
0326: columnModel.addColumn(modelColumn1);
0327: assertEquals(1, columnModel.getColumnCount());
0328: table.setAutoCreateColumnsFromModel(false);
0329: DefaultTableModel model = (DefaultTableModel) table.getModel();
0330: model.addColumn("column1");
0331: model.addColumn("column2");
0332: assertEquals(1, columnModel.getColumnCount());
0333: table.createDefaultColumnsFromModel();
0334: assertEquals(2, columnModel.getColumnCount());
0335: assertEquals("column1", columnModel.getColumn(0)
0336: .getIdentifier());
0337: assertEquals("column2", columnModel.getColumn(1)
0338: .getIdentifier());
0339: model.addColumn("column3");
0340: model.addColumn("column4");
0341: assertEquals(2, columnModel.getColumnCount());
0342: table.setAutoCreateColumnsFromModel(true);
0343: assertEquals(4, columnModel.getColumnCount());
0344: }
0345:
0346: public void testGetSetDefaultRenderer() throws Exception {
0347: assertEquals(8, table.defaultRenderersByColumnClass.size());
0348: assertNotNull(table.getDefaultRenderer(String.class));
0349: assertSame(table.defaultRenderersByColumnClass
0350: .get(Object.class), table
0351: .getDefaultRenderer(String.class));
0352: DefaultTableCellRenderer stringRenderer = new DefaultTableCellRenderer();
0353: table.setDefaultRenderer(String.class, stringRenderer);
0354: DefaultTableCellRenderer objectRenderer = new DefaultTableCellRenderer();
0355: table.setDefaultRenderer(Object.class, objectRenderer);
0356: assertEquals(9, table.defaultRenderersByColumnClass.size());
0357: assertSame(stringRenderer, table
0358: .getDefaultRenderer(String.class));
0359: assertSame(objectRenderer, table
0360: .getDefaultRenderer(JTable.class));
0361: table.setDefaultRenderer(Object.class, null);
0362: assertEquals(8, table.defaultRenderersByColumnClass.size());
0363: assertNull(table.getDefaultRenderer(JTable.class));
0364: DefaultTableCellRenderer actionRenderer = new DefaultTableCellRenderer();
0365: table.setDefaultRenderer(Action.class, actionRenderer);
0366: assertSame(actionRenderer, table
0367: .getDefaultRenderer(Action.class));
0368: assertNull(table.getDefaultRenderer(AbstractAction.class));
0369: }
0370:
0371: public void testGetSetDefaultEditor() throws Exception {
0372: assertEquals(3, table.defaultEditorsByColumnClass.size());
0373: assertNotNull(table.getDefaultEditor(String.class));
0374: assertSame(table.defaultEditorsByColumnClass.get(Object.class),
0375: table.getDefaultEditor(String.class));
0376: DefaultCellEditor componentEditor = new DefaultCellEditor(
0377: new JTextField());
0378: table.setDefaultEditor(JComponent.class, componentEditor);
0379: DefaultCellEditor booleanEditor = new DefaultCellEditor(
0380: new JCheckBox());
0381: table.setDefaultEditor(Boolean.class, booleanEditor);
0382: assertEquals(4, table.defaultEditorsByColumnClass.size());
0383: assertSame(componentEditor, table
0384: .getDefaultEditor(JTable.class));
0385: assertSame(booleanEditor, table.getDefaultEditor(Boolean.class));
0386: table.setDefaultEditor(Boolean.class, null);
0387: assertEquals(3, table.defaultEditorsByColumnClass.size());
0388: assertNotNull(table.getDefaultEditor(Boolean.class));
0389: assertNotNull(table.getDefaultEditor(Math.class));
0390: }
0391:
0392: public void testGetSetDragEnabled() throws Exception {
0393: assertFalse(table.getDragEnabled());
0394: table.setDragEnabled(true);
0395: assertTrue(table.getDragEnabled());
0396: }
0397:
0398: public void testSetSelectionMode() throws Exception {
0399: assertEquals(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION,
0400: table.getSelectionModel().getSelectionMode());
0401: assertEquals(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION,
0402: table.getColumnModel().getSelectionModel()
0403: .getSelectionMode());
0404: table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
0405: assertEquals(ListSelectionModel.SINGLE_SELECTION, table
0406: .getSelectionModel().getSelectionMode());
0407: assertEquals(ListSelectionModel.SINGLE_SELECTION, table
0408: .getColumnModel().getSelectionModel()
0409: .getSelectionMode());
0410: assertFalse(propertyChangeController.isChanged());
0411: }
0412:
0413: public void testGetSetRowSelectionAllowed() throws Exception {
0414: assertTrue(table.getRowSelectionAllowed());
0415: table.setRowSelectionAllowed(false);
0416: assertFalse(table.getRowSelectionAllowed());
0417: assertTrue(propertyChangeController
0418: .isChanged("rowSelectionAllowed"));
0419: }
0420:
0421: public void testGetSetColumnSelectionAllowed() throws Exception {
0422: assertFalse(table.getColumnSelectionAllowed());
0423: assertFalse(table.getColumnModel().getColumnSelectionAllowed());
0424: table.setColumnSelectionAllowed(true);
0425: assertTrue(table.getColumnSelectionAllowed());
0426: assertTrue(table.getColumnModel().getColumnSelectionAllowed());
0427: assertTrue(propertyChangeController
0428: .isChanged("columnSelectionAllowed"));
0429: }
0430:
0431: public void testGetSetCellSelectionEnabled() throws Exception {
0432: assertFalse(table.getCellSelectionEnabled());
0433: assertFalse(table.cellSelectionEnabled);
0434: table.setCellSelectionEnabled(false);
0435: assertFalse(table.cellSelectionEnabled);
0436: assertFalse(table.getCellSelectionEnabled());
0437: assertFalse(table.getRowSelectionAllowed());
0438: assertFalse(table.getColumnSelectionAllowed());
0439: assertFalse(propertyChangeController
0440: .isChanged("cellSelectionEnabled"));
0441: propertyChangeController.reset();
0442: table.setCellSelectionEnabled(true);
0443: assertTrue(table.cellSelectionEnabled);
0444: assertTrue(table.getCellSelectionEnabled());
0445: assertTrue(table.getRowSelectionAllowed());
0446: assertTrue(table.getColumnSelectionAllowed());
0447: assertTrue(propertyChangeController
0448: .isChanged("cellSelectionEnabled"));
0449: table.setRowSelectionAllowed(false);
0450: assertFalse(table.getCellSelectionEnabled());
0451: assertTrue(table.cellSelectionEnabled);
0452: }
0453:
0454: public void testSelectAllClearSelection() throws Exception {
0455: table = new JTable(3, 4);
0456: assertEquals(0,
0457: getSelectedIndices(table.getSelectionModel()).length);
0458: assertEquals(0, getSelectedIndices(table.getColumnModel()
0459: .getSelectionModel()).length);
0460: table.selectAll();
0461: assertEquals(3,
0462: getSelectedIndices(table.getSelectionModel()).length);
0463: assertEquals(4, getSelectedIndices(table.getColumnModel()
0464: .getSelectionModel()).length);
0465: table.clearSelection();
0466: assertEquals(0,
0467: getSelectedIndices(table.getSelectionModel()).length);
0468: assertEquals(0, getSelectedIndices(table.getColumnModel()
0469: .getSelectionModel()).length);
0470: table.setCellSelectionEnabled(false);
0471: table.selectAll();
0472: assertEquals(3,
0473: getSelectedIndices(table.getSelectionModel()).length);
0474: assertEquals(4, getSelectedIndices(table.getColumnModel()
0475: .getSelectionModel()).length);
0476: }
0477:
0478: public void testSetRowSelectionInterval() throws Exception {
0479: table = new JTable(3, 4);
0480: assertEquals(0,
0481: getSelectedIndices(table.getSelectionModel()).length);
0482: table.setRowSelectionInterval(1, 2);
0483: assertEquals(2,
0484: getSelectedIndices(table.getSelectionModel()).length);
0485: assertEquals(1,
0486: getSelectedIndices(table.getSelectionModel())[0]);
0487: assertEquals(2,
0488: getSelectedIndices(table.getSelectionModel())[1]);
0489: table.clearSelection();
0490: table.setRowSelectionAllowed(false);
0491: table.setRowSelectionInterval(1, 1);
0492: assertEquals(1,
0493: getSelectedIndices(table.getSelectionModel()).length);
0494: assertEquals(1,
0495: getSelectedIndices(table.getSelectionModel())[0]);
0496: testExceptionalCase(new IllegalArgumentCase() {
0497: @Override
0498: public void exceptionalAction() throws Exception {
0499: table.setRowSelectionInterval(0, 3);
0500: }
0501: });
0502: testExceptionalCase(new IllegalArgumentCase() {
0503: @Override
0504: public void exceptionalAction() throws Exception {
0505: table.setRowSelectionInterval(-1, 1);
0506: }
0507: });
0508: }
0509:
0510: public void testSetColumnSelectionInterval() throws Exception {
0511: table = new JTable(3, 4);
0512: assertEquals(0, getSelectedIndices(table.getColumnModel()
0513: .getSelectionModel()).length);
0514: table.setColumnSelectionInterval(1, 2);
0515: assertEquals(2, getSelectedIndices(table.getColumnModel()
0516: .getSelectionModel()).length);
0517: assertEquals(1, getSelectedIndices(table.getColumnModel()
0518: .getSelectionModel())[0]);
0519: assertEquals(2, getSelectedIndices(table.getColumnModel()
0520: .getSelectionModel())[1]);
0521: table.clearSelection();
0522: table.setColumnSelectionAllowed(false);
0523: table.setColumnSelectionInterval(1, 1);
0524: assertEquals(1, getSelectedIndices(table.getColumnModel()
0525: .getSelectionModel()).length);
0526: assertEquals(1, getSelectedIndices(table.getColumnModel()
0527: .getSelectionModel())[0]);
0528: testExceptionalCase(new IllegalArgumentCase() {
0529: @Override
0530: public void exceptionalAction() throws Exception {
0531: table.setColumnSelectionInterval(0, 4);
0532: }
0533: });
0534: testExceptionalCase(new IllegalArgumentCase() {
0535: @Override
0536: public void exceptionalAction() throws Exception {
0537: table.setColumnSelectionInterval(-1, 1);
0538: }
0539: });
0540: }
0541:
0542: public void testAddRemoveRowSelectionInterval() throws Exception {
0543: table = new JTable(3, 4);
0544: assertEquals(0,
0545: getSelectedIndices(table.getSelectionModel()).length);
0546: table.addRowSelectionInterval(0, 0);
0547: assertEquals(1,
0548: getSelectedIndices(table.getSelectionModel()).length);
0549: assertEquals(0,
0550: getSelectedIndices(table.getSelectionModel())[0]);
0551: table.addRowSelectionInterval(2, 2);
0552: assertEquals(2,
0553: getSelectedIndices(table.getSelectionModel()).length);
0554: assertEquals(0,
0555: getSelectedIndices(table.getSelectionModel())[0]);
0556: assertEquals(2,
0557: getSelectedIndices(table.getSelectionModel())[1]);
0558: table.removeRowSelectionInterval(2, 1);
0559: assertEquals(1,
0560: getSelectedIndices(table.getSelectionModel()).length);
0561: assertEquals(0,
0562: getSelectedIndices(table.getSelectionModel())[0]);
0563: testExceptionalCase(new IllegalArgumentCase() {
0564: @Override
0565: public void exceptionalAction() throws Exception {
0566: table.addRowSelectionInterval(4, 4);
0567: }
0568: });
0569: testExceptionalCase(new IllegalArgumentCase() {
0570: @Override
0571: public void exceptionalAction() throws Exception {
0572: table.addRowSelectionInterval(-1, 1);
0573: }
0574: });
0575: testExceptionalCase(new IllegalArgumentCase() {
0576: @Override
0577: public void exceptionalAction() throws Exception {
0578: table.removeRowSelectionInterval(-1, 1);
0579: }
0580: });
0581: testExceptionalCase(new IllegalArgumentCase() {
0582: @Override
0583: public void exceptionalAction() throws Exception {
0584: table.removeRowSelectionInterval(4, 4);
0585: }
0586: });
0587: }
0588:
0589: public void testAddRemoveColumnSelectionInterval() throws Exception {
0590: table = new JTable(3, 4);
0591: assertEquals(0, getSelectedIndices(table.getColumnModel()
0592: .getSelectionModel()).length);
0593: table.addColumnSelectionInterval(0, 0);
0594: assertEquals(1, getSelectedIndices(table.getColumnModel()
0595: .getSelectionModel()).length);
0596: assertEquals(0, getSelectedIndices(table.getColumnModel()
0597: .getSelectionModel())[0]);
0598: table.addColumnSelectionInterval(2, 3);
0599: assertEquals(3, getSelectedIndices(table.getColumnModel()
0600: .getSelectionModel()).length);
0601: assertEquals(0, getSelectedIndices(table.getColumnModel()
0602: .getSelectionModel())[0]);
0603: assertEquals(2, getSelectedIndices(table.getColumnModel()
0604: .getSelectionModel())[1]);
0605: assertEquals(3, getSelectedIndices(table.getColumnModel()
0606: .getSelectionModel())[2]);
0607: table.removeColumnSelectionInterval(3, 3);
0608: assertEquals(2, getSelectedIndices(table.getColumnModel()
0609: .getSelectionModel()).length);
0610: assertEquals(0, getSelectedIndices(table.getColumnModel()
0611: .getSelectionModel())[0]);
0612: assertEquals(2, getSelectedIndices(table.getColumnModel()
0613: .getSelectionModel())[1]);
0614: table.removeColumnSelectionInterval(1, 2);
0615: assertEquals(1, getSelectedIndices(table.getColumnModel()
0616: .getSelectionModel()).length);
0617: assertEquals(0, getSelectedIndices(table.getColumnModel()
0618: .getSelectionModel())[0]);
0619: testExceptionalCase(new IllegalArgumentCase() {
0620: @Override
0621: public void exceptionalAction() throws Exception {
0622: table.addColumnSelectionInterval(4, 4);
0623: }
0624: });
0625: testExceptionalCase(new IllegalArgumentCase() {
0626: @Override
0627: public void exceptionalAction() throws Exception {
0628: table.addColumnSelectionInterval(-1, 1);
0629: }
0630: });
0631: testExceptionalCase(new IllegalArgumentCase() {
0632: @Override
0633: public void exceptionalAction() throws Exception {
0634: table.removeColumnSelectionInterval(4, 4);
0635: }
0636: });
0637: testExceptionalCase(new IllegalArgumentCase() {
0638: @Override
0639: public void exceptionalAction() throws Exception {
0640: table.removeColumnSelectionInterval(-1, 1);
0641: }
0642: });
0643: }
0644:
0645: public void testGetSelectedRow() throws Exception {
0646: table = new JTable(3, 4);
0647: assertEquals(-1, table.getSelectedRow());
0648: table.setRowSelectionInterval(1, 1);
0649: assertEquals(1, table.getSelectedRow());
0650: table.addRowSelectionInterval(2, 2);
0651: assertEquals(1, table.getSelectedRow());
0652: }
0653:
0654: public void testGetSelectedColumn() throws Exception {
0655: table = new JTable(3, 4);
0656: assertEquals(-1, table.getSelectedColumn());
0657: table.setColumnSelectionInterval(1, 1);
0658: assertEquals(1, table.getSelectedColumn());
0659: table.addColumnSelectionInterval(2, 3);
0660: assertEquals(1, table.getSelectedColumn());
0661: }
0662:
0663: public void testGetSelectedRows() throws Exception {
0664: table = new JTable(3, 4);
0665: assertEquals(0, table.getSelectedRows().length);
0666: table.setRowSelectionInterval(1, 1);
0667: assertEquals(1, table.getSelectedRows().length);
0668: assertEquals(1, table.getSelectedRows()[0]);
0669: table.addRowSelectionInterval(2, 2);
0670: assertEquals(2, table.getSelectedRows().length);
0671: assertEquals(1, table.getSelectedRows()[0]);
0672: assertEquals(2, table.getSelectedRows()[1]);
0673: }
0674:
0675: public void testGetSelectedColumns() throws Exception {
0676: table = new JTable(3, 4);
0677: assertEquals(0, table.getSelectedColumns().length);
0678: table.setColumnSelectionInterval(0, 1);
0679: assertEquals(2, table.getSelectedColumns().length);
0680: assertEquals(0, table.getSelectedColumns()[0]);
0681: assertEquals(1, table.getSelectedColumns()[1]);
0682: table.addColumnSelectionInterval(3, 3);
0683: assertEquals(3, table.getSelectedColumns().length);
0684: assertEquals(0, table.getSelectedColumns()[0]);
0685: assertEquals(1, table.getSelectedColumns()[1]);
0686: assertEquals(3, table.getSelectedColumns()[2]);
0687: }
0688:
0689: public void testGetSelectedRowCount() throws Exception {
0690: table = new JTable(3, 4);
0691: assertEquals(0, table.getSelectedRowCount());
0692: table.setRowSelectionInterval(1, 1);
0693: assertEquals(1, table.getSelectedRowCount());
0694: table.addRowSelectionInterval(2, 2);
0695: assertEquals(2, table.getSelectedRowCount());
0696: }
0697:
0698: public void testGetSelectedColumnCount() throws Exception {
0699: table = new JTable(3, 4);
0700: assertEquals(0, table.getSelectedColumnCount());
0701: table.setColumnSelectionInterval(0, 1);
0702: assertEquals(2, table.getSelectedColumnCount());
0703: table.addColumnSelectionInterval(3, 3);
0704: assertEquals(3, table.getSelectedColumnCount());
0705: }
0706:
0707: public void testIsRowSelected() throws Exception {
0708: table = new JTable(3, 4);
0709: assertFalse(table.isRowSelected(0));
0710: assertFalse(table.isRowSelected(1));
0711: assertFalse(table.isRowSelected(2));
0712: table.addRowSelectionInterval(0, 1);
0713: assertTrue(table.isRowSelected(0));
0714: assertTrue(table.isRowSelected(1));
0715: assertFalse(table.isRowSelected(2));
0716: assertFalse(table.isRowSelected(-1));
0717: assertFalse(table.isRowSelected(4));
0718: }
0719:
0720: public void testIsColumnSelected() throws Exception {
0721: table = new JTable(3, 4);
0722: assertFalse(table.isColumnSelected(0));
0723: assertFalse(table.isColumnSelected(1));
0724: assertFalse(table.isColumnSelected(2));
0725: assertFalse(table.isColumnSelected(3));
0726: table.addColumnSelectionInterval(0, 1);
0727: table.addColumnSelectionInterval(3, 3);
0728: assertTrue(table.isColumnSelected(0));
0729: assertTrue(table.isColumnSelected(1));
0730: assertFalse(table.isColumnSelected(2));
0731: assertTrue(table.isColumnSelected(3));
0732: assertFalse(table.isColumnSelected(-1));
0733: assertFalse(table.isColumnSelected(5));
0734: }
0735:
0736: public void testIsCellSelected() throws Exception {
0737: table = new JTable(3, 4);
0738: assertTrue(table.getRowSelectionAllowed());
0739: assertFalse(table.getColumnSelectionAllowed());
0740: assertFalse(table.isCellSelected(0, 0));
0741: assertFalse(table.isCellSelected(0, 1));
0742: assertFalse(table.isCellSelected(0, 2));
0743: assertFalse(table.isCellSelected(0, 3));
0744: assertFalse(table.isCellSelected(1, 0));
0745: assertFalse(table.isCellSelected(1, 1));
0746: assertFalse(table.isCellSelected(1, 2));
0747: assertFalse(table.isCellSelected(1, 3));
0748: assertFalse(table.isCellSelected(2, 0));
0749: assertFalse(table.isCellSelected(2, 1));
0750: assertFalse(table.isCellSelected(2, 2));
0751: assertFalse(table.isCellSelected(2, 3));
0752: table.addRowSelectionInterval(0, 0);
0753: table.addRowSelectionInterval(2, 2);
0754: assertTrue(table.isCellSelected(0, 0));
0755: assertTrue(table.isCellSelected(0, 1));
0756: assertTrue(table.isCellSelected(0, 2));
0757: assertTrue(table.isCellSelected(0, 3));
0758: assertFalse(table.isCellSelected(1, 0));
0759: assertFalse(table.isCellSelected(1, 1));
0760: assertFalse(table.isCellSelected(1, 2));
0761: assertFalse(table.isCellSelected(1, 3));
0762: assertTrue(table.isCellSelected(2, 0));
0763: assertTrue(table.isCellSelected(2, 1));
0764: assertTrue(table.isCellSelected(2, 2));
0765: assertTrue(table.isCellSelected(2, 3));
0766: table.setCellSelectionEnabled(true);
0767: assertFalse(table.isCellSelected(0, 0));
0768: assertFalse(table.isCellSelected(0, 1));
0769: assertFalse(table.isCellSelected(0, 2));
0770: assertFalse(table.isCellSelected(0, 3));
0771: assertFalse(table.isCellSelected(1, 0));
0772: assertFalse(table.isCellSelected(1, 1));
0773: assertFalse(table.isCellSelected(1, 2));
0774: assertFalse(table.isCellSelected(1, 3));
0775: assertFalse(table.isCellSelected(2, 0));
0776: assertFalse(table.isCellSelected(2, 1));
0777: assertFalse(table.isCellSelected(2, 2));
0778: assertFalse(table.isCellSelected(2, 3));
0779: table.addColumnSelectionInterval(1, 1);
0780: table.addColumnSelectionInterval(3, 3);
0781: assertFalse(table.isCellSelected(0, 0));
0782: assertTrue(table.isCellSelected(0, 1));
0783: assertFalse(table.isCellSelected(0, 2));
0784: assertTrue(table.isCellSelected(0, 3));
0785: assertFalse(table.isCellSelected(1, 0));
0786: assertFalse(table.isCellSelected(1, 1));
0787: assertFalse(table.isCellSelected(1, 2));
0788: assertFalse(table.isCellSelected(1, 3));
0789: assertFalse(table.isCellSelected(2, 0));
0790: assertTrue(table.isCellSelected(2, 1));
0791: assertFalse(table.isCellSelected(2, 2));
0792: assertTrue(table.isCellSelected(2, 3));
0793: table.setCellSelectionEnabled(false);
0794: assertFalse(table.isCellSelected(0, 0));
0795: assertFalse(table.isCellSelected(0, 1));
0796: assertFalse(table.isCellSelected(0, 2));
0797: assertFalse(table.isCellSelected(0, 3));
0798: assertFalse(table.isCellSelected(1, 0));
0799: assertFalse(table.isCellSelected(1, 1));
0800: assertFalse(table.isCellSelected(1, 2));
0801: assertFalse(table.isCellSelected(1, 3));
0802: assertFalse(table.isCellSelected(2, 0));
0803: assertFalse(table.isCellSelected(2, 1));
0804: assertFalse(table.isCellSelected(2, 2));
0805: assertFalse(table.isCellSelected(2, 3));
0806: table.setColumnSelectionAllowed(true);
0807: table.setRowSelectionAllowed(false);
0808: assertFalse(table.isCellSelected(0, 0));
0809: assertTrue(table.isCellSelected(0, 1));
0810: assertFalse(table.isCellSelected(0, 2));
0811: assertTrue(table.isCellSelected(0, 3));
0812: assertFalse(table.isCellSelected(1, 0));
0813: assertTrue(table.isCellSelected(1, 1));
0814: assertFalse(table.isCellSelected(1, 2));
0815: assertTrue(table.isCellSelected(1, 3));
0816: assertFalse(table.isCellSelected(2, 0));
0817: assertTrue(table.isCellSelected(2, 1));
0818: assertFalse(table.isCellSelected(2, 2));
0819: assertTrue(table.isCellSelected(2, 3));
0820: table.setColumnSelectionAllowed(false);
0821: table.setRowSelectionAllowed(true);
0822: assertTrue(table.isCellSelected(0, 0));
0823: assertTrue(table.isCellSelected(0, 1));
0824: assertTrue(table.isCellSelected(0, 2));
0825: assertTrue(table.isCellSelected(0, 3));
0826: assertFalse(table.isCellSelected(1, 0));
0827: assertFalse(table.isCellSelected(1, 1));
0828: assertFalse(table.isCellSelected(1, 2));
0829: assertFalse(table.isCellSelected(1, 3));
0830: assertTrue(table.isCellSelected(2, 0));
0831: assertTrue(table.isCellSelected(2, 1));
0832: assertTrue(table.isCellSelected(2, 2));
0833: assertTrue(table.isCellSelected(2, 3));
0834: table.setCellSelectionEnabled(true);
0835: assertFalse(table.isCellSelected(-1, 0));
0836: assertFalse(table.isCellSelected(0, -1));
0837: assertFalse(table.isCellSelected(0, 5));
0838: assertFalse(table.isCellSelected(4, 2));
0839: }
0840:
0841: public void testChangeSelection() throws Exception {
0842: table = new JTable(3, 4);
0843: table.setCellSelectionEnabled(true);
0844: table.changeSelection(1, 1, true, false);
0845: assertTrue(table.isCellSelected(1, 1));
0846: assertFalse(table.isCellSelected(1, 2));
0847: assertFalse(table.isCellSelected(2, 1));
0848: assertFalse(table.isCellSelected(2, 2));
0849: table.changeSelection(2, 2, true, false);
0850: assertTrue(table.isCellSelected(1, 1));
0851: assertTrue(table.isCellSelected(1, 2));
0852: assertTrue(table.isCellSelected(2, 1));
0853: assertTrue(table.isCellSelected(2, 2));
0854: assertEquals(2, table.getSelectionModel()
0855: .getAnchorSelectionIndex());
0856: assertEquals(2, table.getColumnModel().getSelectionModel()
0857: .getAnchorSelectionIndex());
0858: assertEquals(2, table.getSelectionModel()
0859: .getLeadSelectionIndex());
0860: assertEquals(2, table.getColumnModel().getSelectionModel()
0861: .getLeadSelectionIndex());
0862: table.changeSelection(2, 3, true, true);
0863: assertTrue(table.isCellSelected(1, 1));
0864: assertTrue(table.isCellSelected(1, 2));
0865: assertTrue(table.isCellSelected(2, 1));
0866: assertTrue(table.isCellSelected(2, 2));
0867: assertFalse(table.isCellSelected(2, 4));
0868: assertEquals(2, table.getSelectionModel()
0869: .getLeadSelectionIndex());
0870: assertEquals(2, table.getColumnModel().getSelectionModel()
0871: .getLeadSelectionIndex());
0872: assertEquals(2, table.getSelectionModel()
0873: .getAnchorSelectionIndex());
0874: assertEquals(3, table.getColumnModel().getSelectionModel()
0875: .getAnchorSelectionIndex());
0876: table.changeSelection(0, 3, false, true);
0877: assertFalse(table.isCellSelected(1, 1));
0878: assertFalse(table.isCellSelected(1, 2));
0879: assertFalse(table.isCellSelected(2, 1));
0880: assertFalse(table.isCellSelected(2, 2));
0881: assertTrue(table.isCellSelected(1, 3));
0882: assertTrue(table.isCellSelected(1, 3));
0883: assertTrue(table.isCellSelected(2, 3));
0884: assertTrue(table.isCellSelected(2, 3));
0885: assertFalse(table.isCellSelected(0, 4));
0886: assertFalse(table.isCellSelected(1, 4));
0887: assertFalse(table.isCellSelected(2, 4));
0888: table.changeSelection(1, 2, false, true);
0889: assertFalse(table.isCellSelected(1, 1));
0890: assertTrue(table.isCellSelected(1, 2));
0891: table.changeSelection(1, 1, true, false);
0892: assertTrue(table.isCellSelected(1, 1));
0893: table.changeSelection(1, 1, true, false);
0894: assertFalse(table.isCellSelected(1, 1));
0895: table.changeSelection(2, 3, false, false);
0896: assertTrue(table.isCellSelected(2, 3));
0897: assertEquals(1, table.getSelectedRowCount());
0898: assertEquals(1, table.getSelectedColumnCount());
0899: }
0900:
0901: public void testGetSetSelectionForegroundBackground()
0902: throws Exception {
0903: assertEquals(UIManager.getColor("Table.selectionForeground"),
0904: table.getSelectionForeground());
0905: assertEquals(UIManager.getColor("Table.selectionBackground"),
0906: table.getSelectionBackground());
0907: table.setSelectionForeground(Color.BLUE);
0908: assertTrue(propertyChangeController
0909: .isChanged("selectionForeground"));
0910: propertyChangeController.reset();
0911: table.setSelectionBackground(Color.RED);
0912: assertTrue(propertyChangeController
0913: .isChanged("selectionBackground"));
0914: assertEquals(Color.BLUE, table.getSelectionForeground());
0915: assertEquals(Color.RED, table.getSelectionBackground());
0916: }
0917:
0918: public void testGetColumn() throws Exception {
0919: table = new JTable(3, 4);
0920: assertNotNull(table.getColumn("A"));
0921: assertNotNull(table.getColumn("B"));
0922: assertNotNull(table.getColumn("C"));
0923: assertNotNull(table.getColumn("D"));
0924: table.getColumn("C").setIdentifier("ANY");
0925: assertNotNull(table.getColumn("ANY"));
0926: testExceptionalCase(new IllegalArgumentCase() {
0927: @Override
0928: public void exceptionalAction() throws Exception {
0929: assertNull(table.getColumn("C"));
0930: }
0931: });
0932: testExceptionalCase(new IllegalArgumentCase() {
0933: @Override
0934: public void exceptionalAction() throws Exception {
0935: assertNull(table.getColumn("E"));
0936: }
0937: });
0938: }
0939:
0940: public void testConvertColumnIndexToModel() throws Exception {
0941: assertEquals(-5, table.convertColumnIndexToModel(-5));
0942: table = new JTable(3, 4);
0943: assertEquals(0, table.convertColumnIndexToModel(0));
0944: assertEquals(1, table.convertColumnIndexToModel(1));
0945: assertEquals(2, table.convertColumnIndexToModel(2));
0946: assertEquals(3, table.convertColumnIndexToModel(3));
0947: table.moveColumn(0, 2);
0948: assertEquals(1, table.convertColumnIndexToModel(0));
0949: assertEquals(2, table.convertColumnIndexToModel(1));
0950: assertEquals(0, table.convertColumnIndexToModel(2));
0951: assertEquals(3, table.convertColumnIndexToModel(3));
0952: table.getColumnModel().getColumn(0).setModelIndex(-5);
0953: table.getColumnModel().getColumn(1).setModelIndex(-5);
0954: table.getColumnModel().getColumn(2).setModelIndex(-5);
0955: table.getColumnModel().getColumn(3).setModelIndex(-5);
0956: assertEquals(-5, table.convertColumnIndexToModel(0));
0957: assertEquals(-5, table.convertColumnIndexToModel(1));
0958: assertEquals(-5, table.convertColumnIndexToModel(2));
0959: assertEquals(-5, table.convertColumnIndexToModel(3));
0960: testExceptionalCase(new ExceptionalCase() {
0961: @Override
0962: public void exceptionalAction() throws Exception {
0963: table.convertColumnIndexToModel(100);
0964: }
0965: });
0966: }
0967:
0968: public void testConvertColumnIndexToView() throws Exception {
0969: assertEquals(-5, table.convertColumnIndexToView(-5));
0970: assertEquals(-1, table.convertColumnIndexToView(10));
0971: table = new JTable(3, 4);
0972: assertEquals(0, table.convertColumnIndexToView(0));
0973: assertEquals(1, table.convertColumnIndexToView(1));
0974: assertEquals(2, table.convertColumnIndexToView(2));
0975: assertEquals(3, table.convertColumnIndexToView(3));
0976: table.moveColumn(0, 2);
0977: assertEquals(2, table.convertColumnIndexToView(0));
0978: assertEquals(0, table.convertColumnIndexToView(1));
0979: assertEquals(1, table.convertColumnIndexToView(2));
0980: assertEquals(3, table.convertColumnIndexToView(3));
0981: table.getColumnModel().getColumn(0).setModelIndex(-5);
0982: table.getColumnModel().getColumn(1).setModelIndex(-5);
0983: table.getColumnModel().getColumn(2).setModelIndex(-5);
0984: table.getColumnModel().getColumn(3).setModelIndex(-5);
0985: assertEquals(-1, table.convertColumnIndexToView(0));
0986: assertEquals(-1, table.convertColumnIndexToView(1));
0987: assertEquals(-1, table.convertColumnIndexToView(2));
0988: assertEquals(-1, table.convertColumnIndexToView(3));
0989: }
0990:
0991: public void testGetRowCount() throws Exception {
0992: assertEquals(0, table.getRowCount());
0993: table = new JTable(3, 4);
0994: assertEquals(3, table.getRowCount());
0995: table = new JTable(new DefaultTableModel() {
0996: @Override
0997: public int getRowCount() {
0998: return 10;
0999: }
1000: });
1001: assertEquals(10, table.getRowCount());
1002: }
1003:
1004: public void testGetColumnCount() throws Exception {
1005: assertEquals(0, table.getColumnCount());
1006: table = new JTable(3, 4);
1007: assertEquals(4, table.getColumnCount());
1008: table = new JTable(new DefaultTableModel() {
1009: @Override
1010: public int getColumnCount() {
1011: return 10;
1012: }
1013: });
1014: assertEquals(10, table.getColumnCount());
1015: }
1016:
1017: public void testGetColumnName() throws Exception {
1018: table = new JTable(3, 4);
1019: assertEquals("A", table.getColumnName(0));
1020: assertEquals("B", table.getColumnName(1));
1021: assertEquals("C", table.getColumnName(2));
1022: assertEquals("D", table.getColumnName(3));
1023: table.moveColumn(0, 2);
1024: assertEquals("B", table.getColumnName(0));
1025: assertEquals("C", table.getColumnName(1));
1026: assertEquals("A", table.getColumnName(2));
1027: assertEquals("D", table.getColumnName(3));
1028: testExceptionalCase(new ExceptionalCase() {
1029: @Override
1030: public void exceptionalAction() throws Exception {
1031: table.getColumnName(-1);
1032: }
1033: });
1034: testExceptionalCase(new ExceptionalCase() {
1035: @Override
1036: public void exceptionalAction() throws Exception {
1037: table.getColumnName(100);
1038: }
1039: });
1040: }
1041:
1042: public void testGetColumnClass() throws Exception {
1043: table = new JTable(new DefaultTableModel(3, 4) {
1044: @Override
1045: public Class<?> getColumnClass(final int columnIndex) {
1046: return columnIndex < 2 ? Object.class : String.class;
1047: }
1048: });
1049: assertEquals(Object.class, table.getColumnClass(0));
1050: assertEquals(Object.class, table.getColumnClass(1));
1051: assertEquals(String.class, table.getColumnClass(2));
1052: assertEquals(String.class, table.getColumnClass(3));
1053: table.moveColumn(0, 2);
1054: assertEquals(Object.class, table.getColumnClass(0));
1055: assertEquals(String.class, table.getColumnClass(1));
1056: assertEquals(Object.class, table.getColumnClass(2));
1057: assertEquals(String.class, table.getColumnClass(3));
1058: assertEquals(Object.class, table.getColumnClass(-1));
1059: testExceptionalCase(new ExceptionalCase() {
1060: @Override
1061: public void exceptionalAction() throws Exception {
1062: table.getColumnName(100);
1063: }
1064: });
1065: }
1066:
1067: public void testGetSetvalueAt() throws Exception {
1068: table = new JTable(3, 4);
1069: assertNull(table.getValueAt(0, 0));
1070: assertNull(table.getValueAt(0, 1));
1071: assertNull(table.getValueAt(0, 2));
1072: assertNull(table.getValueAt(0, 3));
1073: table.setValueAt("a", 0, 0);
1074: table.setValueAt("b", 0, 1);
1075: table.setValueAt("c", 0, 2);
1076: table.setValueAt("d", 0, 3);
1077: assertEquals("a", table.getValueAt(0, 0));
1078: assertEquals("b", table.getValueAt(0, 1));
1079: assertEquals("c", table.getValueAt(0, 2));
1080: assertEquals("d", table.getValueAt(0, 3));
1081: table.moveColumn(0, 2);
1082: assertEquals("b", table.getValueAt(0, 0));
1083: assertEquals("c", table.getValueAt(0, 1));
1084: assertEquals("a", table.getValueAt(0, 2));
1085: assertEquals("d", table.getValueAt(0, 3));
1086: table.setValueAt("another a", 0, 2);
1087: assertEquals("another a", table.getValueAt(0, 2));
1088: }
1089:
1090: public void testIsCellEditable() throws Exception {
1091: table = new JTable(new DefaultTableModel(3, 4) {
1092: @Override
1093: public boolean isCellEditable(final int row,
1094: final int column) {
1095: return column < 2;
1096: }
1097: });
1098: assertTrue(table.isCellEditable(0, 0));
1099: assertTrue(table.isCellEditable(0, 1));
1100: assertFalse(table.isCellEditable(0, 2));
1101: assertFalse(table.isCellEditable(0, 3));
1102: table.moveColumn(0, 2);
1103: assertTrue(table.isCellEditable(0, 0));
1104: assertFalse(table.isCellEditable(0, 1));
1105: assertTrue(table.isCellEditable(0, 2));
1106: assertFalse(table.isCellEditable(0, 3));
1107: assertTrue(table.isCellEditable(100, -1));
1108: testExceptionalCase(new ExceptionalCase() {
1109: @Override
1110: public void exceptionalAction() throws Exception {
1111: table.isCellEditable(0, 100);
1112: }
1113: });
1114: }
1115:
1116: public void testAddColumn() throws Exception {
1117: assertEquals(0, table.getColumnCount());
1118: TableColumn column1 = new TableColumn(0);
1119: column1.setIdentifier("column1");
1120: table.addColumn(column1);
1121: assertEquals(1, table.getColumnCount());
1122: assertEquals(0, table.getModel().getColumnCount());
1123: assertSame(column1, table.getColumn("column1"));
1124: assertEquals(0, table.getColumnModel()
1125: .getColumnIndex("column1"));
1126: TableColumn column2 = new TableColumn(1);
1127: table.addColumn(column2);
1128: assertEquals(2, table.getColumnCount());
1129: assertSame(column2, table.getColumn("B"));
1130: assertEquals(1, table.getColumnModel().getColumnIndex("B"));
1131: TableColumn column3 = new TableColumn(20);
1132: table.addColumn(column3);
1133: assertEquals(3, table.getColumnCount());
1134: assertSame(column3, table.getColumn("U"));
1135: assertEquals(2, table.getColumnModel().getColumnIndex("U"));
1136: TableColumn column31 = new TableColumn(20);
1137: table.addColumn(column31);
1138: assertEquals(4, table.getColumnCount());
1139: assertSame(column3, table.getColumn("U"));
1140: assertEquals(2, table.getColumnModel().getColumnIndex("U"));
1141: assertEquals(2, table.getColumnModel().getColumnIndex("U"));
1142: assertEquals("U", table.getColumnModel().getColumn(2)
1143: .getIdentifier());
1144: assertEquals("U", table.getColumnModel().getColumn(3)
1145: .getIdentifier());
1146: TableColumn column5 = new TableColumn();
1147: column5.setIdentifier("column1");
1148: assertNull(column5.getHeaderValue());
1149: table.addColumn(column5);
1150: assertEquals("A", column5.getHeaderValue());
1151: TableColumn column6 = new TableColumn();
1152: column6.setIdentifier("column1");
1153: column6.setHeaderValue("header value");
1154: table.addColumn(column6);
1155: assertEquals("header value", column6.getHeaderValue());
1156: }
1157:
1158: public void testRemoveColumn() throws Exception {
1159: assertEquals(0, table.getColumnCount());
1160: TableColumn column1 = new TableColumn(0);
1161: column1.setIdentifier("column1");
1162: table.addColumn(column1);
1163: assertEquals(1, table.getColumnCount());
1164: assertSame(column1, table.getColumn("column1"));
1165: assertEquals(0, table.getColumnModel()
1166: .getColumnIndex("column1"));
1167: TableColumn column2 = new TableColumn(1);
1168: table.addColumn(column2);
1169: assertEquals(2, table.getColumnCount());
1170: assertSame(column2, table.getColumn("B"));
1171: assertEquals(1, table.getColumnModel().getColumnIndex("B"));
1172: table.removeColumn(column1);
1173: assertEquals(1, table.getColumnCount());
1174: table.removeColumn(new TableColumn(1));
1175: assertEquals(1, table.getColumnCount());
1176: }
1177:
1178: public void testMoveColumn() throws Exception {
1179: TableColumn column1 = new TableColumn(0);
1180: table.addColumn(column1);
1181: assertEquals(0, table.getColumnModel().getColumnIndex("A"));
1182: TableColumn column2 = new TableColumn(1);
1183: table.addColumn(column2);
1184: assertEquals(1, table.getColumnModel().getColumnIndex("B"));
1185: table.moveColumn(0, 1);
1186: assertEquals(0, table.getColumnModel().getColumnIndex("B"));
1187: assertEquals(1, table.getColumnModel().getColumnIndex("A"));
1188: }
1189:
1190: public void testColumnAtPoint() throws Exception {
1191: assertEquals(-1, table.columnAtPoint(new Point(10, 10)));
1192: assertEquals(-1, table.columnAtPoint(new Point(-10, 10)));
1193: table.addColumn(new TableColumn(0, 20));
1194: table.addColumn(new TableColumn(1, 30));
1195: assertEquals(0, table.columnAtPoint(new Point(10, 100)));
1196: assertEquals(0, table.columnAtPoint(new Point(19, 100)));
1197: assertEquals(1, table.columnAtPoint(new Point(20, 100)));
1198: assertEquals(1, table.columnAtPoint(new Point(49, 100)));
1199: table.moveColumn(0, 1);
1200: assertEquals(0, table.columnAtPoint(new Point(10, 100)));
1201: assertEquals(0, table.columnAtPoint(new Point(29, 100)));
1202: assertEquals(1, table.columnAtPoint(new Point(30, 100)));
1203: assertEquals(1, table.columnAtPoint(new Point(49, 100)));
1204: }
1205:
1206: public void testRowAtPoint() throws Exception {
1207: assertEquals(-1, table.rowAtPoint(new Point(10, 10)));
1208: assertEquals(-1, table.rowAtPoint(new Point(10, -10)));
1209: table = new JTable(3, 4);
1210: table.setRowHeight(10);
1211: table.setRowHeight(1, 30);
1212: assertEquals(0, table.rowAtPoint(new Point(1000, 0)));
1213: assertEquals(0, table.rowAtPoint(new Point(1000, 9)));
1214: assertEquals(1, table.rowAtPoint(new Point(1000, 10)));
1215: assertEquals(1, table.rowAtPoint(new Point(1000, 39)));
1216: assertEquals(2, table.rowAtPoint(new Point(1000, 40)));
1217: assertEquals(2, table.rowAtPoint(new Point(1000, 49)));
1218: if (isHarmony()) {
1219: assertEquals(-1, table.rowAtPoint(new Point(10, -100)));
1220: } else {
1221: assertEquals(0, table.rowAtPoint(new Point(10, -100)));
1222: }
1223: assertEquals(-1, table.rowAtPoint(new Point(10, 50)));
1224: }
1225:
1226: public void testGetCellRect() throws Exception {
1227: assertEquals(new Rectangle(), table.getCellRect(0, 0, true));
1228: assertEquals(new Rectangle(), table.getCellRect(0, 0, false));
1229: assertEquals(new Rectangle(), table.getCellRect(10, 10, true));
1230: assertEquals(new Rectangle(), table.getCellRect(-10, -10, true));
1231: table = new JTable(3, 4);
1232: table.setIntercellSpacing(new Dimension(12, 10));
1233: assertEquals(new Rectangle(0, 0, 75, 16), table.getCellRect(0,
1234: 0, true));
1235: assertEquals(new Rectangle(6, 5, 63, 6), table.getCellRect(0,
1236: 0, false));
1237: assertEquals(new Rectangle(75, 16, 75, 16), table.getCellRect(
1238: 1, 1, true));
1239: assertEquals(new Rectangle(81, 21, 63, 6), table.getCellRect(1,
1240: 1, false));
1241: assertEquals(new Rectangle(225, 32, 75, 16), table.getCellRect(
1242: 2, 3, true));
1243: assertEquals(new Rectangle(231, 37, 63, 6), table.getCellRect(
1244: 2, 3, false));
1245: assertEquals(new Rectangle(0, 32, 0, 16), table.getCellRect(2,
1246: 4, true));
1247: assertEquals(new Rectangle(0, 32, 0, 16), table.getCellRect(2,
1248: 4, false));
1249: assertEquals(new Rectangle(0, 32, 0, 16), table.getCellRect(2,
1250: -1, true));
1251: assertEquals(new Rectangle(0, 32, 0, 16), table.getCellRect(2,
1252: -1, false));
1253: assertEquals(new Rectangle(225, 0, 75, 0), table.getCellRect(3,
1254: 3, true));
1255: assertEquals(new Rectangle(225, 0, 75, 0), table.getCellRect(3,
1256: 3, false));
1257: assertEquals(new Rectangle(225, 0, 75, 0), table.getCellRect(
1258: -1, 3, true));
1259: assertEquals(new Rectangle(225, 0, 75, 0), table.getCellRect(
1260: -1, 3, false));
1261: }
1262:
1263: public void testGetToolTipText() throws Exception {
1264: assertNull(table.getToolTipText(new MouseEvent(table,
1265: MouseEvent.MOUSE_ENTERED, 0, 0, 0, 0, 0, false)));
1266: table = new JTable(3, 4) {
1267: @Override
1268: public TableCellRenderer getCellRenderer(final int row,
1269: final int column) {
1270: DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) getDefaultRenderer(Object.class);
1271: renderer
1272: .setToolTipText("[" + row + ", " + column + "]");
1273: return renderer;
1274: }
1275: };
1276: assertNull(table.getToolTipText(new MouseEvent(table,
1277: MouseEvent.MOUSE_ENTERED, 0, 0, -10, -10, 0, false)));
1278: assertNull(table.getToolTipText(new MouseEvent(table,
1279: MouseEvent.MOUSE_ENTERED, 0, 0, 800, 20, 0, false)));
1280: assertEquals("[0, 0]", table.getToolTipText(new MouseEvent(
1281: table, MouseEvent.MOUSE_ENTERED, 0, 0, 0, 0, 0, false)));
1282: assertEquals("[0, 0]", table
1283: .getToolTipText(new MouseEvent(table,
1284: MouseEvent.MOUSE_ENTERED, 0, 0, 30, 10, 0,
1285: false)));
1286: assertEquals("[1, 0]", table
1287: .getToolTipText(new MouseEvent(table,
1288: MouseEvent.MOUSE_ENTERED, 0, 0, 30, 20, 0,
1289: false)));
1290: assertEquals("[1, 1]", table
1291: .getToolTipText(new MouseEvent(table,
1292: MouseEvent.MOUSE_ENTERED, 0, 0, 80, 20, 0,
1293: false)));
1294: }
1295:
1296: public void testGetSetSurrendersFocusOnKeystroke() throws Exception {
1297: assertFalse(table.getSurrendersFocusOnKeystroke());
1298: table.setSurrendersFocusOnKeystroke(true);
1299: assertTrue(table.getSurrendersFocusOnKeystroke());
1300: assertFalse(propertyChangeController.isChanged());
1301: }
1302:
1303: @SuppressWarnings("deprecation")
1304: public void testEditCellAt() throws Exception {
1305: assertFalse(table.editCellAt(0, 0));
1306: assertFalse(table.isEditing());
1307: assertEquals(-1, table.getEditingRow());
1308: assertEquals(-1, table.getEditingColumn());
1309: assertNull(table.getCellEditor());
1310: assertNull(table.getEditorComponent());
1311: table = new JTable(3, 4);
1312: assertTrue(table.editCellAt(0, 0));
1313: assertTrue(table.isEditing());
1314: assertNotNull(table.getEditorComponent());
1315: assertEquals(0, table.getEditingRow());
1316: assertEquals(0, table.getEditingColumn());
1317: assertTrue(table.editCellAt(1, 2, new KeyEvent(table,
1318: KeyEvent.KEY_PRESSED, 0, 0, 0)));
1319: assertTrue(table.isEditing());
1320: assertNotNull(table.getEditorComponent());
1321: assertEquals(1, table.getEditingRow());
1322: assertEquals(2, table.getEditingColumn());
1323: assertNotNull(table.getCellEditor());
1324: assertFalse(table.editCellAt(0, 0, new MouseEvent(table,
1325: MouseEvent.MOUSE_ENTERED, 0, 0, 0, 0, 0, false)));
1326: assertFalse(table.isEditing());
1327: assertEquals(-1, table.getEditingRow());
1328: assertEquals(-1, table.getEditingColumn());
1329: assertNull(table.getCellEditor());
1330: assertNull(table.getEditorComponent());
1331: table = new JTable(new DefaultTableModel() {
1332: @Override
1333: public boolean isCellEditable(final int row,
1334: final int column) {
1335: return false;
1336: }
1337: });
1338: assertFalse(table.editCellAt(1, 2));
1339: assertFalse(table.isEditing());
1340: }
1341:
1342: public void testIsEditing() throws Exception {
1343: assertFalse(table.isEditing());
1344: table.cellEditor = new DefaultCellEditor(new JCheckBox());
1345: assertTrue(table.isEditing());
1346: }
1347:
1348: public void testGetEditingComponent() throws Exception {
1349: assertNull(table.getEditorComponent());
1350: table.editorComp = new JLabel();
1351: assertEquals(table.editorComp, table.getEditorComponent());
1352: }
1353:
1354: public void testGetSetEditingRowColumn() throws Exception {
1355: assertEquals(-1, table.getEditingRow());
1356: assertEquals(-1, table.getEditingColumn());
1357: table.editingRow = 25;
1358: table.editingColumn = -5;
1359: assertEquals(25, table.getEditingRow());
1360: assertEquals(-5, table.getEditingColumn());
1361: table.setEditingRow(15);
1362: assertEquals(15, table.getEditingRow());
1363: assertFalse(propertyChangeController.isChanged());
1364: }
1365:
1366: public void testGetSetUpdateUI() throws Exception {
1367: assertTrue(table.getUI() instanceof BasicTableUI);
1368: TableUI ui = new BasicTableUI();
1369: table.setUI(ui);
1370: assertSame(ui, table.getUI());
1371: table.updateUI();
1372: assertNotSame(ui, table.getUI());
1373: }
1374:
1375: public void testGetUIClassID() throws Exception {
1376: assertEquals("TableUI", table.getUIClassID());
1377: }
1378:
1379: public void testGetSetModel() throws Exception {
1380: DefaultTableModel oldModel = (DefaultTableModel) table
1381: .getModel();
1382: assertNotNull(oldModel);
1383: assertEquals(1, oldModel.getTableModelListeners().length);
1384: assertEquals(table, oldModel.getTableModelListeners()[0]);
1385: DefaultTableModel model = new DefaultTableModel(3, 4);
1386: table.setModel(model);
1387: assertEquals(0, oldModel.getTableModelListeners().length);
1388: assertEquals(1, model.getTableModelListeners().length);
1389: assertEquals(4, table.getColumnModel().getColumnCount());
1390: assertTrue(propertyChangeController.isChanged("model"));
1391: table.setAutoCreateColumnsFromModel(false);
1392: table.setModel(new DefaultTableModel(1, 2));
1393: assertEquals(4, table.getColumnModel().getColumnCount());
1394: testExceptionalCase(new IllegalArgumentCase() {
1395: @Override
1396: public void exceptionalAction() throws Exception {
1397: table.setModel(null);
1398: }
1399: });
1400: }
1401:
1402: public void testGetSetColumnModel() throws Exception {
1403: table = new JTable(3, 4);
1404: DefaultTableColumnModel oldModel = (DefaultTableColumnModel) table
1405: .getColumnModel();
1406: assertNotNull(oldModel);
1407: assertEquals(2, oldModel.getColumnModelListeners().length);
1408: assertEquals(table.getTableHeader(), oldModel
1409: .getColumnModelListeners()[0]);
1410: assertEquals(table, oldModel.getColumnModelListeners()[1]);
1411: DefaultTableColumnModel model = new DefaultTableColumnModel();
1412: table.setColumnModel(model);
1413: assertEquals(0, oldModel.getColumnModelListeners().length);
1414: assertEquals(2, model.getColumnModelListeners().length);
1415: assertEquals(0, table.getColumnModel().getColumnCount());
1416: assertFalse(propertyChangeController.isChanged());
1417: testExceptionalCase(new IllegalArgumentCase() {
1418: @Override
1419: public void exceptionalAction() throws Exception {
1420: table.setColumnModel(null);
1421: }
1422: });
1423: }
1424:
1425: public void testGetSetSelectionModel() throws Exception {
1426: DefaultListSelectionModel oldModel = (DefaultListSelectionModel) table
1427: .getSelectionModel();
1428: assertNotNull(oldModel);
1429: assertEquals(1, oldModel.getListSelectionListeners().length);
1430: assertEquals(table, oldModel.getListSelectionListeners()[0]);
1431: DefaultListSelectionModel model = new DefaultListSelectionModel();
1432: table.setSelectionModel(model);
1433: assertEquals(0, oldModel.getListSelectionListeners().length);
1434: assertEquals(1, model.getListSelectionListeners().length);
1435: assertTrue(propertyChangeController.isChanged("selectionModel"));
1436: testExceptionalCase(new IllegalArgumentCase() {
1437: @Override
1438: public void exceptionalAction() throws Exception {
1439: table.setSelectionModel(null);
1440: }
1441: });
1442: }
1443:
1444: public void testEditingCanceled() throws Exception {
1445: table = new JTable(3, 4);
1446: table.editCellAt(0, 0);
1447: assertTrue(table.isEditing());
1448: assertNotNull(table.getCellEditor());
1449: assertNotNull(table.getEditorComponent());
1450: assertEquals(0, table.getEditingRow());
1451: assertEquals(0, table.getEditingColumn());
1452: table.setCellEditor(new DefaultCellEditor((JTextField) table
1453: .getEditorComponent()));
1454: ((JTextField) table.getEditorComponent()).setText("new value");
1455: table.editingCanceled(null);
1456: assertFalse(table.isEditing());
1457: assertNull(table.getValueAt(0, 0));
1458: }
1459:
1460: public void testEditingStoppped() throws Exception {
1461: table = new JTable(3, 4);
1462: table.editCellAt(0, 0);
1463: assertTrue(table.isEditing());
1464: assertNotNull(table.getCellEditor());
1465: assertNotNull(table.getEditorComponent());
1466: assertEquals(0, table.getEditingRow());
1467: assertEquals(0, table.getEditingColumn());
1468: table.setCellEditor(new DefaultCellEditor((JTextField) table
1469: .getEditorComponent()));
1470: ((JTextField) table.getEditorComponent()).setText("new value");
1471: table.editingStopped(null);
1472: assertFalse(table.isEditing());
1473: assertEquals("new value", table.getValueAt(0, 0));
1474: }
1475:
1476: public void testGetSetPreferredScrollableViewportSize()
1477: throws Exception {
1478: assertEquals(new Dimension(450, 400), table
1479: .getPreferredScrollableViewportSize());
1480: table = new JTable(3, 4);
1481: assertEquals(new Dimension(450, 400), table
1482: .getPreferredScrollableViewportSize());
1483: table = new JTable(300, 400);
1484: assertEquals(new Dimension(450, 400), table
1485: .getPreferredScrollableViewportSize());
1486: table.setBorder(BorderFactory.createLineBorder(Color.RED, 20));
1487: assertEquals(new Dimension(450, 400), table
1488: .getPreferredScrollableViewportSize());
1489: Dimension size = new Dimension(200, 300);
1490: table.setPreferredScrollableViewportSize(size);
1491: assertSame(size, table.getPreferredScrollableViewportSize());
1492: assertFalse(propertyChangeController.isChanged());
1493: }
1494:
1495: //TODO
1496: public void testGetScrollableUnitIncrement() throws Exception {
1497: }
1498:
1499: //TODO
1500: public void testGetScrollableBlockIncrement() throws Exception {
1501: }
1502:
1503: public void testGetScrollableTracksViewportWidth() throws Exception {
1504: assertTrue(table.getScrollableTracksViewportWidth());
1505: table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
1506: assertFalse(table.getScrollableTracksViewportWidth());
1507: }
1508:
1509: public void testGetScrollableTracksViewportHeight()
1510: throws Exception {
1511: assertFalse(table.getScrollableTracksViewportHeight());
1512: }
1513:
1514: public void testCreateDefaultRenderers() throws Exception {
1515: assertEquals(8, table.defaultRenderersByColumnClass.size());
1516: table.defaultRenderersByColumnClass = null;
1517: table.createDefaultRenderers();
1518: assertEquals(8, table.defaultRenderersByColumnClass.size());
1519: }
1520:
1521: public void testCreateDefaultEditors() throws Exception {
1522: assertEquals(3, table.defaultEditorsByColumnClass.size());
1523: table.defaultEditorsByColumnClass = null;
1524: table.createDefaultEditors();
1525: assertEquals(3, table.defaultEditorsByColumnClass.size());
1526: }
1527:
1528: public void testInitializeLocalVars() throws Exception {
1529: table.rowMargin = 0;
1530: table.tableHeader = null;
1531: table.rowHeight = 0;
1532: table.showHorizontalLines = false;
1533: table.showVerticalLines = false;
1534: table.autoResizeMode = 100;
1535: table.preferredViewportSize = null;
1536: table.rowSelectionAllowed = false;
1537: table.defaultEditorsByColumnClass = null;
1538: table.defaultRenderersByColumnClass = null;
1539: table.initializeLocalVars();
1540: assertEquals(1, table.rowMargin);
1541: assertNotNull(table.tableHeader);
1542: assertEquals(16, table.rowHeight);
1543: assertTrue(table.showHorizontalLines);
1544: assertTrue(table.showVerticalLines);
1545: assertEquals(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS,
1546: table.autoResizeMode);
1547: assertEquals(new Dimension(450, 400),
1548: table.preferredViewportSize);
1549: assertTrue(table.rowSelectionAllowed);
1550: assertEquals(3, table.defaultEditorsByColumnClass.size());
1551: assertEquals(8, table.defaultRenderersByColumnClass.size());
1552: }
1553:
1554: public void testCreateDefaultDataModel() throws Exception {
1555: assertSame(DefaultTableModel.class, table
1556: .createDefaultDataModel().getClass());
1557: assertNotSame(table.createDefaultDataModel(), table
1558: .createDefaultDataModel());
1559: }
1560:
1561: public void testCreateDefaultColumnModel() throws Exception {
1562: assertSame(DefaultTableColumnModel.class, table
1563: .createDefaultColumnModel().getClass());
1564: assertNotSame(table.createDefaultColumnModel(), table
1565: .createDefaultColumnModel());
1566: }
1567:
1568: public void testCreateDefaultSelectionModel() throws Exception {
1569: assertSame(DefaultListSelectionModel.class, table
1570: .createDefaultSelectionModel().getClass());
1571: assertNotSame(table.createDefaultSelectionModel(), table
1572: .createDefaultSelectionModel());
1573: }
1574:
1575: public void testCreateDefaultTableHeader() throws Exception {
1576: assertSame(JTableHeader.class, table.createDefaultTableHeader()
1577: .getClass());
1578: assertNotSame(table.createDefaultTableHeader(), table
1579: .createDefaultTableHeader());
1580: }
1581:
1582: public void testGetSetCellEditor() throws Exception {
1583: assertNull(table.getCellEditor());
1584: TableCellEditor editor = new DefaultCellEditor(new JTextField());
1585: table.setCellEditor(editor);
1586: assertSame(editor, table.getCellEditor());
1587: }
1588:
1589: public void testGetCellRenderer() throws Exception {
1590: table = new JTable(3, 4);
1591: assertEquals(table.defaultRenderersByColumnClass
1592: .get(Object.class), table.getCellRenderer(0, 0));
1593: TableCellRenderer renderer = new DefaultTableCellRenderer();
1594: table.getColumnModel().getColumn(0).setCellRenderer(renderer);
1595: assertEquals(renderer, table.getCellRenderer(0, 0));
1596: assertEquals(renderer, table.getCellRenderer(1, 0));
1597: assertEquals(renderer, table.getCellRenderer(2, 0));
1598: table.moveColumn(0, 2);
1599: assertEquals(renderer, table.getCellRenderer(0, 2));
1600: assertEquals(renderer, table.getCellRenderer(1, 2));
1601: assertEquals(renderer, table.getCellRenderer(2, 2));
1602: testExceptionalCase(new ExceptionalCase() {
1603: @Override
1604: public void exceptionalAction() throws Exception {
1605: table.getCellRenderer(100, 100);
1606: }
1607: });
1608: }
1609:
1610: public void testPrepareRenderer() throws Exception {
1611: table = new JTable(3, 4);
1612: DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
1613: assertSame(renderer, table.prepareRenderer(renderer, 0, 0));
1614: assertEquals(table.getBackground(), renderer.getBackground());
1615: assertEquals(table.getForeground(), renderer.getForeground());
1616: table.getSelectionModel().setSelectionInterval(0, 0);
1617: table.prepareRenderer(renderer, 0, 0);
1618: assertEquals(table.getSelectionBackground(), renderer
1619: .getBackground());
1620: assertEquals(table.getSelectionForeground(), renderer
1621: .getForeground());
1622: }
1623:
1624: public void testPrepareEditor() throws Exception {
1625: table = new JTable(3, 4);
1626: JTextField editorComponent = new JTextField();
1627: DefaultCellEditor editor = new DefaultCellEditor(
1628: editorComponent);
1629: table.setCellEditor(editor);
1630: table.editorComp = editorComponent;
1631: assertSame(editorComponent, table.prepareEditor(editor, 0, 0));
1632: assertNull(editorComponent.getParent());
1633: assertEquals(new Rectangle(), editorComponent.getBounds());
1634: }
1635:
1636: public void testGetCellEditor() throws Exception {
1637: table = new JTable(3, 4);
1638: assertEquals(table.defaultEditorsByColumnClass
1639: .get(Object.class), table.getCellEditor(0, 0));
1640: TableCellEditor editor = new DefaultCellEditor(new JTextField());
1641: table.getColumnModel().getColumn(0).setCellEditor(editor);
1642: assertEquals(editor, table.getCellEditor(0, 0));
1643: assertEquals(editor, table.getCellEditor(1, 0));
1644: assertEquals(editor, table.getCellEditor(2, 0));
1645: table.moveColumn(0, 2);
1646: assertEquals(editor, table.getCellEditor(0, 2));
1647: assertEquals(editor, table.getCellEditor(1, 2));
1648: assertEquals(editor, table.getCellEditor(2, 2));
1649: testExceptionalCase(new ExceptionalCase() {
1650: @Override
1651: public void exceptionalAction() throws Exception {
1652: table.getCellEditor(100, 100);
1653: }
1654: });
1655: }
1656:
1657: public void testRemoveEditor() throws Exception {
1658: table.removeEditor();
1659: int childrenCount = table.getComponentCount();
1660: table.setCellEditor(new DefaultCellEditor(new JTextField()));
1661: table.editorComp = new JTextField();
1662: table.add(table.editorComp);
1663: table.setEditingColumn(10);
1664: table.setEditingRow(10);
1665: assertEquals(childrenCount + 1, table.getComponentCount());
1666: table.removeEditor();
1667: assertNull(table.getCellEditor());
1668: assertNull(table.getEditorComponent());
1669: assertEquals(childrenCount, table.getComponentCount());
1670: assertEquals(-1, table.getEditingColumn());
1671: assertEquals(-1, table.getEditingRow());
1672: }
1673:
1674: private int[] getSelectedIndices(final ListSelectionModel selModel) {
1675: int count = 0;
1676: for (int i = selModel.getMinSelectionIndex(); i <= selModel
1677: .getMaxSelectionIndex(); i++) {
1678: if (selModel.isSelectedIndex(i)) {
1679: count++;
1680: }
1681: }
1682: int[] result = new int[count];
1683: count = 0;
1684: for (int i = selModel.getMinSelectionIndex(); i <= selModel
1685: .getMaxSelectionIndex(); i++) {
1686: if (selModel.isSelectedIndex(i)) {
1687: result[count++] = i;
1688: }
1689: }
1690: return result;
1691: }
1692: }
|