/[classpath]/classpath/javax/swing/table/DefaultTableModel.java
ViewVC logotype

Diff of /classpath/javax/swing/table/DefaultTableModel.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.8 by mkoch, Fri Oct 22 12:44:01 2004 UTC revision 1.9 by mkoch, Fri Jan 7 18:32:49 2005 UTC
# Line 1  Line 1 
1  /* DefaultTableModel.java --  /* DefaultTableModel.java --
2     Copyright (C) 2002, 2004  Free Software Foundation, Inc.     Copyright (C) 2002, 2004, 2005,  Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 44  import java.util.Vector; Line 44  import java.util.Vector;
44  import javax.swing.event.TableModelEvent;  import javax.swing.event.TableModelEvent;
45    
46  /**  /**
47   * DefaultTableModel   * A two dimensional data structure used to store <code>Object</code>
48     * instances, usually for display in a <code>JTable</code> component.
49     *
50   * @author      Andrew Selkirk   * @author      Andrew Selkirk
51   */   */
52  public class DefaultTableModel extends AbstractTableModel  public class DefaultTableModel extends AbstractTableModel
53    implements Serializable    implements Serializable
54  {  {
55    static final long serialVersionUID = 6680042567037222321L;    static final long serialVersionUID = 6680042567037222321L;
56    
57    /**    /**
58     * dataVector     * Storage for the rows in the table (each row is itself
59       * a <code>Vector</code>).
60     */     */
61    protected Vector dataVector;    protected Vector dataVector;
62    
# Line 62  public class DefaultTableModel extends A Line 66  public class DefaultTableModel extends A
66    protected Vector columnIdentifiers;    protected Vector columnIdentifiers;
67    
68    /**    /**
69     * Constructor DefaultTableModel     * Creates an empty table with zero rows and zero columns.
70     */     */
71    public DefaultTableModel()    public DefaultTableModel()
72    {    {
# Line 70  public class DefaultTableModel extends A Line 74  public class DefaultTableModel extends A
74    }    }
75        
76    /**    /**
77     * Constructor DefaultTableModel     * Creates a new table with the specified number of rows and columns.
78     * @param value0 TODO     * All cells in the table are initially empty (set to <code>null</code>).
79     * @param value1 TODO     *
80       * @param numRows  the number of rows.
81       * @param numColumns  the number of columns.
82     */     */
83    public DefaultTableModel(int numRows, int numColumns)    public DefaultTableModel(int numRows, int numColumns)
84    {    {
# Line 81  public class DefaultTableModel extends A Line 87  public class DefaultTableModel extends A
87      for (int i = 0; i < numColumns; i++)      for (int i = 0; i < numColumns; i++)
88        {        {
89          defaultNames.add(super.getColumnName(i));          defaultNames.add(super.getColumnName(i));
90          }          
91        for (int r = 0; r < numRows; r++)
92          {
93          Vector tmp = new Vector(numColumns);          Vector tmp = new Vector(numColumns);
94          tmp.setSize(numColumns);          tmp.setSize(numColumns);
95          data.add(tmp);          data.add(tmp);
96        }                  }
97      setDataVector(defaultNames, data);      setDataVector(data, defaultNames);
98    }    }
99        
100    /**    /**
101     * Constructor DefaultTableModel     * Creates a new table with the specified column names and number of
102     * @param value0 TODO     * rows.  The number of columns is determined by the number of column
103     * @param value1 TODO     * names supplied.
104       *  
105       * @param columnNames the column names.
106       * @param numRows the number of rows.
107     */     */
108    public DefaultTableModel(Vector columnNames, int numRows)    public DefaultTableModel(Vector columnNames, int numRows)
109    {    {
110        if (numRows < 0)
111          throw new IllegalArgumentException("numRows < 0");
112      Vector data = new Vector();      Vector data = new Vector();
113      int numColumns = 0;      int numColumns = 0;
114    
# Line 111  public class DefaultTableModel extends A Line 125  public class DefaultTableModel extends A
125    }    }
126    
127    /**    /**
128     * Constructor DefaultTableModel     * Creates a new table with the specified column names and row count.
129     * @param value0 TODO     *
130     * @param value1 TODO     * @param columnNames the column names.
131       * @param numRows the number of rows.
132     */     */
133    public DefaultTableModel(Object[] columnNames, int numRows)    public DefaultTableModel(Object[] columnNames, int numRows)
134    {    {
# Line 121  public class DefaultTableModel extends A Line 136  public class DefaultTableModel extends A
136    }    }
137        
138    /**    /**
139     * Constructor DefaultTableModel     * Creates a new table with the specified data values and column names.
140     * @param value0 TODO     *
141     * @param value1 TODO     * @param data the data values.
142       * @param columnNames the column names.
143     */     */
144    public DefaultTableModel(Vector data, Vector columnNames)    public DefaultTableModel(Vector data, Vector columnNames)
145    {    {
# Line 131  public class DefaultTableModel extends A Line 147  public class DefaultTableModel extends A
147    }    }
148    
149    /**    /**
150     * Constructor DefaultTableModel     * Creates a new table with the specified data values and column names.
151     * @param value0 TODO     *
152     * @param value1 TODO     * @param data the data values.
153       * @param columnNames the column names.
154     */     */
155    public DefaultTableModel(Object[][] data, Object[] columnNames)    public DefaultTableModel(Object[][] data, Object[] columnNames)
156    {    {
# Line 141  public class DefaultTableModel extends A Line 158  public class DefaultTableModel extends A
158    }    }
159    
160    /**    /**
161     * getDataVector     * Returns the vector containing the row data for the table.
162     * @returns Vector     *
163       * @returns The data vector.
164     */     */
165    public Vector getDataVector()    public Vector getDataVector()
166    {    {
# Line 150  public class DefaultTableModel extends A Line 168  public class DefaultTableModel extends A
168    }    }
169    
170    /**    /**
171     * setDataVector     * Sets the data and column identifiers for the table.  The data vector
172     * @param value0 TODO     * contains a <code>Vector</code> for each row in the table - if the
173     * @param value1 TODO     * 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.
176       *
177       * @param data the data for the table (a vector of row vectors).
178       * @param columnNames the column names.
179       *
180       * @throws NullPointerException if either argument is <code>null</code>.
181     */     */
182    public void setDataVector(Vector data, Vector columnNames)    public void setDataVector(Vector data, Vector columnNames)
183    {    {
# Line 164  public class DefaultTableModel extends A Line 189  public class DefaultTableModel extends A
189    }    }
190    
191    /**    /**
192     * setDataVector     * Sets the data and column identifiers for the table.
193     * @param value0 TODO     *
194     * @param value1 TODO     * @param data the data for the table.
195       * @param columnNames the column names.
196       *
197       * @throws NullPointerException if either argument is <code>null</code>.
198     */     */
199    public void setDataVector(Object[][] data, Object[] columnNames)    public void setDataVector(Object[][] data, Object[] columnNames)
200    {    {
# Line 175  public class DefaultTableModel extends A Line 203  public class DefaultTableModel extends A
203    }    }
204        
205    /**    /**
206     * newDataAvailable     * Sends the specified <code>event</code> to all registered listeners.
207     * @param value0 TODO     * This method is equivalent to
208       * {@link AbstractTableModel#fireTableChanged(TableModelEvent)}.
209       *
210       * @param event the event.
211     */     */
212    public void newDataAvailable(TableModelEvent event)    public void newDataAvailable(TableModelEvent event)
213    {    {
# Line 184  public class DefaultTableModel extends A Line 215  public class DefaultTableModel extends A
215    }    }
216    
217    /**    /**
218     * newRowsAdded     * Sends the specified <code>event</code> to all registered listeners.
219     * @param value0 TODO     * This method is equivalent to
220       * {@link AbstractTableModel#fireTableChanged(TableModelEvent)}.
221       *
222       * @param event the event.
223     */     */
224    public void newRowsAdded(TableModelEvent event)    public void newRowsAdded(TableModelEvent event)
225    {    {
# Line 193  public class DefaultTableModel extends A Line 227  public class DefaultTableModel extends A
227    }    }
228    
229    /**    /**
230     * rowsRemoved     * Sends the specified <code>event</code> to all registered listeners.
231     * @param value0 TODO     * This method is equivalent to
232       * {@link AbstractTableModel#fireTableChanged(TableModelEvent)}.
233       *
234       * @param event the event.
235     */     */
236    public void rowsRemoved(TableModelEvent event)    public void rowsRemoved(TableModelEvent event)
237    {    {
# Line 202  public class DefaultTableModel extends A Line 239  public class DefaultTableModel extends A
239    }    }
240    
241    /**    /**
242     * setColumnIdentifiers     * Sets the column identifiers, updates the data rows (truncating
243     * @param value0 TODO     * 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.
246       *
247       * @param columnIdentifiers the column identifiers.
248     */     */
249    public void setColumnIdentifiers(Vector columnIdentifiers)    public void setColumnIdentifiers(Vector columnIdentifiers)
250    {    {
251      this.columnIdentifiers = columnIdentifiers;      this.columnIdentifiers = columnIdentifiers;
252      setColumnCount(columnIdentifiers.size());      setColumnCount((columnIdentifiers == null ? 0 : columnIdentifiers.size()));
253    }    }
254        
255    /**    /**
256     * setColumnIdentifiers     * Sets the column identifiers, updates the data rows (truncating
257     * @param value0 TODO     * 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.
260       *
261       * @param columnIdentifiers the column identifiers.
262     */     */
263    public void setColumnIdentifiers(Object[] columnIdentifiers)    public void setColumnIdentifiers(Object[] columnIdentifiers)
264    {    {
# Line 221  public class DefaultTableModel extends A Line 266  public class DefaultTableModel extends A
266    }    }
267    
268    /**    /**
269     * setNumRows     * This method is obsolete, use {@link #setRowCount(int)} instead.
270     * @param value0 TODO     *
271       * @param numRows the number of rows.
272     */     */
273    public void setNumRows(int numRows)    public void setNumRows(int numRows)
274    {    {
# Line 230  public class DefaultTableModel extends A Line 276  public class DefaultTableModel extends A
276    }    }
277    
278    /**    /**
279     * setRowCount     * Sets the number of rows in the table.  If <code>rowCount</code> is less
280     * @param value0 TODO     * 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.
283       *
284       * @param the row count.
285     */     */
286    public void setRowCount(int rowCount)    public void setRowCount(int rowCount)
287    {    {
288      dataVector.setSize(rowCount);      int existingRowCount = dataVector.size();
289      fireTableDataChanged();      if (rowCount < existingRowCount)
290        {
291          dataVector.setSize(rowCount);
292          fireTableRowsDeleted(rowCount,existingRowCount-1);      
293        }
294        else
295        {
296          int rowsToAdd = rowCount - existingRowCount;
297          for (int i = 0; i < rowsToAdd; i++)
298            {
299              Vector tmp = new Vector();
300              tmp.setSize(columnIdentifiers.size());
301              dataVector.add(tmp);
302            }
303          fireTableRowsInserted(existingRowCount,rowCount-1);
304        }
305    }    }
306    
307    /**    /**
308     * setColumnCount     * Sets the number of columns in the table.  Existing rows are truncated
309     * @param value0 TODO     * or padded with <code>null</code> values to match the new column count.
310       * A {@link TableModelEvent} is sent to all registered listeners.
311       *
312       * @param columnCount the column count.
313     */     */
314    public void setColumnCount(int columnCount)    public void setColumnCount(int columnCount)
315    {    {
# Line 249  public class DefaultTableModel extends A Line 317  public class DefaultTableModel extends A
317        {        {
318          ((Vector) dataVector.get(i)).setSize(columnCount);          ((Vector) dataVector.get(i)).setSize(columnCount);
319        }        }
320      columnIdentifiers.setSize(columnCount);      if (columnIdentifiers != null)  
321          columnIdentifiers.setSize(columnCount);
322      fireTableDataChanged();      fireTableDataChanged();
323    }    }
324    
325    /**    /**
326     * addColumn     * Adds a column with the specified name to the table.  All cell values
327     * @param value0 TODO     * for the column are initially set to <code>null</code>.
328       *
329       * @param columnName the column name (<code>null</code> permitted).
330     */     */
331    public void addColumn(Object columnName)    public void addColumn(Object columnName)
332    {    {
# Line 263  public class DefaultTableModel extends A Line 334  public class DefaultTableModel extends A
334    }    }
335    
336    /**    /**
337     * addColumn     * Adds a column with the specified name and data values to the table.  
338     * @param value0 TODO     *
339     * @param value1 TODO     * @param columnName the column name (<code>null</code> permitted).
340       * @param columnData the column data.
341     */     */
342    public void addColumn(Object columnName, Vector columnData)    public void addColumn(Object columnName, Vector columnData)
343    {    {
344      addColumn(columnName, columnData == null ? null : columnData.toArray());      Object[] dataArray = null;
345        if (columnData != null)
346        {
347          int rowCount = dataVector.size();
348          if (columnData.size() < rowCount)
349            columnData.setSize(rowCount);
350          dataArray = columnData.toArray();
351        }
352        addColumn(columnName, dataArray);
353    }    }
354    
355    /**    /**
356     * addColumn     * Adds a column with the specified name and data values to the table.
357     * @param value0 TODO     *
358     * @param value1 TODO     * @param columnName the column name (<code>null</code> permitted).
359       * @param columnData the column data.
360     */     */
361    public void addColumn(Object columnName, Object[] columnData) {    public void addColumn(Object columnName, Object[] columnData) {
362        if (columnData != null)
363        {
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())
367          {
368            int rowsToAdd = columnData.length - dataVector.size();
369            for (int i = 0; i < rowsToAdd; i++)
370            {
371              Vector tmp = new Vector();
372              tmp.setSize(columnIdentifiers.size());
373              dataVector.add(tmp);
374            }
375          }
376          else if (columnData.length < dataVector.size())
377          {
378            Object[] tmp = new Object[dataVector.size()];
379            System.arraycopy(columnData, 0, tmp, 0, columnData.length);
380            columnData = tmp;
381          }
382        }
383      for (int i = 0; i < dataVector.size(); ++i)      for (int i = 0; i < dataVector.size(); ++i)
384        {        {
385          ((Vector) dataVector.get(i)).add(columnData == null ? null : columnData[i]);          ((Vector) dataVector.get(i)).add(columnData == null ? null : columnData[i]);
# Line 287  public class DefaultTableModel extends A Line 389  public class DefaultTableModel extends A
389    }    }
390    
391    /**    /**
392     * addRow     * Adds a new row containing the specified data to the table and sends a
393     * @param value0 TODO     * {@link TableModelEvent} to all registered listeners.
394       *
395       * @param rowData the row data (<code>null</code> permitted).
396     */     */
397    public void addRow(Vector rowData) {    public void addRow(Vector rowData) {
398      dataVector.add(rowData);      dataVector.add(rowData);
399      fireTableDataChanged();      newRowsAdded(new TableModelEvent(
400          this, dataVector.size(), dataVector.size(), -1, TableModelEvent.INSERT)
401        );
402    }    }
403    
404    /**    /**
405     * addRow     * Adds a new row containing the specified data to the table and sends a
406     * @param value0 TODO     * {@link TableModelEvent} to all registered listeners.
407       *
408       * @param rowData the row data (<code>null</code> permitted).
409     */     */
410    public void addRow(Object[] rowData) {    public void addRow(Object[] rowData) {
411      addRow(convertToVector(rowData));      addRow(convertToVector(rowData));
412    }    }
413    
414    /**    /**
415     * insertRow     * Inserts a new row into the table.
416     * @param value0 TODO     *
417     * @param value1 TODO     * @param row the row index.
418       * @param rowData the row data.
419     */     */
420    public void insertRow(int row, Vector rowData) {    public void insertRow(int row, Vector rowData) {
421      dataVector.add(row, rowData);      dataVector.add(row, rowData);
422      fireTableDataChanged();      fireTableRowsInserted(row,row);
423    }    }
424    
425    /**    /**
426     * insertRow     * Inserts a new row into the table.
427     * @param value0 TODO     *
428     * @param value1 TODO     * @param row the row index.
429       * @param rowData the row data.
430     */     */
431    public void insertRow(int row, Object[] rowData) {    public void insertRow(int row, Object[] rowData) {
432      insertRow(row, convertToVector(rowData));      insertRow(row, convertToVector(rowData));
433    }    }
434    
435    /**    /**
436     * moveRow     * Moves the rows from <code>startIndex</code> to <code>endIndex</code>
437     * @param value0 TODO     * (inclusive) to the specified row.
438     * @param value1 TODO     *
439     * @param value2 TODO     * @param startIndex the start row.
440       * @param endIndex the end row.
441       * @param toIndex the row to move to.
442     */     */
443    public void moveRow(int startIndex, int endIndex, int toIndex) {    public void moveRow(int startIndex, int endIndex, int toIndex) {
444      for (int index = 0; index < (endIndex - startIndex); index++) {      Vector removed = new Vector();
445        Vector vector = (Vector) dataVector.remove(startIndex);      for (int i = endIndex; i >= startIndex; i--)
446        dataVector.add(toIndex, vector);      {
447          removed.add(this.dataVector.remove(i));
448        }
449        for (int i = 0; i <= endIndex - startIndex; i++)
450        {
451          dataVector.insertElementAt(removed.get(i), toIndex);  
452      }      }
453      fireTableDataChanged();      fireTableDataChanged();
454    }    }
455    
456    /**    /**
457     * removeRow     * Removes a row from the table and sends a {@link TableModelEvent} to
458     * @param value0 TODO     * all registered listeners.
459       *
460       * @param row the row index.
461     */     */
462    public void removeRow(int row) {    public void removeRow(int row) {
463      dataVector.remove(row);      dataVector.remove(row);
464      fireTableDataChanged();      fireTableRowsDeleted(row,row);
465    }    }
466    
467    /**    /**
# Line 354  public class DefaultTableModel extends A Line 473  public class DefaultTableModel extends A
473    }    }
474    
475    /**    /**
476     * getColumnCount     * Returns the number of columns in the model.
477     * @returns int     *
478       * @return The column count.
479     */     */
480    public int getColumnCount() {    public int getColumnCount() {
481      return columnIdentifiers.size();      return (columnIdentifiers == null ? 0 : columnIdentifiers.size());
482    }    }
483    
484    /**    /**
485     * getColumnName     * Returns the name of the specified column.
486     * @param value0 TODO     *
487     * @returns String     * @param column the column index.
488       *
489       * @returns The column name.
490     */     */
491    public String getColumnName(int column) {    public String getColumnName(int column) {
492      // Check for Column      String result = "";
493      if (columnIdentifiers == null || column >= getColumnCount()) {      if (columnIdentifiers == null)
494        return super.getColumnName(column);        result = super.getColumnName(column);
495        else
496        {
497          if (column < getColumnCount())
498          {  
499            Object id = columnIdentifiers.get(column);
500            if (id != null)
501              result = id.toString();
502            else
503              result = super.getColumnName(column);
504          }
505      }      }
506                  return result;
     // Return Column name  
     return (String) columnIdentifiers.get(column);            
507    }    }
508    
509    /**    /**
510     * isCellEditable     * Returns <code>true</code> if the specified cell can be modified, and
511     * @param value0 TODO     * <code>false</code> otherwise.  For this implementation, the method
512     * @param value1 TODO     * always returns <code>true</code>.
513     * @returns boolean     *
514       * @param row the row index.
515       * @param column the column index.
516       *
517       * @returns <code>true</code> in all cases.
518     */     */
519    public boolean isCellEditable(int row, int column) {    public boolean isCellEditable(int row, int column) {
520      return true;      return true;
521    }    }
522    
523    /**    /**
524     * getValueAt     * Returns the value at the specified cell in the table.
525     * @param value0 TODO     *
526     * @param value1 TODO     * @param row the row index.
527     * @returns Object     * @param column the column index.
528       *
529       * @returns The value (<code>Object</code>, possibly <code>null</code>) at
530       *          the specified cell in the table.
531     */     */
532    public Object getValueAt(int row, int column) {    public Object getValueAt(int row, int column) {
533      return ((Vector) dataVector.get(row)).get(column);      return ((Vector) dataVector.get(row)).get(column);
534    }    }
535    
536    /**    /**
537     * setValueAt     * Sets the value for the specified cell in the table and sends a
538     * @param value0 TODO     * {@link TableModelEvent} to all registered listeners.
539     * @param value1 TODO     *
540     * @param value2 TODO     * @param value the value (<code>Object</code>, <code>null</code> permitted).
541       * @param row the row index.
542       * @param column the column index.
543     */     */
544    public void setValueAt(Object value, int row, int column) {    public void setValueAt(Object value, int row, int column) {
545      ((Vector) dataVector.get(row)).set(column, value);      ((Vector) dataVector.get(row)).set(column, value);
546      fireTableDataChanged();      fireTableCellUpdated(row,column);
547    }    }
548    
549    /**    /**
550     * convertToVector     * Converts the data array to a <code>Vector</code>.
551     * @param value0 TODO     *
552     * @returns Vector     * @param data the data array (<code>null</code> permitted).
553       *
554       * @returns A vector (or <code>null</code> if the data array
555       *          is <code>null</code>).
556     */     */
557    protected static Vector convertToVector(Object[] data) {    protected static Vector convertToVector(Object[] data) {
558      if (data == null)      if (data == null)
# Line 422  public class DefaultTableModel extends A Line 564  public class DefaultTableModel extends A
564    }    }
565        
566    /**    /**
567     * convertToVector     * Converts the data array to a <code>Vector</code> of rows.
568     * @param value0 TODO     *
569     * @returns Vector     * @param the data array (<code>null</code> permitted).
570       *
571       * @returns A vector (or <code>null</code> if the data array
572       *          is <code>null</code>.
573     */     */
574    protected static Vector convertToVector(Object[][] data) {    protected static Vector convertToVector(Object[][] data) {
575      if (data == null)      if (data == null)

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.9

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26