//======================================================
// java termodinamik paketi
// pipe2Table sınıfı boru basınç düşümü apleti
// Dr. Turhan Coban
// =====================================================

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 pipe2Table extends JApplet implements ActionListener,ItemListener,Serializable
{
  protected JPanel inputPanel;
  protected JLabel promptGmixName;         // Label prompt GmixName
  protected JLabel promptTemperature;      // Label prompt Temperature
  protected JLabel promptPressure;         // Label prompt Pressure
  protected JLabel promptPipeLength;       // Label prompt Pipe Length
  protected JLabel promptShape;            // Label prompt cross-section shape
  protected JLabel promptD1;               // Label prompt first dimension
  protected JLabel promptD2;               // Label prompt second dimension
  protected JLabel promptKL;               // LP local PD coefficient
  protected JLabel promptMass;             // LP mass flow rate
  protected JLabel prompte;                // LP pipe equivalent roughness
  protected JComboBox inputGmixName;      // input GmixName
  protected JTextField inputTemperature;   // input Temperature
  protected JTextField inputPressure;      // input Pressure
  protected JTextField inputPipeLength;    // input pipe length
  protected JComboBox inputShape;
  protected String pipeShapeArray[]={"silindir","dikdörtgen",
  "silindirler arası","silindir düzlem","silindir dilimi","dik üçgen"};
  protected JTextField inputD1;
  protected JTextField inputD2;
  protected JTextField inputKL;
  protected JTextField inputMass;
  protected JTextField inpute;
  protected JTable jt;
  double T;
  double P;
  double TK;
  double pipeLength;
  String pipeShape;
  double D1,D2;
  String GmixName;
  String pipeName;
  double mass_flow_rate;
  liquid_CS g1;
  pipe2 p1;
  pipe2Model gm;
  double KL;
  double e;
  StringTokenizer token;
  Color bg;
  Color fg;
  
  public void init()
  {
    Container c=getContentPane();
    c.setLayout(new FlowLayout());
    bg=c.getBackground();
    fg=c.getForeground();
	  //adding max-min prompts and input fields
    inputPanel=new JPanel();
    inputPanel.setLayout(new GridLayout(10,2));
    GmixName=new String("su");
       g1=new liquid_CS(GmixName);
       token=new StringTokenizer(g1.readGasNames());
    pipeName=new String("hava");
    T=27.0;
    TK=T+273;
    P=1.0;
    pipeLength=1.0;
    KL=0.0;
    D1=0.05;
    D2=0.05;
    e=0.00001;
    mass_flow_rate=0.01;
    pipeShape =new String("silindir");
    promptGmixName=new JLabel(" Sıvı ismi       : ");
    Font fn=promptGmixName.getFont();
    inputGmixName=new JComboBox();
    String st[]=new String[token.countTokens()];
    int i=0;
    while(token.hasMoreTokens())
    { st[i++]=new String((String)token.nextToken());
    }
    inputGmixName=new JComboBox(st);
    //inputGmixName=new TextField(15);
    promptTemperature= new JLabel(" Sıcaklık (derece C)       : ");
    inputTemperature=new JTextField(15);
    promptPressure=   new JLabel("  Basınç (Bar)        :  ");
    inputPressure=new JTextField(15);
    promptPipeLength=  new JLabel(" Boru boyu, m         :  ");
    inputPipeLength=new JTextField(15);
    promptShape=  new JLabel(" Boru Şekli                  :  ");
    inputShape=new JComboBox(pipeShapeArray);
    promptD1=     new JLabel(" Boru Boyutu D1, m        :  ");
    inputD1= new JTextField(15);
    promptD2=     new JLabel(" Boru Boyutu D2, m        :  ");
    inputD2= new JTextField(15);
    promptKL=     new JLabel(" Boru yerel basınç düşümü katsayısı K,  :  ");
    inputKL= new JTextField(15);
    promptMass=   new JLabel(" debi , kg/s       :  ");
    inputMass= new JTextField(15);
    prompte=      new JLabel(" boru eşdeğer pürüzlülüğü, m:  ");
    inpute= new JTextField(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));
     c.setLayout(new BorderLayout());
     p1= new pipe2(pipeName,
               pipeShape,
               GmixName,
               D1,
               D2,
               mass_flow_rate,
               e,
               P,
               KL,
               pipeLength);
   gm=new pipe2Model(p1,T,P); 
   jt=new JTable(gm);
   jt.setFont(fn);
   jt.setBackground(bg);
   jt.setForeground(fg);
   inputPanel.setBackground(bg);
   inputPanel.setForeground(fg);
   inputTemperature.setBackground(bg);
   inputTemperature.setForeground(fg);
   inputPressure.setBackground(bg);
   inputPressure.setForeground(fg);
   inputPipeLength.setBackground(bg);
   inputPipeLength.setForeground(fg);
   inputShape.setBackground(bg);
   inputShape.setForeground(fg);
   
   inputD1.setBackground(bg);
   inputD1.setForeground(fg);
   
   inputD2.setBackground(bg);
   inputD2.setForeground(fg);
   
   inputKL.setBackground(bg);
   inputKL.setForeground(fg);
   
   inputMass.setBackground(bg);
   inputMass.setForeground(fg);
   
   inpute.setBackground(bg);
   inpute.setForeground(fg);
      inputShape.setBackground(bg);
   inputShape.setForeground(fg);
   c.add( inputPanel,BorderLayout.NORTH);
   c.add(jt,BorderLayout.SOUTH);              
  }

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

public void actionPerformed( ActionEvent ee)
{
  GmixName=(String)inputGmixName.getSelectedItem();
  pipeShape=(String)inputShape.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 pipe2(pipeName,
               pipeShape,
               GmixName,
               D1,
               D2,
               mass_flow_rate,
               e,
               P,
               KL,
               pipeLength);
  gm.setValues(p1,T,P);
  repaint();
}
    public static void main(String s[])
    {
        JFrame f = new JFrame("Boru basınç düşümü");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JApplet applet = new pipe2Table();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(800,500));
        f.setVisible(true);
    }

}



