// ======================================================
// Thermodynamics package in java
// ReactionTable class to calculate properties of gas Reactions
// user interface (JFrame)
// Dr. Turhan Coban
// EGE Üniversitesi Mühendislik Fakultesi Makina böl.
// =====================================================

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 reactionTable extends JApplet implements ActionListener,ItemListener,Serializable
{
  static final long serialVersionUID = 608749200350614100L;
  String s1[]={"SI","EN"};
  String s2[]={"mole"};
  String unit;
  String base;
  JPanel inputPanel;
  JPanel outputPanel;
  JPanel totalPanel;
  JLabel l1[]=new JLabel[6];           // Label
  JLabel l2[]=new JLabel[6];           // Label
  JTextField t1[]=new JTextField[4];   // input fields
  JComboBox c1[]=new JComboBox[3];     // selection field
  JTextArea outputTextArea;
  public Reaction g1;
  JTable jt;
  double TP,TR;
  double P;
  public String reactionName;
  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
    reactionName=new String("c8h18");
    unit="SI";
    base="mole";
    TR=25.0;
    TP=25.0;
    P=1.01325;
    inputPanel=new JPanel();
    inputPanel.setLayout(new GridLayout(6,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);
    try{
       g1=new Reaction(reactionName);
       token=new StringTokenizer(g1.readReactionNames());
       st=new String[token.countTokens()];
       } catch(IOException ioe) {System.out.println("IOException");}

    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[3] = new JTextField();
    t1[3].setText(reactionName);
    reactionName=(String)c1[2].getSelectedItem();
    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("Reactant temperature   ");
    l1[4]=new JLabel("Product temperature   ");
    l1[5]=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(" degree C              ");
    l2[5]=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(""+TR);
    t1[1]=new JTextField(""+TP);
    t1[2]=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);
    t1[3].addActionListener(this);
    double TPK=TP+273;
    double TRK=TR+273;
    setArea();
    outputPanel.add(outputTextArea,BorderLayout.NORTH);
    totalPanel.add(inputPanel,BorderLayout.NORTH);
    totalPanel.add(outputPanel,BorderLayout.SOUTH);
    c.add(totalPanel,BorderLayout.NORTH);
  }

 public void setArea()
 {
    try{
       g1=new Reaction(reactionName);
       } catch(IOException ioe) {System.out.println("IOException");}
   g1.base(base);
   g1.unit(unit);
   t1[0].setText(" ");
   t1[1].setText(" ");
    double cc=273,cc1=298.0;
    if(!g1.SI) {cc=459.67;cc1=537;}
    double TRK=TR+cc;
    double TPK=TP+cc;
    double Tadyabatikalev= g1.Taf(TRK,cc1, 3000)-cc;
    t1[0].setText(Double.toString(TR));
    t1[1].setText(Double.toString(TP));
    t1[2].setText(Double.toString(P));
    String s="Dr. Turhan Çoban, \n";
    s+="Ege Üniversitesi Mühendislik Fakultesi Makina müh..\n";
    s+="Bornova İzmir Turkey,\n";
    s+="phone : 90(232)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+="Reaction name : "+g1.toString("name")+"\n";
    s+="Reaction formula : "+g1.toString("formula")+"\n";
    s+="Reaction composition : "+g1.toString("composition")+"\n";
    if(unit.equals("SI"))
    {
    s+="Reaction enthalpy : "+g1.H(TRK,TPK)+"  KJ/kmol fuel\n";
    s+="Reaction entropy : "+g1.S(TRK,TPK)+"  KJ/kmol fuel K\n";
    s+="Reaction gibbs energy : "+g1.G(TRK,TPK)+"  KJ/kmol fuel K\n";
    s+="Reaction gibbs energy 0 state : "+g1.G0(TRK,TPK)+"  KJ/kmol fuel K\n";
    s+="Adiabatic flame temperature : "+Tadyabatikalev+" degree  C = "+(Tadyabatikalev+273.15)+" Degree K \n";
    s+="Vi="+g1.Vi(298,298,2)+" V\n";
    s+="Vi0="+g1.Vi0(298,298,2)+" V\n";
    s+="fuel cell eficiency : "+g1.G(TRK,TPK)/g1.H(TRK,TPK)+"\n";
    s+="fuel cell eficiency o state : "+g1.G0(TRK,TPK)/g1.H(TRK,TPK)+"\n";
    }
    else
    {    s+="Reaction enthalpy : "+g1.H(TRK,TPK)+"  BTU/lbmol fuel\n";
         s+="Reaction entropy  : "+g1.S(TRK,TPK)+"  BTU/lbmol fuel F\n";
         s+="Adiabatic flame temperature : "+Tadyabatikalev+"  R \n";
    }
    outputTextArea.setText(s);
 }

  public void itemStateChanged(ItemEvent ev)
  {
  reactionName=(String)c1[2].getSelectedItem();
  t1[3].setText(reactionName);
  unit=(String)c1[0].getSelectedItem();
  if(unit=="EN") {l2[3].setText(" degree F              ");
                  l2[4].setText(" degree F              ");
                  l2[5].setText(" psia (lbf/in^2)       ");}
  else           {l2[3].setText(" degree C              ");
                  l2[4].setText(" degree C              ");
                  l2[5].setText(" bars                 ");}
  base=(String)c1[1].getSelectedItem();
  setArea();
  repaint();
  }


public void actionPerformed( ActionEvent e)
{
  if(e.getSource()==t1[3])
  {
  String st1=(String)c1[2].getSelectedItem();
  reactionName=st1;
  String st2=t1[3].getText();
     for(int j=0;j<st.length;j++)
     {
        if(st[j].equals(st2))
        {
            reactionName=st2;
            c1[2].setSelectedItem(st2);
            break;
        }
     }
  }
  Double valTempR=new Double(t1[0].getText());
  TR=valTempR.doubleValue();
  Double valTempP=new Double(t1[1].getText());
  TP=valTempP.doubleValue();
  Double valPressure=new Double(t1[2].getText());
  P=valPressure.doubleValue();
  getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
  setArea();
  repaint();
}
        public static void main(String s[]) {
        JFrame f = new JFrame("Thermodynamics of Reaction ");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JApplet applet = new reactionTable();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(500,650));
        f.setVisible(true);
    }
}





