// ======================================================
// Thermodynamics package in java
// GasPropertySWF 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.*;
import GasModel;

public class GasPropertySWF extends JFrame implements ActionListener,ItemListener
{
  JPanel inputPanel;
  JPanel outputPanel;
  JPanel totalPanel;
  JTextField promptGasName;     // Label prompt GasName
  JTextField promptTemperature; // Label prompt Temperature
  JTextField promptPressure;    // Label prompt Pressure
  JComboBox inputGasName;       // input GasName
  JTextField inputTemperature;  // input (from list )Temperature
  JTextField inputPressure;     // input Pressure
  JTextArea outputTextArea;
  GasModel gm;
  JTable jt;
  double T;
  double P;
  String gasName;
  Gas g1;
  protected File gmixFile;
  protected StringTokenizer token;
  String st[];
    final static Color bg=Color.lightGray;
    final static Color fg=Color.black;
    final static Color kirmizi=Color.red;
    final static Color beyaz=Color.white;

  public GasPropertySWF()
  {
    super("Properties of perfect gases ");
    Container c=getContentPane();
    c.setLayout(new FlowLayout());
    //adding max-min prompts and input fields
    gasName=new String("air");
    T=27.0;
    P=1.0;
    inputPanel=new JPanel();
    inputPanel.setLayout(new GridLayout(3,2));
    outputPanel=new JPanel();
    outputPanel.setLayout(new BorderLayout());
    totalPanel=new JPanel();
    totalPanel.setLayout(new BorderLayout());
    try{
       g1=new Gas(gasName);
       token=new StringTokenizer(g1.readGasNames());
       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());
    }
    inputGasName=new JComboBox(st);
    gasName=(String)inputGasName.getSelectedItem();
    try{
       g1=new Gas(gasName);
       } catch(IOException ioe) {System.out.println("IOException");}
    promptGasName=    new JTextField(20);
    promptGasName.setText("Name of gas : ");
    promptGasName.setBackground(Color.lightGray);
    promptTemperature=new JTextField(20);
    promptTemperature.setText("Temperature (°C) : ");
    promptTemperature.setBackground(Color.lightGray);
    inputTemperature=new JTextField(20);
    inputTemperature.setBackground(Color.lightGray);
    promptPressure=  new JTextField(20);
    promptPressure.setText("Pressure (Bars)  : ");
    promptPressure.setBackground(Color.lightGray);
    outputTextArea = new JTextArea();
    outputTextArea.setBackground(Color.lightGray);
    inputPressure=new JTextField(20);
    inputPressure.setBackground(Color.lightGray);
    inputPanel.add(promptGasName);
    inputPanel.add(inputGasName);
    inputPanel.add(promptTemperature);
    inputPanel.add(inputTemperature);
    inputPanel.add(promptPressure);
    inputPanel.add(inputPressure);
    inputGasName.addItemListener(this);
    inputTemperature.addActionListener(this);
    inputPressure.addActionListener(this);
    gm=new GasModel(g1,T,P);
    jt=new JTable(gm);
    jt.setBackground(c.getBackground());
    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()
 {
    double TK=T+273.0;
    inputTemperature.setText(Double.toString(T));
    inputPressure.setText(Double.toString(P));
    String s="additional data can be added to Gas.txt file \n";
    s+="Dr. Turhan Çoban, \n";
    s+="University of Nebraska-Lincoln, Mechanical Engineering dept.\n";
    s+="Walter Scott Engineering Building Office N106 phone 472-3793\n";
    s+="     \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,T,P);
    outputTextArea.setText(s);
 }

  public void itemStateChanged(ItemEvent ev)
  {
  gasName=(String)inputGasName.getSelectedItem();
  try{
  g1=new Gas(gasName);
  } catch(IOException ioe1) {System.out.println("IOException");}
  setArea();
  repaint();
  }


public void actionPerformed( ActionEvent e)
{
  Double valTemp=new Double(inputTemperature.getText());
  T=valTemp.doubleValue();
  Double valPressure=new Double(inputPressure.getText());
  P=valPressure.doubleValue();
  getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
  setArea();
  repaint();
}
      public static void main(String[] args)
      {
      GasPropertySWF pencere= new GasPropertySWF();
      pencere.addWindowListener(
      new WindowAdapter()
      {
      public void windowClosing(WindowEvent e) {System.exit(0);}
      }                        );
      pencere.pack();
      pencere.setVisible(true);
      }
}





