import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;

class genelModel extends AbstractTableModel implements TableModelListener
{
  Object[][] veri;
  String[] baslik;

  public genelModel(Object[][] verii,String basliki[])
  {
    int n=veri.length;
    int m=veri[0].length;
    veri=new Object[n][m];
    baslik=new String[m];
    for(int j=0;j<m;j++)
    {for(int i=0;i<n;i++)
       {veri[i][j]=verii[i][j];}
       baslik[j]=basliki[j];
    }
    addTableModelListener(this);
  }
    
  public genelModel(Object[][] verii)
  {
    int n=veri.length;
    int m=veri[0].length;
    veri=new Object[n][m];
    baslik=new String[m];
    for(int j=0;j<m;j++)
    {for(int i=0;i<n;i++)
       {veri[i][j]=verii[i][j];}
       baslik[j]=""+j;
    }
  }
  
  public genelModel(int n,int m)
  {
    veri=new Object[n][m];
    baslik=new String[m];  
    /*
    for(int j=0;j<m;j++)
    {for(int i=0;i<n;i++)
       {veri[i][j]=(Object)"";}	  
    }
    */
  }
  
  public genelModel(double[][] verii)
  {  
    int n=verii.length;
    int m=verii[0].length;
    veri=new Object[n][m];
    baslik=new String[m];
    veri=doubletoDouble(verii);
    for(int j=0;j<m;j++)
       baslik[j]=""+j;
  }
  public genelModel(String[][] verii)
  {  
    int n=verii.length;
    int m=verii[0].length;
    veri=new Object[n][m];
    baslik=new String[m];
    veri=verii;
    for(int j=0;j<m;j++)
       baslik[j]=""+j;
  }
  public genelModel(String[][] verii,String basliki[])
  {  
    int n=verii.length;
    int m=verii[0].length;
    int m1=basliki.length;
    
    veri=new Object[n][m];
    baslik=new String[m];
    veri=verii;
    if(m1>=m)
    {
    for(int j=0;j<m;j++)
       baslik[j]=basliki[j];
    }
    else
    {
	for(int j=0;j<m1;j++)
       baslik[j]=basliki[j];
    for(int j=m1;j<m;j++)
       baslik[j]=""+j;   
	}       
  }
  public genelModel(double[][] verii,String basliki[])
  {  
    int n=verii.length;
    int m=verii[0].length;
    int m1=basliki.length;
    
    veri=new Object[n][m];
    baslik=new String[m];
    veri=doubletoDouble(verii);
    if(m1>=m)
    {
    for(int j=0;j<m;j++)
       baslik[j]=basliki[j];
    }
    else
    {
	for(int j=0;j<m1;j++)
       baslik[j]=basliki[j];
    for(int j=m1;j<m;j++)
       baslik[j]=""+j;   
	}       
  }
  
  public genelModel(String filename)
  {
  double a[][]=readDouble(filename);
  int n=a.length;
  int m=a[0].length;  
  veri=new Object[n][m];
  baslik=new String[m];	  
  veri=doubletoDouble(a);
  for(int j=0;j<m;j++)
       baslik[j]=""+j;
  }
  
  public genelModel(String filename,String basliki[])
  {
  double a[][]=readDouble(filename);
  int n=a.length;
  int m=a[0].length;  
  veri=new Object[n][m];
  baslik=new String[m];	  
  veri=doubletoDouble(a);
  for(int j=0;j<m;j++)
       baslik[j]=basliki[j];
  }
    
  public static Double[][] doubletoDouble(double xi[][])
  {
  int n=xi.length;
  int m=xi[0].length;
  Double X[][]=new Double[n][m];
  for(int j=0;j<m;j++)
    {for(int i=0;i<n;i++)
       {X[i][j]=new Double(xi[i][j]);}	  
    }
    return X;
  }  
  

    
  public static double[][] readDouble(String filename)
      {
        //veriyi oku bu metodun verisayısı sınırı yoktur
        //istediğimiz uzunluktaki veriyi okuyabilir
        Vector xi=new Vector(); 
        int nn;
        int m=2;
        int i=0;
        BufferedReader fin;
        try{
	    fin=new BufferedReader(new FileReader(filename));
	    try {
		try{ 
		try{            	    
	    String s1=Text.readStringLine(fin);        
        //System.out.println(s1);
	    StringTokenizer token=new StringTokenizer(s1);
        int n=token.countTokens()-1;
        m=n+1;        //m tokens boyutludeğişken kolon sayısı  
        while(token.hasMoreTokens())
        {
        String s2=token.nextToken();
        xi.add(s2);
        }
        i=0;              
        while(fin != null)
        {
        i++;
	    String s11=Text.readStringLine(fin);
	    //System.out.println(s11);        
        if(s11=="") break;
	    StringTokenizer token1=new StringTokenizer(s11);       
        while(token1.hasMoreTokens())
        {
        String s2=token1.nextToken();
        xi.add(s2);
        }
        }
        } catch(ArrayIndexOutOfBoundsException e_eof) { }
        } catch(StringIndexOutOfBoundsException e_eof) { }
        } catch(EOFException e_eof) {System.out.println("end of file"); }
        } catch(IOException e_io) {System.out.println("file name is not existed"); }      
        nn=i;
        //System.out.println("m="+m+"nn="+nn);
        double aa[][]=new double[nn][m];
        Enumeration nx=xi.elements();
        for(i=0;i<nn;i++)
        {
        for(int j=0;j<m;j++)
        {
	    String s1=(String)nx.nextElement();    
	    Double ax=new Double(s1);
        aa[i][j]=ax.doubleValue();  
        }
      } 
        return aa;
     }
     
     
  public int getRowCount() {return veri.length;}
  
  public int getColumnCount() {return baslik.length;}
  
  public Object getValueAt(int satir,int sutun) {return veri[satir][sutun];}
  
  public String getColumnName(int c) {return baslik[c];}
  
  public void setValueAt(Object val, int row, int col)
  {veri[row][col] = val;}
  
  public void setValues(String filename)
  {veri=doubletoDouble(readDouble(filename));}
  
  public void tableChanged(TableModelEvent e)
  {
  
  } 
  
  public double[][] todouble()
  {  int n=veri.length;
     int m=veri[0].length;
     double x[][]=new double[n][m];
  for(int j=0;j<m;j++)
    {for(int i=0;i<n;i++)
       {x[i][j]=Double.parseDouble((String)(veri[i][j]));}	  
    }
  return x;
  }  
  
  public void setValues(double xi[][],String[] basliki)
  {
  int n=xi.length;
  int m=xi[0].length;
  Double X[][]=new Double[n][m];
  for(int j=0;j<m;j++)
    {for(int i=0;i<n;i++)
       {X[i][j]=new Double(xi[i][j]);}	  
     baslik[j]=basliki[j];
    }
  veri=X;
  }
  
  public Object[][] getValues(){return veri;}
  
  public boolean isCellEditable(int row, int col) {return true;}
}


