
/**
 * WavelengthConversionPanel.java
 * Used to perform different kinds of scientific conversions
 *
 * Created: September 27, 2005
 *
 * @author Nathan Stevens
 * @version 0.1 10/10/05
 */
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;

public class WavelengthConversionPanel extends JPanel implements ActionListener {
  private ConversionPlugin conversionPlugin = null;
  private JTextField tf1 = null; // to enter wavelength in nanometers (nm)
  private JTextField tf2 = null; // to enter wavelength in wavenumbers (cm^-1)
  private JTextField tf3 = null; // to enter wavelength in frequency (sec^-1)
  private JTextField tf4 = null; // to enter wavelength in energy (kcal/mol)
  private JTextField tf5 = null; // to enter wavelength in energy (eV)
  
  // default constructor
  public WavelengthConversionPanel() { }
  
  // the real constructor
  public WavelengthConversionPanel(ConversionPlugin conversionPlugin) {
    this.conversionPlugin = conversionPlugin;
    
    // set the layout
    setLayout(new BorderLayout());
    
    //The panel that hold the 
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(0, 5));
    
    JLabel label;  // the conversion labels
    
    /* add the labels to the panel */
    
    // Row number 1
    label = new JLabel("<html><font color=blue>Wavelength (nm)</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=blue> Wave number (cm<sup>-1</sup>) </font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=blue> Frequency (sec<sup>-1</sup>) </font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=blue>Energy (kcal/mole)</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=blue>Energy (eV)</font>");
    panel.add(label);
    
    // Row number 2
    label = new JLabel("<html><font color=black>200</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>50,000</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>1.5 x 10<sup>15</sup></font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>143.0</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>6.20</font>");
    panel.add(label);
    
    // Row number 3
    label = new JLabel("<html><font color=black>250</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>40,000</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>1.2 x 10<sup>15</sup></font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>114.4</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>4.96</font>");
    panel.add(label);
    
    // Row number 4
    label = new JLabel("<html><font color=black>300</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>33,333</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>1.0 x 10<sup>15</sup></font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>95.3</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>4.13</font>");
    panel.add(label);
    
    // Row number 5
    label = new JLabel("<html><font color=black>350</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>28,571</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>8.7 x 10<sup>14</sup></font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>81.7</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>3.54</font>");
    panel.add(label);
    
    // Row number 6
    label = new JLabel("<html><font color=black>400</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>25,000</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>7.5 x 10<sup>14</sup></font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>71.5</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>3.10</font>");
    panel.add(label);
    
    // Row number 7
    label = new JLabel("<html><font color=black>450</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>22,222</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>6.6 x 10<sup>14</sup></font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>63.5</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>2.75</font>");
    panel.add(label);
    
    // Row number 8
    label = new JLabel("<html><font color=black>500</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>20,000</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>6.0 x 10<sup>14</sup></font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>57.2</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>2.48</font>");
    panel.add(label);
    
    // Row number 9
    label = new JLabel("<html><font color=black>550</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>18,182</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>5.4 x 10<sup>14</sup></font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>52.0</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>2.25</font>");
    panel.add(label);
    
    // Row number 10
    label = new JLabel("<html><font color=black>600</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>16,666</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>5.0 x 10<sup>14</sup></font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>47.7</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>2.07</font>");
    panel.add(label);
    
    // Row number 11
    label = new JLabel("<html><font color=black>650</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>15,385</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>4.6 x 10<sup>14</sup></font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>44.0</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>1.91</font>");
    panel.add(label);
    
    // Row number 12
    label = new JLabel("<html><font color=black>700</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>14,286</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>4.2 x 10<sup>14</sup></font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>40.8</font>");
    panel.add(label);
    
    label = new JLabel("<html><font color=black>1.77</font>");
    panel.add(label);
    
    // Row number 13
    tf1 = new JTextField("0"); // nm
    tf1.addActionListener(this);
    tf1.setActionCommand("tf1");
    tf1.setForeground(Color.red);
    panel.add(tf1);
    
    tf2 = new JTextField("0"); // cm^-1
    tf2.addActionListener(this);
    tf2.setActionCommand("tf2");
    tf2.setForeground(Color.red);
    panel.add(tf2);
    
    tf3 = new JTextField("0"); // sec^-1
    tf3.addActionListener(this);
    tf3.setActionCommand("tf3");
    tf3.setForeground(Color.red);
    panel.add(tf3);
    
    tf4 = new JTextField("0"); // kcal^-1
    tf4.addActionListener(this);
    tf4.setActionCommand("tf4");
    tf4.setForeground(Color.red);
    panel.add(tf4);
    
    tf5 = new JTextField("0"); // eV
    tf5.addActionListener(this);
    tf5.setActionCommand("tf5");
    tf5.setForeground(Color.red);
    panel.add(tf5);
    
    //Create the scroll pane and add the table to it. 
    JScrollPane scrollPane = new JScrollPane(panel);
    
    //Add the scroll pane to this window.
    add(scrollPane, "Center");
  }
  
  // handel any action events
  public void actionPerformed(ActionEvent ae) {
    String cmd = ae.getActionCommand();
    
    if(cmd.equals("tf1")) {
      nanometerTo();
		}
    else if(cmd.equals("tf2")) {
      wavenumberTo();
		}
    else if(cmd.equals("tf3")) {
      frequencyTo();
		}
    else if(cmd.equals("tf4")) {
      kcalTo();
		}
    else if(cmd.equals("tf5")) {
      evTo();
		}
    else {
      System.err.println("Action Command not handeled in class WavelengthConversionPanel...");
      System.err.println(cmd);
    }
  }
  
  // convert from nanometers
  private void nanometerTo() {
    try {
      float nm = Float.parseFloat(tf1.getText());
      nanometerTo(nm);
    }
    catch(NumberFormatException nfe) {
      tf1.setText("error");
    }
  }
  
  // convert from nanometers
  private void nanometerTo(float nm) {
    String value;
    
    NumberFormat nf = NumberFormat.getInstance();
    DecimalFormat df = (DecimalFormat)nf;
    df.setDecimalSeparatorAlwaysShown(true);
    
    //set the nanometer
    df.applyPattern("0");
    value = df.format(nm); // the quantum yield
    tf1.setText(value);
    
    // convert to wavenumbers
    float wavenumber = 1.0f/(nm*1.0e-7f);
    
    df.applyPattern("0.#");
    value = df.format(wavenumber); // the quantum yield
    tf2.setText(value);
    
    // convert to frequency
    float frequency = 3.0e10f/(nm*1.0e-7f);
    
    df.applyPattern("0.0E00");
    value = df.format(frequency); // the quantum yield
    tf3.setText(value);
    
    // convert to kcal
    float kcal = 2.86e4f/nm;
    
    df.applyPattern("0.00");
    value = df.format(kcal); // the quantum yield
    tf4.setText(value);
    
    //convert to eV
    float eV = wavenumber/8.0663e3f;
    
    df.applyPattern("0.00");
    value = df.format(eV); // the quantum yield
    tf5.setText(value);
  }
  
  // convert from wavenumbers
  private void wavenumberTo() {
    try {
      float wavenumber = Float.parseFloat(tf2.getText());
      
      float nm = 1.0f/(wavenumber*1.0e-7f);
      nanometerTo(nm);
    }
    catch(NumberFormatException nfe) {
      tf2.setText("error");
    }
  }
  
  // convert from frequency
  private void frequencyTo() {
    try {
      float frequency = Float.parseFloat(tf3.getText());
      
      float wavenumber = 3.0e10f/frequency;
      
      float nm = wavenumber/1.0e-7f;
      nanometerTo(nm);
    }
    catch(NumberFormatException nfe) {
      tf3.setText("error");
    }
  }
  
  // convert from kcal
  private void kcalTo() {
    try {
      float kcal = Float.parseFloat(tf4.getText());
      
      float nm = 2.86e4f/kcal;
      nanometerTo(nm);
    }
    catch(NumberFormatException nfe) {
      tf4.setText("error");
    }
  }
  
  // convert from eV
  private void evTo() {
    try {
      float eV = Float.parseFloat(tf5.getText());
      
      float wavenumber = eV*8.0663e3f;
      
      float nm = 1/(wavenumber*1.0e-7f);
      
      nanometerTo(nm);
    }
    catch(NumberFormatException nfe) {
      tf5.setText("error");
    }
  }
} //--end class WavelengthConversionPanel
