jdk/src/share/classes/javax/swing/table/DefaultTableModel.java
changeset 25568 b906a74c6882
parent 25172 b345c47c02b3
child 26351 4050cc242edf
equal deleted inserted replaced
25567:93f3ca259c48 25568:b906a74c6882
    68 
    68 
    69     /**
    69     /**
    70      * The <code>Vector</code> of <code>Vectors</code> of
    70      * The <code>Vector</code> of <code>Vectors</code> of
    71      * <code>Object</code> values.
    71      * <code>Object</code> values.
    72      */
    72      */
    73     protected Vector    dataVector;
    73     protected Vector<Vector<Object>>    dataVector;
    74 
    74 
    75     /** The <code>Vector</code> of column identifiers. */
    75     /** The <code>Vector</code> of column identifiers. */
    76     protected Vector    columnIdentifiers;
    76     protected Vector<Object>    columnIdentifiers;
    77 
    77 
    78 //
    78 //
    79 // Constructors
    79 // Constructors
    80 //
    80 //
    81 
    81 
    85      */
    85      */
    86     public DefaultTableModel() {
    86     public DefaultTableModel() {
    87         this(0, 0);
    87         this(0, 0);
    88     }
    88     }
    89 
    89 
    90     private static Vector newVector(int size) {
    90     private static <E> Vector<E> newVector(int size) {
    91         Vector v = new Vector(size);
    91         Vector<E> v = new Vector<>(size);
    92         v.setSize(size);
    92         v.setSize(size);
    93         return v;
    93         return v;
    94     }
    94     }
    95 
    95 
    96     /**
    96     /**
   119      *                          <code>null</code> then the model has no columns
   119      *                          <code>null</code> then the model has no columns
   120      * @param rowCount           the number of rows the table holds
   120      * @param rowCount           the number of rows the table holds
   121      * @see #setDataVector
   121      * @see #setDataVector
   122      * @see #setValueAt
   122      * @see #setValueAt
   123      */
   123      */
   124     public DefaultTableModel(Vector columnNames, int rowCount) {
   124     public DefaultTableModel(Vector<Object> columnNames, int rowCount) {
   125         setDataVector(newVector(rowCount), columnNames);
   125         setDataVector(newVector(rowCount), columnNames);
   126     }
   126     }
   127 
   127 
   128     /**
   128     /**
   129      *  Constructs a <code>DefaultTableModel</code> with as many
   129      *  Constructs a <code>DefaultTableModel</code> with as many
   154      * @param columnNames       <code>vector</code> containing the names
   154      * @param columnNames       <code>vector</code> containing the names
   155      *                          of the new columns
   155      *                          of the new columns
   156      * @see #getDataVector
   156      * @see #getDataVector
   157      * @see #setDataVector
   157      * @see #setDataVector
   158      */
   158      */
   159     public DefaultTableModel(Vector data, Vector columnNames) {
   159     public DefaultTableModel(Vector<Vector<Object>> data, Vector<Object> columnNames) {
   160         setDataVector(data, columnNames);
   160         setDataVector(data, columnNames);
   161     }
   161     }
   162 
   162 
   163     /**
   163     /**
   164      *  Constructs a <code>DefaultTableModel</code> and initializes the table
   164      *  Constructs a <code>DefaultTableModel</code> and initializes the table
   189      *
   189      *
   190      * @see #newDataAvailable
   190      * @see #newDataAvailable
   191      * @see #newRowsAdded
   191      * @see #newRowsAdded
   192      * @see #setDataVector
   192      * @see #setDataVector
   193      */
   193      */
   194     public Vector getDataVector() {
   194     public Vector<Vector<Object>> getDataVector() {
   195         return dataVector;
   195         return dataVector;
   196     }
   196     }
   197 
   197 
   198     private static Vector nonNullVector(Vector v) {
   198     private static <E> Vector<E> nonNullVector(Vector<E> v) {
   199         return (v != null) ? v : new Vector();
   199         return (v != null) ? v : new Vector<>();
   200     }
   200     }
   201 
   201 
   202     /**
   202     /**
   203      *  Replaces the current <code>dataVector</code> instance variable
   203      *  Replaces the current <code>dataVector</code> instance variable
   204      *  with the new <code>Vector</code> of rows, <code>dataVector</code>.
   204      *  with the new <code>Vector</code> of rows, <code>dataVector</code>.
   217      *
   217      *
   218      * @param   dataVector         the new data vector
   218      * @param   dataVector         the new data vector
   219      * @param   columnIdentifiers     the names of the columns
   219      * @param   columnIdentifiers     the names of the columns
   220      * @see #getDataVector
   220      * @see #getDataVector
   221      */
   221      */
   222     public void setDataVector(Vector dataVector, Vector columnIdentifiers) {
   222     public void setDataVector(Vector<Vector<Object>> dataVector,
       
   223                               Vector<Object> columnIdentifiers) {
   223         this.dataVector = nonNullVector(dataVector);
   224         this.dataVector = nonNullVector(dataVector);
   224         this.columnIdentifiers = nonNullVector(columnIdentifiers);
   225         this.columnIdentifiers = nonNullVector(columnIdentifiers);
   225         justifyRows(0, getRowCount());
   226         justifyRows(0, getRowCount());
   226         fireTableStructureChanged();
   227         fireTableStructureChanged();
   227     }
   228     }
   262         // is overridden.
   263         // is overridden.
   263         dataVector.setSize(getRowCount());
   264         dataVector.setSize(getRowCount());
   264 
   265 
   265         for (int i = from; i < to; i++) {
   266         for (int i = from; i < to; i++) {
   266             if (dataVector.elementAt(i) == null) {
   267             if (dataVector.elementAt(i) == null) {
   267                 dataVector.setElementAt(new Vector(), i);
   268                 dataVector.setElementAt(new Vector<>(), i);
   268             }
   269             }
   269             ((Vector)dataVector.elementAt(i)).setSize(getColumnCount());
   270             ((Vector)dataVector.elementAt(i)).setSize(getColumnCount());
   270         }
   271         }
   271     }
   272     }
   272 
   273 
   347      *  <code>null</code> values unless <code>rowData</code> is specified.
   348      *  <code>null</code> values unless <code>rowData</code> is specified.
   348      *  Notification of the row being added will be generated.
   349      *  Notification of the row being added will be generated.
   349      *
   350      *
   350      * @param   rowData          optional data of the row being added
   351      * @param   rowData          optional data of the row being added
   351      */
   352      */
   352     public void addRow(Vector rowData) {
   353     public void addRow(Vector<Object> rowData) {
   353         insertRow(getRowCount(), rowData);
   354         insertRow(getRowCount(), rowData);
   354     }
   355     }
   355 
   356 
   356     /**
   357     /**
   357      *  Adds a row to the end of the model.  The new row will contain
   358      *  Adds a row to the end of the model.  The new row will contain
   371      *
   372      *
   372      * @param   row             the row index of the row to be inserted
   373      * @param   row             the row index of the row to be inserted
   373      * @param   rowData         optional data of the row being added
   374      * @param   rowData         optional data of the row being added
   374      * @exception  ArrayIndexOutOfBoundsException  if the row was invalid
   375      * @exception  ArrayIndexOutOfBoundsException  if the row was invalid
   375      */
   376      */
   376     public void insertRow(int row, Vector rowData) {
   377     public void insertRow(int row, Vector<Object> rowData) {
   377         dataVector.insertElementAt(rowData, row);
   378         dataVector.insertElementAt(rowData, row);
   378         justifyRows(row, row+1);
   379         justifyRows(row, row+1);
   379         fireTableRowsInserted(row, row);
   380         fireTableRowsInserted(row, row);
   380     }
   381     }
   381 
   382 
   394 
   395 
   395     private static int gcd(int i, int j) {
   396     private static int gcd(int i, int j) {
   396         return (j == 0) ? i : gcd(j, i%j);
   397         return (j == 0) ? i : gcd(j, i%j);
   397     }
   398     }
   398 
   399 
   399     private static void rotate(Vector v, int a, int b, int shift) {
   400     private static <E> void rotate(Vector<E> v, int a, int b, int shift) {
   400         int size = b - a;
   401         int size = b - a;
   401         int r = size - shift;
   402         int r = size - shift;
   402         int g = gcd(size, r);
   403         int g = gcd(size, r);
   403         for(int i = 0; i < g; i++) {
   404         for(int i = 0; i < g; i++) {
   404             int to = i;
   405             int to = i;
   405             Object tmp = v.elementAt(a + to);
   406             E tmp = v.elementAt(a + to);
   406             for(int from = (to + r) % size; from != i; from = (to + r) % size) {
   407             for(int from = (to + r) % size; from != i; from = (to + r) % size) {
   407                 v.setElementAt(v.elementAt(a + from), a + to);
   408                 v.setElementAt(v.elementAt(a + from), a + to);
   408                 to = from;
   409                 to = from;
   409             }
   410             }
   410             v.setElementAt(tmp, a + to);
   411             v.setElementAt(tmp, a + to);
   481      * @param   columnIdentifiers  vector of column identifiers.  If
   482      * @param   columnIdentifiers  vector of column identifiers.  If
   482      *                          <code>null</code>, set the model
   483      *                          <code>null</code>, set the model
   483      *                          to zero columns
   484      *                          to zero columns
   484      * @see #setNumRows
   485      * @see #setNumRows
   485      */
   486      */
   486     public void setColumnIdentifiers(Vector columnIdentifiers) {
   487     public void setColumnIdentifiers(Vector<Object> columnIdentifiers) {
   487         setDataVector(dataVector, columnIdentifiers);
   488         setDataVector(dataVector, columnIdentifiers);
   488     }
   489     }
   489 
   490 
   490     /**
   491     /**
   491      * Replaces the column identifiers in the model.  If the number of
   492      * Replaces the column identifiers in the model.  If the number of
   531      *  uses <code>null</code> as the data vector.
   532      *  uses <code>null</code> as the data vector.
   532      *
   533      *
   533      * @param   columnName the identifier of the column being added
   534      * @param   columnName the identifier of the column being added
   534      */
   535      */
   535     public void addColumn(Object columnName) {
   536     public void addColumn(Object columnName) {
   536         addColumn(columnName, (Vector)null);
   537         addColumn(columnName, (Vector<Object>)null);
   537     }
   538     }
   538 
   539 
   539     /**
   540     /**
   540      *  Adds a column to the model.  The new column will have the
   541      *  Adds a column to the model.  The new column will have the
   541      *  identifier <code>columnName</code>, which may be null.
   542      *  identifier <code>columnName</code>, which may be null.
   547      *  <code>tableChanged</code> notification message to all the listeners.
   548      *  <code>tableChanged</code> notification message to all the listeners.
   548      *
   549      *
   549      * @param   columnName the identifier of the column being added
   550      * @param   columnName the identifier of the column being added
   550      * @param   columnData       optional data of the column being added
   551      * @param   columnData       optional data of the column being added
   551      */
   552      */
   552     public void addColumn(Object columnName, Vector columnData) {
   553     public void addColumn(Object columnName, Vector<Object> columnData) {
   553         columnIdentifiers.addElement(columnName);
   554         columnIdentifiers.addElement(columnName);
   554         if (columnData != null) {
   555         if (columnData != null) {
   555             int columnSize = columnData.size();
   556             int columnSize = columnData.size();
   556             if (columnSize > getRowCount()) {
   557             if (columnSize > getRowCount()) {
   557                 dataVector.setSize(columnSize);
   558                 dataVector.setSize(columnSize);
   558             }
   559             }
   559             justifyRows(0, getRowCount());
   560             justifyRows(0, getRowCount());
   560             int newColumn = getColumnCount() - 1;
   561             int newColumn = getColumnCount() - 1;
   561             for(int i = 0; i < columnSize; i++) {
   562             for(int i = 0; i < columnSize; i++) {
   562                   Vector row = (Vector)dataVector.elementAt(i);
   563                   Vector<Object> row = dataVector.elementAt(i);
   563                   row.setElementAt(columnData.elementAt(i), newColumn);
   564                   row.setElementAt(columnData.elementAt(i), newColumn);
   564             }
   565             }
   565         }
   566         }
   566         else {
   567         else {
   567             justifyRows(0, getRowCount());
   568             justifyRows(0, getRowCount());
   649      * @return                  the value Object at the specified cell
   650      * @return                  the value Object at the specified cell
   650      * @exception  ArrayIndexOutOfBoundsException  if an invalid row or
   651      * @exception  ArrayIndexOutOfBoundsException  if an invalid row or
   651      *               column was given
   652      *               column was given
   652      */
   653      */
   653     public Object getValueAt(int row, int column) {
   654     public Object getValueAt(int row, int column) {
   654         Vector rowVector = (Vector)dataVector.elementAt(row);
   655         Vector<Object> rowVector = dataVector.elementAt(row);
   655         return rowVector.elementAt(column);
   656         return rowVector.elementAt(column);
   656     }
   657     }
   657 
   658 
   658     /**
   659     /**
   659      * Sets the object value for the cell at <code>column</code> and
   660      * Sets the object value for the cell at <code>column</code> and
   665      * @param   column          the column whose value is to be changed
   666      * @param   column          the column whose value is to be changed
   666      * @exception  ArrayIndexOutOfBoundsException  if an invalid row or
   667      * @exception  ArrayIndexOutOfBoundsException  if an invalid row or
   667      *               column was given
   668      *               column was given
   668      */
   669      */
   669     public void setValueAt(Object aValue, int row, int column) {
   670     public void setValueAt(Object aValue, int row, int column) {
   670         Vector rowVector = (Vector)dataVector.elementAt(row);
   671         Vector<Object> rowVector = dataVector.elementAt(row);
   671         rowVector.setElementAt(aValue, column);
   672         rowVector.setElementAt(aValue, column);
   672         fireTableCellUpdated(row, column);
   673         fireTableCellUpdated(row, column);
   673     }
   674     }
   674 
   675 
   675 //
   676 //
   680      * Returns a vector that contains the same objects as the array.
   681      * Returns a vector that contains the same objects as the array.
   681      * @param anArray  the array to be converted
   682      * @param anArray  the array to be converted
   682      * @return  the new vector; if <code>anArray</code> is <code>null</code>,
   683      * @return  the new vector; if <code>anArray</code> is <code>null</code>,
   683      *                          returns <code>null</code>
   684      *                          returns <code>null</code>
   684      */
   685      */
   685     protected static Vector convertToVector(Object[] anArray) {
   686     protected static Vector<Object> convertToVector(Object[] anArray) {
   686         if (anArray == null) {
   687         if (anArray == null) {
   687             return null;
   688             return null;
   688         }
   689         }
   689         Vector<Object> v = new Vector<Object>(anArray.length);
   690         Vector<Object> v = new Vector<>(anArray.length);
   690         for (Object o : anArray) {
   691         for (Object o : anArray) {
   691             v.addElement(o);
   692             v.addElement(o);
   692         }
   693         }
   693         return v;
   694         return v;
   694     }
   695     }
   697      * Returns a vector of vectors that contains the same objects as the array.
   698      * Returns a vector of vectors that contains the same objects as the array.
   698      * @param anArray  the double array to be converted
   699      * @param anArray  the double array to be converted
   699      * @return the new vector of vectors; if <code>anArray</code> is
   700      * @return the new vector of vectors; if <code>anArray</code> is
   700      *                          <code>null</code>, returns <code>null</code>
   701      *                          <code>null</code>, returns <code>null</code>
   701      */
   702      */
   702     protected static Vector convertToVector(Object[][] anArray) {
   703     protected static Vector<Vector<Object>> convertToVector(Object[][] anArray) {
   703         if (anArray == null) {
   704         if (anArray == null) {
   704             return null;
   705             return null;
   705         }
   706         }
   706         Vector<Vector> v = new Vector<Vector>(anArray.length);
   707         Vector<Vector<Object>> v = new Vector<>(anArray.length);
   707         for (Object[] o : anArray) {
   708         for (Object[] o : anArray) {
   708             v.addElement(convertToVector(o));
   709             v.addElement(convertToVector(o));
   709         }
   710         }
   710         return v;
   711         return v;
   711     }
   712     }