Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / table / DefaultTableModel.java
blobaab4202012e7239a1f0611ee3e4fe3a3c70fcc52
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 * columnIdentifiers
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 * @returns 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 dataVector = data;
185 columnIdentifiers = columnNames;
186 for (int r = 0; r < data.size(); r++) {
187 ((Vector) dataVector.get(r)).setSize(columnNames.size());
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 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 fireTableDataChanged();
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 fireTableDataChanged();
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 dataVector.add(rowData);
399 newRowsAdded(new TableModelEvent(
400 this, dataVector.size(), dataVector.size(), -1, TableModelEvent.INSERT)
405 * Adds a new row containing the specified data to the table and sends a
406 * {@link TableModelEvent} to all registered listeners.
408 * @param rowData the row data (<code>null</code> permitted).
410 public void addRow(Object[] rowData) {
411 addRow(convertToVector(rowData));
415 * Inserts a new row into the table.
417 * @param row the row index.
418 * @param rowData the row data.
420 public void insertRow(int row, Vector rowData) {
421 dataVector.add(row, rowData);
422 fireTableRowsInserted(row,row);
426 * Inserts a new row into the table.
428 * @param row the row index.
429 * @param rowData the row data.
431 public void insertRow(int row, Object[] rowData) {
432 insertRow(row, convertToVector(rowData));
436 * Moves the rows from <code>startIndex</code> to <code>endIndex</code>
437 * (inclusive) to the specified row.
439 * @param startIndex the start row.
440 * @param endIndex the end row.
441 * @param toIndex the row to move to.
443 public void moveRow(int startIndex, int endIndex, int toIndex) {
444 Vector removed = new Vector();
445 for (int i = endIndex; i >= startIndex; i--)
447 removed.add(this.dataVector.remove(i));
449 for (int i = 0; i <= endIndex - startIndex; i++)
451 dataVector.insertElementAt(removed.get(i), toIndex);
453 fireTableDataChanged();
457 * Removes a row from the table and sends a {@link TableModelEvent} to
458 * all registered listeners.
460 * @param row the row index.
462 public void removeRow(int row) {
463 dataVector.remove(row);
464 fireTableRowsDeleted(row,row);
468 * getRowCount
469 * @returns int
471 public int getRowCount() {
472 return dataVector.size();
476 * Returns the number of columns in the model.
478 * @return The column count.
480 public int getColumnCount() {
481 return (columnIdentifiers == null ? 0 : columnIdentifiers.size());
485 * Returns the name of the specified column.
487 * @param column the column index.
489 * @returns The column name.
491 public String getColumnName(int column) {
492 String result = "";
493 if (columnIdentifiers == null)
494 result = super.getColumnName(column);
495 else
497 if (column < getColumnCount())
499 Object id = columnIdentifiers.get(column);
500 if (id != null)
501 result = id.toString();
502 else
503 result = super.getColumnName(column);
506 return result;
510 * Returns <code>true</code> if the specified cell can be modified, and
511 * <code>false</code> otherwise. For this implementation, the method
512 * always returns <code>true</code>.
514 * @param row the row index.
515 * @param column the column index.
517 * @returns <code>true</code> in all cases.
519 public boolean isCellEditable(int row, int column) {
520 return true;
524 * Returns the value at the specified cell in the table.
526 * @param row the row index.
527 * @param column the column index.
529 * @returns The value (<code>Object</code>, possibly <code>null</code>) at
530 * the specified cell in the table.
532 public Object getValueAt(int row, int column) {
533 return ((Vector) dataVector.get(row)).get(column);
537 * Sets the value for the specified cell in the table and sends a
538 * {@link TableModelEvent} to all registered listeners.
540 * @param value the value (<code>Object</code>, <code>null</code> permitted).
541 * @param row the row index.
542 * @param column the column index.
544 public void setValueAt(Object value, int row, int column) {
545 ((Vector) dataVector.get(row)).set(column, value);
546 fireTableCellUpdated(row,column);
550 * Converts the data array to a <code>Vector</code>.
552 * @param data the data array (<code>null</code> permitted).
554 * @returns A vector (or <code>null</code> if the data array
555 * is <code>null</code>).
557 protected static Vector convertToVector(Object[] data) {
558 if (data == null)
559 return null;
560 Vector vector = new Vector(data.length);
561 for (int i = 0; i < data.length; i++)
562 vector.add(data[i]);
563 return vector;
567 * Converts the data array to a <code>Vector</code> of rows.
569 * @param the data array (<code>null</code> permitted).
571 * @returns A vector (or <code>null</code> if the data array
572 * is <code>null</code>.
574 protected static Vector convertToVector(Object[][] data) {
575 if (data == null)
576 return null;
577 Vector vector = new Vector(data.length);
578 for (int i = 0; i < data.length; i++)
579 vector.add(convertToVector(data[i]));
580 return vector;