/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.datamodel;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;

import com.approximatrix.draco.general.ui.StatTableModelUpdatable;

public class VarViewTableModel extends AbstractTableModel 
	implements TableModelListener, StatTableModelUpdatable {

	private static final int NUM_COLUMNS = 4;
	private static final int COLUMN_NAME = 0;
	private static final int COLUMN_OBSERVATIONS = 1;
	private static final int COLUMN_TYPE = 2;
	private static final int COLUMN_DESCRIPTION = 3;
	
	StatTableModel data = null;
	
	public VarViewTableModel(StatTableModel base) {
		data = base;
	}
	
	public int getRowCount() {
		return data.getColumnCount();
	}

	public int getColumnCount() {
		return NUM_COLUMNS;
	}

	public Object getValueAt(int arg0, int arg1) {
		Object return_val = null;
		String column_name = data.getColumnName(arg0);
		switch(arg1) {
			case COLUMN_NAME:
				return_val = column_name;
				break;
			case COLUMN_OBSERVATIONS:
				//return_val = new Integer(data.getNonNullRows(column_name).length);
				return_val = new Integer(data.getObeservationCount(column_name));
				break;
			case COLUMN_TYPE:
				String full_text = data.getColumnClass(arg0).toString();
				int dot = full_text.lastIndexOf(".");
				if(dot != -1)
					return_val = full_text.substring(dot+1);
				break;
			case COLUMN_DESCRIPTION:
				return_val = data.getDescription(arg0);
				break;
		}
		
		return return_val;
	}

	public String getColumnName(int col) {
		String return_val = null;
		switch(col) {
			case COLUMN_NAME:
				return_val = "Variable";
				break;
			case COLUMN_OBSERVATIONS:
				return_val = "# Observations";
				break;
			case COLUMN_TYPE:
				return_val = "Type";
				break;
			case COLUMN_DESCRIPTION:
				return_val = "Description";
				break;
		}
		
		return return_val;
    }
	
	public void tableChanged(TableModelEvent e) {
		
	}
	
	public void update(StatTableModel caller) {
		this.fireTableStructureChanged();
		this.fireTableDataChanged();
	}
}
