/*
 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.equation;

import javax.swing.event.TableModelEvent;
import javax.swing.table.TableColumnModel;

import com.jstatcom.component.MatrixBorder;
import com.jstatcom.model.JSCNArray;
import com.jstatcom.model.JSCTypeDef;
import com.jstatcom.model.JSCTypes;
import com.jstatcom.model.SymbolEvent;
import com.jstatcom.model.SymbolListener;
import com.jstatcom.model.SymbolTable;
import com.jstatcom.table.JSCCellRendererTypes;
import com.jstatcom.table.JSCNArrayTable;

/**
 * Special JSCNArrayTable to display coefficients of a model.
 * Usually a CoeffTable is part of an EqTerm and
 * displayed in a EqPanel.
 * 
 * @author Alexander Benkwitz, Markus Kraetzig
 */
public final class CoeffTable extends JSCNArrayTable {
    private JSCTypeDef subsetResDef = null;

    private SubMatModel subMatrixModel = null;

    /**
     * Default constructor.
     */
    public CoeffTable() {
        this(-1);
    }

    /**
     * Creates a CoeffTable with an index denoting the part in the equation.
     * 
     * @param subsetIndex
     *            inded of equation part
     */
    public CoeffTable(int subsetIndex) {
        super();
        setBorder(new MatrixBorder());
        setEditable(false);
        setPrecision(3);
        if (subsetIndex >= 0) {
            subMatrixModel = new SubMatModel(subsetIndex);
            super.setColumnModel(subMatrixModel);
        }
        setCellRenderer(JSCCellRendererTypes.COEFF_DEFAULT);
        setRowSelectionAllowed(false);
    }

    /**
     * Gets the data object storing the subset restrictions or null
     * if no subset restrictions are set.
     * 
     * @return data object with subset restrictions
     */
    public JSCNArray getJSCNArraySubsetRes() {
        if (subsetResDef == null)
            return null;

        SymbolTable sTable = getSymbolTable();
        if (sTable == null) // not explicitely set
            sTable = getSymbolScope().getSymbolTable(this);

        if (sTable != null)
            return sTable.get(subsetResDef).getJSCNArray();

        return null;
    }

    /**
     * Gets the underlying SubMatModel.
     * 
     * @return sub matrix model
     */
    public SubMatModel getSubMatModel() {
        return subMatrixModel;
    }

    /**
     * Gets the symbolname for the subset restrictions or null if
     * none was set.
     * 
     * @return symbol name
     */
    public String getSymbolNameSubsetRes() {
        if (subsetResDef == null)
            return null;

        return subsetResDef.name;
    }

    /**
     * Manages visibility of this component.
     */
    private void makeVisible() {
        int c = getColumnCount();
        boolean b = isVisible();
        if (c < 1 && b)
            setVisible(false);
        if (c > 0 && !b)
            setVisible(true);
    }

    /**
     * Sets the column count of this table.
     * 
     * @param arg
     *            number of cols
     */
    public void setColumnCount(int arg) {
        if (subMatrixModel == null) {
            subMatrixModel = new SubMatModel(0);
            subMatrixModel.setNumberOfColumns(arg);
            super.setColumnModel(subMatrixModel);
        } else {
            int oldCols = getColumnCount();
            if (oldCols != arg) {
                subMatrixModel.setNumberOfColumns(arg);
                createDefaultColumnsFromModel();
            }
        }
        makeVisible();

    }

    /**
     * Sets the column model for all columns of this table. Works only, if a
     * submatrix model was set before.
     * 
     * @param newModel
     *            the table model
     * @throws IllegalStateException
     *             if (subMatrixModel != null)
     */
    public void setColumnModel(TableColumnModel newModel) {
        if (subMatrixModel != null)
            throw new IllegalStateException("No submatrix model set.");

        super.setColumnModel(newModel);
    }

    /**
     * Sets the index denoting the part in the equation for this table.
     * 
     * @param arg
     *            equation index
     */
    public void setIndex(int arg) {
        if (subMatrixModel == null) {
            subMatrixModel = new SubMatModel(arg);
            super.setColumnModel(subMatrixModel);
        } else
            subMatrixModel.setIndex(arg);

    }

    /**
     * Sets the symbol name of the subset restrictions that correspond to the
     * coefficients displayed in this table. How the restrictions affect the
     * behaviour of this table is determined by the cell renderer, mouse
     * listener, editor and table popup.
     * 
     * @param newSymbolNameSubsetRes
     * @throws IllegalArgumentException
     *             if newSymbolNameSubsetRes is not a valid name
     *             for a symbol
     */
    public void setSymbolNameSubsetRes(String newSymbolNameSubsetRes) {
        if (newSymbolNameSubsetRes == null) {
            subsetResDef = null;
            return;
        }

        subsetResDef = new JSCTypeDef(newSymbolNameSubsetRes, JSCTypes.NARRAY);

        SymbolTable sTable = getSymbolTable();
        if (sTable == null) // not explicitely set
            sTable = getSymbolScope().getSymbolTable(this);

        if (sTable != null) {
            sTable.get(subsetResDef).addSymbolListener(new SymbolListener() {
                public void valueChanged(SymbolEvent evt) {
                    resizeAndRepaint();
                }
            });
        }
    }

    /**
     * Extends the super method by firering the (boolean) property change
     * "visible".
     * 
     * @param aFlag
     *            true to make the component visible
     */
    public void setVisible(boolean aFlag) {
        boolean oldValue = isVisible();
        super.setVisible(aFlag);
        firePropertyChange("visible", oldValue, isVisible());
    }

    /**
     * Manages visibility of this table on table changed events.
     */
    public void tableChanged(TableModelEvent e) {
        super.tableChanged(e);
        makeVisible();
    }

}
 

Ads help maintain this website.