import java.lang.Integer;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.Locale;
import java.text.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.*;

public class girdiP1 extends JPanel implements ActionListener,ItemListener,Serializable
{
  String fuel[];
  String air[];
  double nfuel[];
  double excess_air;
  int n1;
  JPanel p1,p2,p3,p4,p;
  JLabel l1[];           // Label prompt unit
  JLabel l2[];           // Label prompt unit
  JTextField t1[];
  JComboBox c2[];
  JTextField t2[];
  JTextField t3[];
  double a[];
  String gas[];
  double ngas[];
  double ngas_percent[];
  JLabel heading;
  //JButton jb;
  int n;


  public girdiP1(String g1[],double gn1[],String s1[],String s2[],double t[],String hh)
  { 
	Gas1 g=new Gas1("ch4");
	gas=new String[g1.length];
	ngas=new double[gn1.length];
	ngas_percent=new double[gn1.length];
    n1=g1.length;
    String baslik[]={"Gaz ismi","gazın mol miktarı"," mol % değeri"};
	JLabel jl[]=new JLabel[3];
	jl[0]=new JLabel(baslik[0]);
	jl[1]=new JLabel(baslik[1]);
	jl[2]=new JLabel(baslik[2]);
    StringTokenizer token=new StringTokenizer(g.readGasNames());
    String st[]=new String[token.countTokens()];
    int i=0;
    while(token.hasMoreTokens())
    {
    st[i++]=new String((String)token.nextToken());
    }

	n=t.length;
    l1=new JLabel[n];
    l2=new JLabel[n];
    t1=new JTextField[n];
    a=new double[n];
    heading=new JLabel(hh);
    //jb=new JButton("hesapla");

    p1=new JPanel();
    p2=new JPanel();
    p3=new JPanel();
    p4=new JPanel();
    p4.setLayout(new GridLayout(n1+1,3,5,5)); 
    c2=new JComboBox[n1];
    t2=new JTextField[n1];
    t3=new JTextField[n1]; 
    double ntotal=0; 
    p4.add(jl[0]);
    p4.add(jl[1]);
    p4.add(jl[2]);
    for(int iz=0;iz<n1;iz++)
    { gas[iz]=g1[iz];
      ngas[iz]=gn1[iz];
      ntotal+=ngas[iz];
	  c2[iz]=new JComboBox(st);
	  t2[iz]=new JTextField(ngas[iz]+"              ");
      t3[iz]=new JTextField(ngas[iz]+"              ");
	  t2[iz].setFont(new Font("TimesRoman",Font.BOLD,14));
	  t3[iz].setFont(new Font("TimesRoman",Font.BOLD,14));
      c2[iz].setSelectedItem(g1[iz]);
      t2[iz].addActionListener(this);
	  p4.add(c2[iz]);
	  p4.add(t2[iz]);
	  p4.add(t3[iz]);	  
      c2[iz].addItemListener(this);
    }
    for(int iz=0;iz<n1;iz++)
    {
    ngas_percent[iz]=ngas[iz]/ntotal;
    t3[iz].setText(ngas_percent[iz]+"              ");
    }
       p=new JPanel()
       {
        public Dimension getPrefferedSize()
        {
            Dimension size=super.getPreferredSize();
            size.width=200;
            size.height=100;
            return size;
        }
        };   
    p2.setLayout(new FlowLayout(FlowLayout.CENTER));
    p1.add(heading);
    p.setLayout(new BorderLayout(5,5));
    p2.setLayout(new GridLayout(n,3,5,5));
    for(int iw=0;iw<n;iw++)
    {
	 l1[iw]=new JLabel(s1[iw]);
	 l2[iw]=new JLabel(s2[iw]);
	 t1[iw]=new JTextField(t[iw]+"                      ");	
	 t1[iw].setFont(new Font("TimesRoman",Font.BOLD,14)); 
	 t1[iw].addActionListener(this);
	 p2.add(l1[iw]);
	 p2.add(t1[iw]);
	 p2.add(l2[iw]);
	 a[iw]=t[iw];
	}
	p.add(p4,BorderLayout.NORTH);
	p.add(p1,BorderLayout.CENTER);
	p.add(p2,BorderLayout.SOUTH);
	add(p);
  }
    
  public void actionPerformed( ActionEvent e)
  {
  int i=0;
  for(i=0;i<n;i++)
  {	if(e.getSource()==t1[i])
    { a[i]=Double.parseDouble(t1[i].getText());break;}
  }
  for(i=0;i<n1;i++)
  {	if(e.getSource()==t2[i])
    { ngas[i]=Double.parseDouble(t2[i].getText());
      double ntotal=0;   
      for(int j=0;j<n1;j++)
       {ntotal+=ngas[j];}
	  for(int j=0;j<n1;j++)
       {
	   ngas_percent[j]=ngas[j]/ntotal;
       t3[j].setText(ngas_percent[j]+"              ");
	   } 
      break;
    }
  }
  repaint();
  }
  
  public void itemStateChanged(ItemEvent ev)
  {
  for(int i=0;i<n1;i++)
  {	if(ev.getSource()==c2[i])
    { gas[i]=(String)c2[i].getSelectedItem();break;}
  }
  }
    
  public double[] readText()
  {return a;}
  
    
 }

