1   package org.wcb.gui.forms.e6b;
2   
3   import org.wcb.e6b.UnknownWind;
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's unknown cross wind calculation fields. Wind Direction
33   */
34  
35  public class UnknownXwindPanel extends JPanel implements ICalculateForm{
36  
37      private JTextField heading;
38      private JTextField groundSpeed;
39      private JTextField course;
40      private JTextField trueAirspeed;
41      private JButton calculateButton;
42      private JButton clearButton;
43      private JTextField directionResults;
44      private JTextField windspeedResults;
45  
46      public UnknownXwindPanel() {
47          setBorder(BorderFactory.createTitledBorder("Wind Direction"));
48          initComponents();
49      }
50  
51      public void clearForm() {
52          heading.setText(null);
53          groundSpeed.setText(null);
54          course.setText(null);
55          trueAirspeed.setText(null);
56      }
57  
58      private void initComponents() {
59          heading = new JTextField(5);
60          groundSpeed = new JTextField(5);
61          course = new JTextField(5);
62          trueAirspeed = new JTextField(5);
63          calculateButton = new JButton("Calculate Wind Direction");
64          clearButton = new JButton("Clear Wind Direction Values");
65          directionResults = new JTextField(10);
66          windspeedResults = new JTextField(10);
67          calculateButton.addActionListener(new CalculateFormActionListener(this));
68          clearButton.addActionListener(new CalculateFormActionListener(this));
69  
70          setLayout(new GridBagLayout());
71          GridBagConstraints gridBagConstraints;
72  
73          gridBagConstraints = new GridBagConstraints();
74          gridBagConstraints.gridx = 0;
75          gridBagConstraints.gridy = 1;
76          gridBagConstraints.anchor = GridBagConstraints.WEST;
77          add(new JLabel("Heading"), gridBagConstraints);
78  
79          gridBagConstraints.gridx = 1;
80          gridBagConstraints.gridy = 1;
81          gridBagConstraints.anchor = GridBagConstraints.WEST;
82          add(heading, gridBagConstraints);
83  
84          gridBagConstraints.gridx = 0;
85          gridBagConstraints.gridy = 2;
86          gridBagConstraints.anchor = GridBagConstraints.WEST;
87          add(new JLabel("Ground Speed"), gridBagConstraints);
88  
89          gridBagConstraints.gridx = 1;
90          gridBagConstraints.gridy = 2;
91          gridBagConstraints.anchor = GridBagConstraints.WEST;
92          add(groundSpeed, gridBagConstraints);
93  
94          gridBagConstraints.gridx = 0;
95          gridBagConstraints.gridy = 3;
96          gridBagConstraints.anchor = GridBagConstraints.WEST;
97          add(new JLabel("Course"), gridBagConstraints);
98  
99          gridBagConstraints.gridx = 1;
100         gridBagConstraints.gridy = 3;
101         gridBagConstraints.anchor = GridBagConstraints.WEST;
102         add(course, gridBagConstraints);
103 
104         gridBagConstraints.gridx = 0;
105         gridBagConstraints.gridy = 4;
106         gridBagConstraints.anchor = GridBagConstraints.WEST;
107         add(new JLabel("True Airspeed"), gridBagConstraints);
108 
109         gridBagConstraints.gridx = 1;
110         gridBagConstraints.gridy = 4;
111         gridBagConstraints.anchor = GridBagConstraints.WEST;
112         add(trueAirspeed, gridBagConstraints);
113 
114          //standard calculate and reset buttons
115         gridBagConstraints.gridx = 0;
116         gridBagConstraints.gridy = 5;
117          gridBagConstraints.gridwidth = 2;
118         gridBagConstraints.anchor = GridBagConstraints.WEST;
119         add(calculateButton, gridBagConstraints);
120 
121         gridBagConstraints = new GridBagConstraints();
122         gridBagConstraints.gridx = 2;
123         gridBagConstraints.gridy = 5;
124         gridBagConstraints.anchor = GridBagConstraints.WEST;
125         add(clearButton, gridBagConstraints);
126 
127         gridBagConstraints.gridx = 0;
128         gridBagConstraints.gridy = 6;
129         gridBagConstraints.gridwidth = 2;
130         gridBagConstraints.anchor = GridBagConstraints.WEST;
131         add(new JLabel("Calculated Wind Direction="), gridBagConstraints);
132 
133         gridBagConstraints = new GridBagConstraints();
134         gridBagConstraints.gridx = 2;
135         gridBagConstraints.gridy = 6;
136         gridBagConstraints.anchor = GridBagConstraints.WEST;
137         add(directionResults, gridBagConstraints);
138 
139         gridBagConstraints.gridx = 0;
140         gridBagConstraints.gridy = 7;
141         gridBagConstraints.gridwidth = 2;
142         gridBagConstraints.anchor = GridBagConstraints.WEST;
143         add(new JLabel("Calculated Wind Speed="), gridBagConstraints);
144 
145         gridBagConstraints = new GridBagConstraints();
146         gridBagConstraints.gridx = 2;
147         gridBagConstraints.gridy = 7;
148         gridBagConstraints.anchor = GridBagConstraints.WEST;
149         add(windspeedResults, gridBagConstraints);
150     }
151 
152     public Double getGroundSpeed() {
153         double returnValue;
154         try {
155             returnValue = Double.parseDouble(groundSpeed.getText());
156         } catch (NumberFormatException nfe) {
157             returnValue = 0;
158         }
159         return returnValue;
160     }
161 
162     public void setGroundSpeed(double groundSpeed) {
163         this.groundSpeed.setText(groundSpeed + "");
164     }
165 
166     public Double getCourse() {
167         double returnValue;
168         try {
169             returnValue = Double.parseDouble(course.getText());
170         } catch (NumberFormatException nfe) {
171             returnValue = 0;
172         }
173         return returnValue;
174     }
175 
176     public void setCourse(double course) {
177         this.course.setText(course + "");
178     }
179 
180     public Double getHeading() {
181         double returnValue;
182         try {
183             returnValue = Double.parseDouble(heading.getText());
184         } catch (NumberFormatException nfe) {
185             returnValue = 0;
186         }
187         return returnValue;
188     }
189 
190     public void setHeading(double heading) {
191         this.heading.setText(heading + "");
192     }
193 
194     public Double getTrueAirspeed() {
195         double returnValue;
196         try {
197             returnValue = Double.parseDouble(trueAirspeed.getText());
198         } catch (NumberFormatException nfe) {
199             returnValue = 0;
200         }
201         return returnValue;
202     }
203 
204     public void setTrueAirspeed(double airspeed) {
205         this.trueAirspeed.setText(airspeed + "");
206     }
207 
208     public void calculateResult() {
209         UnknownWind wind = new UnknownWind();
210         wind.setCourse(this.getCourse());
211         wind.setHeading(this.getHeading());
212         wind.setGroundSpeed(this.getGroundSpeed());
213         wind.setTrueAirspeed(this.getTrueAirspeed());
214         wind.calculate();
215         directionResults.setText(wind.getWindDirection() + " degrees");
216         windspeedResults.setText(wind.getWindSpeed() + " knots");
217     }
218 }