// ======================================================
// Thermodynamics package in java
// GasPropertySWF class to calculate properties of gases
// user interface (JFrame)
// Dr. Turhan Coban
// TUBITAK Ulusal Metroloji Enstitüsü
// email : Turhan.Coban@ume.tubitak.gov.tr
// =====================================================

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

public class LKTable extends JApplet implements ActionListener,ItemListener,Serializable 
{
  private static final long serialVersionUID=3580448990L;
  String s1[]={"t p","t v","t h","t s","t x","v p","p h","p s","p x"};
  String s2[]={"mole","mass"};
  double iv[]=new double[2];
  String pair;
  String unit;
  String base;
  JScrollPane ScrollPane1;
  JScrollPane ScrollPane2;
  String stn1;
  String stn2;
  JPanel inputPanel;
  JPanel outputPanel;
  JPanel totalPanel;
  JLabel l1[]=new JLabel[5];           // Label
  JLabel l2[]=new JLabel[5];           // Label
  JTextField t1[]=new JTextField[3];   // input fields
  @SuppressWarnings({ "rawtypes", "unchecked" })
  JComboBox[] c1=new JComboBox[3];     // selection field
  JTextArea outputTextArea;
  LKModel gm;
  public LeeKesler g1;
  JTable jt;
  double T;
  double P;
  public String gasName;

  //protected File gmixFile;
  protected StringTokenizer token;
  String st[];
  Color bg;
  Color fg;

  public void init() 
  {
    Container c=getContentPane();
    c.setLayout(new FlowLayout());
    bg=c.getBackground();
    fg=c.getForeground();
    //adding max-min prompts and input fields
    gasName=new String("CH4");
    unit="SI";
    base="mole";
    pair="t p";
    stn2="tp";
    T=25.0;
    P=1.0;
    iv[0]=27.15;
    iv[1]=1.0135;
    inputPanel=new JPanel();
    inputPanel.setLayout(new GridLayout(5,3,80,1));
    outputPanel=new JPanel();
    outputPanel.setLayout(new BorderLayout());
    totalPanel=new JPanel();
    totalPanel.setLayout(new BorderLayout());
    c1[0]=new JComboBox<String>(s1);
    c1[1]=new JComboBox<String>(s2);    
    int i=0; 
       
    //st=new String[6];
    /*st[0]="methane";
    st[1]="ethane";
    st[2]="oxygen";
    st[3]="carbondioxide";
    st[4]="nitrogen";
    st[5]="water";*/
    try{
	st=g1.readgasnames();
    c1[2]=new JComboBox<String>(st);
	}catch(IOException ioe){System.err.println(ioe.getMessage());}
    t1[0] = new JTextField();
    t1[1] = new JTextField();
    t1[2] = new JTextField();
    t1[2].setText(st[0]);
    //gasName=(String)c1[2].getSelectedItem();
    
    //try{
       g1=new LeeKesler(gasName.toUpperCase());
       //} catch(IOException ioe) {System.out.println("IOException");}*/
       
    l1[0]=new JLabel("known properties       ");
    fg=l1[0].getForeground();
    l1[1]=new JLabel("mass/mole              ");
    l1[2]=new JLabel("gas Name               ");
    l1[3]=new JLabel("temperature            ");
    l1[4]=new JLabel("pressure               ");
    l2[0]=new JLabel(" ");
    l2[1]=new JLabel(" ");
    l2[2]=new JLabel(" ");
    l2[3]=new JLabel(" degree C              ");
    l2[4]=new JLabel(" bars                  ");
     outputTextArea=new JTextArea();
    Font fn=l1[0].getFont();
    for(i=0;i<5;i++)
    {    l1[i].setBackground(Color.lightGray);
         l2[i].setBackground(Color.lightGray);
    }
    t1[0]=new JTextField(""+iv[0]);
    t1[1]=new JTextField(""+iv[1]);
    for(i=0;i<3;i++)
    {    t1[i].setBackground(bg);t1[i].setFont(fn); }
    for(i=0;i<3;i++)
    {    c1[i].setBackground(bg);t1[i].setFont(fn); }
    outputTextArea.setBackground(bg);
    outputTextArea.setForeground(fg);

    outputTextArea.setFont(fn);

    inputPanel.add(l1[0]);
    inputPanel.add(c1[0]);
    inputPanel.add(l2[0]);

    inputPanel.add(l1[1]);
    inputPanel.add(c1[1]);
    inputPanel.add(l2[1]);

    inputPanel.add(l1[2]);
    inputPanel.add(c1[2]);
    inputPanel.add(t1[2]);

    inputPanel.add(l1[3]);
    inputPanel.add(t1[0]);
    inputPanel.add(l2[3]);

    inputPanel.add(l1[4]);
    inputPanel.add(t1[1]);
    inputPanel.add(l2[4]);

    c1[0].addItemListener(this);
    c1[1].addItemListener(this);
    c1[2].addItemListener(this);

    t1[0].addActionListener(this);
    t1[1].addActionListener(this);
    t1[2].addActionListener(this);
    double TK=T+273.0;
    gm=new LKModel("tp",g1,T,P);
    jt=new JTable(gm);
    jt.setBackground(bg);
    jt.setFont(fn);
    setArea();
    ScrollPane1 =new JScrollPane(outputTextArea,
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
     
        ScrollPane1.setPreferredSize(new Dimension(300, 150));
        ScrollPane1.setViewportBorder(
                BorderFactory.createLineBorder(Color.black));
	
    ScrollPane2 =new JScrollPane(jt,
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);         
        ScrollPane1.setViewportBorder(
                BorderFactory.createLineBorder(Color.blue));
                
    outputPanel.add(ScrollPane1,BorderLayout.NORTH);
    outputPanel.add(ScrollPane2,BorderLayout.SOUTH);
    totalPanel.add(inputPanel,BorderLayout.NORTH);
    totalPanel.add(outputPanel,BorderLayout.SOUTH);
    c.add(totalPanel,BorderLayout.NORTH);
  }

 public void setArea()
 {
	
    t1[0].setText(Double.toString(iv[0]));
    t1[1].setText(Double.toString(iv[1]));
    String s="";
    s+="Dr. Turhan Çoban, \n";
    s+="Ege Üniversitesi, Mühendislik Bilimleri Fak., Makina Bölümü, Bornova İZMİR\n";
    s+="phone : 90(232)3115387\n";
    s+="email : turhan.coban@turhancoban.com\n";
    s+="web   : www.turhancoban.com\n";       //note if you would like to list additional information on the screen
    gm.setValues(stn2,g1,unit,base,iv[0],iv[1]);
    outputTextArea.setText(s);
 }

  public void itemStateChanged(ItemEvent ev)
  {
  gasName=(String)c1[2].getSelectedItem();
  t1[2].setText(gasName);
  pair=(String)c1[0].getSelectedItem();
  
  /*if() {l2[3].setText(" degree F              ");
                  l2[4].setText(" psia (lbf/in^2)       ");}
  else           {l2[3].setText(" degree C              ");
                   l2[4].setText(" bars                 ");}*/
  base=(String)c1[1].getSelectedItem();  
  unit="SI";
  g1=new LeeKesler(gasName);
  /*g1.base(base);
  g1.unit(unit);*/
  t1[0].setText(" ");
  t1[1].setText(" ");
  
  //g1.property(T,P);
  
   StringTokenizer stn=new StringTokenizer(pair);
   stn1="";  
   stn2="";
  for(int y=3;y<=4;y++){
  stn1=stn.nextToken();
  //System.out.println(y+" "+stn1);
  if(stn1.equals("t")){
	  l1[y].setText("temperature");
	  iv[y-3]=-100;
  }
  else if(stn1.equals("p")){
  l1[y].setText("pressure");  
  iv[y-3]=1.0;
  }  
  else if(stn1.equals("v")){
	  l1[y].setText("specific volume");
	  iv[y-3]=2.5437;	  
  }
  else if(stn1.equals("h")){	  
	  l1[y].setText("specific enthalpy");
	  iv[y-3]=74.8;
  }
  else if(stn1.equals("s")){
	  l1[y].setText("specific entropy");
	  iv[y-3]=188;
  }
  else if(stn1.equals("x")){
	  l1[y].setText("quality");
	  iv[y-3]=1.0;
  }
  stn2+=stn1;
  //System.out.println(stn2);
  //System.out.println(l1[y].getText());
  }
  setArea();
  repaint();
  }


public void actionPerformed( ActionEvent e)
{
  if(e.getSource()==t1[2])
  {
  String st1=(String)c1[2].getSelectedItem();
  gasName=st1;
  String st2=t1[2].getText();
     for(int j=0;j<st.length;j++)
     {
        if(st[j].equals(st2) || st[j].equals(st2.toUpperCase()) )
        {
            gasName=st2;
            c1[2].setSelectedItem(st2.toUpperCase());
            //try{
               g1=new LeeKesler(gasName.toUpperCase());
               /*g1.base(base);
               g1.unit(unit);*/
               //} catch(IOException ioe1) {System.out.println("IOException");}
            break;
        }
     }
  }
  Double val0=new Double(t1[0].getText());
  iv[0]=val0.doubleValue();
  Double val1=new Double(t1[1].getText());
  iv[1]=val1.doubleValue();
  getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
  setArea();
  repaint();
}
        public static void main(String s[]) throws IOException {
        JFrame f = new JFrame("Properties of Real Gases ");
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        JApplet applet = new LKTable();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(550,755));
        f.setVisible(true);
    }
}





