1   package org.wcb.gui.forms.e6b;
2   
3   import org.wcb.e6b.E6bConverter;
4   import org.wcb.gui.forms.e6b.event.CalculateFormActionListener;
5   
6   import javax.swing.*;
7   import java.awt.*;
8   
9   /**
10   * <small>
11   * <p/>
12   * Copyright (c)  2006  wbogaardt.
13   * This library is free software; you can redistribute it and/or
14   * modify it under the terms of the GNU Lesser General Public
15   * License as published by the Free Software Foundation; either
16   * version 2.1 of the License, or (at your option) any later version.
17   *
18   * This library is distributed in the hope that it will be useful,
19   * but WITHOUT ANY WARRANTY; without even the implied warranty of
20   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21   * Lesser General Public License for more details.
22   *
23   * You should have received a copy of the GNU Lesser General Public
24   * License along with this library; if not, write to the Free Software
25   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26   * <p/>
27   * $File:  $ <br>
28   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Sep 18, 2006 10:29:36 AM $ <br>
29   * </small>
30   *
31   * @author wbogaardt
32   *         Display Panel for pressure Altitude
33   */
34  
35  public class FahrenheitConversionPanel extends JPanel implements ICalculateForm{
36  
37      private JTextField tempuratureFahrenheit;
38      private JButton calculateButton;
39      private JButton clearButton;
40      private JTextField results;
41  
42      public FahrenheitConversionPanel() {
43          setBorder(BorderFactory.createTitledBorder("Fahrenheit to Celsius"));
44          initComponents();
45      }
46  
47      public void clearForm() {
48          tempuratureFahrenheit.setText(null);
49      }
50  
51      private void initComponents() {
52          tempuratureFahrenheit = new JTextField(5);
53          calculateButton = new JButton("Calculate Celsius");
54          clearButton = new JButton("Clear Temperature Values");
55          results = new JTextField(5);
56          calculateButton.addActionListener(new CalculateFormActionListener(this));
57          clearButton.addActionListener(new CalculateFormActionListener(this));
58  
59          setLayout(new GridBagLayout());
60          GridBagConstraints gridBagConstraints;
61  
62          gridBagConstraints = new GridBagConstraints();
63          gridBagConstraints.gridx = 0;
64          gridBagConstraints.gridy = 1;
65          gridBagConstraints.anchor = GridBagConstraints.WEST;
66          add(new JLabel("Enter"), gridBagConstraints);
67  
68          gridBagConstraints.gridx = 1;
69          gridBagConstraints.gridy = 1;
70          gridBagConstraints.anchor = GridBagConstraints.WEST;
71          add(tempuratureFahrenheit, gridBagConstraints);
72  
73          gridBagConstraints.gridx = 2;
74          gridBagConstraints.gridy = 1;
75          gridBagConstraints.anchor = GridBagConstraints.WEST;
76          add(new JLabel("F"), gridBagConstraints);
77  
78          //standard calculate and reset buttons
79          gridBagConstraints.gridx = 0;
80          gridBagConstraints.gridy = 2;
81          gridBagConstraints.gridwidth = 3;
82          gridBagConstraints.anchor = GridBagConstraints.WEST;
83          gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
84          gridBagConstraints.insets = new Insets(4, 0, 4, 0);
85          add(calculateButton, gridBagConstraints);
86  
87          gridBagConstraints = new GridBagConstraints();
88          gridBagConstraints.gridx = 3;
89          gridBagConstraints.gridy = 2;
90          gridBagConstraints.anchor = GridBagConstraints.WEST;
91          add(clearButton, gridBagConstraints);
92  
93          gridBagConstraints.gridx = 0;
94          gridBagConstraints.gridy = 3;
95          gridBagConstraints.gridwidth = 3;
96          gridBagConstraints.anchor = GridBagConstraints.WEST;
97          add(new JLabel("Calculated Tempurature ="), gridBagConstraints);
98  
99          gridBagConstraints = new GridBagConstraints();
100         gridBagConstraints.gridx = 3;
101         gridBagConstraints.gridy = 3;
102         gridBagConstraints.anchor = GridBagConstraints.WEST;
103         add(results, gridBagConstraints);
104     }
105 
106     public Integer getTempuratureFahrenheit() {
107         int returnValue;
108         try {
109             returnValue = Integer.parseInt(tempuratureFahrenheit.getText());
110         } catch (NumberFormatException nfe) {
111             returnValue = 0;
112         }
113         return returnValue;
114     }
115 
116     public void setTempuratureFahrenheit(int tempuratureFahrenheit) {
117         this.tempuratureFahrenheit.setText(tempuratureFahrenheit + "");
118     }
119 
120 
121     public void calculateResult() {
122         Double value = E6bConverter.fahrenheitToCelsius(this.getTempuratureFahrenheit());
123         results.setText(E6bConverter.format(value));
124     }
125 }