// ======================================================
// Thermodynamics package in java
// refrigerant class to calculate properties of refrigerants
// user interface ( JApplet )
// Dr. Turhan Coban, Halil ATALAY
// =====================================================

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 refTableEN extends JApplet implements ActionListener,ItemListener
{ private static final long serialVersionUID =8412746785L;
  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];   // Input field
  JComboBox<String> c1_0;
  JComboBox<String> c1_1;
  JComboBox<String> c1_2; // Selection fields
  JTextArea output;
  JTable jt;
  refModelEN sm;
  refrigerantEN st;
  public String pair;
  public String gas;
  public double v1;
  public double v2;
  protected StringTokenizer token;
  String s1[]={"SI"};
  String s2[]={"tx","tp","tv","th","tu","ts","pv","pt","ph","pu","ps","px","vp","vt"};
  String s3[]={"R22","R12","R124","R123","R134a","R407C","R404A","R410A","R507A","R401B","R401A","R402B","R142B","R125","R718","R717","R744","R728","R732","R1270","R50","R32"};
  Container c;
  Color bg;
  String ss[];
  
  public void init()
  {  
    pair="tx";
    c=getContentPane();
    c.setLayout(new FlowLayout());
    bg=c.getBackground();
    //adding max - min prompts and input fields
    gas="R22";   
    st=new refrigerantEN(gas);
    int i=0;
    v1=0.0;
    v2=1.0;
    sm=new refModelEN(gas,pair,v1,v2);
    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(" Temperature");
    l1[3]=new JLabel(" Pressure    ");
    l1[4]=new JLabel("Refrigerant name");
    l2[0]=new JLabel("");
    l2[1]=new JLabel("");
    l2[2]=new JLabel(" degree C");
    l2[3]=new JLabel(" kg vapour/kg mixture");
    l2[4]=new JLabel();
    c1_0=new JComboBox<String>(s1);
    c1_1=new JComboBox<String>(s2);
    c1_2=new JComboBox<String>(s3);
    t1[0]=new JTextField(""+v1);
    t1[1]=new JTextField(""+v2);
    t1[2] = new JTextField();
    t1[2].setText(pair);
    t1[3]=new JTextField();
    t1[3].setText(gas);    
    inputPanel.add(l1[0]);
    inputPanel.add(c1_0);
    inputPanel.add(l2[4]);        
    inputPanel.add(l1[4]);
    inputPanel.add(c1_2);
    inputPanel.add(t1[3]);    
    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[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_2.setBackground(bg);    
    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);
    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);
    JScrollPane spjt=new JScrollPane(jt);
    outputPanel.add(new JScrollPane(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 Univrsity, School of Engineering\n";
    s+="Mechanical Eng. Dept.\n";
    s+="web : www.turhancoban.com\n";
    s+="several Equation of States are used to model\n";

    output.setText(s);
     sm.setValues(gas,pair,v1,v2);
    output.setBackground(c.getBackground());
 }

  public void itemStateChanged(ItemEvent ev)
  {
  pair=(String)c1_1.getSelectedItem();
  t1[2].setText(pair);
  gas=(String)c1_2.getSelectedItem();
  t1[3].setText(gas);
  sm.setValues(gas,pair,v1,v2);
  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(" Specific 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(" Specific Volume ");
  if(pair.charAt(1)=='h') l1[3].setText(" Entalphy ");
  if(pair.charAt(1)=='u') l1[3].setText(" Internal enegy ");
  if(pair.charAt(1)=='s') l1[3].setText(" Entropy ");
  if(pair.charAt(1)=='x') l1[3].setText(" quality ");
  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 vapour/kg mixture");
  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 if(e.getSource()==t1[3])
  {
  String st1=(String)c1_2.getSelectedItem();
  gas=st1;
  st1=(String)c1_2.getSelectedItem();
  pair=st1;
  String st2=t1[3].getText();
     for(int j=0;j<s3.length;j++)
     {
        if(s3[j].equals(st2))
        {   gas=st2;
            c1_2.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(gas,pair,v1,v2);
  setArea();
  repaint();
  }
}

    public static void main(String s[])
    {
        JFrame f = new JFrame("THERMODYNAMIC PROPERTIES OF REFRIGERANS");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JApplet applet = new refTableEN();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(800,600));
        f.setVisible(true);
    }
}