/* * @(#)DataTableModel.java 0.0.0 99/07/26 * * Copyright (c) 1999 by Willie Wheeler. All rights reserved. */ package stats; import java.util.*; import javax.swing.table.*; import lang.*; /** * A DefaultTableModel with the ability to store and return for * each column an associated Variable. * * @version 0.0.0 07/26/99 * @author Willie Wheeler */ public class DataTableModel extends DefaultTableModel { // Constructors. /** * Builds a DataTableModel with the specified number of rows * and columns. * * @param numRows number of rows * @param numColumns number of columns */ public DataTableModel(int numRows, int numColumns) { super(numRows, numColumns); Vector vars = new Vector(); for (int i = 0; i < numColumns; i++) { vars.add(new TextVariable("Variable " + i)); } setColumnIdentifiers(vars); } /** * Builds a DataTableModel, with column identifiers equal to * the passed variables. The column identifiers must be of type * Variable. * * @param variables variables acting as column identifiers */ public DataTableModel(Vector variables) { this(variables, 0); } /** * Builds a DataTableModel, with column identifiers equal to * the passed variables. * * @param variables variables acting as column identifiers */ public DataTableModel(Variable[] variables) { this(variables, 0); } /** * Builds a DataTableModel, with column identifiers equal to * the passed variables, and having the specified number of rows. The * vector elements must be of type Variable. * * @param variables variables acting as column identifiers * @param numRows number of rows */ public DataTableModel(Vector variables, int numRows) { super(variables, numRows); } /** * Builds a DataTableModel, with column identifiers equal to * the passed variables, and having the specified number of rows. * * @param variables variables acting as column identifiers * @param numRows number of rows */ public DataTableModel(Variable[] variables, int numRows) { this(convertToVector(variables), numRows); } /** * Builds a DataTableModel from the given table data, with * column identifiers equal to the passed variables. The table data must * be a vector of rows, each row in turn being a vector. The vector * elements must be of type Variable. * * @param data table data * @param variables variables acting as column identifiers */ public DataTableModel(Vector data, Vector variables) { super(data, variables); } /** * Builds a DataTableModel from the given table data, with * column identifiers equal to the passed variables. The table data must * be an array of rows, each row in turn being an array. * * @param data table data * @param variables variables acting as column identifiers */ public DataTableModel(Object[][] data, Variable[] variables) { this(convertToVector(data), convertToVector(variables)); } // Accessing table structure. /** * Returns the number of variables in this data table. This number is * exactly the same as the column count, since there is a 1-1 relationship * between the variables and the columns. * * @return number of variables in this data table */ public int getNumVariables() { return getColumnCount(); } /** * Sets the variables for this data table. This causes the table to be * restructured, since changing the variables amounts to changing the * columns. The vector elements must be of type Variable. * * @param variables variables acting as column identifiers */ public void setVariables(Vector variables) { setColumnIdentifiers(variables); } /** * Sets the variables for this data table. This causes the table to be * restructured, since changing the variables amounts to changing the * columns. * * @param variables variables acting as column identifiers */ public void setVariables(Variable[] variables) { setColumnIdentifiers(convertToVector(variables)); } /** * Returns all of the variables for this data table. * * @return all of the variables for this data table */ public Vector getVariables() { return columnIdentifiers; } /** * Returns a single variable for this data table. * * @param index variable index * @return the corresponding variable */ public Variable getVariable(int index) { return (Variable)columnIdentifiers.get(index); } /** * Returns the class associated with the column values. * * @param columnIndex column index * @return class associated with the column values */ public Class getColumnClass(int columnIndex) { return getVariable(columnIndex).getValueClass(); } // Accessing table content. /** * Returns the number of data in the table. This is exactly the same as * the row count. * * @return number of data in the data */ public int getNumData() { return getRowCount(); } /** * Sets the value of the specified cell. The new value is checked against * the column's variable, and if accepted, it is filtered through the * variable as well. For instance, setValueAt() will reject an * attempt to pass the string "Harvey" to a cell in a * ContinuousVariable column, since the values of a continuous * variable must be real. If the string "22.40" is passed, * then it will be filtered into a Double and then written to * the table. * * @param value the new value * @param rowIndex row index * @param columnIndex column index */ public void setValueAt(Object value, int rowIndex, int columnIndex) { Variable var = getVariable(columnIndex); if (var.checkValue(value)) { super.setValueAt(var.filterValue(value), rowIndex, columnIndex); } else { System.err.println("Rejecting value '" + value + "' for variable '" + var + "'."); } } /** * Clears the specified column with the clear value determined by the * column's variable. * * @param columnIndex column index */ public void clearColumn(int columnIndex) { int numData = getNumData(); Vector tableData = getDataVector(); Object clearValue = getVariable(columnIndex).getClearValue(); for (int i = 0; i < numData; i++) { ((Vector)tableData.get(i)).set(columnIndex, clearValue); } fireTableDataChanged(); } }