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;
021:
022: import javax.swing.table.AbstractTableModel;
023:
024: public class JTableRTest extends BasicSwingTestCase {
025: private JTable table;
026:
027: @Override
028: protected void setUp() throws Exception {
029: table = new JTable();
030: propertyChangeController = new PropertyChangeController();
031: table.addPropertyChangeListener(propertyChangeController);
032: }
033:
034: @Override
035: protected void tearDown() throws Exception {
036: table = null;
037: }
038:
039: public void testSelectAllClearSelection() throws Exception {
040: table = new JTable(3, 4);
041: assertEquals(0,
042: getSelectedIndices(table.getSelectionModel()).length);
043: assertEquals(0, getSelectedIndices(table.getColumnModel()
044: .getSelectionModel()).length);
045: table.selectAll();
046: assertEquals(3,
047: getSelectedIndices(table.getSelectionModel()).length);
048: assertEquals(4, getSelectedIndices(table.getColumnModel()
049: .getSelectionModel()).length);
050: table.clearSelection();
051: assertEquals(0,
052: getSelectedIndices(table.getSelectionModel()).length);
053: assertEquals(0, getSelectedIndices(table.getColumnModel()
054: .getSelectionModel()).length);
055: table.setCellSelectionEnabled(false);
056: table.selectAll();
057: assertEquals(3,
058: getSelectedIndices(table.getSelectionModel()).length);
059: assertEquals(4, getSelectedIndices(table.getColumnModel()
060: .getSelectionModel()).length);
061: table.clearSelection();
062: table.setCellSelectionEnabled(true);
063: table.setRowSelectionInterval(1, 2);
064: table.setColumnSelectionInterval(1, 2);
065: assertEquals(2,
066: getSelectedIndices(table.getSelectionModel()).length);
067: assertEquals(1, table.getSelectionModel()
068: .getAnchorSelectionIndex());
069: assertEquals(2, table.getSelectionModel()
070: .getLeadSelectionIndex());
071: assertEquals(1, table.getColumnModel().getSelectionModel()
072: .getAnchorSelectionIndex());
073: assertEquals(2, table.getColumnModel().getSelectionModel()
074: .getLeadSelectionIndex());
075: ((AbstractTableModel) table.getModel()).fireTableRowsInserted(
076: 0, 3);
077: assertEquals(2,
078: getSelectedIndices(table.getSelectionModel()).length);
079: assertEquals(5, table.getSelectionModel()
080: .getAnchorSelectionIndex());
081: assertEquals(6, table.getSelectionModel()
082: .getLeadSelectionIndex());
083: assertEquals(1, table.getColumnModel().getSelectionModel()
084: .getAnchorSelectionIndex());
085: assertEquals(2, table.getColumnModel().getSelectionModel()
086: .getLeadSelectionIndex());
087: ((AbstractTableModel) table.getModel()).fireTableRowsDeleted(0,
088: 2);
089: assertEquals(2,
090: getSelectedIndices(table.getSelectionModel()).length);
091: assertEquals(2, table.getSelectionModel()
092: .getAnchorSelectionIndex());
093: assertEquals(3, table.getSelectionModel()
094: .getLeadSelectionIndex());
095: assertEquals(1, table.getColumnModel().getSelectionModel()
096: .getAnchorSelectionIndex());
097: assertEquals(2, table.getColumnModel().getSelectionModel()
098: .getLeadSelectionIndex());
099: ((AbstractTableModel) table.getModel()).fireTableCellUpdated(0,
100: 2);
101: assertEquals(2,
102: getSelectedIndices(table.getSelectionModel()).length);
103: assertEquals(2, table.getSelectionModel()
104: .getAnchorSelectionIndex());
105: assertEquals(3, table.getSelectionModel()
106: .getLeadSelectionIndex());
107: assertEquals(1, table.getColumnModel().getSelectionModel()
108: .getAnchorSelectionIndex());
109: assertEquals(2, table.getColumnModel().getSelectionModel()
110: .getLeadSelectionIndex());
111: ((AbstractTableModel) table.getModel()).fireTableDataChanged();
112: assertEquals(0,
113: getSelectedIndices(table.getSelectionModel()).length);
114: assertEquals(2, table.getSelectionModel()
115: .getAnchorSelectionIndex());
116: assertEquals(3, table.getSelectionModel()
117: .getLeadSelectionIndex());
118: assertEquals(1, table.getColumnModel().getSelectionModel()
119: .getAnchorSelectionIndex());
120: assertEquals(2, table.getColumnModel().getSelectionModel()
121: .getLeadSelectionIndex());
122: table.clearSelection();
123: table.setRowSelectionInterval(1, 2);
124: assertEquals(2,
125: getSelectedIndices(table.getSelectionModel()).length);
126: assertEquals(1, table.getSelectionModel()
127: .getAnchorSelectionIndex());
128: assertEquals(2, table.getSelectionModel()
129: .getLeadSelectionIndex());
130: assertEquals(1, table.getColumnModel().getSelectionModel()
131: .getAnchorSelectionIndex());
132: assertEquals(2, table.getColumnModel().getSelectionModel()
133: .getLeadSelectionIndex());
134: ((AbstractTableModel) table.getModel())
135: .fireTableStructureChanged();
136: assertEquals(0,
137: getSelectedIndices(table.getSelectionModel()).length);
138: assertEquals(1, table.getSelectionModel()
139: .getAnchorSelectionIndex());
140: assertEquals(2, table.getSelectionModel()
141: .getLeadSelectionIndex());
142: if (isHarmony()) {
143: assertEquals(-1, table.getColumnModel().getSelectionModel()
144: .getAnchorSelectionIndex());
145: } else {
146: assertEquals(0, table.getColumnModel().getSelectionModel()
147: .getAnchorSelectionIndex());
148: }
149: assertEquals(0, table.getColumnModel().getSelectionModel()
150: .getLeadSelectionIndex());
151: }
152:
153: private int[] getSelectedIndices(final ListSelectionModel selModel) {
154: int count = 0;
155: for (int i = selModel.getMinSelectionIndex(); i <= selModel
156: .getMaxSelectionIndex(); i++) {
157: if (selModel.isSelectedIndex(i)) {
158: count++;
159: }
160: }
161: int[] result = new int[count];
162: count = 0;
163: for (int i = selModel.getMinSelectionIndex(); i <= selModel
164: .getMaxSelectionIndex(); i++) {
165: if (selModel.isSelectedIndex(i)) {
166: result[count++] = i;
167: }
168: }
169: return result;
170: }
171: }
|