1   package org.wcb.gui.forms.e6b;
2   
3   import org.wcb.e6b.PressureAltitude;
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 PressureAltitudePanel extends JPanel implements ICalculateForm{
36  
37      private JTextField altitude;
38      private JTextField baroPressure;
39       private JButton calculateButton;
40      private JButton clearButton;
41      private JTextField results;
42  
43      public PressureAltitudePanel() {
44          setBorder(BorderFactory.createTitledBorder("Pressure Altitude"));
45          initComponents();
46      }
47  
48       public void clearForm() {
49          altitude.setText("");
50           baroPressure.setText("");
51      }
52  
53      private void initComponents() {
54          altitude = new JTextField(5);
55          baroPressure = new JTextField(5);
56          calculateButton = new JButton("Calculate Pressure");
57          clearButton = new JButton("Clear Pressure Values");
58          results = new JTextField(5);
59          calculateButton.addActionListener(new CalculateFormActionListener(this));
60          clearButton.addActionListener(new CalculateFormActionListener(this));
61  
62          setLayout(new GridBagLayout());
63          GridBagConstraints gridBagConstraints;
64  
65          gridBagConstraints = new GridBagConstraints();
66          gridBagConstraints.gridx = 0;
67          gridBagConstraints.gridy = 1;
68          gridBagConstraints.anchor = GridBagConstraints.WEST;
69          add(new JLabel("Altitude"), gridBagConstraints);
70  
71          gridBagConstraints = new GridBagConstraints();
72          gridBagConstraints.gridx = 1;
73          gridBagConstraints.gridy = 1;
74          gridBagConstraints.anchor = GridBagConstraints.WEST;
75          add(altitude, gridBagConstraints);
76  
77          gridBagConstraints.gridx = 0;
78          gridBagConstraints.gridy = 2;
79          gridBagConstraints.anchor = GridBagConstraints.WEST;
80          add(new JLabel("Hg Pressure"), gridBagConstraints);
81  
82          gridBagConstraints.gridx = 1;
83          gridBagConstraints.gridy = 2;
84          gridBagConstraints.anchor = GridBagConstraints.WEST;
85          add(baroPressure, gridBagConstraints);
86  
87           //standard calculate and reset buttons
88          gridBagConstraints.gridx = 0;
89          gridBagConstraints.gridy = 3;
90          gridBagConstraints.gridwidth = 2;
91          gridBagConstraints.anchor = GridBagConstraints.WEST;
92          gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
93          add(calculateButton, gridBagConstraints);
94  
95          gridBagConstraints = new GridBagConstraints();
96          gridBagConstraints.gridx = 2;
97          gridBagConstraints.gridy = 3;
98          gridBagConstraints.gridwidth = 2;
99          gridBagConstraints.anchor = GridBagConstraints.WEST;
100         add(clearButton, gridBagConstraints);
101 
102         gridBagConstraints.gridx = 0;
103         gridBagConstraints.gridy = 4;
104         gridBagConstraints.anchor = GridBagConstraints.WEST;
105         gridBagConstraints.gridwidth = 2;
106         add(new JLabel("Calculated Pressure Altitude ="), gridBagConstraints);
107 
108         gridBagConstraints = new GridBagConstraints();
109         gridBagConstraints.gridx = 2;
110         gridBagConstraints.gridy = 4;
111         gridBagConstraints.anchor = GridBagConstraints.WEST;
112         add(results, gridBagConstraints);
113 
114         gridBagConstraints = new GridBagConstraints();
115         gridBagConstraints.gridx = 3;
116         gridBagConstraints.gridy = 4;
117         gridBagConstraints.anchor = GridBagConstraints.WEST;
118         add(new JLabel("Feet"), gridBagConstraints);
119     }
120 
121     public Double getBaroPressure() {
122         double returnValue;
123         try {
124             returnValue = Double.parseDouble(baroPressure.getText());
125         } catch (NumberFormatException nfe) {
126             returnValue = 0;
127         }
128         return returnValue;
129     }
130 
131     public void setBaroPressure(double baroPressure) {
132         this.baroPressure.setText(baroPressure + "");
133     }
134 
135     public Double getAltitude() {
136         double returnValue;
137         try {
138             returnValue = Double.parseDouble(altitude.getText());
139         } catch (NumberFormatException nfe) {
140             returnValue = 0;
141         }
142         return returnValue;
143     }
144 
145     public void setAltitude(double altitude) {
146         this.altitude.setText(altitude + "");
147     }
148 
149 
150     public void calculateResult() {
151         PressureAltitude pressure = new PressureAltitude();
152         pressure.setAltitude(this.getAltitude());
153         pressure.setHg(this.getBaroPressure());
154         pressure.calculate();
155         results.setText(pressure.presureAlitude() + " ");
156     }
157 }