// ======================================================
// Thermodynamics package in java
// Gas1Table class to calculate properties of gases
// user interface (JFrame)
// Dr. Turhan Coban
// TUBITAK Marmara Research Center
// Energy Systems and Environmental Research Institute
// email : Turhan.Coban@posta.mam.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 Gas1Table extends JApplet implements ActionListener,ItemListener,Serializable
{
  String s1[]={"SI","EN"};
  String s2[]={"mole","mass"};
  String unit;
  String base;
  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
  JComboBox c1[]=new JComboBox[3];     // selection field
  JTextArea outputTextArea;
  Gas1Model gm;
  public Gas1 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("air");
    unit="SI";
    base="mole";
    T=27.0;
    P=1.0;
    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(s1);
    c1[1]=new JComboBox(s2);
    g1=new Gas1(gasName);
    token=new StringTokenizer(g1.readGasNames());
    st=new String[token.countTokens()];
    int i=0;
    while(token.hasMoreTokens())
    {
    st[i++]=new String((String)token.nextToken());
    }
    c1[2]=new JComboBox(st);
    t1[0] = new JTextField();
    t1[1] = new JTextField();
    t1[2] = new JTextField();
    t1[2].setText(gasName);
    gasName=(String)c1[2].getSelectedItem();
    g1=new Gas1(gasName);
    l1[0]=new JLabel("unit system            ");
    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(""+T);
    t1[1]=new JTextField(""+P);
    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 Gas1Model(g1,TK,P);
    jt=new JTable(gm);
    jt.setBackground(bg);
    jt.setFont(fn);
    setArea();
    outputPanel.add(outputTextArea,BorderLayout.NORTH);
    outputPanel.add(jt,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(T));
    t1[1].setText(Double.toString(P));
    String s="additional data can be added to Gas.txt file \n";
    s+="Dr. Turhan Çoban, \n";
    s+="Ege Üniversitesi, Mühendislik Fakultesi, Makina Müh.\n";
    s+="tel : 0232 3434000-5387\n";
    s+="email : turhan.coban@mail.ege.edu.tr \n";
    //note if you would like to list additional information on the screen
    //add to string s
    s+="Gas Formula : "+g1.toString()+"\n";
    gm.setValues(g1,unit,base,T,P);
    outputTextArea.setText(s);
 }

  public void itemStateChanged(ItemEvent ev)
  {
  gasName=(String)c1[2].getSelectedItem();
  t1[2].setText(gasName);
  unit=(String)c1[0].getSelectedItem();
  if(unit=="EN") {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();
  g1=new Gas1(gasName);
  g1.base(base);
  g1.unit(unit);
  t1[0].setText(" ");
  t1[1].setText(" ");
  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))
        {
            gasName=st2;
            c1[2].setSelectedItem(st2);
               g1=new Gas1(gasName);
               g1.base(base);
               g1.unit(unit);
            break;
        }
     }
  }
  Double valTemp=new Double(t1[0].getText());
  T=valTemp.doubleValue();
  Double valPressure=new Double(t1[1].getText());
  P=valPressure.doubleValue();
  getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
  setArea();
  repaint();
}
        public static void main(String s[]) {
        JFrame f = new JFrame("Properties of Perfect Gases ");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JApplet applet = new Gas1Table();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(500,600));
        f.setVisible(true);
    }
}





