// ======================================================
// Thermodynamics package in java
// wetairTable class to calculate properties of steam
// user interface (JApplet)
// Dr. Turhan Coban
// University of Nebrasca
// Mechanical Engineering, N104 WSEC, Lincoln NE 68588-0656
// phone :
// email : MCOBAN2@netscape.net
//         MCOBAN2@unl.edu
// 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 wetairTable extends JApplet implements ActionListener,ItemListener,Serializable
{
  JPanel inputPanel;
  JPanel outputPanel;
  JPanel totalPanel;
  JLabel l1[]=new JLabel[5];           // Label prompt unit
  JLabel l2[]=new JLabel[5];           // Label prompt unit
  JTextField t1[]=new JTextField[4];
  JComboBox c1[]=new JComboBox[2];
  JTextArea output;
  JTable jt;
  wetairModel wam;
  wetair wa;
  public String unit;
  public String pair;
  public double v1;
  public double v2;
  public double P;
  protected StringTokenizer token;
  String s1[]={"SI","EN"};
  String s2[]={"tdb_twb","tdb_w","tdb_rh","tdb_tdew","tdb_pv","tdb_dos","tdb_h","tdb_s","w_rh","w_h"};
  Container c;
  Color bg;
  
  public void init()
  {
    c=getContentPane();
    c.setLayout(new FlowLayout());
    bg=c.getBackground();
    //adding max-min prompts and input fields
    wa=new wetair();
    unit="SI";
    pair="tdb_twb";
    v1=20.0;
    v2=15.0;
    P=1.01325;
    wa.setUnit(unit);
    wam=new wetairModel(unit,pair,v1,v2,P);
    inputPanel=new JPanel();
    inputPanel.setLayout(new GridLayout(5,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("Pressure");
    l1[3]=new JLabel(" dry bulb Temperature");
    l1[4]=new JLabel(" wet bulb Temperature");
    l2[0]=new JLabel("");
    l2[1]=new JLabel("");
    l2[2]=new JLabel(" bars");
    l2[3]=new JLabel(" degree C");
    l2[4]=new JLabel(" kg vapor/kg mix.");
    c1[0]=new JComboBox(s1);
    c1[1]=new JComboBox(s2);
    t1[0]=new JTextField(""+P);
    t1[1]=new JTextField(""+v1);
    t1[2]=new JTextField(""+v2);
    t1[3] = new JTextField();
    t1[3].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[3]);

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

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

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

    t1[0].setBackground(bg);
    t1[1].setBackground(bg);
    t1[2].setBackground(bg);
    t1[3].setBackground(bg);
    t1[0].setFont(l1[0].getFont());
    t1[1].setFont(l1[0].getFont());
    t1[2].setFont(l1[0].getFont());
    t1[3].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);
    t1[3].addActionListener(this);
    output=new JTextArea();
    setArea();
    jt=new JTable(wam);
    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 Üniversitesi, Mühendislik Bilimleri Fak., Makina Bölümü\n";
    s+="phone : 90(232)3434000-5387\n";
    s+="email : turhan.coban@mail.ege.edu.tr\n";
    s+="web   : www.axtelsoft.com/turhan.coban\n";   
    //note if you would like to list additional information on the screen
    //add to string s
    output.setText(s);
    wam.setValues(unit,pair,v1,v2,P);
    output.setBackground(c.getBackground());
 }

  public void itemStateChanged(ItemEvent ev)
  {
  unit=(String)c1[0].getSelectedItem();
  pair=(String)c1[1].getSelectedItem();
  int loc=pair.indexOf('_');
  String ss1=pair.substring(0,loc);
  String ss2=pair.substring((loc+1),pair.length());
  t1[3].setText(pair);
  if(ss1.equals("tdb")) l1[3].setText(" dry bulb Temperature ");
  else if(ss1.equals("w")) l1[3].setText(" specific humidity ");
  if(ss2.equals("twb")) l1[4].setText(" wet bulb Temperature ");
  else if(ss2.equals("rh")) l1[4].setText(" relative humidity ");
  else if(ss2.equals("w")) l1[4].setText(" specific humidity ");
  else if(ss2.equals("tdew")) l1[4].setText(" dew point Temperature ");
  else if(ss2.equals("pv")) l1[4].setText(" vapor pressure ");
  else if(ss2.equals("dos")) l1[4].setText(" degree of saturation ");
  else if(ss2.equals("h")) l1[4].setText(" enthalpy ");
  else if(ss2.equals("s")) l1[4].setText(" entropy ");

  if(unit.equals("SI"))
  {
  l2[2].setText(" bars");
  if(ss1.equals("tdb")) l2[3].setText(" degree C ");
  else if(ss1.equals("w")) l2[3].setText(" kg water vapor/kg dry air");
  if(ss2.equals("twb")) l2[4].setText(" degree C");
  else if(ss2.equals("rh")) l2[4].setText(" ");
  else if(ss2.equals("w")) l2[4].setText(" kg water vapor/kg dry air");
  else if(ss2.equals("tdew")) l2[4].setText(" degree C");
  else if(ss2.equals("pv")) l2[4].setText(" bars");
  else if(ss2.equals("dos")) l2[4].setText(" ");
  else if(ss2.equals("h")) l2[4].setText(" KJ/kg dry air");
  else if(ss2.equals("s")) l2[4].setText(" KJ/K kg dry air");
  }
  else
  {
  l2[2].setText(" lbf/in^2 (psia)");
  if(ss1.equals("tdb")) l2[3].setText(" degree F ");
  else if(ss1.equals("w")) l2[3].setText(" lbm water vapor/lbm dry air");
  if(ss2.equals("twb")) l2[4].setText(" degree F");
  else if(ss2.equals("rh")) l2[4].setText(" ");
  else if(ss2.equals("w")) l2[4].setText(" lbm water vapor/lbm dry air");
  else if(ss2.equals("tdew")) l2[4].setText(" degree F");
  else if(ss2.equals("pv")) l2[4].setText(" lbf/in^2 (psia)");
  else if(ss2.equals("dos")) l2[4].setText(" ");
  else if(ss2.equals("h")) l2[4].setText(" BTU/lbm dry air");
  else if(ss2.equals("s")) l2[4].setText(" BTU/R lbm dry air");
  }
  t1[1].setText("");
  t1[2].setText("");
  repaint();
  }


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

    public static void open()
    {
        JFrame f = new JFrame("Applet Properties of air-water mixture (Psychrometric chart)");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JApplet applet = new wetairTable();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(600,550));
        f.setVisible(true);
    }
    
    public static void main(String s[])
    {
        JFrame f = new JFrame("Properties of air-water mixture (Psychrometric chart)");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JApplet applet = new wetairTable();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(600,550));
        f.setVisible(true);
    }
}





