1 /* DefaultTableModel.java --
2 Copyright (C) 2002 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)
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
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
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
;
43 import javax
.swing
.event
.TableModelEvent
;
47 * @author Andrew Selkirk
49 public class DefaultTableModel
extends AbstractTableModel
50 implements Serializable
52 static final long serialVersionUID
= 6680042567037222321L;
54 //-------------------------------------------------------------
55 // Variables --------------------------------------------------
56 //-------------------------------------------------------------
61 protected Vector dataVector
;
66 protected Vector columnIdentifiers
;
69 //-------------------------------------------------------------
70 // Initialization ---------------------------------------------
71 //-------------------------------------------------------------
74 * Constructor DefaultTableModel
76 public DefaultTableModel() {
78 } // DefaultTableModel()
81 * Constructor DefaultTableModel
85 public DefaultTableModel(int numRows
, int numColumns
) {
91 // Create Column Names
92 defaultNames
= new Vector();
93 for (columnIndex
= 0; columnIndex
< numColumns
; columnIndex
++) {
94 defaultNames
.addElement(super.getColumnName(columnIndex
));
98 // setDataVector(defaultNames, numRows);
100 } // DefaultTableModel()
103 * Constructor DefaultTableModel
107 public DefaultTableModel(Vector columnNames
, int numRows
) {
117 if (columnNames
== null) {
120 numColumns
= columnNames
.size();
122 for (rowIndex
= 0; rowIndex
< numRows
; rowIndex
++) {
123 rowData
= new Vector();
124 rowData
.setSize(numColumns
);
125 data
.addElement(rowData
);
129 setDataVector(data
, columnNames
);
131 } // DefaultTableModel()
134 * Constructor DefaultTableModel
138 public DefaultTableModel(Object
[] columnNames
, int numRows
) {
139 this(convertToVector(columnNames
), numRows
);
140 } // DefaultTableModel()
143 * Constructor DefaultTableModel
147 public DefaultTableModel(Vector data
, Vector columnNames
) {
148 setDataVector(data
, columnNames
);
149 } // DefaultTableModel()
152 * Constructor DefaultTableModel
156 public DefaultTableModel(Object
[][] data
, Object
[] columnNames
) {
157 this(convertToVector(data
), convertToVector(columnNames
));
158 } // DefaultTableModel()
161 //-------------------------------------------------------------
162 // Methods ----------------------------------------------------
163 //-------------------------------------------------------------
169 public Vector
getDataVector() {
178 public void setDataVector(Vector data
, Vector columnNames
) {
188 columnIdentifiers
= columnNames
;
191 numRows
= data
.size();
192 numColumns
= columnNames
.size();
193 for (rowIndex
= 0; rowIndex
< numRows
; rowIndex
++) {
194 columnVector
= (Vector
) dataVector
.get(rowIndex
);
195 columnVector
.setSize(numColumns
);
205 public void setDataVector(Object
[][] data
, Object
[] columnNames
) {
206 setDataVector(convertToVector(data
), convertToVector(columnNames
));
213 public void newDataAvailable(TableModelEvent event
) {
214 fireTableChanged(event
);
215 } // newDataAvailable()
221 public void newRowsAdded(TableModelEvent event
) {
229 public void rowsRemoved(TableModelEvent event
) {
230 fireTableChanged(event
);
234 * setColumnIdentifiers
237 public void setColumnIdentifiers(Vector columnIdentifiers
) {
238 this.columnIdentifiers
= columnIdentifiers
;
239 setColumnCount(columnIdentifiers
.size());
240 } // setColumnIdentifiers()
243 * setColumnIdentifiers
246 public void setColumnIdentifiers(Object
[] columnIdentifiers
) {
247 setColumnIdentifiers(convertToVector(columnIdentifiers
));
248 } // setColumnIdentifiers()
254 public void setNumRows(int numRows
) {
255 setRowCount(numRows
);
262 public void setRowCount(int rowCount
) {
270 public void setColumnCount(int columnCount
) {
272 } // setColumnCount()
278 public void addColumn(Object columnName
) {
279 addColumn(columnName
, new Vector(dataVector
.size()));
287 public void addColumn(Object columnName
, Vector columnData
) {
296 public void addColumn(Object columnName
, Object
[] columnData
) {
304 public void addRow(Vector rowData
) {
312 public void addRow(Object
[] rowData
) {
313 addRow(convertToVector(rowData
));
321 public void insertRow(int row
, Vector rowData
) {
322 dataVector
.add(row
, rowData
);
330 public void insertRow(int row
, Object
[] rowData
) {
331 insertRow(row
, convertToVector(rowData
));
340 public void moveRow(int startIndex
, int endIndex
, int toIndex
) {
347 for (index
= 0; index
< (endIndex
- startIndex
); index
++) {
348 vector
= (Vector
) dataVector
.remove(startIndex
);
349 dataVector
.add(toIndex
, vector
);
358 public void removeRow(int row
) {
359 dataVector
.remove(row
);
366 public int getRowCount() {
367 return dataVector
.size();
374 public int getColumnCount() {
375 return columnIdentifiers
.size();
376 } // getColumnCount()
383 public String
getColumnName(int column
) {
386 if (columnIdentifiers
== null || column
>= getColumnCount()) {
387 return super.getColumnName(column
);
390 // Return Column name
391 return (String
) columnIdentifiers
.get(column
);
401 public boolean isCellEditable(int row
, int column
) {
403 } // isCellEditable()
411 public Object
getValueAt(int row
, int column
) {
417 rowVector
= (Vector
) dataVector
.get(row
);
420 return rowVector
.get(column
);
430 public void setValueAt(Object value
, int row
, int column
) {
436 rowVector
= (Vector
) dataVector
.get(row
);
439 rowVector
.remove(column
);
440 rowVector
.add(column
, value
);
449 protected static Vector
convertToVector(Object
[] data
) {
461 vector
= new Vector();
462 for (index
= 0; index
< data
.length
; index
++) {
463 vector
.add(data
[index
]);
469 } // convertToVector()
476 protected static Vector
convertToVector(Object
[][] data
) {
483 vector
= new Vector();
484 for (index
= 0; index
< data
.length
; index
++) {
485 vector
.add(convertToVector(data
[index
]));
491 } // convertToVector()
494 } // DefaultTableModel