//======================================================
// Thermodynamics package in java
// pipeProperty class to calculate pipe
// user interface (applet)
// Dr. Turhan Coban
// =====================================================

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.util.*;

public class pipeProperty extends Applet implements ActionListener,ItemListener
{
  protected Panel inputPanel;
  protected Label promptGmixName;         // Label prompt GmixName
  protected Label promptTemperature;      // Label prompt Temperature
  protected Label promptPressure;         // Label prompt Pressure
  protected Label promptPipeLength;       // Label prompt Pipe Length
  protected Label promptShape;            // Label prompt cross-section shape
  protected Label promptD1;               // Label prompt first dimension
  protected Label promptD2;               // Label prompt second dimension
  protected Label promptKL;               // LP local PD coefficient
  protected Label promptMass;             // LP mass flow rate
  protected Label prompte;                // LP pipe equivalent roughness
  protected Choice inputGmixName;      // input GmixName
  protected TextField inputTemperature;   // input Temperature
  protected TextField inputPressure;      // input Pressure
  protected TextField inputPipeLength;    // input pipe length
  protected Choice inputShape;
  protected String pipeShapeArray[]={"circular","rectangular",
  "concentric_annulus","circular_segment","circular_sector","right_triangle"};
  protected TextField inputD1;
  protected TextField inputD2;
  protected TextField inputKL;
  protected TextField inputMass;
  protected TextField inpute;
  double T;
  double P;
  double TK;
  double pipeLength;
  String pipeShape;
  double D1,D2;
  String GmixName;
  String pipeName;
  double mass_flow_rate;
  Gmix g1;
  pipe p1;
  double KL;
  double e;
  StringTokenizer token;

  public void init()
  {
    //adding max-min prompts and input fields
    inputPanel=new Panel();
    inputPanel.setLayout(new GridLayout(10,2));
    GmixName=new String("natural");
    try{
       g1=new Gmix(GmixName);
       token=new StringTokenizer(g1.readGmixNames());
       } catch(IOException ioe) {System.out.println("IOException");}
    pipeName=new String("P18_natural");
    T=27.0;
    TK=T+273;
    P=1.0;
    pipeLength=1.0;
    KL=0.0;
    D1=0.005;
    D2=0.005;
    e=0.00001;
    mass_flow_rate=0.01;
    pipeShape =new String("circular");
    promptGmixName=    new Label(" Gas mixture Name       : ");
    inputGmixName=new Choice();
    while(token.hasMoreTokens())
    {
    inputGmixName.add(token.nextToken());
    }
    //inputGmixName=new TextField(15);
    promptTemperature= new Label(" Temperature (øC)       : ");
    inputTemperature=new TextField(15);
    promptPressure=   new Label("  Pressure (Bars)        :  ");
    inputPressure=new TextField(15);
    promptPipeLength=  new Label(" Pipe Length, m         :  ");
    inputPipeLength=new TextField(15);
    promptShape=  new Label(" Pipe Shape                  :  ");
    inputShape=new Choice();
    for(int i=0;i<5;i++)
    {
    inputShape.add(pipeShapeArray[i]);
    }
    promptD1=     new Label(" Pipe Dimension D1, m        :  ");
    inputD1= new TextField(15);
    promptD2=     new Label(" Pipe Dimension D2, m        :  ");
    inputD2= new TextField(15);
    promptKL=     new Label(" Pipe Local PD coefficient,  :  ");
    inputKL= new TextField(15);
    promptMass=   new Label(" mass flow rate , kg/s       :  ");
    inputMass= new TextField(15);
    prompte=      new Label(" Pipe equivalent roughness, m:  ");
    inpute= new TextField(15);
//********
    inputGmixName.addItemListener(this);
    inputTemperature.addActionListener(this);
    inputPressure.addActionListener(this);
    inputPipeLength.addActionListener(this);
    inputShape.addItemListener(this);
    inputD1.addActionListener(this);
    inputD2.addActionListener(this);
    inputKL.addActionListener(this);
    inputMass.addActionListener(this);
    inpute.addActionListener(this);

//********
     inputPanel.add(promptGmixName);
     inputPanel.add(inputGmixName);
     inputPanel.add(promptTemperature);
     inputPanel.add(inputTemperature);
     inputPanel.add(promptPressure);
     inputPanel.add(inputPressure);
     inputPanel.add(promptPipeLength);
     inputPanel.add(inputPipeLength);
     inputPanel.add(promptShape);
     inputPanel.add(inputShape);
     inputPanel.add(promptD1);
     inputPanel.add(inputD1);
     inputPanel.add(promptD2);
     inputPanel.add(inputD2);
     inputPanel.add(promptKL);
     inputPanel.add(inputKL);
     inputPanel.add(promptMass);
     inputPanel.add(inputMass);
     inputPanel.add(prompte);
     inputPanel.add(inpute);
     inputTemperature.setText(Double.toString(T));
     inputPressure.setText(Double.toString(P));
     inputPipeLength.setText(Double.toString(pipeLength));
     inputD1.setText(Double.toString(D1));
     inputD2.setText(Double.toString(D2));
     inputKL.setText(Double.toString(KL));
     inputMass.setText(Double.toString(mass_flow_rate));
     inpute.setText(Double.toString(e));
     //inputGmixName.setText(GmixName);
     inputD1.setText(Double.toString(D1));
     inputD2.setText(Double.toString(D2));
     inputD2.setEditable(false);
     inputKL.setText(Double.toString(KL));
     inputMass.setText(Double.toString(mass_flow_rate));
     inpute.setText(Double.toString(e));
     setLayout(new BorderLayout());
     add( inputPanel,BorderLayout.NORTH);
     p1= new pipe(pipeName,
               pipeShape,
               GmixName,
               D1,
               D2,
               mass_flow_rate,
               e,
               P,
               KL,
               pipeLength);
  }

 public void itemStateChanged(ItemEvent ev)
 {
  //pipeShape=pipeShapeArray[inputShape.getSelectedIndex()];
  pipeShape=inputShape.getSelectedItem();
  if(pipeShape.equals("circular"))
  {
  inputD2.setEditable(false);
  }
  else
  {
  inputD2.setEditable(true);
  }
  GmixName=inputGmixName.getSelectedItem();
  p1= new pipe(pipeName,
               pipeShape,
               GmixName,
               D1,
               D2,
               mass_flow_rate,
               e,
               P,
               KL,
               pipeLength);
  repaint();
  showStatus(pipeShape);
 }//end of itemStateChange

  public void paint(Graphics g)
  {
    TK=T+273;
    g.drawString("Pressure Drop , bars              : ",20,250);
    g.drawString(""+p1.dP(TK,P),250,250);
    g.drawString("f friction                        : ",20,265);
    g.drawString(""+p1.f(TK),250,265);
    g.drawString("Velocity , m/s                    : ",20,280);
    g.drawString(""+p1.velocity(T),250,280);
    g.drawString("Reynold's number Re               : ",20,295);
    g.drawString(""+p1.Re(TK),250,295);
    g.drawString("Prandl's number Pr                : ",20,310);
    g.drawString(""+p1.g.Prandtl(TK),250,2310);
    g.drawString("Nusselt's number Nu               : ",20,325);
    g.drawString(""+p1.Nu(TK),250,325);
    g.drawString("Hydraulic entry length, m         : ",20,340);
    g.drawString(""+p1.lentry(TK),250,340);
    g.drawString("Viscosity , N s/m^2               : ",20,355);
    g.drawString(""+p1.g.vis(TK),250,355);
    g.drawString(p1.g.toString("formula"),20,370);
    g.drawString(p1.g.toString("composition"),20,385);
    g.drawString("shape              : "+pipeShape,20,400);

  }//end of method

public void actionPerformed( ActionEvent ee)
{
  GmixName=inputGmixName.getSelectedItem();
  Double valTemp=new Double(inputTemperature.getText());
  T=valTemp.doubleValue();
  Double valPressure=new Double(inputPressure.getText());
  P=valPressure.doubleValue();
  Double valpipeLength=new Double(inputPipeLength.getText());
  pipeLength=valpipeLength.doubleValue();

  Double valD1=new Double(inputD1.getText());
  D1=valD1.doubleValue();
  Double valD2=new Double(inputD2.getText());
  D2=valD2.doubleValue();
  Double valKL=new Double(inputKL.getText());
  KL=valKL.doubleValue();
  Double valMass=new Double(inputMass.getText());
  mass_flow_rate=valMass.doubleValue();
  Double valuee=new Double(inpute.getText());
  e=valuee.doubleValue();
  p1= new pipe(pipeName,
               pipeShape,
               GmixName,
               D1,
               D2,
               mass_flow_rate,
               e,
               P,
               KL,
               pipeLength);
  repaint();
}


}



