1   package org.wcb.gui.forms.e6b;
2   
3   import org.wcb.gui.forms.e6b.event.CalculateFormActionListener;
4   import org.wcb.e6b.E6bConverter;
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 CelsiusConversionPanel extends JPanel implements ICalculateForm{
36  
37      private JTextField tempuratureCelsius;
38      private JButton calculateButton;
39      private JButton clearButton;
40      private JTextField results;
41  
42      public CelsiusConversionPanel() {
43          setBorder(BorderFactory.createTitledBorder("Celsius to Fahrenheit"));
44          initComponents();
45      }
46  
47      public void clearForm() {
48          tempuratureCelsius.setText(null);
49      }
50  
51      private void initComponents() {
52          tempuratureCelsius = new JTextField(5);
53          calculateButton = new JButton("Calculate Fahrenheit");
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(tempuratureCelsius, gridBagConstraints);
72  
73          gridBagConstraints.gridx = 2;
74          gridBagConstraints.gridy = 1;
75          gridBagConstraints.anchor = GridBagConstraints.WEST;
76          add(new JLabel("C"), gridBagConstraints);
77  
78          //standard calculate and reset buttons
79          gridBagConstraints = new GridBagConstraints();
80          gridBagConstraints.gridx = 0;
81          gridBagConstraints.gridy = 2;
82          gridBagConstraints.gridwidth = 3;
83          gridBagConstraints.anchor = GridBagConstraints.WEST;
84          gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
85          gridBagConstraints.insets = new Insets(4, 0, 4, 0);
86          add(calculateButton, gridBagConstraints);
87  
88          gridBagConstraints = new GridBagConstraints();
89          gridBagConstraints.gridx = 3;
90          gridBagConstraints.gridy = 2;
91          gridBagConstraints.anchor = GridBagConstraints.WEST;
92          add(clearButton, gridBagConstraints);
93  
94          gridBagConstraints.gridx = 0;
95          gridBagConstraints.gridy = 3;
96          gridBagConstraints.gridwidth = 3;
97          gridBagConstraints.anchor = GridBagConstraints.WEST;
98          add(new JLabel("Calculated Tempurature ="), gridBagConstraints);
99  
100         gridBagConstraints = new GridBagConstraints();
101         gridBagConstraints.gridx = 3;
102         gridBagConstraints.gridy = 3;
103         gridBagConstraints.anchor = GridBagConstraints.WEST;
104         add(results, gridBagConstraints);
105     }
106 
107     public Double getTempuratureCelsius() {
108         double returnValue;
109         try {
110             returnValue = Double.parseDouble(tempuratureCelsius.getText());
111         } catch (NumberFormatException nfe) {
112             returnValue = 0;
113         }
114         return returnValue;
115     }
116 
117     public void setTempuratureCelsius(int tempuratureCelsius) {
118         this.tempuratureCelsius.setText(tempuratureCelsius + "");
119     }
120 
121 
122     public void calculateResult() {
123         Double value = E6bConverter.celsiusToFahrenheit(this.getTempuratureCelsius());
124         results.setText(E6bConverter.format(value));
125     }
126 }