Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / table / DefaultTableModel.java
blobc281caa079114e84ba0c415057b9cc1767ca4a74
1 /* DefaultTableModel.java --
2 Copyright (C) 2002, 2004, 2005, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.table;
41 import java.io.Serializable;
42 import java.util.Vector;
44 import javax.swing.event.TableModelEvent;
46 /**
47 * A two dimensional data structure used to store <code>Object</code>
48 * instances, usually for display in a <code>JTable</code> component.
50 * @author Andrew Selkirk
52 public class DefaultTableModel extends AbstractTableModel
53 implements Serializable
55 static final long serialVersionUID = 6680042567037222321L;
57 /**
58 * Storage for the rows in the table (each row is itself
59 * a <code>Vector</code>).
61 protected Vector dataVector;
63 /**
64 * Storage for the column identifiers.
66 protected Vector columnIdentifiers;
68 /**
69 * Creates an empty table with zero rows and zero columns.
71 public DefaultTableModel()
73 this(0, 0);
76 /**
77 * Creates a new table with the specified number of rows and columns.
78 * All cells in the table are initially empty (set to <code>null</code>).
80 * @param numRows the number of rows.
81 * @param numColumns the number of columns.
83 public DefaultTableModel(int numRows, int numColumns)
85 Vector defaultNames = new Vector(numColumns);
86 Vector data = new Vector(numRows);
87 for (int i = 0; i < numColumns; i++)
89 defaultNames.add(super.getColumnName(i));
91 for (int r = 0; r < numRows; r++)
93 Vector tmp = new Vector(numColumns);
94 tmp.setSize(numColumns);
95 data.add(tmp);
97 setDataVector(data, defaultNames);
101 * Creates a new table with the specified column names and number of
102 * rows. The number of columns is determined by the number of column
103 * names supplied.
105 * @param columnNames the column names.
106 * @param numRows the number of rows.
108 public DefaultTableModel(Vector columnNames, int numRows)
110 if (numRows < 0)
111 throw new IllegalArgumentException("numRows < 0");
112 Vector data = new Vector();
113 int numColumns = 0;
115 if (columnNames != null)
116 numColumns = columnNames.size();
118 while (0 < numRows--)
120 Vector rowData = new Vector();
121 rowData.setSize(numColumns);
122 data.add(rowData);
124 setDataVector(data, columnNames);
128 * Creates a new table with the specified column names and row count.
130 * @param columnNames the column names.
131 * @param numRows the number of rows.
133 public DefaultTableModel(Object[] columnNames, int numRows)
135 this(convertToVector(columnNames), numRows);
139 * Creates a new table with the specified data values and column names.
141 * @param data the data values.
142 * @param columnNames the column names.
144 public DefaultTableModel(Vector data, Vector columnNames)
146 setDataVector(data, columnNames);
150 * Creates a new table with the specified data values and column names.
152 * @param data the data values.
153 * @param columnNames the column names.
155 public DefaultTableModel(Object[][] data, Object[] columnNames)
157 this(convertToVector(data), convertToVector(columnNames));
161 * Returns the vector containing the row data for the table.
163 * @return The data vector.
165 public Vector getDataVector()
167 return dataVector;
171 * Sets the data and column identifiers for the table. The data vector
172 * contains a <code>Vector</code> for each row in the table - if the
173 * number of objects in each row does not match the number of column
174 * names specified, the row data is truncated or expanded (by adding
175 * <code>null</code> values) as required.
177 * @param data the data for the table (a vector of row vectors).
178 * @param columnNames the column names.
180 * @throws NullPointerException if either argument is <code>null</code>.
182 public void setDataVector(Vector data, Vector columnNames)
184 if (data == null)
185 dataVector = new Vector();
186 else
187 dataVector = data;
188 setColumnIdentifiers(columnNames);
192 * Sets the data and column identifiers for the table.
194 * @param data the data for the table.
195 * @param columnNames the column names.
197 * @throws NullPointerException if either argument is <code>null</code>.
199 public void setDataVector(Object[][] data, Object[] columnNames)
201 setDataVector(convertToVector(data),
202 convertToVector(columnNames));
206 * Sends the specified <code>event</code> to all registered listeners.
207 * This method is equivalent to
208 * {@link AbstractTableModel#fireTableChanged(TableModelEvent)}.
210 * @param event the event.
212 public void newDataAvailable(TableModelEvent event)
214 fireTableChanged(event);
218 * Sends the specified <code>event</code> to all registered listeners.
219 * This method is equivalent to
220 * {@link AbstractTableModel#fireTableChanged(TableModelEvent)}.
222 * @param event the event.
224 public void newRowsAdded(TableModelEvent event)
226 fireTableChanged(event);
230 * Sends the specified <code>event</code> to all registered listeners.
231 * This method is equivalent to
232 * {@link AbstractTableModel#fireTableChanged(TableModelEvent)}.
234 * @param event the event.
236 public void rowsRemoved(TableModelEvent event)
238 fireTableChanged(event);
242 * Sets the column identifiers, updates the data rows (truncating
243 * or padding each row with <code>null</code> values) to match the
244 * number of columns, and sends a {@link TableModelEvent} to all
245 * registered listeners.
247 * @param columnIdentifiers the column identifiers.
249 public void setColumnIdentifiers(Vector columnIdentifiers)
251 this.columnIdentifiers = columnIdentifiers;
252 setColumnCount((columnIdentifiers == null ? 0 : columnIdentifiers.size()));
256 * Sets the column identifiers, updates the data rows (truncating
257 * or padding each row with <code>null</code> values) to match the
258 * number of columns, and sends a {@link TableModelEvent} to all
259 * registered listeners.
261 * @param columnIdentifiers the column identifiers.
263 public void setColumnIdentifiers(Object[] columnIdentifiers)
265 setColumnIdentifiers(convertToVector(columnIdentifiers));
269 * This method is obsolete, use {@link #setRowCount(int)} instead.
271 * @param numRows the number of rows.
273 public void setNumRows(int numRows)
275 setRowCount(numRows);
279 * Sets the number of rows in the table. If <code>rowCount</code> is less
280 * than the current number of rows in the table, rows are discarded.
281 * If <code>rowCount</code> is greater than the current number of rows in
282 * the table, new (empty) rows are added.
284 * @param rowCount the row count.
286 public void setRowCount(int rowCount)
288 int existingRowCount = dataVector.size();
289 if (rowCount < existingRowCount)
291 dataVector.setSize(rowCount);
292 fireTableRowsDeleted(rowCount,existingRowCount-1);
294 else
296 int rowsToAdd = rowCount - existingRowCount;
297 for (int i = 0; i < rowsToAdd; i++)
299 Vector tmp = new Vector();
300 tmp.setSize(columnIdentifiers.size());
301 dataVector.add(tmp);
303 fireTableRowsInserted(existingRowCount,rowCount-1);
308 * Sets the number of columns in the table. Existing rows are truncated
309 * or padded with <code>null</code> values to match the new column count.
310 * A {@link TableModelEvent} is sent to all registered listeners.
312 * @param columnCount the column count.
314 public void setColumnCount(int columnCount)
316 for (int i = 0; i < dataVector.size(); ++i)
318 ((Vector) dataVector.get(i)).setSize(columnCount);
320 if (columnIdentifiers != null)
321 columnIdentifiers.setSize(columnCount);
322 fireTableStructureChanged();
326 * Adds a column with the specified name to the table. All cell values
327 * for the column are initially set to <code>null</code>.
329 * @param columnName the column name (<code>null</code> permitted).
331 public void addColumn(Object columnName)
333 addColumn(columnName, (Object[]) null);
337 * Adds a column with the specified name and data values to the table.
339 * @param columnName the column name (<code>null</code> permitted).
340 * @param columnData the column data.
342 public void addColumn(Object columnName, Vector columnData)
344 Object[] dataArray = null;
345 if (columnData != null)
347 int rowCount = dataVector.size();
348 if (columnData.size() < rowCount)
349 columnData.setSize(rowCount);
350 dataArray = columnData.toArray();
352 addColumn(columnName, dataArray);
356 * Adds a column with the specified name and data values to the table.
358 * @param columnName the column name (<code>null</code> permitted).
359 * @param columnData the column data.
361 public void addColumn(Object columnName, Object[] columnData) {
362 if (columnData != null)
364 // check columnData array for cases where the number of items
365 // doesn't match the number of rows in the existing table
366 if (columnData.length > dataVector.size())
368 int rowsToAdd = columnData.length - dataVector.size();
369 for (int i = 0; i < rowsToAdd; i++)
371 Vector tmp = new Vector();
372 tmp.setSize(columnIdentifiers.size());
373 dataVector.add(tmp);
376 else if (columnData.length < dataVector.size())
378 Object[] tmp = new Object[dataVector.size()];
379 System.arraycopy(columnData, 0, tmp, 0, columnData.length);
380 columnData = tmp;
383 for (int i = 0; i < dataVector.size(); ++i)
385 ((Vector) dataVector.get(i)).add(columnData == null ? null : columnData[i]);
387 columnIdentifiers.add(columnName);
388 fireTableStructureChanged();
392 * Adds a new row containing the specified data to the table and sends a
393 * {@link TableModelEvent} to all registered listeners.
395 * @param rowData the row data (<code>null</code> permitted).
397 public void addRow(Vector rowData) {
398 int rowIndex = dataVector.size();
399 dataVector.add(rowData);
400 newRowsAdded(new TableModelEvent(
401 this, rowIndex, rowIndex, -1, TableModelEvent.INSERT)
406 * Adds a new row containing the specified data to the table and sends a
407 * {@link TableModelEvent} to all registered listeners.
409 * @param rowData the row data (<code>null</code> permitted).
411 public void addRow(Object[] rowData) {
412 addRow(convertToVector(rowData));
416 * Inserts a new row into the table.
418 * @param row the row index.
419 * @param rowData the row data.
421 public void insertRow(int row, Vector rowData) {
422 dataVector.add(row, rowData);
423 fireTableRowsInserted(row,row);
427 * Inserts a new row into the table.
429 * @param row the row index.
430 * @param rowData the row data.
432 public void insertRow(int row, Object[] rowData) {
433 insertRow(row, convertToVector(rowData));
437 * Moves the rows from <code>startIndex</code> to <code>endIndex</code>
438 * (inclusive) to the specified row.
440 * @param startIndex the start row.
441 * @param endIndex the end row.
442 * @param toIndex the row to move to.
444 public void moveRow(int startIndex, int endIndex, int toIndex) {
445 Vector removed = new Vector();
446 for (int i = endIndex; i >= startIndex; i--)
448 removed.add(this.dataVector.remove(i));
450 for (int i = 0; i <= endIndex - startIndex; i++)
452 dataVector.insertElementAt(removed.get(i), toIndex);
454 int firstRow = Math.min(startIndex, toIndex);
455 int lastRow = Math.max(endIndex, toIndex + (endIndex - startIndex));
456 fireTableRowsUpdated(firstRow, lastRow);
460 * Removes a row from the table and sends a {@link TableModelEvent} to
461 * all registered listeners.
463 * @param row the row index.
465 public void removeRow(int row) {
466 dataVector.remove(row);
467 fireTableRowsDeleted(row,row);
471 * Returns the number of rows in the model.
473 * @return The row count.
475 public int getRowCount() {
476 return dataVector.size();
480 * Returns the number of columns in the model.
482 * @return The column count.
484 public int getColumnCount() {
485 return (columnIdentifiers == null ? 0 : columnIdentifiers.size());
489 * Get the name of the column. If the column has the column identifier set,
490 * the return value is the result of the .toString() method call on that
491 * identifier. If the identifier is not explicitly set, the returned value
492 * is calculated by {@link AbstractTableModel#getColumnName(int)}.
494 * @param column the column index.
496 * @return The column name.
498 public String getColumnName(int column) {
499 String result = "";
500 if (columnIdentifiers == null)
501 result = super.getColumnName(column);
502 else
504 if (column < getColumnCount())
506 Object id = columnIdentifiers.get(column);
507 if (id != null)
508 result = id.toString();
509 else
510 result = super.getColumnName(column);
512 else
513 result = super.getColumnName(column);
515 return result;
519 * Returns <code>true</code> if the specified cell can be modified, and
520 * <code>false</code> otherwise. For this implementation, the method
521 * always returns <code>true</code>.
523 * @param row the row index.
524 * @param column the column index.
526 * @return <code>true</code> in all cases.
528 public boolean isCellEditable(int row, int column) {
529 return true;
533 * Returns the value at the specified cell in the table.
535 * @param row the row index.
536 * @param column the column index.
538 * @return The value (<code>Object</code>, possibly <code>null</code>) at
539 * the specified cell in the table.
541 public Object getValueAt(int row, int column) {
542 return ((Vector) dataVector.get(row)).get(column);
546 * Sets the value for the specified cell in the table and sends a
547 * {@link TableModelEvent} to all registered listeners.
549 * @param value the value (<code>Object</code>, <code>null</code> permitted).
550 * @param row the row index.
551 * @param column the column index.
553 public void setValueAt(Object value, int row, int column) {
554 ((Vector) dataVector.get(row)).set(column, value);
555 fireTableCellUpdated(row,column);
559 * Converts the data array to a <code>Vector</code>.
561 * @param data the data array (<code>null</code> permitted).
563 * @return A vector (or <code>null</code> if the data array
564 * is <code>null</code>).
566 protected static Vector convertToVector(Object[] data) {
567 if (data == null)
568 return null;
569 Vector vector = new Vector(data.length);
570 for (int i = 0; i < data.length; i++)
571 vector.add(data[i]);
572 return vector;
576 * Converts the data array to a <code>Vector</code> of rows.
578 * @param data the data array (<code>null</code> permitted).
580 * @return A vector (or <code>null</code> if the data array
581 * is <code>null</code>.
583 protected static Vector convertToVector(Object[][] data) {
584 if (data == null)
585 return null;
586 Vector vector = new Vector(data.length);
587 for (int i = 0; i < data.length; i++)
588 vector.add(convertToVector(data[i]));
589 return vector;