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: /**
019: * @author Anton Avtamonov
020: * @version $Revision$
021: */package javax.swing.table;
022:
023: import java.util.Vector;
024:
025: import javax.swing.event.TableModelEvent;
026:
027: public class DefaultTableModel extends AbstractTableModel {
028: protected Vector dataVector;
029: protected Vector columnIdentifiers;
030:
031: public DefaultTableModel() {
032: setDataVector(new Vector(), new Vector());
033: }
034:
035: public DefaultTableModel(final int rowCount, final int columnCount) {
036: setDataVector(new Vector(), new Vector());
037: setColumnCount(columnCount);
038: setRowCount(rowCount);
039: }
040:
041: public DefaultTableModel(final Vector columnNames,
042: final int rowCount) {
043: setDataVector(new Vector(), columnNames);
044: setRowCount(rowCount);
045: }
046:
047: public DefaultTableModel(final Object[] columnNames,
048: final int rowCount) {
049: this (convertToVector(columnNames), rowCount);
050: }
051:
052: public DefaultTableModel(final Vector data, final Vector columnNames) {
053: setDataVector(data, columnNames);
054: }
055:
056: public DefaultTableModel(final Object[][] data,
057: final Object[] columnNames) {
058: setDataVector(data, columnNames);
059: }
060:
061: public Vector getDataVector() {
062: return dataVector;
063: }
064:
065: public void setDataVector(final Vector data,
066: final Vector identifiers) {
067: columnIdentifiers = identifiers != null ? identifiers
068: : new Vector();
069: dataVector = data != null ? data : new Vector();
070: alignRowsLengthAndCount();
071: for (int i = 0; i < getRowCount(); i++) {
072: Vector row = (Vector) dataVector.get(i);
073: for (int j = 0; j < getColumnCount(); j++) {
074: Vector dataRow = (Vector) data.get(i);
075: if (dataRow.size() < j) {
076: row.setElementAt(dataRow.get(j), i);
077: }
078: }
079: }
080:
081: fireTableStructureChanged();
082: }
083:
084: public void setDataVector(final Object[][] dataVector,
085: final Object[] columnIdentifiers) {
086: setDataVector(convertToVector(dataVector),
087: convertToVector(columnIdentifiers));
088: }
089:
090: public void newDataAvailable(final TableModelEvent e) {
091: fireTableChanged(e);
092: }
093:
094: public void newRowsAdded(final TableModelEvent e) {
095: if (e.getFirstRow() >= 0 && e.getLastRow() >= e.getFirstRow()) {
096: for (int i = e.getFirstRow(); i <= e.getLastRow(); i++) {
097: ((Vector) dataVector.get(i)).setSize(getColumnCount());
098: }
099: }
100: fireTableChanged(e);
101: }
102:
103: public void rowsRemoved(final TableModelEvent e) {
104: fireTableChanged(e);
105: }
106:
107: public void setNumRows(final int rowCount) {
108: setRowCount(rowCount);
109: }
110:
111: public void setRowCount(final int rowCount) {
112: int oldSize = dataVector.size();
113: if (oldSize < rowCount) {
114: for (int i = oldSize; i < rowCount; i++) {
115: dataVector.add(new Vector());
116: }
117: newRowsAdded(new TableModelEvent(this , oldSize,
118: rowCount - 1, TableModelEvent.HEADER_ROW,
119: TableModelEvent.INSERT));
120: } else if (oldSize > rowCount) {
121: dataVector.setSize(rowCount);
122: rowsRemoved(new TableModelEvent(this , rowCount,
123: oldSize - 1, TableModelEvent.HEADER_ROW,
124: TableModelEvent.DELETE));
125: }
126: }
127:
128: public void addRow(final Vector rowData) {
129: dataVector.add(rowData != null ? rowData : new Vector());
130: newRowsAdded(new TableModelEvent(this , dataVector.size() - 1,
131: dataVector.size() - 1, TableModelEvent.HEADER_ROW,
132: TableModelEvent.INSERT));
133: }
134:
135: public void addRow(final Object[] rowData) {
136: addRow(convertToVector(rowData));
137: }
138:
139: public void insertRow(final int row, final Vector rowData) {
140: dataVector.add(row, rowData != null ? rowData : new Vector());
141: newRowsAdded(new TableModelEvent(this , row, row,
142: TableModelEvent.HEADER_ROW, TableModelEvent.INSERT));
143: }
144:
145: public void insertRow(final int row, final Object[] rowData) {
146: insertRow(row, convertToVector(rowData));
147: }
148:
149: public void moveRow(final int start, final int end, final int to) {
150: int intervalLength = end - start + 1;
151: if (start < to) {
152: for (int i = 0; i < intervalLength; i++) {
153: Object row = dataVector.remove(start);
154: dataVector.add(to + intervalLength - 1, row);
155: }
156: fireTableRowsUpdated(start, to + intervalLength - 1);
157: } else {
158: for (int i = 0; i < intervalLength; i++) {
159: Object row = dataVector.remove(start + i);
160: dataVector.add(to + i, row);
161: }
162: fireTableRowsUpdated(to, end);
163: }
164: }
165:
166: public void removeRow(final int row) {
167: dataVector.remove(row);
168: rowsRemoved(new TableModelEvent(this , row, row,
169: TableModelEvent.HEADER_ROW, TableModelEvent.DELETE));
170: }
171:
172: public void setColumnIdentifiers(final Vector identifiers) {
173: columnIdentifiers = identifiers;
174: alignRowsLengthAndCount();
175: fireTableStructureChanged();
176: }
177:
178: public void setColumnIdentifiers(final Object[] identifiers) {
179: setColumnIdentifiers(convertToVector(identifiers));
180: }
181:
182: public void setColumnCount(final int columnCount) {
183: columnIdentifiers.setSize(columnCount);
184: alignRowsLengthAndCount();
185: fireTableStructureChanged();
186: }
187:
188: public void addColumn(final Object columnName) {
189: addColumn(columnName, (Vector) null);
190: }
191:
192: public void addColumn(final Object columnName,
193: final Vector columnData) {
194: columnIdentifiers.add(columnName);
195: alignRowsLengthAndCount();
196:
197: if (columnData != null) {
198: for (int i = 0; i < dataVector.size(); i++) {
199: Vector row = (Vector) dataVector.get(i);
200: if (columnData.size() > i) {
201: row.setElementAt(columnData.get(i),
202: columnIdentifiers.size() - 1);
203: } else {
204: break;
205: }
206: }
207: }
208: fireTableStructureChanged();
209: }
210:
211: public void addColumn(final Object columnName,
212: final Object[] columnData) {
213: addColumn(columnName, convertToVector(columnData));
214: }
215:
216: public int getRowCount() {
217: return dataVector.size();
218: }
219:
220: public int getColumnCount() {
221: return columnIdentifiers.size();
222: }
223:
224: public String getColumnName(final int column) {
225: return columnIdentifiers.size() > column
226: && columnIdentifiers.get(column) != null ? columnIdentifiers
227: .get(column).toString()
228: : super .getColumnName(column);
229: }
230:
231: public boolean isCellEditable(final int row, final int column) {
232: return true;
233: }
234:
235: public Object getValueAt(final int row, final int column) {
236: return ((Vector) dataVector.get(row)).get(column);
237: }
238:
239: public void setValueAt(final Object value, final int row,
240: final int column) {
241: ((Vector) dataVector.get(row)).setElementAt(value, column);
242: fireTableCellUpdated(row, column);
243: }
244:
245: protected static Vector convertToVector(final Object[] data) {
246: if (data == null) {
247: return null;
248: }
249: Vector result = new Vector();
250: for (int i = 0; i < data.length; i++) {
251: result.add(data[i]);
252: }
253:
254: return result;
255: }
256:
257: protected static Vector convertToVector(final Object[][] data) {
258: if (data == null) {
259: return null;
260: }
261: Vector result = new Vector();
262: for (int i = 0; i < data.length; i++) {
263: result.add(convertToVector(data[i]));
264: }
265:
266: return result;
267: }
268:
269: private void alignRowsLengthAndCount() {
270: for (int i = 0; i < getRowCount(); i++) {
271: if (dataVector.size() == i) {
272: dataVector.add(new Vector());
273: }
274: Vector next = (Vector) dataVector.get(i);
275: if (next == null) {
276: next = new Vector();
277: dataVector.setElementAt(next, i);
278: }
279: next.setSize(getColumnCount());
280: }
281: }
282: }
|