jdk/src/share/classes/javax/swing/table/DefaultTableModel.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1301 15e81207e1f2
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.event.TableModelEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This is an implementation of <code>TableModel</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * uses a <code>Vector</code> of <code>Vectors</code> to store the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * cell value objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <strong>Warning:</strong> <code>DefaultTableModel</code> returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * column class of <code>Object</code>.  When
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <code>DefaultTableModel</code> is used with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <code>TableRowSorter</code> this will result in extensive use of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>toString</code>, which for non-<code>String</code> data types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * is expensive.  If you use <code>DefaultTableModel</code> with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <code>TableRowSorter</code> you are strongly encouraged to override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <code>getColumnClass</code> to return the appropriate type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see TableModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see #getDataVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class DefaultTableModel extends AbstractTableModel implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
// Instance Variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * The <code>Vector</code> of <code>Vectors</code> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <code>Object</code> values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    protected Vector    dataVector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /** The <code>Vector</code> of column identifiers. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected Vector    columnIdentifiers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
// Constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *  Constructs a default <code>DefaultTableModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *  which is a table of zero columns and zero rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public DefaultTableModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static Vector newVector(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        Vector v = new Vector(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        v.setSize(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *  Constructs a <code>DefaultTableModel</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *  <code>rowCount</code> and <code>columnCount</code> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *  <code>null</code> object values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param rowCount           the number of rows the table holds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param columnCount        the number of columns the table holds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @see #setValueAt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public DefaultTableModel(int rowCount, int columnCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this(newVector(columnCount), rowCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *  Constructs a <code>DefaultTableModel</code> with as many columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *  as there are elements in <code>columnNames</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *  and <code>rowCount</code> of <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *  object values.  Each column's name will be taken from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *  the <code>columnNames</code> vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param columnNames       <code>vector</code> containing the names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *                          of the new columns; if this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *                          <code>null</code> then the model has no columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param rowCount           the number of rows the table holds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @see #setDataVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @see #setValueAt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public DefaultTableModel(Vector columnNames, int rowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        setDataVector(newVector(rowCount), columnNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *  Constructs a <code>DefaultTableModel</code> with as many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *  columns as there are elements in <code>columnNames</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *  and <code>rowCount</code> of <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *  object values.  Each column's name will be taken from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *  the <code>columnNames</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param columnNames       <code>array</code> containing the names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *                          of the new columns; if this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *                          <code>null</code> then the model has no columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param rowCount           the number of rows the table holds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @see #setDataVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @see #setValueAt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public DefaultTableModel(Object[] columnNames, int rowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        this(convertToVector(columnNames), rowCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *  Constructs a <code>DefaultTableModel</code> and initializes the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *  by passing <code>data</code> and <code>columnNames</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *  to the <code>setDataVector</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param data              the data of the table, a <code>Vector</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *                          of <code>Vector</code>s of <code>Object</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *                          values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param columnNames       <code>vector</code> containing the names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *                          of the new columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @see #getDataVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @see #setDataVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public DefaultTableModel(Vector data, Vector columnNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        setDataVector(data, columnNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *  Constructs a <code>DefaultTableModel</code> and initializes the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *  by passing <code>data</code> and <code>columnNames</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *  to the <code>setDataVector</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *  method. The first index in the <code>Object[][]</code> array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *  the row index and the second is the column index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param data              the data of the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param columnNames       the names of the columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @see #getDataVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @see #setDataVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public DefaultTableModel(Object[][] data, Object[] columnNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        setDataVector(data, columnNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *  Returns the <code>Vector</code> of <code>Vectors</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *  that contains the table's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *  data values.  The vectors contained in the outer vector are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *  each a single row of values.  In other words, to get to the cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *  at row 1, column 5: <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *  <code>((Vector)getDataVector().elementAt(1)).elementAt(5);</code><p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return  the vector of vectors containing the tables data values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @see #newDataAvailable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @see #newRowsAdded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @see #setDataVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public Vector getDataVector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return dataVector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private static Vector nonNullVector(Vector v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return (v != null) ? v : new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *  Replaces the current <code>dataVector</code> instance variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *  with the new <code>Vector</code> of rows, <code>dataVector</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *  Each row is represented in <code>dataVector</code> as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *  <code>Vector</code> of <code>Object</code> values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *  <code>columnIdentifiers</code> are the names of the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *  columns.  The first name in <code>columnIdentifiers</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *  mapped to column 0 in <code>dataVector</code>. Each row in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *  <code>dataVector</code> is adjusted to match the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *  columns in <code>columnIdentifiers</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *  either by truncating the <code>Vector</code> if it is too long,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *  or adding <code>null</code> values if it is too short.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *  <p>Note that passing in a <code>null</code> value for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *  <code>dataVector</code> results in unspecified behavior,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *  an possibly an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param   dataVector         the new data vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param   columnIdentifiers     the names of the columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @see #getDataVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public void setDataVector(Vector dataVector, Vector columnIdentifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        this.dataVector = nonNullVector(dataVector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        this.columnIdentifiers = nonNullVector(columnIdentifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        justifyRows(0, getRowCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        fireTableStructureChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *  Replaces the value in the <code>dataVector</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *  variable with the values in the array <code>dataVector</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *  The first index in the <code>Object[][]</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *  array is the row index and the second is the column index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *  <code>columnIdentifiers</code> are the names of the new columns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param dataVector                the new data vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param columnIdentifiers the names of the columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @see #setDataVector(Vector, Vector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public void setDataVector(Object[][] dataVector, Object[] columnIdentifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        setDataVector(convertToVector(dataVector), convertToVector(columnIdentifiers));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *  Equivalent to <code>fireTableChanged</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param event  the change event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public void newDataAvailable(TableModelEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        fireTableChanged(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
// Manipulating rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private void justifyRows(int from, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // Sometimes the DefaultTableModel is subclassed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // instead of the AbstractTableModel by mistake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // Set the number of rows for the case when getRowCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // is overridden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        dataVector.setSize(getRowCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        for (int i = from; i < to; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (dataVector.elementAt(i) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                dataVector.setElementAt(new Vector(), i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            ((Vector)dataVector.elementAt(i)).setSize(getColumnCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *  Ensures that the new rows have the correct number of columns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *  This is accomplished by  using the <code>setSize</code> method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *  <code>Vector</code> which truncates vectors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *  which are too long, and appends <code>null</code>s if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *  are too short.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *  This method also sends out a <code>tableChanged</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *  notification message to all the listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param e         this <code>TableModelEvent</code> describes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *                           where the rows were added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *                           If <code>null</code> it assumes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *                           all the rows were newly added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @see #getDataVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public void newRowsAdded(TableModelEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        justifyRows(e.getFirstRow(), e.getLastRow() + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        fireTableChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *  Equivalent to <code>fireTableChanged</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *  @param event the change event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public void rowsRemoved(TableModelEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        fireTableChanged(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Obsolete as of Java 2 platform v1.3.  Please use <code>setRowCount</code> instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *  Sets the number of rows in the model.  If the new size is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *  than the current size, new rows are added to the end of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *  If the new size is less than the current size, all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *  rows at index <code>rowCount</code> and greater are discarded. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param   rowCount   the new number of rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @see #setRowCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public void setNumRows(int rowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int old = getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (old == rowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        dataVector.setSize(rowCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (rowCount <= old) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            fireTableRowsDeleted(rowCount, old-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            justifyRows(old, rowCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            fireTableRowsInserted(old, rowCount-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *  Sets the number of rows in the model.  If the new size is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *  than the current size, new rows are added to the end of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *  If the new size is less than the current size, all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *  rows at index <code>rowCount</code> and greater are discarded. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *  @see #setColumnCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public void setRowCount(int rowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        setNumRows(rowCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *  Adds a row to the end of the model.  The new row will contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *  <code>null</code> values unless <code>rowData</code> is specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *  Notification of the row being added will be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @param   rowData          optional data of the row being added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public void addRow(Vector rowData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        insertRow(getRowCount(), rowData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *  Adds a row to the end of the model.  The new row will contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *  <code>null</code> values unless <code>rowData</code> is specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *  Notification of the row being added will be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @param   rowData          optional data of the row being added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public void addRow(Object[] rowData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        addRow(convertToVector(rowData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *  Inserts a row at <code>row</code> in the model.  The new row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *  will contain <code>null</code> values unless <code>rowData</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *  is specified.  Notification of the row being added will be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param   row             the row index of the row to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param   rowData         optional data of the row being added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @exception  ArrayIndexOutOfBoundsException  if the row was invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public void insertRow(int row, Vector rowData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        dataVector.insertElementAt(rowData, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        justifyRows(row, row+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        fireTableRowsInserted(row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *  Inserts a row at <code>row</code> in the model.  The new row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *  will contain <code>null</code> values unless <code>rowData</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *  is specified.  Notification of the row being added will be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @param   row      the row index of the row to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param   rowData          optional data of the row being added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @exception  ArrayIndexOutOfBoundsException  if the row was invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public void insertRow(int row, Object[] rowData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        insertRow(row, convertToVector(rowData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    private static int gcd(int i, int j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return (j == 0) ? i : gcd(j, i%j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    private static void rotate(Vector v, int a, int b, int shift) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        int size = b - a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        int r = size - shift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        int g = gcd(size, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        for(int i = 0; i < g; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            int to = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            Object tmp = v.elementAt(a + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            for(int from = (to + r) % size; from != i; from = (to + r) % size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                v.setElementAt(v.elementAt(a + from), a + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                to = from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            v.setElementAt(tmp, a + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *  Moves one or more rows from the inclusive range <code>start</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *  <code>end</code> to the <code>to</code> position in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *  After the move, the row that was at index <code>start</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *  will be at index <code>to</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *  This method will send a <code>tableChanged</code> notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *  message to all the listeners. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *  <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *  Examples of moves:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *  1. moveRow(1,3,5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *          a|B|C|D|e|f|g|h|i|j|k   - before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *          a|e|f|g|h|B|C|D|i|j|k   - after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *  2. moveRow(6,7,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *          a|b|c|d|e|f|G|H|i|j|k   - before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *          a|G|H|b|c|d|e|f|i|j|k   - after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *  </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @param   start       the starting row index to be moved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param   end         the ending row index to be moved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @param   to          the destination of the rows to be moved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @exception  ArrayIndexOutOfBoundsException  if any of the elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * would be moved out of the table's range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public void moveRow(int start, int end, int to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        int shift = to - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        int first, last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (shift < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            first = to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            last = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            first = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            last = to + end - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        rotate(dataVector, first, last + 1, shift);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        fireTableRowsUpdated(first, last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *  Removes the row at <code>row</code> from the model.  Notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *  of the row being removed will be sent to all the listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @param   row      the row index of the row to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @exception  ArrayIndexOutOfBoundsException  if the row was invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    public void removeRow(int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        dataVector.removeElementAt(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        fireTableRowsDeleted(row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
// Manipulating columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Replaces the column identifiers in the model.  If the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * <code>newIdentifier</code>s is greater than the current number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * of columns, new columns are added to the end of each row in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * If the number of <code>newIdentifier</code>s is less than the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * number of columns, all the extra columns at the end of a row are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * discarded. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @param   columnIdentifiers  vector of column identifiers.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *                          <code>null</code>, set the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *                          to zero columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @see #setNumRows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    public void setColumnIdentifiers(Vector columnIdentifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        setDataVector(dataVector, columnIdentifiers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Replaces the column identifiers in the model.  If the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * <code>newIdentifier</code>s is greater than the current number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * of columns, new columns are added to the end of each row in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * If the number of <code>newIdentifier</code>s is less than the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * number of columns, all the extra columns at the end of a row are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * discarded. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @param   newIdentifiers  array of column identifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *                          If <code>null</code>, set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *                          the model to zero columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @see #setNumRows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public void setColumnIdentifiers(Object[] newIdentifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        setColumnIdentifiers(convertToVector(newIdentifiers));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *  Sets the number of columns in the model.  If the new size is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *  than the current size, new columns are added to the end of the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *  with <code>null</code> cell values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *  If the new size is less than the current size, all columns at index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *  <code>columnCount</code> and greater are discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *  @param columnCount  the new number of columns in the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *  @see #setColumnCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    public void setColumnCount(int columnCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        columnIdentifiers.setSize(columnCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        justifyRows(0, getRowCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        fireTableStructureChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *  Adds a column to the model.  The new column will have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *  identifier <code>columnName</code>, which may be null.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *  will send a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *  <code>tableChanged</code> notification message to all the listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *  This method is a cover for <code>addColumn(Object, Vector)</code> which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *  uses <code>null</code> as the data vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @param   columnName the identifier of the column being added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    public void addColumn(Object columnName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        addColumn(columnName, (Vector)null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *  Adds a column to the model.  The new column will have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *  identifier <code>columnName</code>, which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *  <code>columnData</code> is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *  optional vector of data for the column.  If it is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *  the column is filled with <code>null</code> values.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *  the new data will be added to model starting with the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *  element going to row 0, etc.  This method will send a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *  <code>tableChanged</code> notification message to all the listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @param   columnName the identifier of the column being added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @param   columnData       optional data of the column being added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    public void addColumn(Object columnName, Vector columnData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        columnIdentifiers.addElement(columnName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        if (columnData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            int columnSize = columnData.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            if (columnSize > getRowCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                dataVector.setSize(columnSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            justifyRows(0, getRowCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            int newColumn = getColumnCount() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            for(int i = 0; i < columnSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                  Vector row = (Vector)dataVector.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                  row.setElementAt(columnData.elementAt(i), newColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            justifyRows(0, getRowCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        fireTableStructureChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *  Adds a column to the model.  The new column will have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *  identifier <code>columnName</code>.  <code>columnData</code> is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *  optional array of data for the column.  If it is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *  the column is filled with <code>null</code> values.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *  the new data will be added to model starting with the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *  element going to row 0, etc.  This method will send a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *  <code>tableChanged</code> notification message to all the listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @see #addColumn(Object, Vector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    public void addColumn(Object columnName, Object[] columnData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        addColumn(columnName, convertToVector(columnData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
// Implementing the TableModel interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * Returns the number of rows in this data table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @return the number of rows in the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    public int getRowCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        return dataVector.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * Returns the number of columns in this data table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @return the number of columns in the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public int getColumnCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        return columnIdentifiers.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * Returns the column name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @return a name for this column using the string value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * appropriate member in <code>columnIdentifiers</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * If <code>columnIdentifiers</code> does not have an entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * for this index, returns the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * name provided by the superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    public String getColumnName(int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        Object id = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        // This test is to cover the case when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        // getColumnCount has been subclassed by mistake ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (column < columnIdentifiers.size() && (column >= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            id = columnIdentifiers.elementAt(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        return (id == null) ? super.getColumnName(column)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                            : id.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Returns true regardless of parameter values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @param   row             the row whose value is to be queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @param   column          the column whose value is to be queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @return                  true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @see #setValueAt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    public boolean isCellEditable(int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * Returns an attribute value for the cell at <code>row</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * and <code>column</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @param   row             the row whose value is to be queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @param   column          the column whose value is to be queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @return                  the value Object at the specified cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @exception  ArrayIndexOutOfBoundsException  if an invalid row or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *               column was given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    public Object getValueAt(int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        Vector rowVector = (Vector)dataVector.elementAt(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        return rowVector.elementAt(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * Sets the object value for the cell at <code>column</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * <code>row</code>.  <code>aValue</code> is the new value.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * will generate a <code>tableChanged</code> notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @param   aValue          the new value; this can be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @param   row             the row whose value is to be changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @param   column          the column whose value is to be changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @exception  ArrayIndexOutOfBoundsException  if an invalid row or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *               column was given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    public void setValueAt(Object aValue, int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        Vector rowVector = (Vector)dataVector.elementAt(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        rowVector.setElementAt(aValue, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        fireTableCellUpdated(row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
// Protected Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * Returns a vector that contains the same objects as the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @param anArray  the array to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @return  the new vector; if <code>anArray</code> is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *                          returns <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    protected static Vector convertToVector(Object[] anArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        if (anArray == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        Vector v = new Vector(anArray.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        for (int i=0; i < anArray.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            v.addElement(anArray[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * Returns a vector of vectors that contains the same objects as the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @param anArray  the double array to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @return the new vector of vectors; if <code>anArray</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *                          <code>null</code>, returns <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    protected static Vector convertToVector(Object[][] anArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        if (anArray == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        Vector v = new Vector(anArray.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        for (int i=0; i < anArray.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            v.addElement(convertToVector(anArray[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
} // End of class DefaultTableModel