1   package org.wcb.gui.forms;
2   
3   import javax.swing.*;
4   import java.awt.event.ActionListener;
5   import java.awt.event.ActionEvent;
6   import java.awt.*;
7   import java.util.Date;
8   import java.util.StringTokenizer;
9   import java.text.NumberFormat;
10  
11  import org.wcb.model.vo.hibernate.Logbook;
12  import org.wcb.model.vo.hibernate.AircraftBO;
13  import org.wcb.model.vo.hibernate.AircraftTypeBO;
14  import org.wcb.model.vo.hibernate.AirportBO;
15  import org.wcb.model.bd.LogbookDelegate;
16  import org.wcb.model.bd.AirportDelegate;
17  import org.wcb.model.util.SpringUtil;
18  import org.wcb.model.service.IServicesConstants;
19  import org.wcb.gui.dialog.AirportTableDisplayDialog;
20  import org.wcb.gui.dialog.AircraftTableDisplayDialog;
21  import org.wcb.gui.component.VectorButton;
22  import org.wcb.gui.util.UIHelper;
23  import org.wcb.resources.MessageResourceRegister;
24  import org.wcb.resources.MessageKey;
25  import org.wcb.e6b.SphericalDistanceCalculator;
26  import org.apache.commons.lang.StringUtils;
27  import com.toedter.calendar.JDateChooser;
28  
29  /**
30   * <small>
31   * Copyright (c)  2006  wbogaardt.
32   * This library is free software; you can redistribute it and/or
33   * modify it under the terms of the GNU Lesser General Public
34   * License as published by the Free Software Foundation; either
35   * version 2.1 of the License, or (at your option) any later version.
36   *
37   * This library is distributed in the hope that it will be useful,
38   * but WITHOUT ANY WARRANTY; without even the implied warranty of
39   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
40   * Lesser General Public License for more details.
41   *
42   * You should have received a copy of the GNU Lesser General Public
43   * License along with this library; if not, write to the Free Software
44   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
45   * <p/>
46   * $File:  $ <br>
47   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Mar 28, 2006 10:38:59 AM $ <br>
48   * </small>
49   *
50   * @author wbogaardt
51   * @version 1
52   *          Date: Mar 28, 2006
53   *          Time: 10:38:59 AM
54   */
55  
56  public class LogbookEntryForm extends JPanel implements ActionListener {
57  
58  
59      private Logbook entry;
60      private AircraftBO oAircraft;
61      private LogbookDelegate delegate;
62      private AirportBO fromAirport;
63      private AirportBO toAirport;
64  
65  
66      public LogbookEntryForm() {
67          delegate = (LogbookDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.LOGBOOK_DELEGATE);
68          initComponents();
69      }
70  
71      /**
72       * Initializes components on the form.
73       */
74      private void initComponents() {
75          jTextFieldRegistration = new JTextField(5);
76          jTextAreaVia = new JTextArea();
77  
78          jLFaaFrom = new JLabel();
79          jLFaaFrom.setFont(new Font("Dialog", Font.BOLD, 11));
80          jLFaaTo = new JLabel();
81          jLFaaTo.setFont(new Font("Dialog", Font.BOLD, 11));
82          jLACType = new JLabel();
83          jLACType.setFont(new Font("Dialog", Font.BOLD, 11));
84          NumberFormat format2;
85          format2 = NumberFormat.getInstance();
86          format2.setMaximumFractionDigits(1);
87          jTextFieldFlightTotal = new JFormattedTextField(format2);
88          jTextFieldPIC = new JFormattedTextField(format2);
89          jTextFieldSIC = new JFormattedTextField(format2);
90          jTextFieldCrossCountry = new JFormattedTextField(format2);
91          jTextFieldFlightInstructing = new JFormattedTextField(format2);
92          jTextFieldSafetyPilot = new JFormattedTextField(format2);
93          jTextFieldDual = new JFormattedTextField(format2);
94          jTextFieldSolo = new JFormattedTextField(format2);
95  
96          jSpinnerDayTakeoffs = new JSpinner(new SpinnerNumberModel(0, 0, 10000, 1));
97          jSpinnerDayLandings = new JSpinner(new SpinnerNumberModel(0, 0, 10000, 1));
98          jSpinnerNightTakeoffs = new JSpinner(new SpinnerNumberModel(0, 0, 10000, 1));
99          jSpinnerNightLandings = new JSpinner(new SpinnerNumberModel(0, 0, 10000, 1));
100         jSpinnerApproaches = new JSpinner(new SpinnerNumberModel(0, 0, 10000, 1));
101         jTextFieldNight = new JFormattedTextField(format2);
102         jTextFieldActualInstrument = new JFormattedTextField(format2);
103         jTextFieldSimulatedInstrument = new JFormattedTextField(format2);
104         jTextFieldSimulator = new JFormattedTextField(format2);
105 
106         jButtonRegistration = new VectorButton();
107         jButtonRegistration.setFont(new Font("Dialog", Font.PLAIN, 10));
108         jButtonRegistration.setBorderPainted(false);
109         jButtonRegistration.setForeground(new Color(182, 120, 58));
110         jButtonRegistration.setText("...");
111         jButtonRegistration.setToolTipText("Select AircraftBO");
112 
113         jButtonToAirport = new VectorButton();
114         jButtonToAirport.setBorderPainted(false);
115         jButtonToAirport.setForeground(new Color(223, 199, 173));
116         jButtonToAirport.setText("...");
117         jButtonToAirport.setToolTipText("Select To Airport");
118 
119         jButtonFromAirport = new VectorButton();
120         jButtonFromAirport.setForeground(new Color(223, 199, 173));
121         jButtonFromAirport.setBorderPainted(false);
122         //green jButtonFromAirport.setForeground(new Color(50,255,0));
123         jButtonFromAirport.setText("...");
124         jButtonFromAirport.setToolTipText("Select From Airport");
125         jDateChooser = new JDateChooser();
126         this.setupPanels();
127         this.addListeners();
128     }
129 
130     private void setupPanels() {
131         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
132         add(this.layoutDate());
133         add(this.layoutAircraft());
134         add(this.layoutRoute());
135         add(this.layoutDuration());
136         add(this.layoutFlightTime());
137         add(this.layoutApproaches());
138         add(this.layoutConditions());
139         add(this.layoutSimulator());
140     }
141 
142     private void addListeners() {
143         jButtonToAirport.addActionListener(this);
144         jButtonFromAirport.addActionListener(this);
145         jButtonRegistration.addActionListener(this);
146     }
147 
148     private JPanel layoutDate() {
149         JPanel jPanelDate = new JPanel();
150         jPanelDate.add(new JLabel(MessageResourceRegister.getInstance().getValue(MessageKey.LABEL_DATE)));
151         jPanelDate.add(jDateChooser);
152         return jPanelDate;
153     }
154 
155     private JPanel layoutAircraft() {
156         JPanel jPanelAircraft = new JPanel();
157         GridBagConstraints gridBagConstraints;
158         jPanelAircraft.setLayout(new GridBagLayout());
159 
160         jPanelAircraft.setBorder(javax.swing.BorderFactory.createTitledBorder(MessageResourceRegister.getInstance().getValue("label.aircraft")));
161         gridBagConstraints = new GridBagConstraints();
162         gridBagConstraints.gridx = 0;
163         gridBagConstraints.gridy = 0;
164         gridBagConstraints.anchor = GridBagConstraints.WEST;
165         jPanelAircraft.add(new JLabel(MessageResourceRegister.getInstance().getValue(MessageKey.LABEL_AIRCRAFT_TYPE)), gridBagConstraints);
166 
167         gridBagConstraints = new GridBagConstraints();
168         gridBagConstraints.gridx = 0;
169         gridBagConstraints.gridy = 1;
170         gridBagConstraints.anchor = GridBagConstraints.WEST;
171         jPanelAircraft.add(new JLabel(MessageResourceRegister.getInstance().getValue(MessageKey.LABEL_REGISTRATION)), gridBagConstraints);
172 
173         gridBagConstraints = new GridBagConstraints();
174         gridBagConstraints.gridx = 1;
175         gridBagConstraints.gridy = 0;
176         jPanelAircraft.add(jLACType, gridBagConstraints);
177 
178         gridBagConstraints = new GridBagConstraints();
179         gridBagConstraints.gridx = 1;
180         gridBagConstraints.gridy = 1;
181         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
182         gridBagConstraints.insets = new Insets(0,5,0,5);
183         jPanelAircraft.add(jTextFieldRegistration, gridBagConstraints);
184 
185         gridBagConstraints = new GridBagConstraints();
186         gridBagConstraints.gridx = 2;
187         gridBagConstraints.gridy = 1;
188         jPanelAircraft.add(jButtonRegistration, gridBagConstraints);
189         return jPanelAircraft;
190     }
191 
192     private JPanel layoutRoute() {
193         JPanel jPanelRoute = new JPanel();
194         GridBagConstraints gridBagConstraints;
195         jPanelRoute.setLayout(new GridBagLayout());
196 
197         jPanelRoute.setBorder(javax.swing.BorderFactory.createTitledBorder("Route of Flight"));
198 
199         jPanelRoute.add(new JLabel("From"), new GridBagConstraints());
200 
201         gridBagConstraints = new GridBagConstraints();
202         gridBagConstraints.insets = new Insets(0,5,0,5);
203         jPanelRoute.add(jButtonFromAirport, gridBagConstraints);
204 
205         gridBagConstraints = new GridBagConstraints();
206         gridBagConstraints.anchor = GridBagConstraints.WEST;
207         jPanelRoute.add(jLFaaFrom, gridBagConstraints);
208 
209         gridBagConstraints = new GridBagConstraints();
210         gridBagConstraints.gridx = 0;
211         gridBagConstraints.gridy = 1;
212         gridBagConstraints.insets = new Insets(0,0,0,5);
213         gridBagConstraints.anchor = GridBagConstraints.NORTHEAST;
214         jPanelRoute.add(new JLabel("Via"), gridBagConstraints);
215 
216         JScrollPane jScrollPaneTextArea = new JScrollPane();
217         jTextAreaVia.setColumns(20);
218         jTextAreaVia.setRows(5);
219         jScrollPaneTextArea.setViewportView(jTextAreaVia);
220 
221         gridBagConstraints = new GridBagConstraints();
222         gridBagConstraints.gridx = 1;
223         gridBagConstraints.gridy = 1;
224         gridBagConstraints.gridwidth = 2;
225         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
226         gridBagConstraints.ipady = 52;
227         jPanelRoute.add(jScrollPaneTextArea, gridBagConstraints);
228 
229         gridBagConstraints = new GridBagConstraints();
230         gridBagConstraints.gridx = 0;
231         gridBagConstraints.gridy = 2;
232         gridBagConstraints.anchor = GridBagConstraints.EAST;
233         jPanelRoute.add(new JLabel("To"), gridBagConstraints);
234 
235         gridBagConstraints = new GridBagConstraints();
236         gridBagConstraints.gridx = 1;
237         gridBagConstraints.gridy = 2;
238         gridBagConstraints.insets = new Insets(0,5,0,5);
239         jPanelRoute.add(jButtonToAirport, gridBagConstraints);
240 
241         gridBagConstraints = new GridBagConstraints();
242         gridBagConstraints.gridx = 2;
243         gridBagConstraints.gridy = 2;
244         gridBagConstraints.anchor = GridBagConstraints.WEST;
245         jPanelRoute.add(jLFaaTo, gridBagConstraints);
246 
247 
248         return jPanelRoute;
249     }
250 
251     private JPanel layoutDuration() {
252         JPanel jPanelDuration = new JPanel();
253         jPanelDuration.add(new JLabel("Duration Of FLight"));
254         jTextFieldFlightTotal.setPreferredSize(new Dimension(40, 19));
255         jPanelDuration.add(jTextFieldFlightTotal);
256         return jPanelDuration;
257     }
258 
259     private JPanel layoutFlightTime() {
260         JPanel jPanelPilotTime = new javax.swing.JPanel();
261         GridBagConstraints gridBagConstraints;
262         jPanelPilotTime.setLayout(new GridBagLayout());
263         ImageIcon arrow = UIHelper.getIcon("org/wcb/resources/gui/enter_arrow.jpg");
264         prefillButton1 = new JButton(arrow);
265         prefillButton1.addActionListener(this);
266         prefillButton2 = new JButton(arrow);
267         prefillButton2.addActionListener(this);
268         prefillButton3 = new JButton(arrow);
269         prefillButton3.addActionListener(this);
270         prefillButton4 = new JButton(arrow);
271         prefillButton4.addActionListener(this);
272         prefillButton5 = new JButton(arrow);
273         prefillButton5.addActionListener(this);
274         prefillButton6 = new JButton(arrow);
275         prefillButton6.addActionListener(this);
276         prefillButton7 = new JButton(arrow);
277         prefillButton7.addActionListener(this);
278 
279         jPanelPilotTime.setBorder(javax.swing.BorderFactory.createTitledBorder("Piloting Time"));
280         gridBagConstraints = new GridBagConstraints();
281         gridBagConstraints.anchor = GridBagConstraints.EAST;
282         jPanelPilotTime.add(new JLabel("PIC"), gridBagConstraints);
283 
284         gridBagConstraints = new GridBagConstraints();
285         gridBagConstraints.ipadx = 30;
286         gridBagConstraints.insets = new Insets(0,5,0,5);
287         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
288         jPanelPilotTime.add(jTextFieldPIC, gridBagConstraints);
289 
290         gridBagConstraints = new GridBagConstraints();
291         gridBagConstraints.anchor = GridBagConstraints.WEST;
292         jPanelPilotTime.add(prefillButton1, gridBagConstraints);
293 
294         gridBagConstraints = new GridBagConstraints();
295         gridBagConstraints.gridx = 0;
296         gridBagConstraints.gridy = 1;
297         gridBagConstraints.anchor = GridBagConstraints.EAST;
298         jPanelPilotTime.add(new JLabel("SIC"), gridBagConstraints);
299 
300         gridBagConstraints = new GridBagConstraints();
301         gridBagConstraints.gridx = 1;
302         gridBagConstraints.gridy = 1;
303         gridBagConstraints.insets = new Insets(0,5,0,5);
304         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
305         jPanelPilotTime.add(jTextFieldSIC, gridBagConstraints);
306 
307         gridBagConstraints = new GridBagConstraints();
308         gridBagConstraints.gridx = 2;
309         gridBagConstraints.gridy = 1;
310         gridBagConstraints.anchor = GridBagConstraints.WEST;
311         jPanelPilotTime.add(prefillButton2, gridBagConstraints);
312 
313         gridBagConstraints = new GridBagConstraints();
314         gridBagConstraints.gridx = 0;
315         gridBagConstraints.gridy = 2;
316         gridBagConstraints.anchor = GridBagConstraints.EAST;
317         jPanelPilotTime.add(new JLabel("Cross Country"), gridBagConstraints);
318 
319         gridBagConstraints = new GridBagConstraints();
320         gridBagConstraints.gridx = 1;
321         gridBagConstraints.gridy = 2;
322         gridBagConstraints.insets = new Insets(0,5,0,5);
323         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
324         jPanelPilotTime.add(jTextFieldCrossCountry, gridBagConstraints);
325 
326         gridBagConstraints = new GridBagConstraints();
327         gridBagConstraints.gridx = 2;
328         gridBagConstraints.gridy = 2;
329         gridBagConstraints.anchor = GridBagConstraints.WEST;
330         jPanelPilotTime.add(prefillButton3, gridBagConstraints);
331 
332         gridBagConstraints = new GridBagConstraints();
333         gridBagConstraints.gridx = 0;
334         gridBagConstraints.gridy = 3;
335         gridBagConstraints.anchor = GridBagConstraints.EAST;
336         jPanelPilotTime.add(new JLabel("as Flight Instructor"), gridBagConstraints);
337 
338         gridBagConstraints = new GridBagConstraints();
339         gridBagConstraints.gridx = 1;
340         gridBagConstraints.gridy = 3;
341         gridBagConstraints.insets = new Insets(0,5,0,5);
342         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
343         jPanelPilotTime.add(jTextFieldFlightInstructing, gridBagConstraints);
344 
345         gridBagConstraints = new GridBagConstraints();
346         gridBagConstraints.gridx = 2;
347         gridBagConstraints.gridy = 3;
348         gridBagConstraints.anchor = GridBagConstraints.WEST;
349         jPanelPilotTime.add(prefillButton4, gridBagConstraints);
350 
351         gridBagConstraints = new GridBagConstraints();
352         gridBagConstraints.gridx = 0;
353         gridBagConstraints.gridy = 4;
354         gridBagConstraints.anchor = GridBagConstraints.EAST;
355         jPanelPilotTime.add(new JLabel("Safety Pilot"), gridBagConstraints);
356 
357         gridBagConstraints = new GridBagConstraints();
358         gridBagConstraints.gridx = 1;
359         gridBagConstraints.gridy = 4;
360         gridBagConstraints.insets = new Insets(0,5,0,5);
361         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
362         jPanelPilotTime.add(jTextFieldSafetyPilot, gridBagConstraints);
363 
364         gridBagConstraints = new GridBagConstraints();
365         gridBagConstraints.gridx = 2;
366         gridBagConstraints.gridy = 4;
367         gridBagConstraints.anchor = GridBagConstraints.WEST;
368         jPanelPilotTime.add(prefillButton5, gridBagConstraints);
369 
370         gridBagConstraints = new GridBagConstraints();
371         gridBagConstraints.gridx = 0;
372         gridBagConstraints.gridy = 5;
373         gridBagConstraints.anchor = GridBagConstraints.EAST;
374         jPanelPilotTime.add(new JLabel("Dual Received"), gridBagConstraints);
375 
376         gridBagConstraints = new GridBagConstraints();
377         gridBagConstraints.gridx = 1;
378         gridBagConstraints.gridy = 5;
379         gridBagConstraints.insets = new Insets(0,5,0,5);
380         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
381         jPanelPilotTime.add(jTextFieldDual, gridBagConstraints);
382 
383         gridBagConstraints = new GridBagConstraints();
384         gridBagConstraints.gridx = 2;
385         gridBagConstraints.gridy = 5;
386         gridBagConstraints.anchor = GridBagConstraints.WEST;
387         jPanelPilotTime.add(prefillButton6, gridBagConstraints);
388 
389         gridBagConstraints = new GridBagConstraints();
390         gridBagConstraints.gridx = 0;
391         gridBagConstraints.gridy = 6;
392         gridBagConstraints.anchor = GridBagConstraints.EAST;
393         jPanelPilotTime.add(new JLabel("Solo"), gridBagConstraints);
394 
395         gridBagConstraints = new GridBagConstraints();
396         gridBagConstraints.gridx = 1;
397         gridBagConstraints.gridy = 6;
398         gridBagConstraints.insets = new Insets(0,5,0,5);
399         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
400         jPanelPilotTime.add(jTextFieldSolo, gridBagConstraints);
401 
402         gridBagConstraints = new GridBagConstraints();
403         gridBagConstraints.gridx = 2;
404         gridBagConstraints.gridy = 6;
405         gridBagConstraints.anchor = GridBagConstraints.WEST;
406         jPanelPilotTime.add(prefillButton7, gridBagConstraints);
407 
408         JPanel jPanelDayTime = new JPanel();
409         jPanelDayTime.setLayout(new GridBagLayout());
410 
411         jPanelDayTime.setBorder(javax.swing.BorderFactory.createTitledBorder("Day"));
412         gridBagConstraints = new GridBagConstraints();
413         gridBagConstraints.ipadx = 2;
414         jPanelDayTime.add(new JLabel("Take-offs"), gridBagConstraints);
415 
416         jSpinnerDayTakeoffs.setPreferredSize(new Dimension(35, 18));
417         gridBagConstraints = new GridBagConstraints();
418         gridBagConstraints.insets = new Insets(0,5,0,0);
419         jPanelDayTime.add(jSpinnerDayTakeoffs, gridBagConstraints);
420 
421         gridBagConstraints = new GridBagConstraints();
422         gridBagConstraints.gridx = 0;
423         gridBagConstraints.gridy = 1;
424         gridBagConstraints.ipadx = 2;
425         jPanelDayTime.add(new JLabel("Landings"), gridBagConstraints);
426 
427         jSpinnerDayLandings.setPreferredSize(new Dimension(35, 18));
428         gridBagConstraints = new GridBagConstraints();
429         gridBagConstraints.gridx = 1;
430         gridBagConstraints.gridy = 1;
431         gridBagConstraints.insets = new Insets(0,5,0,0);
432         jPanelDayTime.add(jSpinnerDayLandings, gridBagConstraints);
433 
434         gridBagConstraints = new GridBagConstraints();
435         gridBagConstraints.gridx = 0;
436         gridBagConstraints.gridy = 7;
437         gridBagConstraints.gridwidth = 3;
438         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
439         jPanelPilotTime.add(jPanelDayTime, gridBagConstraints);
440 
441         JPanel jPanelNightTime = new javax.swing.JPanel();
442         jPanelNightTime.setLayout(new GridBagLayout());
443 
444         jPanelNightTime.setBorder(javax.swing.BorderFactory.createTitledBorder("Night"));
445         gridBagConstraints = new GridBagConstraints();
446         gridBagConstraints.ipadx = 2;
447         gridBagConstraints.anchor = GridBagConstraints.EAST;
448         jPanelNightTime.add(new JLabel("Take-offs"), gridBagConstraints);
449 
450         jSpinnerNightTakeoffs.setPreferredSize(new Dimension(35, 18));
451         gridBagConstraints = new GridBagConstraints();
452         gridBagConstraints.ipadx = 2;
453         gridBagConstraints.insets = new Insets(0,5,0,0);
454         jPanelNightTime.add(jSpinnerNightTakeoffs, gridBagConstraints);
455 
456         gridBagConstraints = new GridBagConstraints();
457         gridBagConstraints.gridx = 0;
458         gridBagConstraints.gridy = 1;
459         gridBagConstraints.ipadx = 2;
460         gridBagConstraints.anchor = GridBagConstraints.EAST;
461         jPanelNightTime.add(new JLabel("Landings"), gridBagConstraints);
462 
463         jSpinnerNightLandings.setPreferredSize(new Dimension(35, 18));
464         gridBagConstraints = new GridBagConstraints();
465         gridBagConstraints.gridx = 1;
466         gridBagConstraints.gridy = 1;
467         gridBagConstraints.insets = new Insets(0,5,0,0);
468         jPanelNightTime.add(jSpinnerNightLandings, gridBagConstraints);
469 
470         gridBagConstraints = new GridBagConstraints();
471         gridBagConstraints.gridx = 0;
472         gridBagConstraints.gridy = 8;
473         gridBagConstraints.gridwidth = 3;
474         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
475         jPanelPilotTime.add(jPanelNightTime, gridBagConstraints);
476         return jPanelPilotTime;
477     }
478 
479     private JPanel layoutApproaches() {
480         JPanel jPanelApproaches = new JPanel();
481         jPanelApproaches.add(new JLabel("Instrument Approaches"));
482 
483         jSpinnerApproaches.setPreferredSize(new Dimension(35, 18));
484         jPanelApproaches.add(jSpinnerApproaches);
485         return jPanelApproaches;
486     }
487 
488     private JPanel layoutConditions() {
489         JPanel jPanelConditions = new JPanel();
490         jPanelConditions.setLayout(new GridBagLayout());
491 
492         jPanelConditions.setBorder(javax.swing.BorderFactory.createTitledBorder("Conditions of Flight"));
493         GridBagConstraints gridConstraints = new GridBagConstraints();
494 
495         gridConstraints.anchor = GridBagConstraints.EAST;
496         jPanelConditions.add(new JLabel("Night"), gridConstraints);
497 
498         gridConstraints = new GridBagConstraints();
499         gridConstraints.gridx = 1;
500         gridConstraints.gridy = 0;
501         gridConstraints.insets = new Insets(0,5,0,0);
502         gridConstraints.fill = GridBagConstraints.HORIZONTAL;
503         jTextFieldNight.setPreferredSize(new Dimension(35, 19));
504         jPanelConditions.add(jTextFieldNight, gridConstraints);
505 
506         gridConstraints = new GridBagConstraints();
507         gridConstraints.gridx = 0;
508         gridConstraints.gridy = 1;
509         gridConstraints.anchor = GridBagConstraints.EAST;
510         jPanelConditions.add(new JLabel("Actual Instrument"), gridConstraints);
511 
512         gridConstraints = new GridBagConstraints();
513         gridConstraints.gridx = 1;
514         gridConstraints.gridy = 1;
515         gridConstraints.insets = new Insets(0,5,0,0);
516         gridConstraints.fill = GridBagConstraints.HORIZONTAL;
517         jPanelConditions.add(jTextFieldActualInstrument, gridConstraints);
518 
519         gridConstraints = new GridBagConstraints();
520         gridConstraints.gridx = 0;
521         gridConstraints.gridy = 2;
522         gridConstraints.anchor = GridBagConstraints.EAST;
523         jPanelConditions.add(new JLabel("Simulated Instrument "), gridConstraints);
524 
525         gridConstraints = new GridBagConstraints();
526         gridConstraints.gridx = 1;
527         gridConstraints.gridy = 2;
528         gridConstraints.insets = new Insets(0,5,0,0);
529         gridConstraints.fill = GridBagConstraints.HORIZONTAL;
530         jPanelConditions.add(jTextFieldSimulatedInstrument, gridConstraints);
531         return jPanelConditions;
532     }
533 
534     private JPanel layoutSimulator() {
535         JPanel jPanelSimulator = new JPanel();
536         jPanelSimulator.add(new JLabel("Flight Simulator"));
537         jTextFieldSimulator.setPreferredSize(new Dimension(35, 19));
538         jPanelSimulator.add(jTextFieldSimulator);
539         return jPanelSimulator;
540     }
541 
542     /**
543      * This gets the entry date from the dateField object.
544      * @return Returns a date value.
545      */
546     private Date getEntryDate() {
547         Date selectedDate;
548         selectedDate = jDateChooser.getDate();
549         return selectedDate;
550     }
551 
552     private void setEntryDate(Date date) {
553         if (date != null) {
554             jDateChooser.setDate(date);
555         }
556     }
557 
558     /**
559      * Takes a entry object and marshalls data from the form into the entry
560      * object itself.
561      * @param book the Entry object to get data from the form.
562      * @return Same object with the modified data.
563      */
564     private Logbook marshallFromEntry(Logbook book) {
565         book.setEntryDate(this.getEntryDate());
566         book.setRegistration(this.getRegistration());
567         book.setFaaFrom(this.getFromAirport());
568         book.setFaaVia(this.getVia());
569         book.setFaaTo(this.getToAirport());
570         book.setFlightDuration(this.getFlightTotal());
571         book.setPic(this.getPIC());
572         book.setSic(this.getSIC());
573         book.setCrossCountry(this.getCrossCountry());
574         book.setFlightInstructing(this.getFlightInstructing());
575         book.setDualReceived(this.getDual());
576         book.setSafetyPilot(this.getSafetyPilot());
577         book.setSolo(this.getSolo());
578         book.setConditionNight(this.getNight());
579         book.setDayTakeoffs(this.getDayTakeoffs());
580         book.setDayLandings(this.getDayLandings());
581         book.setNightLandings(this.getNightLandings());
582         book.setNightTakeoffs(this.getNightTakeoffs());
583         book.setInstrumentApproaches(this.getApproaches());
584         book.setConditionActualImc(this.getActualInstrument());
585         book.setConditionSimulatedImc(this.getSimulatedInstrument());
586         book.setConditionFlightSim(this.getSimulator());
587         return book;
588     }
589 
590     private void marshallToForm(Logbook oBook) {
591         this.setEntryDate(oBook.getEntryDate());
592         this.setAircraft(delegate.getAircraftForLogEntry(oBook));
593         this.setRegistration(oBook.getRegistration());
594         this.setFromAirport(getAirportById(oBook.getFaaFrom()));
595         this.setVia(oBook.getFaaVia());
596         this.setToAirport(getAirportById(oBook.getFaaTo()));
597         this.setFlightTotal(oBook.getFlightDuration());
598         this.setPIC(oBook.getPic());
599         this.setSIC(oBook.getSic());
600         this.setCrossCountry(oBook.getCrossCountry());
601         this.setFlightInstructing(oBook.getFlightInstructing());
602         this.setDual(oBook.getDualReceived());
603         this.setSafetyPilot(oBook.getSafetyPilot());
604         this.setSolo(oBook.getSolo());
605         this.setNight(oBook.getConditionNight());
606         this.setDayLandings(oBook.getDayLandings());
607         this.setDayTakeoffs(oBook.getDayTakeoffs());
608         this.setNightLandings(oBook.getNightLandings());
609         this.setNightTakeoffs(oBook.getNightTakeoffs());
610         this.setApproaches(oBook.getInstrumentApproaches());
611         this.setActualInstrument(oBook.getConditionActualImc());
612         this.setSimulatedInstrument(oBook.getConditionSimulatedImc());
613         this.setSimulator(oBook.getConditionFlightSim());
614     }
615 
616     /**
617      * Use the delegate to find the airport object and use
618      * it to display the airport information for the
619      * to and from fields
620      * @param faacode
621      * @return AirportBO object.
622      */
623     private AirportBO getAirportById(String faacode) {
624         AirportDelegate delegateSearch = (AirportDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.AIRPORT_DELEGATE);
625         AirportBO returnValue = null;
626         if (! StringUtils.trimToEmpty(faacode).equalsIgnoreCase("")) {
627             returnValue = delegateSearch.getAirportByFaaCode(faacode);
628         }
629         return (returnValue != null) ? returnValue : new AirportBO();
630     }
631 
632     public void reset() {
633         this.jLACType.setText("");
634         this.setRegistration("");
635         this.setVia("");
636         this.setFlightTotal(null);
637         this.setPIC(null);
638         this.setSIC(null);
639         this.setFlightInstructing(null);
640         this.setCrossCountry(null);
641         this.setSafetyPilot(null);
642         this.setDual(null);
643         this.setSolo(null);
644         this.setApproaches(0);
645         this.setDayLandings(0);
646         this.setDayTakeoffs(0);
647         this.setNightTakeoffs(0);
648         this.setNightLandings(0);
649         this.setNight(null);
650         this.setActualInstrument(null);
651         this.setSimulatedInstrument(null);
652         this.setSimulator(null);
653     }
654 
655     public void actionPerformed(ActionEvent evt) {
656         Object src = evt.getSource();
657         if (src.equals(jButtonRegistration))
658         {
659             AircraftTableDisplayDialog dialogEdit = new AircraftTableDisplayDialog(this);
660             dialogEdit.setVisible(true);
661         }
662         if (src.equals(jButtonToAirport))
663         {
664             AirportTableDisplayDialog dialog = new AirportTableDisplayDialog(this);
665             dialog.setToFrom(true);
666             dialog.setVisible(true);
667         }
668         if (src.equals(jButtonFromAirport))
669         {
670             AirportTableDisplayDialog dialog = new AirportTableDisplayDialog(this);
671             dialog.setToFrom(false);
672             dialog.setVisible(true);
673         }
674         if (src.equals(prefillButton1))
675         {
676             this.setPIC(this.getFlightTotal());
677         }
678         if (src.equals(prefillButton2))
679         {
680             this.setSIC(this.getFlightTotal());
681         }
682         if (src.equals(prefillButton3))
683         {
684             if (! this.isValidCrossCountry()) {
685                 JOptionPane.showMessageDialog(this, "This flight entry is less then 50nm!" + " \n Make sure this flight qualifies for cross country time. ", "Warning: Possible Cross Country Violation" , JOptionPane.WARNING_MESSAGE);
686             }
687             this.setCrossCountry(this.getFlightTotal());
688         }
689         if (src.equals(prefillButton4))
690         {
691             this.setFlightInstructing(this.getFlightTotal());
692         }
693         if (src.equals(prefillButton5))
694         {
695             this.setSafetyPilot(this.getFlightTotal());
696         }
697         if (src.equals(prefillButton6))
698         {
699             this.setDual(this.getFlightTotal());
700         }
701         if (src.equals(prefillButton7))
702         {
703             this.setSolo(this.getFlightTotal());
704         }
705     }
706 
707     public boolean isValidCrossCountry() {
708         if (this.toAirport != null &&
709                 this.fromAirport != null &&
710                 StringUtils.trimToNull(this.toAirport.getLatitude()) != null &&
711                 StringUtils.trimToNull(this.fromAirport.getLatitude()) != null) {
712             SphericalDistanceCalculator calculate = new SphericalDistanceCalculator();
713             setStartLatitude(fromAirport.getLatitude(), calculate);
714             setStartLongitude(fromAirport.getLongitude(), calculate);
715             setEndLatitude(toAirport.getLatitude(), calculate);
716             setEndLongitude(toAirport.getLongitude(), calculate);
717             calculate.calculate(); //calculate
718             if (calculate.getCalculatedDistance() >= 49d) {
719                 return true;
720             }
721         } else if(StringUtils.trimToNull(this.toAirport.getLatitude()) == null || StringUtils.trimToNull(this.fromAirport.getLatitude()) == null) {
722             return true; //TODO don't have data so don't force the person to fail.
723         }
724         return false;
725     }
726 
727     /**
728      * allows setting of latitude in degrees, minutes seconds the passed in string should
729      * look like this 34-12-02.9000N then it will parse correctly.
730      * @param latitude the latitude in degrees-minutes-seconds[delineation]
731      */
732     public void setStartLatitude(String latitude, SphericalDistanceCalculator calculate) {
733         if (StringUtils.trimToNull(latitude) != null) {
734             StringTokenizer tokens = new StringTokenizer(latitude, "-");
735             int startLatDeg = Integer.parseInt(tokens.nextToken());
736             double startLatMin = Double.parseDouble(tokens.nextToken());
737             String secondsPartial = tokens.nextToken();
738             String secondsToken = secondsPartial.substring(0,secondsPartial.length()-1);
739             double startLatSec = Double.parseDouble(secondsToken);
740             String ns = secondsPartial.substring(secondsPartial.length()-1);
741             calculate.setLatitudeFrom(startLatDeg, startLatMin, startLatSec, this.getSphericalLocation(ns));
742         }
743     }
744 
745     /**
746      * allows setting of longitude in degrees, minutes seconds the passed in string should
747      * look like this 119-12-26.0000W then it will parse correctly.
748      * @param longitude the longitude in degrees-minutes-seconds[delineation]
749      */
750     public void setStartLongitude(String longitude, SphericalDistanceCalculator calculate) {
751         if (StringUtils.trimToNull(longitude) != null) {
752             StringTokenizer tokens = new StringTokenizer(longitude, "-");
753             int startLonDeg = Integer.parseInt(tokens.nextToken());
754             double startLonMin = Double.parseDouble(tokens.nextToken());
755             String secondsPartial = tokens.nextToken();
756             String secondsToken = secondsPartial.substring(0,secondsPartial.length() - 1);
757             double startLonSec = Double.parseDouble(secondsToken);
758             String ew = secondsPartial.substring(secondsPartial.length() - 1);
759             calculate.setLongitudeFrom(startLonDeg, startLonMin, startLonSec, this.getSphericalLocation(ew));
760         }
761     }
762     /**
763      * Converts the hemispherical values over so that the e6b calculator functions
764      * understand what the user selected.
765      * @param selectValue The value can be N, S, E, W
766      * @return the proper E6b value;
767      */
768     private String getSphericalLocation(String selectValue) {
769         if (selectValue.equalsIgnoreCase("N")) {
770             return SphericalDistanceCalculator.NORTH_HEMISPHERE;
771         }
772         if (selectValue.equalsIgnoreCase("S")) {
773             return SphericalDistanceCalculator.SOUTH_HEMISPHERE;
774         }
775         if (selectValue.equalsIgnoreCase("E")) {
776             return SphericalDistanceCalculator.EAST_HEMISPHERE;
777         }
778         if (selectValue.equalsIgnoreCase("W")) {
779             return SphericalDistanceCalculator.WEST_HEMISPHERE;
780         }
781         return "";
782     }
783 
784 
785     /**
786      * allows setting of latitude in degrees, minutes seconds the passed in string should
787      * look like this 34-12-02.9000N then it will parse correctly.
788      * @param latitude the latitude in degrees-minutes-seconds[delineation]
789      */
790     public void setEndLatitude(String latitude, SphericalDistanceCalculator calculate) {
791         if (StringUtils.trimToNull(latitude) != null) {
792             StringTokenizer tokens = new StringTokenizer(latitude, "-");
793             int startLatDeg = Integer.parseInt(tokens.nextToken());
794             double startLatMin = Double.parseDouble(tokens.nextToken());
795             String secondsPartial = tokens.nextToken();
796             String secondsToken = secondsPartial.substring(0, secondsPartial.length() - 1);
797             double startLatSec = Double.parseDouble(secondsToken);
798             String ns = secondsPartial.substring(secondsPartial.length()-1);
799             calculate.setLatitudeTo(startLatDeg, startLatMin, startLatSec, this.getSphericalLocation(ns));
800         }
801     }
802 
803     /**
804      * allows setting of longitude in degrees, minutes seconds the passed in string should
805      * look like this 119-12-26.0000W then it will parse correctly.
806      * @param longitude the longitude in degrees-minutes-seconds[delineation]
807      */
808     public void setEndLongitude(String longitude, SphericalDistanceCalculator calculate) {
809         if (StringUtils.trimToNull(longitude) != null) {
810             StringTokenizer tokens = new StringTokenizer(longitude, "-");
811             int startLonDeg = Integer.parseInt(tokens.nextToken());
812             double startLonMin = Double.parseDouble(tokens.nextToken());
813             String secondsPartial = tokens.nextToken();
814             String secondsToken = secondsPartial.substring(0,secondsPartial.length() - 1);
815             double startLonSec = Double.parseDouble(secondsToken);
816             String ew = secondsPartial.substring(secondsPartial.length() - 1);
817             calculate.setLongitudeTo(startLonDeg, startLonMin, startLonSec, this.getSphericalLocation(ew));
818         }
819     }
820     /**
821      * If the aircraft is passed into the entry form
822      * then it sets up the registration field and aircraft type.
823      * @param oCraft the aircraft value object
824      */
825     public void setAircraft(AircraftBO oCraft) {
826         oAircraft = oCraft;
827         jTextFieldRegistration.setText(oAircraft.getRegistrationNumber());
828         AircraftTypeBO type = delegate.getAircraftType(oAircraft);
829         jLACType.setText(type.getAbbreviation());
830     }
831 
832     public void setLogEntry(Logbook vEntry) {
833         this.entry = vEntry;
834         this.marshallToForm(this.entry);
835     }
836 
837     public Logbook getLogEntry() {
838         if (this.entry == null)
839         {
840             this.entry = new Logbook();
841         }
842         this.entry = marshallFromEntry(this.entry);
843         return this.entry;
844     }
845 
846     public void setToAirport(AirportBO airportTo) {
847         this.toAirport = airportTo;
848         this.jLFaaTo.setText(StringUtils.trimToEmpty(toAirport.getFaa()) + " (" + StringUtils.trimToEmpty(toAirport.getName()) + ")");
849     }
850 
851     public String getToAirport() {
852         if(this.toAirport == null) {
853             return null;
854         }
855         return this.toAirport.getFaa();
856     }
857 
858     public void setFromAirport(AirportBO from) {
859         this.fromAirport = from;
860         this.jLFaaFrom.setText(StringUtils.trimToEmpty(fromAirport.getFaa()) + " (" + StringUtils.trimToEmpty(fromAirport.getName()) + ")");
861     }
862 
863     public String getFromAirport() {
864         if(this.fromAirport == null) {
865             return null;
866         }
867         return this.fromAirport.getFaa();
868     }
869 
870     public void setVia(String sValue) {
871         this.jTextAreaVia.setText(sValue);
872     }
873 
874     public String getVia() {
875         return this.jTextAreaVia.getText();
876     }
877 
878     public void setRegistration(String sValue) {
879         if(StringUtils.trimToNull(sValue) == null) {
880             this.jLACType.setText("");
881         }
882         this.jTextFieldRegistration.setText(sValue);
883     }
884 
885     public String getRegistration() {
886         return this.jTextFieldRegistration.getText();
887     }
888 
889     public void setFlightTotal(Double dValue) {
890         this.jTextFieldFlightTotal.setValue(dValue);
891     }
892 
893     public Double getFlightTotal() {
894         return makeDoubleValue(this.jTextFieldFlightTotal.getValue());
895     }
896 
897     public void setPIC(Double dValue) {
898         this.jTextFieldPIC.setValue(dValue);
899     }
900 
901     public Double getPIC() {
902 
903         return makeDoubleValue(this.jTextFieldPIC.getValue());
904     }
905 
906     public void setSIC(Double dValue) {
907         this.jTextFieldSIC.setValue(dValue);
908     }
909 
910     public Double getSIC() {
911 
912         return makeDoubleValue(this.jTextFieldSIC.getValue());
913     }
914 
915     public void setCrossCountry(Double dValue) {
916         this.jTextFieldCrossCountry.setValue(dValue);
917     }
918 
919     public Double getCrossCountry() {
920         return makeDoubleValue(this.jTextFieldCrossCountry.getValue());
921     }
922 
923     public void setFlightInstructing(Double dValue) {
924         this.jTextFieldFlightInstructing.setValue(dValue);
925     }
926 
927     public Double getFlightInstructing() {
928         return makeDoubleValue(this.jTextFieldFlightInstructing.getValue());
929     }
930 
931     public void setSafetyPilot(Double dValue) {
932         this.jTextFieldSafetyPilot.setValue(dValue);
933     }
934 
935     public Double getSafetyPilot() {
936         return makeDoubleValue(this.jTextFieldSafetyPilot.getValue());
937     }
938 
939     public void setDual(Double dValue) {
940         this.jTextFieldDual.setValue(dValue);
941     }
942 
943     public Double getDual() {
944         return makeDoubleValue(this.jTextFieldDual.getValue());
945     }
946 
947     public void setSolo(Double dValue) {
948         this.jTextFieldSolo.setValue(dValue);
949     }
950 
951     public Double getSolo() {
952         return makeDoubleValue(this.jTextFieldSolo.getValue());
953 
954     }
955 
956     public void setNight(Double dValue) {
957         this.jTextFieldNight.setValue(dValue);
958     }
959 
960     public Double getNight() {
961         return makeDoubleValue(this.jTextFieldNight.getValue());
962     }
963 
964     public void setActualInstrument(Double dValue) {
965         this.jTextFieldActualInstrument.setValue(dValue);
966     }
967 
968     public Double getActualInstrument() {
969         return makeDoubleValue(this.jTextFieldActualInstrument.getValue());
970     }
971 
972     public void setSimulatedInstrument(Double dValue) {
973         this.jTextFieldSimulatedInstrument.setValue(dValue);
974     }
975 
976     public Double getSimulatedInstrument() {
977         return makeDoubleValue(this.jTextFieldSimulatedInstrument.getValue());
978     }
979 
980     public void setSimulator(Double dValue) {
981         this.jTextFieldSimulator.setValue(dValue);
982     }
983 
984     public Double getSimulator() {
985         return makeDoubleValue(this.jTextFieldSimulator.getValue());
986     }
987 
988     public void setApproaches(Integer iValue) {
989         if (iValue != null)
990         {
991             jSpinnerApproaches.setValue(iValue);
992         }
993     }
994 
995     public Integer getApproaches() {
996         return (Integer) jSpinnerApproaches.getModel().getValue();
997     }
998 
999     public void setDayLandings(Integer iValue) {
1000         if (iValue != null)
1001         {
1002             jSpinnerDayLandings.setValue(iValue);
1003         }
1004     }
1005 
1006     public Integer getDayLandings() {
1007         return (Integer) jSpinnerDayLandings.getModel().getValue();
1008     }
1009 
1010     public void setDayTakeoffs(Integer iValue) {
1011         if (iValue != null)
1012         {
1013             jSpinnerDayTakeoffs.setValue(iValue);
1014         }
1015     }
1016 
1017     public Integer getDayTakeoffs() {
1018         return (Integer) jSpinnerDayTakeoffs.getModel().getValue();
1019     }
1020 
1021     public void setNightTakeoffs(Integer iValue) {
1022         if (iValue != null)
1023         {
1024             jSpinnerNightTakeoffs.setValue(iValue);
1025         }
1026     }
1027 
1028     public Integer getNightTakeoffs() {
1029         return (Integer) jSpinnerNightTakeoffs.getModel().getValue();
1030     }
1031 
1032     public void setNightLandings(Integer iValue) {
1033         if (iValue != null)
1034         {
1035             jSpinnerNightLandings.setValue(iValue);
1036         }
1037     }
1038 
1039     public Integer getNightLandings() {
1040         return (Integer) jSpinnerNightLandings.getModel().getValue();
1041     }
1042 
1043     /**
1044      * This takes the value of an object in this case from the various textfields
1045      * which have a number formatter and should get a double value out of them.
1046      * @param value The object that can be a long or double or null
1047      * @return  proper double value
1048      */
1049     private Double makeDoubleValue(Object value) {
1050         if (value != null)
1051         {
1052             if (value instanceof Long)
1053             {
1054                 return new Double(((Long) value).doubleValue());
1055             }
1056             return (Double) value;
1057         }
1058         return new Double(0);
1059     }
1060 
1061     private JButton jButtonFromAirport;
1062     private JButton jButtonToAirport;
1063     private JButton jButtonRegistration;
1064     private JButton prefillButton1;
1065     private JButton prefillButton2;
1066     private JButton prefillButton3;
1067     private JButton prefillButton4;
1068     private JButton prefillButton5;
1069     private JButton prefillButton6;
1070     private JButton prefillButton7;
1071     private JDateChooser jDateChooser;
1072     private JTextField jTextFieldRegistration;
1073     private JFormattedTextField jTextFieldFlightTotal;
1074     private JFormattedTextField jTextFieldPIC;
1075     private JFormattedTextField jTextFieldSIC;
1076     private JFormattedTextField jTextFieldCrossCountry;
1077     private JFormattedTextField jTextFieldFlightInstructing;
1078     private JFormattedTextField jTextFieldDual;
1079     private JFormattedTextField jTextFieldSolo;
1080     private JFormattedTextField jTextFieldNight;
1081     private JFormattedTextField jTextFieldActualInstrument;
1082     private JFormattedTextField jTextFieldSimulatedInstrument;
1083 
1084     private JSpinner jSpinnerApproaches;
1085     private JSpinner jSpinnerDayLandings;
1086     private JSpinner jSpinnerDayTakeoffs;
1087     private JSpinner jSpinnerNightLandings;
1088     private JSpinner jSpinnerNightTakeoffs;
1089     private JTextArea jTextAreaVia;
1090     private JFormattedTextField jTextFieldSafetyPilot;
1091     private JFormattedTextField jTextFieldSimulator;
1092     private JLabel jLFaaFrom;
1093     private JLabel jLFaaTo;
1094     private JLabel jLACType;
1095 }