/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.table;

import com.jstatcom.model.JSCTypes;
import com.jstatcom.model.Symbol;

/**
 * Implementation of JSCAbstractTableModel for
 * JSCSArray data objects. It takes the transposed
 * property into account.
 * 
 * @author Markus Kraetzig
 */
public class JSCSArrayTableModel extends JSCAbstractTableModel {
    private boolean transposed = false;

    /**
     * Constructs a table model for a symbol of type
     * JSCTypes.SARRAY. The argument can be null.
     * 
     * @param symbol
     *            must be of type JSCTypes.SARRAY, can be
     *            null
     * @throws IllegalArgumentException
     *             if (symbol.type != JSCTypes.SARRAY)
     */
    public JSCSArrayTableModel(Symbol symbol) {
        super(symbol);
        if (symbol != null)
            if (symbol.type != JSCTypes.SARRAY)
                throw new IllegalArgumentException("Symbol type " + symbol.type
                        + " different from table type " + JSCTypes.SARRAY + ".");
    }

    /*
     * @see javax.swing.table.TableModel#getColumnCount()
     */
    public int getColumnCount() {
        if (getCurrentSymbol() == null || getCurrentSymbol().isEmpty())
            return 0;

        if (isTransposed())
            return getCurrentSymbol().getJSCSArray().rows();

        return getCurrentSymbol().getJSCSArray().cols();

    }

    /*
     * @see javax.swing.table.TableModel#getRowCount()
     */
    public int getRowCount() {
        if (getCurrentSymbol() == null || getCurrentSymbol().isEmpty())
            return 0;

        if (isTransposed())
            return getCurrentSymbol().getJSCSArray().cols();

        return getCurrentSymbol().getJSCSArray().rows();
    }

    /*
     * @see javax.swing.table.TableModel#getValueAt(int, int)
     */
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (getCurrentSymbol() == null || getCurrentSymbol().isEmpty())
            return null;

        if (isTransposed())
            return getCurrentSymbol().getJSCSArray().stringAt(columnIndex,
                    rowIndex);

        return getCurrentSymbol().getJSCSArray()
                .stringAt(rowIndex, columnIndex);

    }

    /**
     * The data model is editable. This method sets a value in the underlying
     * data.
     * 
     * @param aValue
     *            must be non null
     * @param rowIndex
     *            the row index, will be treated with taking
     *            transposed into account
     * @param columnIndex
     *            the column index, will be treated with taking
     *            transposed into account
     * @throws IllegalArgumentException
     *             if (aValue == null)
     */
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if (aValue == null)
            throw new IllegalArgumentException("Value at [" + rowIndex + ":"
                    + columnIndex + "] is null.");

        if (getCurrentSymbol() == null || getCurrentSymbol().isEmpty())
            throw new IllegalStateException("Symbol is empty or null.");

        if (isTransposed())
            getCurrentSymbol().getJSCSArray().setValAt(aValue.toString(),
                    columnIndex, rowIndex);
        else
            getCurrentSymbol().getJSCSArray().setValAt(aValue.toString(),
                    rowIndex, columnIndex);

    }

    /**
     * Gets whether the underlying data array is transposed through this model.
     * 
     * @return true if transposed
     */
    public final boolean isTransposed() {
        return transposed;
    }

    /**
     * Sets whether the underlying data array is transposed through this model.
     * Subclasses must take this into account while overwriting
     * getRowCount, getColumnCount and
     * getValueAt. This property should not affect the original
     * data but only the model view.
     * 
     * @param isTransposed
     *            true if transposed
     */
    public final void setTransposed(boolean isTransposed) {
        transposed = isTransposed;
        fireTableStructureChanged();
    }

}

 

Ads help maintain this website.