/*
 * InputDataModel.java
 *
 * Created on Ketvirtadienis, 2007, Sausio 25, 17.11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Main.data;

import javax.swing.*;
import java.beans.*;

import java.util.*;
import javax.swing.table.*;

public class InputDataModel extends AbstractTableModel
{
	private List dataList;
	public static final String NR= "N.";
	static public String columns[] = {NR,"P", "G", "Q"};
    static final public int initLengths[] = { 20, 40, 40, 40 };
	
	public final static String COLUM_CHANGE= "COLUM_CHANGE";
	
	private List mListeners= new ArrayList();
	
	private JTable mTable;
    //static final public int NUM_COLUMNS = columns.length;
	
	public InputDataModel()
	{
		super();
        setDefaultList();
	}
	
	public void setDefaultList()
    {
       	dataList= new Vector(100);
		/*for(int i= 0; i<20; i++)
		{
			dataList.addElement( new InputDataSlot());
		}*/
    }
    	
	public List getData()
	{return dataList;}
	
	public double[][] getDataAsArray()
	{
		double[][] matrix= new double[dataList.size()][InputDataSlot.DATA_NAMES.length];
		for(int i= 0;i<dataList.size();i++)
		{
			InputDataSlot slot= (InputDataSlot)dataList.get(i);
			for(int j= 0; j< matrix[i].length; j++)
			{
				matrix[i][j]= slot.getDataAt(j);
			}
		}
		return matrix;
	}
    	
	public void setData(List data)
	{
		dataList= data;
		fireTableDataChanged();
	}
    	
    public Object getValueAt(int row,int col)
	{
	        if (row < 0 || row>=getRowCount())
	            return "";
	        InputDataSlot r = (InputDataSlot)dataList.get(row);
	        switch (col)
	        {
				case 0:
					return new Integer(row);
	            default:
	                return new Float(r.getDataAt(col-1));
	            
	        }
	    }
	
    public  void  setValueAt ( Object object, int row, int col )
    {
        if (row < 0 || row>=getRowCount())
            return;
        InputDataSlot r = (InputDataSlot)dataList.get(row);
		if(col >0)
		{
			r.setDataAt(col-1, new Float((String)object).floatValue());
		}
        /*switch (col)
        {
            case 1:
                r.setP(new Float((String)object).floatValue());
				break;
            case 2:
				r.setG(new Float((String)object).floatValue());
				break;
            case 3:
                r.setQ(new Float((String)object).floatValue());
				break;

        }*/

	}

	public int getRowCount()
	{
		return dataList.size();
	}
	
	public int getColumnCount()
	{
		return columns.length; 
	}
	
	public  Object  getRow ( int index )
    {
        return dataList.get(index);
    }
	
	public boolean isCellEditable(int row, int col)
    {
		if(col == 0)
			return false;
         return true;
    }
	
	public void addRow(InputDataSlot slot)
	{
		dataList.add(slot);
		fireTableRowsInserted(dataList.size()-1,dataList.size()-1);
	}
	
	public void removeRow(int index)
	{
		if(index < 0 || index >= dataList.size())
			return;
		dataList.remove(index);
		fireTableRowsDeleted(index, index);
	}
	
	public void setColumns(String[] col)
	{
		String[] tmp= new String[col.length+1];
		tmp[0]= NR;
		for(int i= 1; i<= col.length; i++)
		{
			tmp[i]= col[i-1];
		}
		String[] old= (String[])columns.clone();
		columns= tmp;
		firePropertyChange(new PropertyChangeEvent(this, COLUM_CHANGE, old, columns));
	}
	
	public void changeColumns(String[] col)
	{
		int[] indexes= InputDataSlot.updateColumnNames(col);
		int numberToAdd= col.length- indexes.length;
		for(int i= 0; i< dataList.size(); i++)
		{
			InputDataSlot slot= (InputDataSlot)dataList.get(i);
		    slot.removeDataFields(indexes);
			slot.addDataFields(numberToAdd);
		}
		
		setColumns(col);
		fireTableDataChanged();
	}
	
	public void addPropertyChangeListener(PropertyChangeListener listener)
	{
		if(!mListeners.contains(listener))
			mListeners.add(listener);
	}
	
	public void firePropertyChange(PropertyChangeEvent e)
	{
		if(mListeners != null)
		{
			List list= Collections.unmodifiableList( mListeners );
			for(int i= 0; i< list.size(); i++)
			{
				((PropertyChangeListener)list.get(i)).propertyChange(e);
			}
			
		}
	}
	
}
