1   package org.wcb.gui.forms;
2   
3   import org.wcb.model.vo.hibernate.AirportBO;
4   import org.apache.commons.lang.StringUtils;
5   
6   import javax.swing.*;
7   import java.awt.*;
8   import java.util.StringTokenizer;
9   
10  /**
11   * <small>
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: Mar 29, 2006 2:23:05 PM $ <br>
29   * </small>
30   *
31   * @author wbogaardt
32   * @version 1
33   *          Date: Mar 29, 2006
34   *          Time: 2:23:05 PM
35   */
36  
37  public class AirportAdvancedEntryForm extends JPanel {
38  
39      private JTextField jTextFieldLatitudeDegree;
40      private JTextField jTextFieldLatitudeMinute;
41      private JTextField jTextFieldLatitudeSecond;
42      private JComboBox jCBSphereNS;
43      private JTextField jTextFieldLongitudeDegree;
44      private JTextField jTextFieldLongitudeMinute;
45      private JTextField jTextFieldLongitudeSecond;
46      private JComboBox jCBSphereEW;
47      private JTextField jTextFieldAltitude;
48      private AirportBO oAirport;
49  
50      public AirportAdvancedEntryForm() {
51          initComponents();
52      }
53  
54      private void initComponents() {
55          GridBagConstraints gridBagConstraints;
56          jTextFieldLatitudeDegree = new JTextField(3);
57          jTextFieldLatitudeDegree.setToolTipText("Latitude degrees entered must be < 90");
58          jTextFieldLatitudeMinute = new JTextField(2);
59          jTextFieldLatitudeMinute.setToolTipText("Latitude minutes must be < 60");
60          jTextFieldLatitudeSecond = new JTextField(6);
61          jTextFieldLatitudeSecond.setToolTipText("Latitude seconds must be < 60 e.g. 54 or 54.1000");
62          jCBSphereNS = new JComboBox(new String[]{"N","S"});
63          jTextFieldLongitudeDegree = new JTextField(3);
64          jTextFieldLongitudeDegree.setToolTipText("Longitude degrees entered must be < 180");
65          jTextFieldLongitudeMinute = new JTextField(2);
66          jTextFieldLongitudeMinute.setToolTipText("Longitude minutes must be < 60");
67          jTextFieldLongitudeSecond = new JTextField(6);
68          jTextFieldLongitudeSecond.setToolTipText("Longitude seconds must be < 60 e.g. 54 or 54.1000");
69          jCBSphereEW = new JComboBox(new String[]{"W","E"});
70          jTextFieldAltitude = new JTextField(5);
71  
72          setLayout(new GridBagLayout());
73          gridBagConstraints = new GridBagConstraints();
74  
75          gridBagConstraints.gridwidth = 6;
76          add(new JLabel("Location in spherical data format"), gridBagConstraints);
77  
78          gridBagConstraints = new GridBagConstraints();
79          gridBagConstraints.gridx = 0;
80          gridBagConstraints.gridy = 1;
81          gridBagConstraints.anchor = GridBagConstraints.EAST;
82          add(new JLabel("Latitude"), gridBagConstraints);
83  
84          gridBagConstraints = new GridBagConstraints();
85          gridBagConstraints.gridx = 1;
86          gridBagConstraints.gridy = 1;
87          gridBagConstraints.anchor = GridBagConstraints.WEST;
88          add(jTextFieldLatitudeDegree, gridBagConstraints);
89  
90          gridBagConstraints = new GridBagConstraints();
91          gridBagConstraints.gridx = 2;
92          gridBagConstraints.gridy = 1;
93          gridBagConstraints.anchor = GridBagConstraints.WEST;
94          add(new JLabel("-"), gridBagConstraints);
95  
96          gridBagConstraints = new GridBagConstraints();
97          gridBagConstraints.gridx = 3;
98          gridBagConstraints.gridy = 1;
99          gridBagConstraints.anchor = GridBagConstraints.WEST;
100         add(jTextFieldLatitudeMinute, gridBagConstraints);
101 
102         gridBagConstraints = new GridBagConstraints();
103         gridBagConstraints.gridx = 4;
104         gridBagConstraints.gridy = 1;
105         gridBagConstraints.anchor = GridBagConstraints.WEST;
106         add(new JLabel("-"), gridBagConstraints);
107 
108         gridBagConstraints = new GridBagConstraints();
109         gridBagConstraints.gridx = 5;
110         gridBagConstraints.gridy = 1;
111         gridBagConstraints.anchor = GridBagConstraints.WEST;
112         add(jTextFieldLatitudeSecond, gridBagConstraints);
113 
114         gridBagConstraints = new GridBagConstraints();
115         gridBagConstraints.gridx = 6;
116         gridBagConstraints.gridy = 1;
117         gridBagConstraints.anchor = GridBagConstraints.WEST;
118         add(jCBSphereNS, gridBagConstraints);
119 
120         gridBagConstraints = new GridBagConstraints();
121         gridBagConstraints.gridx = 0;
122         gridBagConstraints.gridy = 2;
123         gridBagConstraints.anchor = GridBagConstraints.EAST;
124         add(new JLabel("Longitude"), gridBagConstraints);
125 
126         gridBagConstraints = new GridBagConstraints();
127         gridBagConstraints.gridx = 1;
128         gridBagConstraints.gridy = 2;
129         gridBagConstraints.anchor = GridBagConstraints.WEST;
130         add(jTextFieldLongitudeDegree, gridBagConstraints);
131 
132         gridBagConstraints = new GridBagConstraints();
133         gridBagConstraints.gridx = 2;
134         gridBagConstraints.gridy = 2;
135         gridBagConstraints.anchor = GridBagConstraints.WEST;
136         add(new JLabel("-"), gridBagConstraints);
137 
138         gridBagConstraints = new GridBagConstraints();
139         gridBagConstraints.gridx = 3;
140         gridBagConstraints.gridy = 2;
141         gridBagConstraints.anchor = GridBagConstraints.WEST;
142         add(jTextFieldLongitudeMinute, gridBagConstraints);
143 
144         gridBagConstraints = new GridBagConstraints();
145         gridBagConstraints.gridx = 4;
146         gridBagConstraints.gridy = 2;
147         gridBagConstraints.anchor = GridBagConstraints.WEST;
148         add(new JLabel("-"), gridBagConstraints);
149 
150         gridBagConstraints = new GridBagConstraints();
151         gridBagConstraints.gridx = 5;
152         gridBagConstraints.gridy = 2;
153         gridBagConstraints.anchor = GridBagConstraints.WEST;
154         add(jTextFieldLongitudeSecond, gridBagConstraints);
155 
156         gridBagConstraints = new GridBagConstraints();
157         gridBagConstraints.gridx = 6;
158         gridBagConstraints.gridy = 2;
159         gridBagConstraints.anchor = GridBagConstraints.WEST;
160         add(jCBSphereEW, gridBagConstraints);
161 
162         gridBagConstraints = new GridBagConstraints();
163         gridBagConstraints.gridx = 0;
164         gridBagConstraints.gridy = 3;
165         gridBagConstraints.anchor = GridBagConstraints.EAST;
166         add(new JLabel("Altitude"), gridBagConstraints);
167 
168         gridBagConstraints = new GridBagConstraints();
169         gridBagConstraints.gridx = 1;
170         gridBagConstraints.gridy = 3;
171         gridBagConstraints.gridwidth = 3;
172         gridBagConstraints.anchor = GridBagConstraints.WEST;
173         add(jTextFieldAltitude, gridBagConstraints);
174 
175         gridBagConstraints = new GridBagConstraints();
176         gridBagConstraints.gridx = 4;
177         gridBagConstraints.gridy = 3;
178         gridBagConstraints.gridwidth = 2;
179         gridBagConstraints.anchor = GridBagConstraints.WEST;
180         add(new JLabel(" MSL"), gridBagConstraints);
181 
182     }
183 
184     /**
185      * This marshalls an object to populate
186      * values in the form.
187      * @param airport The object to marshall into the form.
188      */
189     private void marshallToForm(AirportBO airport) {
190         this.setLongitude(airport.getLongitude());
191         this.setLatitude(airport.getLatitude());
192         this.setAltitude(airport.getAltitude());
193     }
194 
195     /**
196      * This marshals airport information from the form
197      * @param airport The inputed object
198      * @return the same object with updated values.
199      */
200     private AirportBO marshallFromForm(AirportBO airport) {
201         airport.setLongitude(this.getLongitude());
202         airport.setLatitude(this.getLatitude());
203         airport.setAltitude(this.getAltitude());
204         return airport;
205     }
206 
207     /**
208      * Takes the airport information and marshalls
209      * the information into the form for purposes of editing.
210      * If it is a new aiport this would be a new airport value.
211      * @param vAirport airport to marshall into the form.
212      */
213     public void setAirport(AirportBO vAirport) {
214         this.oAirport = vAirport;
215         this.marshallToForm(this.oAirport);
216     }
217 
218     /**
219      * Access to get the form's airport information If the
220      * airport was not set in this class then it is considered a new
221      * airport and will get a new object. If it is not new
222      * then it will be simply remarshalled with the updated data as in the
223      * case of editing a row.
224      * @return the airport value object.
225      */
226     public AirportBO getAirport() {
227         if(oAirport == null) {
228             oAirport = new AirportBO();
229         }
230         return marshallFromForm(oAirport);
231     }
232 
233     /**
234      * Airport field elevation in feet
235      * @return Field elevation in feet
236      */
237     public String getAltitude() {
238         return jTextFieldAltitude.getText();
239     }
240 
241     /**
242      * The airport field elevation in feet MSL
243      * @param sValue Field elevation in feet msl
244      */
245     public void setAltitude(String sValue) {
246         jTextFieldAltitude.setText(sValue);
247     }
248 
249     /**
250      * This takes the values entered in the form and returns them as the following example
251      * string 119-12-26.0000W
252      * @return  The longitude in degrees-minutes-seconds[deliniation] format
253      */
254     public String getLongitude() {
255         if(     StringUtils.trimToNull(jTextFieldLongitudeDegree.getText()) != null
256                 && StringUtils.trimToNull(jTextFieldLongitudeMinute.getText()) != null
257                 && StringUtils.trimToNull(jTextFieldLongitudeSecond.getText()) != null)
258         {
259             StringBuilder builderResult = new StringBuilder(jTextFieldLongitudeDegree.getText());
260             builderResult.append("-");
261             builderResult.append(jTextFieldLongitudeMinute.getText());
262             builderResult.append("-");
263             builderResult.append(jTextFieldLongitudeSecond.getText());
264             builderResult.append((String)jCBSphereEW.getSelectedItem());
265             return builderResult.toString();
266         }
267         return "";
268     }
269 
270     /**
271      * allows setting of longitude in degrees, minutes seconds the passed in string should
272      * look like this 119-12-26.0000W then it will parse correctly.
273      * @param longitude the longitude in degrees-minutes-seconds[delineation]
274      */
275     public void setLongitude(String longitude) {
276         if (StringUtils.trimToNull(longitude) != null) {
277             StringTokenizer tokens = new StringTokenizer(longitude, "-");
278             this.jTextFieldLongitudeDegree.setText(tokens.nextToken());
279             this.jTextFieldLongitudeMinute.setText(tokens.nextToken());
280             String secondsPartial = tokens.nextToken();
281             String secondsToken = secondsPartial.substring(0,secondsPartial.length()-1);
282             this.jTextFieldLongitudeSecond.setText(secondsToken);
283             String ew = secondsPartial.substring(secondsPartial.length()-1);
284             this.jCBSphereEW.setSelectedItem(ew);
285         }
286     }
287 
288     /**
289      * This takes the values entered in the form and returns them as the following example
290      * string 34-12-02.9000N
291      * @return  The latitude in degrees-minutes-seconds[deliniation] format
292      */
293     public String getLatitude() {
294         if(     StringUtils.trimToNull(jTextFieldLatitudeDegree.getText()) != null
295                 && StringUtils.trimToNull(jTextFieldLatitudeMinute.getText()) != null
296                 && StringUtils.trimToNull(jTextFieldLatitudeSecond.getText()) != null)
297         {
298             StringBuilder builderResult = new StringBuilder(jTextFieldLatitudeDegree.getText());
299             builderResult.append("-");
300             builderResult.append(jTextFieldLatitudeMinute.getText());
301             builderResult.append("-");
302             builderResult.append(jTextFieldLatitudeSecond.getText());
303             builderResult.append((String)jCBSphereNS.getSelectedItem());
304             return builderResult.toString();
305         }
306         return "";
307     }
308 
309     /**
310      * allows setting of latitude in degrees, minutes seconds the passed in string should
311      * look like this 34-12-02.9000N then it will parse correctly.
312      * @param latitude the latitude in degrees-minutes-seconds[delineation]
313      */
314     public void setLatitude(String latitude) {
315         if (StringUtils.trimToNull(latitude) != null) {
316             StringTokenizer tokens = new StringTokenizer(latitude, "-");
317             this.jTextFieldLatitudeDegree.setText(tokens.nextToken());
318             this.jTextFieldLatitudeMinute.setText(tokens.nextToken());
319             String secondsPartial = tokens.nextToken();
320             String secondsToken = secondsPartial.substring(0,secondsPartial.length()-1);
321             this.jTextFieldLatitudeSecond.setText(secondsToken);
322             String ns = secondsPartial.substring(secondsPartial.length()-1);
323             this.jCBSphereNS.setSelectedItem(ns);
324         }
325     }
326 
327 }