1   package org.wcb.gui.component;
2   
3   import org.wcb.gui.dialog.*;
4   import org.wcb.gui.event.*;
5   import org.wcb.gui.util.UIHelper;
6   import org.wcb.gui.util.PrintUtilities;
7   import org.wcb.gui.LogbookFrame;
8   import org.wcb.resources.MessageKey;
9   import org.wcb.resources.MessageResourceRegister;
10  import org.wcb.resources.HSQLDatabaseInitializer;
11  import org.wcb.model.bd.AircraftDelegate;
12  import org.wcb.model.util.SpringUtil;
13  import org.wcb.model.service.IServicesConstants;
14  import org.wcb.model.vo.hibernate.AircraftBO;
15  
16  
17  import javax.swing.*;
18  import java.awt.event.ActionEvent;
19  import java.awt.event.ActionListener;
20  import java.awt.event.KeyEvent;
21  import java.awt.*;
22  import java.sql.SQLException;
23  import java.util.logging.Logger;
24  import java.util.logging.Level;
25  
26  
27  /**
28   * <small>
29   * Copyright (c)  2006  wbogaardt.
30   * This library is free software; you can redistribute it and/or
31   * modify it under the terms of the GNU Lesser General Public
32   * License as published by the Free Software Foundation; either
33   * version 2.1 of the License, or (at your option) any later version.
34   *
35   * This library is distributed in the hope that it will be useful,
36   * but WITHOUT ANY WARRANTY; without even the implied warranty of
37   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
38   * Lesser General Public License for more details.
39   *
40   * You should have received a copy of the GNU Lesser General Public
41   * License along with this library; if not, write to the Free Software
42   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
43   * <p/>
44   * $File:  $ <br>
45   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Apr 3, 2006 9:14:10 AM $ <br>
46   * </small>
47   *
48   * @author wbogaardt
49   * @version 1
50   *          Date: Apr 3, 2006
51   *          Time: 9:14:10 AM
52   */
53  
54  public class JPilotMenu extends JMenuBar implements ActionListener {
55  
56      private Logger LOG = Logger.getLogger(JMenuBar.class.getName());
57      private JMenu jmFile, jmReport, jmTools, jmHelp;
58      private JMenuItem jmiImport, jmiExport, jmiNew, jmiPrint, jmiClose;
59      private JMenuItem jmiTotalTime, jmiTimeAircraft, jmiIMC, jmiLanding, jmi8710, jmiRecency, jmiAircrafts;
60      private JMenuItem jmiE6b, jmi8710wizard, jmiConfigure, jmiUpdateLogin, jmiImportAirport;
61      private JMenuItem jmiAbout, jmiHelp;
62      private LogbookFrame frame;
63  
64      public JPilotMenu() {
65          this(null);
66      }
67  
68      public JPilotMenu(LogbookFrame frame) {
69          this.frame = frame;
70          this.initComponents();
71          this.setupListeners();
72      }
73  
74      private void initComponents() {
75          jmFile = new JMenu(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_FILE));
76          jmReport = new JMenu(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_REPORT));
77          jmTools = new JMenu(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_TOOLS));
78          jmHelp = new JMenu(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_HELP));
79          jmiImport = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_IMPORT), UIHelper.getIcon("org/wcb/resources/gui/import_obj.gif"));
80          jmiExport = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_EXPORT), UIHelper.getIcon("org/wcb/resources/gui/exportapp_wiz.gif"));
81          jmiNew = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_NEW), UIHelper.getIcon("org/wcb/resources/gui/page_new.gif"));
82          jmiPrint = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_PRINT), UIHelper.getIcon("org/wcb/resources/gui/action_print.gif"));
83          jmiClose = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_CLOSE));
84          jmiE6b = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_FLIGHT_COMPUTER), UIHelper.getIcon("org/wcb/resources/gui/e6b.gif"));
85          jmiTotalTime = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_TOTAL), UIHelper.getIcon("org/wcb/resources/gui/statistics.png"));
86          jmiTimeAircraft = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_TIME_IN_CRAFT), UIHelper.getIcon("org/wcb/resources/gui/icon16.png"));
87          jmiIMC = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_IMC), UIHelper.getIcon("org/wcb/resources/gui/metar.png"));
88          jmiLanding = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_TAKEOFF_LANDING), UIHelper.getIcon("org/wcb/resources/gui/aircraftchart16.png"));
89          jmi8710 = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_8710), UIHelper.getIcon("org/wcb/resources/gui/report.gif"));
90          jmiRecency = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_ANALYZE), UIHelper.getIcon("org/wcb/resources/gui/lens.png"));
91          jmiAircrafts = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_AIRCRAFT_GROUP_REPORT), UIHelper.getIcon("org/wcb/resources/gui/statistics.png"));
92          jmiAbout = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_ABOUT));
93          jmiHelp = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_HELP));
94          jmiHelp.setEnabled(false);
95          jmiConfigure = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_CONFIGURE));
96          jmi8710wizard = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_8710_WIZARD));
97          jmiUpdateLogin = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_UPDATE_PASSWORD));
98          jmiImportAirport = new JMenuItem(MessageResourceRegister.getInstance().getValue(MessageKey.MENU_IMPORT_AIRPORT), UIHelper.getIcon("org/wcb/resources/gui/import_obj.gif"));
99          jmFile.add(jmiNew);
100         jmFile.add(jmiImport);
101         jmFile.add(jmiExport);
102         jmFile.add(jmiClose);
103         jmReport.add(jmiTotalTime);
104         jmReport.add(jmiTimeAircraft);
105         jmReport.add(jmiIMC);
106         jmReport.add(jmiLanding);
107         jmReport.add(jmi8710);
108         jmReport.add(jmiRecency);
109         jmReport.add(jmiAircrafts);
110         jmTools.add(jmiE6b);
111         jmTools.add(jmiConfigure);
112         jmTools.add(jmi8710wizard);
113         jmTools.add(jmiUpdateLogin);
114         jmTools.add(jmiImportAirport);
115         jmHelp.add(jmiAbout);
116         jmHelp.add(jmiHelp);
117         add(jmFile);
118         add(jmReport);
119         add(jmTools);
120         add(jmHelp);
121     }
122 
123     private void setupListeners() {
124         jmiImport.addActionListener(this);
125         jmiImport.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, Event.CTRL_MASK));
126         jmiExport.addActionListener(this);
127         jmiExport.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, Event.CTRL_MASK));
128         jmiNew.addActionListener(this);
129         jmiNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK));
130         jmiPrint.addActionListener(this);
131         jmiPrint.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK));
132         jmiClose.addActionListener(new ApplicationCloseListener(frame));
133         jmiClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK));
134         jmiTotalTime.addActionListener(new ReportDialogActionListener(frame, new ReportDisplayDialog(0)));
135         jmiTimeAircraft.addActionListener(this);
136         jmiIMC.addActionListener(new ReportDialogActionListener(frame, new ReportDisplayDialog(2)));
137         jmiLanding.addActionListener(new ReportDialogActionListener(frame, new ReportDisplayDialog(3)));
138         jmiAbout.addActionListener(new AboutListener(frame));
139         jmi8710.addActionListener(new Faa8710DialogListener(frame));
140         jmiE6b.addActionListener(this);
141         jmiRecency.addActionListener(new FlightExperienceListener(frame));
142         jmiAircrafts.addActionListener(new ReportDialogActionListener(frame, new ReportDisplayDialog(4)));
143         jmiConfigure.addActionListener(this);
144         jmiUpdateLogin.addActionListener(this);
145         jmiUpdateLogin.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, Event.CTRL_MASK));
146         jmi8710wizard.addActionListener(this);
147         jmiImportAirport.addActionListener(this);
148     }
149 
150     public void actionPerformed(ActionEvent evt) {
151         Object src = evt.getSource();
152         if (src.equals(jmiClose)) {
153             System.exit(0);
154         }
155         if (src.equals(jmiNew)) {
156             doNewLogbook();
157         }
158         if (src.equals(jmiImport)) {
159             doImport();
160         }
161         if (src.equals(jmiExport)) {
162             doExport();
163         }
164         if (src.equals(jmiTimeAircraft)) {
165             showAircraftReport();
166         }
167         if (src.equals(jmiConfigure)) {
168             ConfigureDialog configure = new ConfigureDialog();
169             configure.setVisible(true);
170         }
171         if (src.equals(jmiE6b)) {
172             E6BCalculatorDialog dialog = new E6BCalculatorDialog();
173             dialog.setVisible(true);
174         }
175         if (src.equals(jmiUpdateLogin)) {
176             JLoginUpdateUserDialog dialogUpdate = new JLoginUpdateUserDialog(frame, "Update Password", true);
177             dialogUpdate.setVisible(true);
178         }
179         if (src.equals(jmiPrint)) {
180             PrintUtilities.printTable();
181         }
182         if (src.equals(jmi8710wizard)) {
183             this.save8710Pdf();
184         }
185         if (src.equals(jmiImportAirport)) {
186             this.doImportAirportData();
187         }
188     }
189 
190     /**
191      * User is deleting the existing log book.
192      */
193     private void doNewLogbook() {
194         int iAnswered = JOptionPane.showConfirmDialog(this,
195                 "You will delete the existing log entries.<BR> Are you sure you want to do this?", "information",
196                 JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
197         if (iAnswered == JOptionPane.YES_OPTION) {
198             HSQLDatabaseInitializer database = new HSQLDatabaseInitializer();
199             try {
200                 database.clearDatabase();
201             }
202             catch (SQLException sqle) {
203                 LOG.log(Level.WARNING, "Error executing new log book ", sqle);
204             }
205         }
206     }
207 
208     /**
209      * Imports a comma delimited file when the user selects the .csv file to import.
210      * If there is a failure in reading the file because of formatting it will notify the user
211      * of the problem.
212      */
213     private void doImport() {
214         CSVDataImportWizardDialog importer = new CSVDataImportWizardDialog();
215         importer.setVisible(true);
216     }
217 
218     /**
219      * This starts the import of airport data into our system.
220      */
221     private void doImportAirportData() {
222         AirportCSVImportWizardDialog importer = new AirportCSVImportWizardDialog();
223         importer.setVisible(true);
224     }
225     /**
226      * Calls the file export dialog box and when the user creats a file to save then the file
227      * gets passed to the service to write a comma delimited file out with a .csv extension.
228      */
229     private void doExport() {
230         CSVDataExportWizardDialog exporter = new CSVDataExportWizardDialog();
231         exporter.setVisible(true);
232     }
233 
234     private void save8710Pdf() {
235         PdfWizardDialog dialog = new PdfWizardDialog();
236         dialog.setVisible(true);
237     }
238 
239     /**
240      * Shows the hours break down for a selected aircraft.
241      */
242     private void showAircraftReport() {
243         AircraftDelegate delegate = (AircraftDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.AIRCRAFT_DELEGATE);
244         java.util.List items =  delegate.getAllAircraft();
245         AircraftBO sRegistration  = (AircraftBO) JOptionPane.showInputDialog(frame, "Select aircraft by Registration Number", "Display By AircraftBO", JOptionPane.QUESTION_MESSAGE, null, items.toArray(), "");
246         if (sRegistration != null) {
247             ReportDisplayDialog dialog = new ReportDisplayDialog(1, sRegistration.getRegistrationNumber());
248             dialog.addActionListener(new DialogCloseActionListener(dialog));
249             dialog.setVisible(true);
250         }
251     }
252 }