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

public class VariableSelectTableModel extends AbstractTableModel {

	String[] values = null;
	boolean[] selected = null; 
	
	private String[] columnNames = {"Column","Selected"};
	
	public VariableSelectTableModel(String[] variables) {
		if(variables == null) {
			throw(new NullPointerException());
		}
		
		values = variables;
		selected = new boolean[values.length];
		for(int i=0;i<values.length;i++)
			selected[i] = false;
	}
	
	public int getColumnCount() {
		return 2;
	}

	public int getRowCount() {
		return values.length;
	}

	public Object getValueAt(int arg0, int arg1) {
		if(arg1 == 0)
			return values[arg0];
		else
			return new Boolean(selected[arg0]);
	}
	
	/*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    
    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col < 1) {
            return false;
        } else {
            return true;
        }
    }
    
    public void setValueAt(Object value, int row, int col) {
    	if(col==0) return;
    	
    	if(value.getClass().isAssignableFrom(Boolean.class)) {
    		selected[row] = ((Boolean)value).booleanValue();
    	}
    	fireTableDataChanged(); 
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }
    
    public String[] getSelected() {
    	String[] return_val = null;
    	
    	int count=0;
    	for(int i=0;i<this.getRowCount();i++) {
    		if(selected[i]) count++;
    	}
    	
    	if(count == 0) return null;
    	
    	return_val = new String[count];
    	count = 0;
    	for(int i=0;i<this.getRowCount() && count<return_val.length;i++) {
    		if(selected[i]) {
    			return_val[count] = values[i];
    			count++;
    		}
    	}
    	
    	return return_val;
    }
    
    public void setSelected(String[] variables) {

    	if(values == null || selected == null) return;
    	if(variables == null) return;
    	
    	for(int sel_count=0;sel_count<variables.length;sel_count++) {
    		for(int tbl_count=0;tbl_count<values.length && tbl_count<selected.length;tbl_count++) {

    			if(values[tbl_count].compareTo(variables[sel_count]) == 0) {
    				selected[tbl_count] = true;
    				break;
    			}
    		}
    	}
    } 
}
