56 *
57 * @author Philip Milne
58 *
59 * @see TableModel
60 * @see #getDataVector
61 */
62 @SuppressWarnings("serial") // Same-version serialization only
63 public class DefaultTableModel extends AbstractTableModel implements Serializable {
64
65 //
66 // Instance Variables
67 //
68
69 /**
70 * The <code>Vector</code> of <code>Vectors</code> of
71 * <code>Object</code> values.
72 */
73 protected Vector<Vector<Object>> dataVector;
74
75 /** The <code>Vector</code> of column identifiers. */
76 protected Vector<Object> columnIdentifiers;
77
78 //
79 // Constructors
80 //
81
82 /**
83 * Constructs a default <code>DefaultTableModel</code>
84 * which is a table of zero columns and zero rows.
85 */
86 public DefaultTableModel() {
87 this(0, 0);
88 }
89
90 private static <E> Vector<E> newVector(int size) {
91 Vector<E> v = new Vector<>(size);
92 v.setSize(size);
93 return v;
94 }
95
96 /**
139 * @see #setDataVector
140 * @see #setValueAt
141 */
142 public DefaultTableModel(Object[] columnNames, int rowCount) {
143 this(convertToVector(columnNames), rowCount);
144 }
145
146 /**
147 * Constructs a <code>DefaultTableModel</code> and initializes the table
148 * by passing <code>data</code> and <code>columnNames</code>
149 * to the <code>setDataVector</code> method.
150 *
151 * @param data the data of the table, a <code>Vector</code>
152 * of <code>Vector</code>s of <code>Object</code>
153 * values
154 * @param columnNames <code>vector</code> containing the names
155 * of the new columns
156 * @see #getDataVector
157 * @see #setDataVector
158 */
159 public DefaultTableModel(Vector<Vector<Object>> data, Vector<Object> columnNames) {
160 setDataVector(data, columnNames);
161 }
162
163 /**
164 * Constructs a <code>DefaultTableModel</code> and initializes the table
165 * by passing <code>data</code> and <code>columnNames</code>
166 * to the <code>setDataVector</code>
167 * method. The first index in the <code>Object[][]</code> array is
168 * the row index and the second is the column index.
169 *
170 * @param data the data of the table
171 * @param columnNames the names of the columns
172 * @see #getDataVector
173 * @see #setDataVector
174 */
175 public DefaultTableModel(Object[][] data, Object[] columnNames) {
176 setDataVector(data, columnNames);
177 }
178
179 /**
203 * Replaces the current <code>dataVector</code> instance variable
204 * with the new <code>Vector</code> of rows, <code>dataVector</code>.
205 * Each row is represented in <code>dataVector</code> as a
206 * <code>Vector</code> of <code>Object</code> values.
207 * <code>columnIdentifiers</code> are the names of the new
208 * columns. The first name in <code>columnIdentifiers</code> is
209 * mapped to column 0 in <code>dataVector</code>. Each row in
210 * <code>dataVector</code> is adjusted to match the number of
211 * columns in <code>columnIdentifiers</code>
212 * either by truncating the <code>Vector</code> if it is too long,
213 * or adding <code>null</code> values if it is too short.
214 * <p>Note that passing in a <code>null</code> value for
215 * <code>dataVector</code> results in unspecified behavior,
216 * an possibly an exception.
217 *
218 * @param dataVector the new data vector
219 * @param columnIdentifiers the names of the columns
220 * @see #getDataVector
221 */
222 public void setDataVector(Vector<Vector<Object>> dataVector,
223 Vector<Object> columnIdentifiers) {
224 this.dataVector = nonNullVector(dataVector);
225 this.columnIdentifiers = nonNullVector(columnIdentifiers);
226 justifyRows(0, getRowCount());
227 fireTableStructureChanged();
228 }
229
230 /**
231 * Replaces the value in the <code>dataVector</code> instance
232 * variable with the values in the array <code>dataVector</code>.
233 * The first index in the <code>Object[][]</code>
234 * array is the row index and the second is the column index.
235 * <code>columnIdentifiers</code> are the names of the new columns.
236 *
237 * @param dataVector the new data vector
238 * @param columnIdentifiers the names of the columns
239 * @see #setDataVector(Vector, Vector)
240 */
241 public void setDataVector(Object[][] dataVector, Object[] columnIdentifiers) {
242 setDataVector(convertToVector(dataVector), convertToVector(columnIdentifiers));
243 }
467 fireTableRowsDeleted(row, row);
468 }
469
470 //
471 // Manipulating columns
472 //
473
474 /**
475 * Replaces the column identifiers in the model. If the number of
476 * <code>newIdentifier</code>s is greater than the current number
477 * of columns, new columns are added to the end of each row in the model.
478 * If the number of <code>newIdentifier</code>s is less than the current
479 * number of columns, all the extra columns at the end of a row are
480 * discarded.
481 *
482 * @param columnIdentifiers vector of column identifiers. If
483 * <code>null</code>, set the model
484 * to zero columns
485 * @see #setNumRows
486 */
487 public void setColumnIdentifiers(Vector<Object> columnIdentifiers) {
488 setDataVector(dataVector, columnIdentifiers);
489 }
490
491 /**
492 * Replaces the column identifiers in the model. If the number of
493 * <code>newIdentifier</code>s is greater than the current number
494 * of columns, new columns are added to the end of each row in the model.
495 * If the number of <code>newIdentifier</code>s is less than the current
496 * number of columns, all the extra columns at the end of a row are
497 * discarded.
498 *
499 * @param newIdentifiers array of column identifiers.
500 * If <code>null</code>, set
501 * the model to zero columns
502 * @see #setNumRows
503 */
504 public void setColumnIdentifiers(Object[] newIdentifiers) {
505 setColumnIdentifiers(convertToVector(newIdentifiers));
506 }
507
533 *
534 * @param columnName the identifier of the column being added
535 */
536 public void addColumn(Object columnName) {
537 addColumn(columnName, (Vector<Object>)null);
538 }
539
540 /**
541 * Adds a column to the model. The new column will have the
542 * identifier <code>columnName</code>, which may be null.
543 * <code>columnData</code> is the
544 * optional vector of data for the column. If it is <code>null</code>
545 * the column is filled with <code>null</code> values. Otherwise,
546 * the new data will be added to model starting with the first
547 * element going to row 0, etc. This method will send a
548 * <code>tableChanged</code> notification message to all the listeners.
549 *
550 * @param columnName the identifier of the column being added
551 * @param columnData optional data of the column being added
552 */
553 public void addColumn(Object columnName, Vector<Object> columnData) {
554 columnIdentifiers.addElement(columnName);
555 if (columnData != null) {
556 int columnSize = columnData.size();
557 if (columnSize > getRowCount()) {
558 dataVector.setSize(columnSize);
559 }
560 justifyRows(0, getRowCount());
561 int newColumn = getColumnCount() - 1;
562 for(int i = 0; i < columnSize; i++) {
563 Vector<Object> row = dataVector.elementAt(i);
564 row.setElementAt(columnData.elementAt(i), newColumn);
565 }
566 }
567 else {
568 justifyRows(0, getRowCount());
569 }
570
571 fireTableStructureChanged();
572 }
635 * @param row the row whose value is to be queried
636 * @param column the column whose value is to be queried
637 * @return true
638 * @see #setValueAt
639 */
640 public boolean isCellEditable(int row, int column) {
641 return true;
642 }
643
644 /**
645 * Returns an attribute value for the cell at <code>row</code>
646 * and <code>column</code>.
647 *
648 * @param row the row whose value is to be queried
649 * @param column the column whose value is to be queried
650 * @return the value Object at the specified cell
651 * @exception ArrayIndexOutOfBoundsException if an invalid row or
652 * column was given
653 */
654 public Object getValueAt(int row, int column) {
655 Vector<Object> rowVector = dataVector.elementAt(row);
656 return rowVector.elementAt(column);
657 }
658
659 /**
660 * Sets the object value for the cell at <code>column</code> and
661 * <code>row</code>. <code>aValue</code> is the new value. This method
662 * will generate a <code>tableChanged</code> notification.
663 *
664 * @param aValue the new value; this can be null
665 * @param row the row whose value is to be changed
666 * @param column the column whose value is to be changed
667 * @exception ArrayIndexOutOfBoundsException if an invalid row or
668 * column was given
669 */
670 public void setValueAt(Object aValue, int row, int column) {
671 Vector<Object> rowVector = dataVector.elementAt(row);
672 rowVector.setElementAt(aValue, column);
673 fireTableCellUpdated(row, column);
674 }
675
676 //
677 // Protected Methods
678 //
679
680 /**
681 * Returns a vector that contains the same objects as the array.
682 * @param anArray the array to be converted
683 * @return the new vector; if <code>anArray</code> is <code>null</code>,
684 * returns <code>null</code>
685 */
686 protected static Vector<Object> convertToVector(Object[] anArray) {
687 if (anArray == null) {
688 return null;
689 }
690 Vector<Object> v = new Vector<>(anArray.length);
691 for (Object o : anArray) {
692 v.addElement(o);
|
56 *
57 * @author Philip Milne
58 *
59 * @see TableModel
60 * @see #getDataVector
61 */
62 @SuppressWarnings("serial") // Same-version serialization only
63 public class DefaultTableModel extends AbstractTableModel implements Serializable {
64
65 //
66 // Instance Variables
67 //
68
69 /**
70 * The <code>Vector</code> of <code>Vectors</code> of
71 * <code>Object</code> values.
72 */
73 protected Vector<Vector<Object>> dataVector;
74
75 /** The <code>Vector</code> of column identifiers. */
76 @SuppressWarnings("rawtypes")
77 protected Vector columnIdentifiers;
78 // Unfortunately, for greater source compatibility this Vector is
79 // being left raw. The Vector is read as well as written so using
80 // Vector<?> is not suitable and using Vector<Object> (without
81 // adding copying of input Vectors), would disallow existing code
82 // that used, say, a Vector<String> as an input parameter.
83
84 //
85 // Constructors
86 //
87
88 /**
89 * Constructs a default <code>DefaultTableModel</code>
90 * which is a table of zero columns and zero rows.
91 */
92 public DefaultTableModel() {
93 this(0, 0);
94 }
95
96 private static <E> Vector<E> newVector(int size) {
97 Vector<E> v = new Vector<>(size);
98 v.setSize(size);
99 return v;
100 }
101
102 /**
145 * @see #setDataVector
146 * @see #setValueAt
147 */
148 public DefaultTableModel(Object[] columnNames, int rowCount) {
149 this(convertToVector(columnNames), rowCount);
150 }
151
152 /**
153 * Constructs a <code>DefaultTableModel</code> and initializes the table
154 * by passing <code>data</code> and <code>columnNames</code>
155 * to the <code>setDataVector</code> method.
156 *
157 * @param data the data of the table, a <code>Vector</code>
158 * of <code>Vector</code>s of <code>Object</code>
159 * values
160 * @param columnNames <code>vector</code> containing the names
161 * of the new columns
162 * @see #getDataVector
163 * @see #setDataVector
164 */
165 public DefaultTableModel(Vector<Vector<Object>> data, Vector<?> columnNames) {
166 setDataVector(data, columnNames);
167 }
168
169 /**
170 * Constructs a <code>DefaultTableModel</code> and initializes the table
171 * by passing <code>data</code> and <code>columnNames</code>
172 * to the <code>setDataVector</code>
173 * method. The first index in the <code>Object[][]</code> array is
174 * the row index and the second is the column index.
175 *
176 * @param data the data of the table
177 * @param columnNames the names of the columns
178 * @see #getDataVector
179 * @see #setDataVector
180 */
181 public DefaultTableModel(Object[][] data, Object[] columnNames) {
182 setDataVector(data, columnNames);
183 }
184
185 /**
209 * Replaces the current <code>dataVector</code> instance variable
210 * with the new <code>Vector</code> of rows, <code>dataVector</code>.
211 * Each row is represented in <code>dataVector</code> as a
212 * <code>Vector</code> of <code>Object</code> values.
213 * <code>columnIdentifiers</code> are the names of the new
214 * columns. The first name in <code>columnIdentifiers</code> is
215 * mapped to column 0 in <code>dataVector</code>. Each row in
216 * <code>dataVector</code> is adjusted to match the number of
217 * columns in <code>columnIdentifiers</code>
218 * either by truncating the <code>Vector</code> if it is too long,
219 * or adding <code>null</code> values if it is too short.
220 * <p>Note that passing in a <code>null</code> value for
221 * <code>dataVector</code> results in unspecified behavior,
222 * an possibly an exception.
223 *
224 * @param dataVector the new data vector
225 * @param columnIdentifiers the names of the columns
226 * @see #getDataVector
227 */
228 public void setDataVector(Vector<Vector<Object>> dataVector,
229 Vector<?> columnIdentifiers) {
230 this.dataVector = nonNullVector(dataVector);
231 this.columnIdentifiers = nonNullVector(columnIdentifiers);
232 justifyRows(0, getRowCount());
233 fireTableStructureChanged();
234 }
235
236 /**
237 * Replaces the value in the <code>dataVector</code> instance
238 * variable with the values in the array <code>dataVector</code>.
239 * The first index in the <code>Object[][]</code>
240 * array is the row index and the second is the column index.
241 * <code>columnIdentifiers</code> are the names of the new columns.
242 *
243 * @param dataVector the new data vector
244 * @param columnIdentifiers the names of the columns
245 * @see #setDataVector(Vector, Vector)
246 */
247 public void setDataVector(Object[][] dataVector, Object[] columnIdentifiers) {
248 setDataVector(convertToVector(dataVector), convertToVector(columnIdentifiers));
249 }
473 fireTableRowsDeleted(row, row);
474 }
475
476 //
477 // Manipulating columns
478 //
479
480 /**
481 * Replaces the column identifiers in the model. If the number of
482 * <code>newIdentifier</code>s is greater than the current number
483 * of columns, new columns are added to the end of each row in the model.
484 * If the number of <code>newIdentifier</code>s is less than the current
485 * number of columns, all the extra columns at the end of a row are
486 * discarded.
487 *
488 * @param columnIdentifiers vector of column identifiers. If
489 * <code>null</code>, set the model
490 * to zero columns
491 * @see #setNumRows
492 */
493 public void setColumnIdentifiers(Vector<?> columnIdentifiers) {
494 setDataVector(dataVector, columnIdentifiers);
495 }
496
497 /**
498 * Replaces the column identifiers in the model. If the number of
499 * <code>newIdentifier</code>s is greater than the current number
500 * of columns, new columns are added to the end of each row in the model.
501 * If the number of <code>newIdentifier</code>s is less than the current
502 * number of columns, all the extra columns at the end of a row are
503 * discarded.
504 *
505 * @param newIdentifiers array of column identifiers.
506 * If <code>null</code>, set
507 * the model to zero columns
508 * @see #setNumRows
509 */
510 public void setColumnIdentifiers(Object[] newIdentifiers) {
511 setColumnIdentifiers(convertToVector(newIdentifiers));
512 }
513
539 *
540 * @param columnName the identifier of the column being added
541 */
542 public void addColumn(Object columnName) {
543 addColumn(columnName, (Vector<Object>)null);
544 }
545
546 /**
547 * Adds a column to the model. The new column will have the
548 * identifier <code>columnName</code>, which may be null.
549 * <code>columnData</code> is the
550 * optional vector of data for the column. If it is <code>null</code>
551 * the column is filled with <code>null</code> values. Otherwise,
552 * the new data will be added to model starting with the first
553 * element going to row 0, etc. This method will send a
554 * <code>tableChanged</code> notification message to all the listeners.
555 *
556 * @param columnName the identifier of the column being added
557 * @param columnData optional data of the column being added
558 */
559 @SuppressWarnings("unchecked") // Adding element to raw columnIdentifiers
560 public void addColumn(Object columnName, Vector<Object> columnData) {
561 columnIdentifiers.addElement(columnName);
562 if (columnData != null) {
563 int columnSize = columnData.size();
564 if (columnSize > getRowCount()) {
565 dataVector.setSize(columnSize);
566 }
567 justifyRows(0, getRowCount());
568 int newColumn = getColumnCount() - 1;
569 for(int i = 0; i < columnSize; i++) {
570 Vector<Object> row = dataVector.elementAt(i);
571 row.setElementAt(columnData.elementAt(i), newColumn);
572 }
573 }
574 else {
575 justifyRows(0, getRowCount());
576 }
577
578 fireTableStructureChanged();
579 }
642 * @param row the row whose value is to be queried
643 * @param column the column whose value is to be queried
644 * @return true
645 * @see #setValueAt
646 */
647 public boolean isCellEditable(int row, int column) {
648 return true;
649 }
650
651 /**
652 * Returns an attribute value for the cell at <code>row</code>
653 * and <code>column</code>.
654 *
655 * @param row the row whose value is to be queried
656 * @param column the column whose value is to be queried
657 * @return the value Object at the specified cell
658 * @exception ArrayIndexOutOfBoundsException if an invalid row or
659 * column was given
660 */
661 public Object getValueAt(int row, int column) {
662 return dataVector.elementAt(row).elementAt(column);
663 }
664
665 /**
666 * Sets the object value for the cell at <code>column</code> and
667 * <code>row</code>. <code>aValue</code> is the new value. This method
668 * will generate a <code>tableChanged</code> notification.
669 *
670 * @param aValue the new value; this can be null
671 * @param row the row whose value is to be changed
672 * @param column the column whose value is to be changed
673 * @exception ArrayIndexOutOfBoundsException if an invalid row or
674 * column was given
675 */
676 public void setValueAt(Object aValue, int row, int column) {
677 dataVector.elementAt(row).setElementAt(aValue, column);
678 fireTableCellUpdated(row, column);
679 }
680
681 //
682 // Protected Methods
683 //
684
685 /**
686 * Returns a vector that contains the same objects as the array.
687 * @param anArray the array to be converted
688 * @return the new vector; if <code>anArray</code> is <code>null</code>,
689 * returns <code>null</code>
690 */
691 protected static Vector<Object> convertToVector(Object[] anArray) {
692 if (anArray == null) {
693 return null;
694 }
695 Vector<Object> v = new Vector<>(anArray.length);
696 for (Object o : anArray) {
697 v.addElement(o);
|