/*
 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 java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeListener;

import javax.swing.JLabel;
import javax.swing.JPopupMenu;
import javax.swing.table.TableCellRenderer;

import com.jstatcom.model.Scope;
import com.jstatcom.table.JSCPopupTypes;

/**
 * Default implementation of EqTerm.
 * 
 * @author A. Benkwitz, Markus Kraetzig
 */
public class EqTermDefault extends EqTerm implements PropertyChangeListener {
	private CoeffTable coeff;
	private VarNamesVector variables;
	private JLabel leadingSign;
	private boolean coeffMatRequired = false;
	private MouseListener mouseListenerCoeff = null;
	private JPopupMenu tablePopup = JSCPopupTypes.PRECISION;
	private MouseListener triggerPopUp = null;
	/**
	 * Default constructor that sets submatrix index to -1 and
	 * lag index to 0.
	 */
	public EqTermDefault() {
		this(-1, 0);
	}

	/**
	 * Creates an EqTermDefault for a sub matrix index.
	 * The lag index is set to 0.
	 * 
	 * @param subMatrixIndex the index in the sub matrix model
	 */
	public EqTermDefault(int subMatrixIndex) {
		this(subMatrixIndex, 0);
	}

	/**
	 * Creates an EqTermDefault for a sub matrix index
	 * and a lag index.
	 * 
	 * @param subMatrixIndex the index in the sub matrix model
	 * @param lagIndex the index for the lag
	 */
	public EqTermDefault(int subMatrixIndex, int lagIndex) {
		super(new FlowLayout(FlowLayout.LEFT, 0, 0));
		leadingSign = new JLabel(" + ");

		coeff = new CoeffTable(subMatrixIndex);
		coeff.setVisible(false);

		variables = new VarNamesVector(lagIndex);
		variables.setCoeffTable(coeff);
		variables.setVisible(false);

		coeff.addPropertyChangeListener(this);
		variables.addPropertyChangeListener(this);

		add(leadingSign);
		add(coeff);
		add(variables);

		// popup managed by EqTermDefault, 
		coeff.setTablePopup(null);
		setTablePopup(tablePopup);
	}

	/**
	 * Gets the column width of the underlying coefficients table.
	 * 
	 * @return column width
	 */
	public int getColumnWidth() {
		return coeff.getColumnWidth();
	}

	/**
	 * Gets the index for the lag.
	 * 
	 * @return lag index
	 */
	public int getLagIndex() {
		return variables.getIndex();
	}

	/**
	 * Gets the mouse listener set for the coefficients table.
	 * 
	 * @return java.awt.event.MouseListener
	 */
	public MouseListener getMouseListenerCoeff() {
		return mouseListenerCoeff;
	}

	/**
	 * Gets the precision of the underlying coefficients table.
	 * 
	 * @return precision
	 */
	public int getPrecision() {
		return coeff.getPrecision();
	}

	/**
	 * Gets the cell renderer of the underlying coefficients table.
	 * 
	 * @return javax.swing.table.TableCellRenderer
	 */
	public TableCellRenderer getRendererCoeff() {
		return coeff.getCellRenderer();
	}

	/**
	 * Gets the symbol name for the coefficients.
	 * 
	 * @return symbol name
	 */
	public String getSymbolNameCoeff() {
		return coeff.getSymbolName();
	}

	/**
	 * Gets the symbol name for the subset restrictions
	 * that are set for the coefficients.
	 * 
	 * @return symbol name
	 */
	public String getSymbolNameSubsetRes() {
		return coeff.getSymbolNameSubsetRes();
	}

	/**
	 * Gets the symbol name for the variable names 
	 * corresponding to the coefficients.
	 * 
	 * @return symbol name
	 */
	public String getSymbolNameVariables() {
		return variables.getSymbolName();
	}

	/**
	 * Gets the popup menu that is set for the 
	 * underlying coefficients table.
	 * 
	 * @return popup menu
	 */
	public JPopupMenu getTablePopup() {
		return tablePopup;
	}

	/**
	 * Gets whether a coefficients matrix is required
	 * to display this equation term. It might be false
	 * to just display variable names, for example for the residuals.
	 * 
	 * @return true if coefficients are required
	 */
	public boolean isCoeffMatRequired() {
		return coeffMatRequired;
	}

	/**
	 * Gets whether the underlying coefficients table is editable.
	 * 
	 * @return true if editable
	 */
	public boolean isEditable() {
		return coeff.isEditable();
	}

	/**
	 * Gets whether the matrix displaying the coefficients
	 * is centered in the term. This is not true for an EC term,
	 * for example.
	 * 
	 * @return true if centered
	 */
	public boolean isMatrixCentered() {
		return getLayout() instanceof FlowLayout;
	}

	/**
	 * Gets whether the variable names vector should 
	 * have a time index to indicate the lag, 
	 * for example income(t) or income(t-1). This should be 
	 * false for deterministic variables.
	 * 
	 * @return true if time index shown
	 */
	public boolean isShowingTimeIndex() {
		return variables.isShowingTimeIndex();
	}

	/**
	 * Manages visibility of this component with respect to
	 * the available coefficients and the coeffMatRequired
	 * property.
	 * 
	 * @param evt property change event
	 */
	public void propertyChange(java.beans.PropertyChangeEvent evt) {
		if (evt.getPropertyName() == "visible") {
			boolean b = true;
			if (coeffMatRequired) {
				b = coeff.isVisible();
			}
			b = b && variables.isVisible();
			if (b != isVisible()) {
				// also sets term visible/invisible	
				setDataAvailable(b);
			}
		}
	}

	/**
	 * Sets whether a coefficients matrix is required
	 * to display this equation term. It might be false
	 * to just display variable names, for example for the residuals.
	 * 
	 * @param isCoeffMatRequired true if coefficients are required
	 */
	public void setCoeffMatRequired(boolean isCoeffMatRequired) {
		coeffMatRequired = isCoeffMatRequired;
	}

	/**
	 * Sets the column width of the underlying coefficients
	 * table.
	 * 
	 * @param colWidth column width
	 */
	public void setColumnWidth(int colWidth) {
		coeff.setColumnWidth(colWidth);
	}

	/**
	 * Sets whether the underlying coefficients table is 
	 * editable.
	 * 
	 * @param isEditable true if editable
	 */
	public void setEditable(boolean isEditable) {
		coeff.setEditable(isEditable);
	}

	/**
	 * Sets the lag index of this equation term. It determines
	 * how the time index is created for the variables.
	 * 
	 * @param index lag
	 */
	public void setLagIndex(int index) {
		variables.setIndex(index);
	}

	/**
	 * Manages visibility of the leading sign label.
	 * 
	 * @param isVisible true if visible
	 */
	public void setLeadingSignVisible(boolean isVisible) {
		leadingSign.setVisible(isVisible);
	}

	/**
	 * Sets whether the matrix displaying the coefficients
	 * is centered in the term. This is not true 
	 * for an EC term, for example.
	 * 
	 * @param centered true if centered
	 */
	public void setMatrixCentered(boolean centered) {
		if (centered)
			setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
		else {
			GridBagLayout gridBagLayout = new GridBagLayout();
			setLayout(gridBagLayout);
			GridBagConstraints c = new GridBagConstraints();
			c.fill = GridBagConstraints.VERTICAL;
			c.weightx = 0.0;
			c.weighty = 0.0;
			c.gridx = 0;
			c.gridy = 0;
			gridBagLayout.setConstraints(leadingSign, c);
			//
			c.fill = GridBagConstraints.NONE;
			c.gridx = 1;
			c.anchor = GridBagConstraints.NORTH;
			gridBagLayout.setConstraints(coeff, c);
			//
			c.gridx = 2;
			c.anchor = GridBagConstraints.NORTH;
			gridBagLayout.setConstraints(variables, c);
		}
	}

	/**
	 * Sets the mouse listener for the underlying coefficients table.
	 * 
	 * @param listener java.awt.event.MouseListener
	 */
	public void setMouseListenerCoeff(java.awt.event.MouseListener listener) {
		coeff.setMouseListener(listener);
		mouseListenerCoeff = listener;

	}

	/**
	 * Sets the precision for the underlying coefficients table.
	 *
	 * @param precision
	 */
	public void setPrecision(int precision) {
		coeff.setPrecision(precision);
	}

	/**
	 * Sets the cell renderer for the underlying coefficients table.
	 * 
	 * @param renderer javax.swing.table.TableCellRenderer
	 */
	public void setRendererCoeff(TableCellRenderer renderer) {
		coeff.setCellRenderer(renderer);
	}

	/**
	 * Sets whether the variable names vector should 
	 * have a time index to indicate the lag, 
	 * for example income(t) or income(t-1). This should be 
	 * false for deterministic variables.
	 * 
	 * @param isShowingTimeIndex true if time index shown
	 */
	public void setShowingTimeIndex(boolean isShowingTimeIndex) {
		variables.setShowingTimeIndex(isShowingTimeIndex);
	}

	/**
	 * Sets the symbol name for the coefficients.
	 * 
	 * @param symbolName for coefficients
	 */
	public void setSymbolNameCoeff(String symbolName) {
		coeff.setSymbolName(symbolName);
	}

	/**
	 * Sets the symbol name for the subset restrictions
	 * that are set for the coefficients.
	 * 
	 * @param symbolName for subset restrictions
	 */
	public void setSymbolNameSubsetRes(String symbolName) {
		coeff.setSymbolNameSubsetRes(symbolName);
	}

	/**
	 * Sets the symbol name ffor the variable names 
	 * corresponding to the coefficients.
	 * 
	 * @param symbolName for variable names
	 */
	public void setSymbolNameVariables(String symbolName) {
		variables.setSymbolName(symbolName);
	}

	/**
	 * Sets the popup menu for the underlying coefficients
	 * tables.
	 * 
	 * @param newTablePopup popup component
	 */
	public void setTablePopup(JPopupMenu newTablePopup) {
		tablePopup = newTablePopup;
		if (tablePopup == null)
			coeff.removeMouseListener(triggerPopUp);
		else {
			triggerPopUp = new MouseAdapter() {
				public void mouseReleased(MouseEvent e) {
					tablePopup.processMouseEvent(e, null, null);
				}
			};
			coeff.addMouseListener(triggerPopUp);
		}

	}

	/**
	 * Sets the symbol scope, which defines which symbol
	 * table to use (local, global
	 * or upper). 
	 * 
	 * @param scope the symbol scope
	 * @throws IllegalArgumentException if (scope == null)
	 */
	public void setSymbolScope(Scope scope) {
		super.setSymbolScope(scope);
		coeff.setSymbolScope(scope);
		variables.setSymbolScope(scope);
	}

	

}
 

Ads help maintain this website.