1   package org.wcb.gui.forms.wizard;
2   
3   import org.wcb.gui.component.JXTitlePanel;
4   import org.wcb.gui.renderer.AircraftTypeListCellRender;
5   import org.wcb.model.vo.hibernate.AircraftTypeBO;
6   import org.wcb.model.vo.hibernate.Logbook;
7   import org.wcb.model.vo.hibernate.AircraftBO;
8   import org.wcb.model.bd.AircraftDelegate;
9   import org.wcb.model.bd.LogbookDelegate;
10  import org.wcb.model.util.SpringUtil;
11  import org.wcb.model.service.IServicesConstants;
12  import org.jdesktop.swingx.border.DropShadowBorder;
13  import org.jdesktop.swingx.VerticalLayout;
14  import org.apache.commons.lang.StringUtils;
15  
16  import javax.swing.border.Border;
17  import javax.swing.border.CompoundBorder;
18  import javax.swing.*;
19  import java.awt.event.ItemEvent;
20  import java.awt.event.ItemListener;
21  import java.awt.*;
22  import java.util.Date;
23  import java.text.DecimalFormat;
24  
25  import com.toedter.calendar.JDateChooser;
26  
27  /**
28   * <small>
29   * <p/>
30   * Copyright (C)  2006  wbogaardt.
31   * This library is free software; you can redistribute it and/or
32   * modify it under the terms of the GNU Lesser General Public
33   * License as published by the Free Software Foundation; either
34   * version 2.1 of the License, or (at your option) any later version.
35   * <p/>
36   * This library is distributed in the hope that it will be useful,
37   * but WITHOUT ANY WARRANTY; without even the implied warranty of
38   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
39   * Lesser General Public License for more details.
40   * <p/>
41   * You should have received a copy of the GNU Lesser General Public
42   * License along with this library; if not, write to the Free Software
43   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
44   * <p/>
45   * $File:  $ <br>
46   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Oct 26, 2006 2:43:12 PM $ <br>
47   * </small>
48   *
49   * @author wbogaardt
50   *        Page 5 basis for application.
51   */
52  
53  public class Step5of8710Wizard extends JXTitlePanel implements ItemListener {
54  
55      private JPanel displayPanel;
56      private final DecimalFormat formatter = new DecimalFormat("###,##0.0");
57  
58      public Step5of8710Wizard() {
59          super("Step 5 of 6 - Basis for Application", new Color(0x3779ff));
60          Border shadow = new DropShadowBorder(Color.BLACK, 1, 5, .3f, 15, true, true, true, true);
61          setBorder(new CompoundBorder(shadow, getBorder()));
62          initComponents();
63      }
64  
65      /** This method is called from within the constructor to
66       * initialize the form.
67       * WARNING: Do NOT modify this code. The content of this method is
68       * always regenerated by the Form Editor.
69       */
70      private void initComponents() {
71          completionOfTestRb = new JRadioButton("Completion of Required Test", true);
72          militaryComplianceRb = new JRadioButton("Military Competence Obtained in", false);
73          graduationOfCourseRb = new JRadioButton("Graduate of Approved Course", false);
74          holderForeignLicenseRb = new JRadioButton("Holder of Foreign License Issued By", false);
75          yesRb = new JRadioButton("Yes", false);
76          noRb = new JRadioButton("No", true);
77          completionOfTestRb.addItemListener(this);
78          militaryComplianceRb.addItemListener(this);
79          graduationOfCourseRb.addItemListener(this);
80          holderForeignLicenseRb.addItemListener(this);
81  
82          picTimeTf = new JTextField(5);
83          totalTimeInAircraftTf = new JTextField(5);
84          aircraftToBeUsedCB = new JComboBox(this.getAircraftTypes().toArray());
85          aircraftToBeUsedCB.setRenderer(new AircraftTypeListCellRender());
86          aircraftToBeUsedCB.addItemListener(this);
87  
88          serviceTf = new JTextField(10);
89          dateRated = new JDateChooser();
90          rankTf = new JTextField(10);
91          serviceNumberTf = new JTextField(10);
92          militaryAircraftTf = new JTextField(10);
93          militaryAircraftCheckTf = new JTextField(10);
94  
95  
96          setLayout(new java.awt.BorderLayout());
97          displayPanel = new JPanel(new CardLayout());
98          displayPanel.add(this.getCompletionTestPanel(), "a");
99          displayPanel.add(this.getMilitaryPanel(), "b");
100         displayPanel.add(this.getGraduateCoursePanel(), "c");
101         displayPanel.add(this.getForeignLicensePanel(), "d");
102 
103 
104         add(displayPanel, java.awt.BorderLayout.CENTER);
105         add(getBasisApplicationPanel(), java.awt.BorderLayout.NORTH);
106 
107     }
108 
109     private JPanel getCompletionTestPanel() {
110         JPanel certificatePanel = new JPanel();
111         certificatePanel.setBorder(BorderFactory.createTitledBorder("Completion of required test"));
112 
113         certificatePanel.setLayout(new java.awt.GridBagLayout());
114 
115         GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
116         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
117         certificatePanel.add(new JLabel("1. AircraftBO to be used(if flight test required)"), gridBagConstraints);
118 
119         gridBagConstraints = new java.awt.GridBagConstraints();
120         gridBagConstraints.ipadx = 3;
121         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
122         certificatePanel.add(aircraftToBeUsedCB, gridBagConstraints);
123 
124         gridBagConstraints = new java.awt.GridBagConstraints();
125         gridBagConstraints.gridx = 0;
126         gridBagConstraints.gridy = 1;
127         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
128         certificatePanel.add(new JLabel("2a. Total time in this aircraft/SIM/FTD"), gridBagConstraints);
129 
130         gridBagConstraints = new java.awt.GridBagConstraints();
131         gridBagConstraints.gridx = 1;
132         gridBagConstraints.gridy = 1;
133         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
134         certificatePanel.add(totalTimeInAircraftTf, gridBagConstraints);
135 
136         gridBagConstraints = new java.awt.GridBagConstraints();
137         gridBagConstraints.gridx = 0;
138         gridBagConstraints.gridy = 2;
139         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
140         certificatePanel.add(new JLabel("2b. Pilot In command"), gridBagConstraints);
141 
142         gridBagConstraints = new java.awt.GridBagConstraints();
143         gridBagConstraints.gridx = 1;
144         gridBagConstraints.gridy = 2;
145         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
146         certificatePanel.add(picTimeTf, gridBagConstraints);
147         return certificatePanel;
148     }
149 
150     private JPanel getBasisApplicationPanel() {
151         JPanel physicalInfoPanel = new JPanel();
152         physicalInfoPanel.setBorder(BorderFactory.createTitledBorder("Certificate or Rating Applied For on Basis of:"));
153 
154         physicalInfoPanel.setLayout(new VerticalLayout());
155         ButtonGroup group = new ButtonGroup();
156         group.add(completionOfTestRb);
157         group.add(militaryComplianceRb);
158         group.add(graduationOfCourseRb);
159         group.add(holderForeignLicenseRb);
160         physicalInfoPanel.add(completionOfTestRb);
161         physicalInfoPanel.add(militaryComplianceRb);
162         physicalInfoPanel.add(graduationOfCourseRb);
163         physicalInfoPanel.add(holderForeignLicenseRb);
164 
165         JPanel failedTestPanel = new JPanel(new GridLayout());
166         ButtonGroup yesNoGroup = new ButtonGroup();
167         yesNoGroup.add(yesRb);
168         yesNoGroup.add(noRb);
169         GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
170         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
171         failedTestPanel.add(new JLabel("Have you failed a test for this certificate or rating?"), gridBagConstraints);
172 
173         gridBagConstraints = new java.awt.GridBagConstraints();
174         gridBagConstraints.ipadx = 3;
175         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
176         failedTestPanel.add(yesRb, gridBagConstraints);
177 
178         gridBagConstraints = new java.awt.GridBagConstraints();
179         gridBagConstraints.ipadx = 3;
180         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
181         failedTestPanel.add(noRb, gridBagConstraints);
182 
183         physicalInfoPanel.add(failedTestPanel);
184 
185         return physicalInfoPanel;
186     }
187 
188     /**
189      * TODO: need to have values of the panel be able to be sent to the form
190      * @return The military panel
191      */
192     private JPanel getMilitaryPanel() {
193         JPanel certificatePanel = new JPanel();
194         certificatePanel.setBorder(BorderFactory.createTitledBorder("Military Competence"));
195         certificatePanel.add(new JLabel("Not implemented yet!"));
196         return certificatePanel;
197 
198         /*certificatePanel.setLayout(new java.awt.GridBagLayout());
199 
200         GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
201         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
202         certificatePanel.add(new JLabel("1. Service"), gridBagConstraints);
203 
204         gridBagConstraints = new java.awt.GridBagConstraints();
205         gridBagConstraints.ipadx = 3;
206         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
207         certificatePanel.add(serviceTf, gridBagConstraints);
208 
209         gridBagConstraints = new java.awt.GridBagConstraints();
210         gridBagConstraints.gridx = 0;
211         gridBagConstraints.gridy = 1;
212         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
213         certificatePanel.add(new JLabel("2. Date Rated"), gridBagConstraints);
214 
215         gridBagConstraints = new java.awt.GridBagConstraints();
216         gridBagConstraints.gridx = 1;
217         gridBagConstraints.gridy = 1;
218         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
219         certificatePanel.add(dateRated, gridBagConstraints);
220 
221         gridBagConstraints = new java.awt.GridBagConstraints();
222         gridBagConstraints.gridx = 0;
223         gridBagConstraints.gridy = 2;
224         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
225         certificatePanel.add(new JLabel("Rank or Grade"), gridBagConstraints);
226 
227         gridBagConstraints = new java.awt.GridBagConstraints();
228         gridBagConstraints.gridx = 1;
229         gridBagConstraints.gridy = 2;
230         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
231         certificatePanel.add(rankTf, gridBagConstraints);
232 
233         gridBagConstraints = new java.awt.GridBagConstraints();
234         gridBagConstraints.gridx = 0;
235         gridBagConstraints.gridy = 3;
236         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
237         certificatePanel.add(new JLabel("Service Number"), gridBagConstraints);
238 
239         gridBagConstraints = new java.awt.GridBagConstraints();
240         gridBagConstraints.gridx = 1;
241         gridBagConstraints.gridy = 3;
242         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
243         certificatePanel.add(serviceNumberTf, gridBagConstraints);
244 
245         gridBagConstraints = new java.awt.GridBagConstraints();
246         gridBagConstraints.gridx = 0;
247         gridBagConstraints.gridy = 4;
248         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
249         certificatePanel.add(new JLabel("4a. Flown 10 hours PIC in last 12 months in the following Military AircraftBO."), gridBagConstraints);
250 
251         gridBagConstraints = new java.awt.GridBagConstraints();
252         gridBagConstraints.gridx = 1;
253         gridBagConstraints.gridy = 4;
254         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
255         certificatePanel.add(militaryAircraftTf, gridBagConstraints);
256 
257         gridBagConstraints = new java.awt.GridBagConstraints();
258         gridBagConstraints.gridx = 0;
259         gridBagConstraints.gridy = 5;
260         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
261         certificatePanel.add(new JLabel("4b. US Military PIC & Instrument check in last 12 months"), gridBagConstraints);
262 
263         gridBagConstraints = new java.awt.GridBagConstraints();
264         gridBagConstraints.gridx = 1;
265         gridBagConstraints.gridy = 5;
266         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
267         certificatePanel.add(militaryAircraftCheckTf, gridBagConstraints);
268         return certificatePanel;
269         */
270     }
271 
272     /**
273      * TODO add functionality to getApprovedCourse()
274      * @return the panel for approved courses
275      */
276     private JPanel getGraduateCoursePanel() {
277         JPanel certificatePanel = new JPanel();
278         certificatePanel.setBorder(BorderFactory.createTitledBorder("Graduate of Approved Course"));
279         certificatePanel.add(new JLabel("Not implemented yet!"));
280         return certificatePanel;
281 
282     }
283 
284     /**
285      * TODO add functionality to getForeignLicense()
286      * @return the panel for foreign licenses
287      */
288     private JPanel getForeignLicensePanel() {
289         JPanel certificatePanel = new JPanel();
290         certificatePanel.setBorder(BorderFactory.createTitledBorder("Holder of Foreign License Issued By"));
291         certificatePanel.add(new JLabel("Not implemented yet!"));
292         return certificatePanel;
293 
294     }
295 
296     public void itemStateChanged(ItemEvent evt) {
297         Object src = evt.getSource();
298         if (completionOfTestRb.isSelected()) {
299             showNextPanel("a");
300         }
301         if (militaryComplianceRb.isSelected()) {
302             showNextPanel("b");
303         }
304         if (graduationOfCourseRb.isSelected()) {
305             showNextPanel("c");
306         }
307         if (holderForeignLicenseRb.isSelected()) {
308             showNextPanel("d");
309         }
310         if(src.equals(aircraftToBeUsedCB)) {
311             this.populateTimeFields();
312         }
313     }
314 
315     /**
316      * Prefills the two time fields when the user selects an aircraft
317      * make and model. The user can still override this field value
318      * but it helps to have some prefilling.
319      */
320     private void populateTimeFields() {
321         AircraftTypeBO type = (AircraftTypeBO) aircraftToBeUsedCB.getSelectedItem();
322         String matchAbreviation = trimModelVersion(type.getAbbreviation());
323         LogbookDelegate delegate = (LogbookDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.LOGBOOK_DELEGATE);
324         java.util.List<Logbook> entries = delegate.getAllLogbookEntries();
325         Double totalTime = 0.0;
326         Double picTime = 0.0;
327         if(type.getModel() != null) { // this is a case where the Select One is chosen it would be a null pointer issue
328             for(Logbook item: entries) {
329                 AircraftBO aircraft = delegate.getAircraftForLogEntry(item);
330                 AircraftTypeBO aircraftType = delegate.getAircraftType(aircraft);
331                 String abname = trimModelVersion(aircraftType.getAbbreviation());
332                 //match all aircraft abreviations so a C-172M and a C-172P are considered same aircrafts for type flying.
333                 if (aircraft.getTypeId() == type.getId() || abname.equalsIgnoreCase(matchAbreviation)) {
334                     totalTime = totalTime + item.getFlightDuration();
335                     picTime = picTime + item.getPic();
336                 }
337             }
338             totalTimeInAircraftTf.setText(formatter.format(totalTime));
339             picTimeTf.setText(formatter.format(picTime));
340         }
341     }
342 
343     /**
344      * The aircraft abreviation might be something like this C-150M we want
345      * to trim this down to C-150;
346      * @param aircraftAbrev  The full abreviation
347      * @return Return a trimmed version say like C-150
348      */
349     private String trimModelVersion(String aircraftAbrev) {
350         if(StringUtils.trimToNull(aircraftAbrev) != null) {
351             String val = aircraftAbrev.substring(aircraftAbrev.length());
352             if (StringUtils.isAlpha(val)) {
353                 return aircraftAbrev.substring(0,aircraftAbrev.length()-1);
354             }
355         }
356         return aircraftAbrev;
357     }
358 
359     /**
360      * Common functionality to show next panel.
361      * @param panelName
362      */
363     private void showNextPanel(String panelName) {
364         CardLayout cl = (CardLayout) displayPanel.getLayout();
365         cl.show(displayPanel, panelName);
366     }
367 
368     public String getTotalTimeInAircraft() {
369         return totalTimeInAircraftTf.getText();
370     }
371 
372     /**
373      * The aircraft to be used in Manufacture/Model format for example
374      * Cessna/172. If the selected box happens to be the Select One option
375      * then it returns a empty string.
376      * @return The manufacture/model string.
377      */
378     public String getAircraftToBeUsed() {
379         AircraftTypeBO type = (AircraftTypeBO) aircraftToBeUsedCB.getSelectedItem();
380         if(type.getAbbreviation() == null) {
381             return "";
382         }
383         return type.getManufacturer() + "/" + trimModelVersion(type.getModel());
384     }
385 
386     public String getPICTime() {
387         return picTimeTf.getText();
388     }
389 
390     /**
391      * Question IV. Have you failed a test for this certificate rating?
392      * @return true yes failed, else false
393      */
394     public boolean hasFailedCertificate() {
395         return yesRb.isSelected();
396     }
397 
398     public String getApplyFor() {
399         if (completionOfTestRb.isSelected()) {
400             return "a";
401         }
402         if (militaryComplianceRb.isSelected()) {
403             return "b";
404         }
405         if (graduationOfCourseRb.isSelected()) {
406             return "c";
407         }
408         if (holderForeignLicenseRb.isSelected()) {
409             return "d";
410         }
411         return "a";
412     }
413 
414     public Date getDateRatedMilitary() {
415         return dateRated.getDate();
416     }
417 
418     private java.util.List<AircraftTypeBO> getAircraftTypes() {
419         AircraftDelegate delegate = (AircraftDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.AIRCRAFT_DELEGATE);
420         java.util.List<AircraftTypeBO> types = delegate.getAircraftTypes();
421         AircraftTypeBO newType = new AircraftTypeBO("Select One", null, null, -1);
422         types.add(0, newType);
423         return types;
424     }
425 
426     // Variables declaration - do not modify
427     private JComboBox aircraftToBeUsedCB;
428     private JTextField totalTimeInAircraftTf;
429     private JTextField picTimeTf;
430     private JRadioButton completionOfTestRb;
431     private JRadioButton militaryComplianceRb;
432     private JRadioButton graduationOfCourseRb;
433     private JRadioButton holderForeignLicenseRb;
434 
435     private JRadioButton yesRb;
436     private JRadioButton noRb;
437 
438     private JTextField serviceTf;
439     private JDateChooser dateRated;
440     private JTextField rankTf;
441     private JTextField serviceNumberTf;
442     private JTextField militaryAircraftTf;
443     private JTextField militaryAircraftCheckTf;
444 
445     private JTextField trainingCenterTf;
446     private JTextField certificationNumberTf;
447     private JTextField curriculumTf;
448     private JDateChooser courseDate;
449     // End of variables declaration
450 }