// ======================================================
// Thermodynamics package in java
// GmixPropertySWF class to calculate properties of perfect
// gas mixtures 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 GmixModel;

public class GmixPropertySWA extends JApplet implements ActionListener,ItemListener
{
  final static String MAIN = "Main Page";
  final static String GASFORMULA = "Gas Formula";
  JTabbedPane tabbedPane;
  JPanel inputPanel;
  JPanel outputPanel;
  JPanel totalPanel;
  JTextField promptGmixName;     // Label prompt GmixName
  JTextField promptTemperature; // Label prompt Temperature
  JTextField promptPressure;    // Label prompt Pressure
  JComboBox inputGmixName;       // input GmixName
  JTextField inputTemperature;  // input (from list )Temperature
  JTextField inputPressure;     // input Pressure
  JTextArea outputTextArea;
  JTextArea outputTextArea1;
  GmixModel gm;
  JTable jt;
  double T;
  double P;
  String GmixName;
  Gmix 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 void init()
  {
    Container c=getContentPane();
    c.setLayout(new FlowLayout());
    tabbedPane = new JTabbedPane();
    //adding max-min prompts and input fields
    GmixName=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 Gmix(GmixName);
       token=new StringTokenizer(g1.readGmixNames());
       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());
    }
    inputGmixName=new JComboBox(st);
    GmixName=(String)inputGmixName.getSelectedItem();
    try{
       g1=new Gmix(GmixName);
       } catch(IOException ioe) {System.out.println("IOException");}
    promptGmixName=    new JTextField(20);
    promptGmixName.setText("Simple Gmix Name  : ");
    promptGmixName.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);
    outputTextArea1 = new JTextArea();
    outputTextArea1.setBackground(Color.lightGray);
    inputPressure=new JTextField(20);
    inputPressure.setBackground(Color.lightGray);
    inputPanel.add(promptGmixName);
    inputPanel.add(inputGmixName);
    inputPanel.add(promptTemperature);
    inputPanel.add(inputTemperature);
    inputPanel.add(promptPressure);
    inputPanel.add(inputPressure);
    inputGmixName.addItemListener(this);
    inputTemperature.addActionListener(this);
    inputPressure.addActionListener(this);
    gm=new GmixModel(g1,T,P);
    jt=new JTable(gm);
    jt.setBackground(c.getBackground());
    setArea();
    outputPanel.add(outputTextArea,BorderLayout.CENTER);
    outputPanel.add(jt,BorderLayout.NORTH);
    totalPanel.add(inputPanel,BorderLayout.NORTH);
    totalPanel.add(outputPanel,BorderLayout.CENTER);
    tabbedPane.addTab(MAIN,    totalPanel);
    tabbedPane.addTab(GASFORMULA, outputTextArea1);
    c.add(tabbedPane);
  }

 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 Gmix.txt file \n";
    s+="Dr. Turhan Çoban, TUBİTAK Marmara Research Center\n";
    s+="Energy Systems & Environmental Research Institute\n";
    s+="PO Box 21, Gebze - Kocaeli, Turkey\n";
    s+="email: Turhan.Coban@posta.mam.gov.tr";
    //note if you would like to list additional information on the screen
    //add to string s
    String s1="";
    s1+="Gas mixture formula     :\n";
    s1+="=========================\n"+g1.toString("formula")+"\n";
    s1+="Gas mixture composition :\n";
    s1+="=========================\n"+g1.toString("composition")+"\n";
    gm.setValues(g1,T,P);
    outputTextArea.setText(s);
    outputTextArea1.setText(s1);
 }

  public void itemStateChanged(ItemEvent ev)
  {
  GmixName=(String)inputGmixName.getSelectedItem();
  try{
  g1=new Gmix(GmixName);
  } 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();
}

}





