import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class KL_P extends JPanel implements ActionListener
{private static final long serialVersionUID = 24578245L;
public KL K[];
public JTextField lb[];
public JTextField anaisim[];
public JComboBox ikinciisimL[];
public JTextField ikinciisim[];
public JComboBox degerL[];
public JTextField deger[];
public JTextField degerbirim[];
public JTextField kl[];
public JTextField sayi[];
public JTextField toplamkl[];
public JPanel j1,j1A,j1B;
JPanel j2,j3,j4,j5;
public JLabel lb1[];
public JLabel lb2[];
public double sigmakl;
int ni[];
double kli[]; 
double totkli[];
double degeri[];
int n;
public KL_P()
{
sigmakl=0;
lb=new JTextField[9];
lb[0]=new JTextField("Yerel basınç kaybı elemanı ismi");
lb[1]=new JTextField("Yerel basınç kaybı elemanı tipi");
lb[2]=new JTextField("tip değeri");
lb[3]=new JTextField("ikinci değ.");
lb[4]=new JTextField("ikinci değ.");
lb[5]=new JTextField("ikinci değ. isim");
lb[6]=new JTextField("KL");
lb[7]=new JTextField("sayı");
lb[8]=new JTextField("toplam");

lb1=new JLabel[9];
lb1[0]=new JLabel("Toplam Yerel basınç kaybı KL");
lb1[1]=new JLabel(" ");
lb1[2]=new JLabel(" ");
lb1[3]=new JLabel(" ");
lb1[4]=new JLabel(" ");
lb1[5]=new JLabel(" ");
lb1[6]=new JLabel(" ");
lb1[7]=new JLabel(" ");
lb1[8]=new JLabel(" ");

lb2=new JLabel[9];
lb2[0]=new JLabel("Toplam Yerel basınç kaybı KL");
lb2[1]=new JLabel(" ");
lb2[2]=new JLabel(" ");
lb2[3]=new JLabel(" ");
lb2[4]=new JLabel(" ");
lb2[5]=new JLabel(" ");
lb2[6]=new JLabel(" ");
lb2[7]=new JLabel(" ");
lb2[8]=new JLabel(" ");


KL k1=new KL("ekKL");
n=k1.type.length;
int np1=n+1;
int np2=n+2;
int np3=n+3;
K=new KL[n];
j1=new JPanel();
j1.setLayout(new BorderLayout());
j1A=new JPanel();
j1A.setLayout(new GridLayout(np3,2));
j1B=new JPanel();
j1B.setLayout(new GridLayout(np3,7));
        j2 = new JPanel()
        { private static final long serialVersionUID = 426298L;
        public Dimension getPrefferedSize()
        {
            Dimension size=super.getPreferredSize();
            size.width=5;
            return size;
        }
        };
       j3 = new JPanel()
        {private static final long serialVersionUID = 983176L;
        public Dimension getPrefferedSize()
        {
            Dimension size=super.getPreferredSize();
            size.width=5;
            return size;
        }
        };
        j4 = new JPanel()
        {private static final long serialVersionUID = 81644L;
        public Dimension getPrefferedSize()
        {
            Dimension size=super.getPreferredSize();
            size.width=5;
            return size;
        }
        };
        j5 = new JPanel()
        { private static final long serialVersionUID = 83907946L;
        public Dimension getPrefferedSize()
        { 
            Dimension size=super.getPreferredSize();
            size.width=5;
            return size;
        }
        };
        ni=new int[n];
        kli=new double[n];
        totkli=new double[n];
        degeri=new double[n];
anaisim=new JTextField[np1];
ikinciisimL=new JComboBox[np1];
ikinciisim=new JTextField[np1];
degerL=new JComboBox[np1];
deger=new JTextField[np1];
degerbirim=new JTextField[np1];
kl=new JTextField[np1];
sayi=new JTextField[np1];
toplamkl=new JTextField[np2];
double x;
j1A.add(lb[0]);
j1A.add(lb[1]);
j1B.add(lb[2]);j1B.add(lb[3]);j1B.add(lb[4]);j1B.add(lb[5]);j1B.add(lb[6]);j1B.add(lb[7]);j1B.add(lb[8]);
for(int i=0;i<n;i++)
   { K[i]=new KL(k1.type[i]);
    anaisim[i]=new JTextField(k1.type[i]);
    ikinciisimL[i]=new JComboBox<String>(K[i].hm.deg1);
    //ikinciisimL[i].setVisibleRowCount(1);
    //j2=new JScrollPane(ikinciisimL[i]);
    ikinciisim[i]=new JTextField(""+K[i].hm.d1[0]);
    degerL[i]=new JComboBox<String>(K[i].hm.d2s);
    //degerL[i].setVisibleRowCount(1);
    deger[i]=new JTextField(""+K[i].hm.d2[0]);
    degerbirim[i]=new JTextField(""+K[i].hm.deg2);
    x=K[i].cikti(K[i].hm.deg1[0],K[i].hm.d2[0]);
    kl[i]=new JTextField(""+x);
    sayi[i]=new JTextField(""+0);
    toplamkl[i]=new JTextField(""+0);
    j1A.add(anaisim[i]);
    j1A.add(ikinciisimL[i]);
    ikinciisimL[i].addActionListener(this);
    j1B.add(ikinciisim[i]);
    j1B.add(degerL[i]);
    degerL[i].addActionListener(this);
    j1B.add(deger[i]);
     deger[i].addActionListener(this);
    j1B.add(degerbirim[i]);   
    j1B.add(kl[i]);
    j1B.add(sayi[i]);
    sayi[i].addActionListener(this);
    j1B.add(toplamkl[i]);   
   }
    anaisim[n]=new JTextField("ek KL değeri");
    ikinciisimL[n]=new JComboBox<String>(k1.hm.deg1);
    //ikinciisimL[n].setVisibleRowCount(1);
    ikinciisim[n]=new JTextField(""+k1.hm.d1[0]);
    degerL[n]=new JComboBox<String>(k1.hm.d2s);
   // degerL[n].setVisibleRowCount(1);
    deger[n]=new JTextField(""+k1.hm.d2[0]);
    degerbirim[n]=new JTextField(""+k1.hm.deg2);
    kl[n]=new JTextField("   ");
    sayi[n]=new JTextField(""+0);
    toplamkl[n]=new JTextField(""+0);
    toplamkl[n].addActionListener(this);
    toplamkl[n+1]=new JTextField(""+0);
    j1A.add(anaisim[n]);
    j1A.add(lb2[1]);
    j1B.add(lb2[2]);
    j1B.add(lb2[3]);
    j1B.add(lb2[4]);
    j1B.add(lb2[5]);  
    j1B.add(lb2[6]);
    j1B.add(lb2[7]);
    j1B.add(toplamkl[n]);
    // ================ Toplam değer
    j1A.add(lb1[0]);
    j1A.add(lb1[1]);    
    j1B.add(lb1[2]);
    j1B.add(lb1[3]);
    j1B.add(lb1[4]);
    j1B.add(lb1[5]);
    j1B.add(lb1[6]);
    j1B.add(lb1[7]);
    j1B.add(toplamkl[n+1]);  
    // =================
    j1.add(j1A,BorderLayout.WEST);
    j1.add(j1B,BorderLayout.EAST); 
    JScrollPane jsp=new JScrollPane(j1);
    jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    //jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    add(jsp); 	
}

 public void hesapla(ActionEvent e)
 {
 sigmakl=0;
 for(int i=0;i<n;i++)
 {   int i1=ikinciisimL[i].getSelectedIndex();
     String s=(String)ikinciisimL[i].getItemAt(i1);
     ikinciisim[i].setText(s);
     degeri[i]=Double.parseDouble(deger[i].getText());
     if(e.getSource()==degerL[i])
     {
     int i2=degerL[i].getSelectedIndex();
     String s1=(String)degerL[i].getItemAt(i2);
     deger[i].setText(s1);
     degeri[i]=Double.parseDouble(s1);
     }   
	 kli[i]=K[i].cikti(s,degeri[i]);
	 kl[i].setText(""+kli[i]);
	 ni[i]=Integer.parseInt(sayi[i].getText());
     totkli[i]=kli[i]*ni[i];
     toplamkl[i].setText(""+totkli[i]);
     sigmakl+= totkli[i];
 } 
     sigmakl+=Double.parseDouble(toplamkl[n].getText());
     toplamkl[n+1].setText(""+sigmakl);
 }
 
 public double toKL()
 {return sigmakl;}
 
 public void actionPerformed(ActionEvent e)
 { 
 hesapla(e);
 validate();
 repaint();
 }
 


}