// ======================================================
// Thermodynamics package in java
// steamTable class to calculate properties of steam
// user interface (JApplet)
// Dr. Turhan Coban
// Dr. Turhan Coban
// TUBITAK Ulusal Metroloji Enstitüs
// email : Turhan.Coban@ume.tubitak.gov.tr
// File Name   : steamTable.java
// requires    : steam.java, steamModel.java
// =====================================================

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 steamTable1 extends JApplet implements ActionListener,ItemListener
{
  JPanel inputPanel;
  JPanel outputPanel;
  JPanel totalPanel;
  JLabel l1[]=new JLabel[4];           // Label prompt unit
  JLabel l2[]=new JLabel[4];           // Label prompt unit
  JTextField t1[]=new JTextField[3];
  JComboBox c1[]=new JComboBox[2];
  JTextArea output;
  JTable jt;
  steamModel1 sm;
  steam1 st;
  public String unit;
  public String pair;
  public double v1;
  public double v2;
  protected StringTokenizer token;
  String s1[]={"SI","EN"};
  String s2[]={"tx","tp","tv","th","tu","ts","pv","pt","ph","pu","ps","px","vp","vt"};
  Container c;
  Color bg;
  public void init()
  {
    c=getContentPane();
    c.setLayout(new FlowLayout());
    bg=c.getBackground();
    //adding max-min prompts and input fields
    st=new steam1();
    unit="SI";
    pair="tx";
    v1=100.0;
    v2=1.0;
    st.setUnit(unit);
    sm=new steamModel1(unit,pair,v1,v2);
    inputPanel=new JPanel();
    inputPanel.setLayout(new GridLayout(4,3));
    outputPanel=new JPanel();
    outputPanel.setLayout(new BorderLayout());
    totalPanel=new JPanel();
    totalPanel.setLayout(new BorderLayout());
    l1[0]=new JLabel("unit");
    l1[1]=new JLabel("select known property pair : ");
    l1[2]=new JLabel(" Temperature");
    l1[3]=new JLabel(" Quality    ");
    l2[0]=new JLabel("");
    l2[1]=new JLabel("");
    l2[2]=new JLabel(" degree C");
    l2[3]=new JLabel(" kg vapor/kg mix.");
    c1[0]=new JComboBox(s1);
    c1[1]=new JComboBox(s2);
    t1[0]=new JTextField(""+v1);
    t1[1]=new JTextField(""+v2);
    t1[2] = new JTextField();
    t1[2].setText(pair);
    inputPanel.add(l1[0]);
    inputPanel.add(c1[0]);
    inputPanel.add(l2[0]);

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

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

    inputPanel.add(l1[3]);
    inputPanel.add(t1[1]);
    inputPanel.add(l2[3]);
    t1[0].setBackground(bg);
    t1[1].setBackground(bg);
    t1[2].setBackground(bg);
    t1[0].setFont(l1[0].getFont());
    t1[1].setFont(l1[0].getFont());
    t1[2].setFont(l1[0].getFont());
    c1[0].setBackground(bg);
    c1[1].setBackground(bg);
    c1[0].addItemListener(this);
    c1[1].addItemListener(this);
    t1[0].addActionListener(this);
    t1[1].addActionListener(this);
    t1[2].addActionListener(this);
    output=new JTextArea();
    setArea();
    jt=new JTable(sm);
    jt.setBackground(bg);
    jt.setFont(l1[0].getFont());
    output.setFont(l1[0].getFont());
    output.setForeground(l1[0].getForeground());
    outputPanel.add(output,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()
 {
    String s="";
    s+="Dr. Turhan Çoban, \n";
    s+="Ege University, School of Engineering, Mechanical Eng. Dept.\n";
    s+="phone : 90(232)3434000-5387\n";
    s+="email : turhan.coban@ege.edu.tr\n";
    s+="web   : www.yto.com.tr/turhan_coban\n";    
    output.setText(s);
    sm.setValues(unit,pair,v1,v2);
    output.setBackground(c.getBackground());
 }

  public void itemStateChanged(ItemEvent ev)
  {
  unit=(String)c1[0].getSelectedItem();
  pair=(String)c1[1].getSelectedItem();
  t1[2].setText(pair);
  if(pair.charAt(0)=='t') l1[2].setText(" Temperature ");
  if(pair.charAt(0)=='p') l1[2].setText(" Pressure ");
  if(pair.charAt(0)=='v') l1[2].setText(" sp. volume ");
  if(pair.charAt(1)=='t') l1[3].setText(" Temperature ");
  if(pair.charAt(1)=='p') l1[3].setText(" Pressure ");
  if(pair.charAt(1)=='v') l1[3].setText(" sp. volume ");
  if(pair.charAt(1)=='h') l1[3].setText(" Enthpalpy ");
  if(pair.charAt(1)=='u') l1[3].setText(" Internal Energy ");
  if(pair.charAt(1)=='s') l1[3].setText(" Entropy ");
  if(pair.charAt(1)=='x') l1[3].setText(" Quality ");
  if(unit.equals("SI"))
  {
  if(pair.charAt(0)=='t') l2[2].setText(" degree C");
  if(pair.charAt(0)=='p') l2[2].setText(" kPa");
  if(pair.charAt(0)=='v') l2[2].setText(" m^3/kg");
  if(pair.charAt(1)=='t') l2[3].setText(" degree C");
  if(pair.charAt(1)=='p') l2[3].setText(" kPa");
  if(pair.charAt(1)=='v') l2[3].setText(" m^3/kg");
  if(pair.charAt(1)=='h') l2[3].setText(" KJ/kg");
  if(pair.charAt(1)=='u') l2[3].setText(" KJ/kg");
  if(pair.charAt(1)=='s') l2[3].setText(" KJ/kg K");
  if(pair.charAt(1)=='x') l2[3].setText(" kg vapor/kg mix");
  }
  else
  {
  if(pair.charAt(0)=='t') l2[2].setText(" degree F");
  if(pair.charAt(0)=='p') l2[2].setText(" lbf/in^2");
  if(pair.charAt(0)=='v') l2[2].setText(" ft^3/lbm");
  if(pair.charAt(1)=='t') l2[3].setText(" degree F");
  if(pair.charAt(1)=='p') l2[3].setText(" lbf/in^2");
  if(pair.charAt(1)=='v') l2[3].setText(" ft^3/lbm");
  if(pair.charAt(1)=='h') l2[3].setText(" BTU/lbm");
  if(pair.charAt(1)=='u') l2[3].setText(" BTU/lbm");
  if(pair.charAt(1)=='s') l2[3].setText(" BTU/lbm R");
  if(pair.charAt(1)=='x') l2[3].setText(" lbm vapor/lbm mix");
  }
  t1[1].setText("");
  t1[0].setText("");
  repaint();
  }


public void actionPerformed( ActionEvent e)
{
  if(e.getSource()==t1[2])
  {
  String st1=(String)c1[1].getSelectedItem();
  pair=st1;
  String st2=t1[2].getText();
     for(int j=0;j<s2.length;j++)
     {
        if(s2[j].equals(st2))
        {   pair=st2;
            c1[1].setSelectedItem(st2);
            break;
        }
     }
  }
  else
  {
  Double V1=new Double(t1[0].getText());
  v1=V1.doubleValue();
  Double V2=new Double(t1[1].getText());
  v2=V2.doubleValue();
  sm.setValues(unit,pair,v1,v2);
  setArea();
  repaint();
  }
}

    public static void main(String s[])
    {
        JFrame f = new JFrame("Properties of water and steam");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JApplet applet = new steamTable1();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(800,400));
        f.setVisible(true);
    }
}





