1   package org.wcb.gui.forms.wizard;
2   
3   import org.wcb.gui.util.UIHelper;
4   import org.wcb.gui.component.JCloudPane;
5   import org.wcb.model.service.IImportService;
6   import org.wcb.model.service.IServicesConstants;
7   import org.wcb.model.util.SpringUtil;
8   import org.wcb.exception.ServiceException;
9   
10  import javax.swing.*;
11  import java.awt.event.ActionListener;
12  import java.awt.event.ActionEvent;
13  import java.awt.*;
14  import java.io.File;
15  
16  /**
17   * <small>
18   * <p/>
19   * Copyright (C)  2006  wbogaardt.
20   * This library is free software; you can redistribute it and/or
21   * modify it under the terms of the GNU Lesser General Public
22   * License as published by the Free Software Foundation; either
23   * version 2.1 of the License, or (at your option) any later version.
24   * <p/>
25   * This library is distributed in the hope that it will be useful,
26   * but WITHOUT ANY WARRANTY; without even the implied warranty of
27   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28   * Lesser General Public License for more details.
29   * <p/>
30   * You should have received a copy of the GNU Lesser General Public
31   * License along with this library; if not, write to the Free Software
32   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
33   * <p/>
34   * $File:  $ <br>
35   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Oct 26, 2006 2:57:54 PM $ <br>
36   * </small>
37   *
38   * @author wbogaardt
39   *    This is the main control for the wizard that asks the user questions
40   *   so it can properly fill out the FAA for 8710.
41   */
42  
43  public class ImportAirportWizard extends JPanel implements ActionListener {
44  
45      private JPanel centerPanel;
46      private Step1ofImportWizard step1Panel;
47      private CompletionofWizard completionPanel;
48      private JButton backButton;
49      private JButton nextButton;
50      private int stepCounter;
51      private int MAX_STEPS=1;
52  
53      public ImportAirportWizard() {
54          initComponents();
55          stepCounter = 1;
56      }
57  
58      private void initComponents() {
59          ImageIcon titleIcon = UIHelper.getIcon("org/wcb/resources/gui/importpref_wiz.gif");
60          JLabel wizardLabel = new JLabel(titleIcon);
61          JCloudPane wizard = new JCloudPane();
62          wizard.add(wizardLabel);
63  
64          backButton = new JButton("Back");
65          nextButton = new JButton("Next");
66          backButton.addActionListener(this);
67          nextButton.addActionListener(this);
68  
69          step1Panel = new Step1ofImportWizard();
70  
71          completionPanel = new CompletionofWizard();
72          completionPanel.setTextToLabel("You have successfully imported data into your Log book!");
73          centerPanel = new JPanel(new CardLayout());
74          centerPanel.add(step1Panel, "step1");
75          centerPanel.add(completionPanel,"completion");
76  
77          JPanel buttonPanel = new JPanel();
78          buttonPanel.add(backButton);
79          buttonPanel.add(nextButton);
80  
81          setLayout(new BorderLayout());
82          add(wizardLabel, BorderLayout.WEST);
83          add(centerPanel, BorderLayout.CENTER);
84          add(buttonPanel, BorderLayout.SOUTH);
85  
86      }
87  
88      public void actionPerformed(ActionEvent evt) {
89          Object src = evt.getSource();
90          if (src.equals(nextButton)) {
91              if(nextButton.getText().equals("Finish")) {
92                  doImport();
93                  showNextPanel("completion");
94                  nextButton.setEnabled(false);
95                  backButton.setEnabled(false);
96              } else {
97                  stepCounter = stepCounter + 1;
98                  if(stepCounter >= MAX_STEPS) {
99                      stepCounter = MAX_STEPS;
100                     nextButton.setText("Finish");
101                 }
102                 showNextPanel("step" + stepCounter);
103             }
104         }
105         if (src.equals(backButton)) {
106             if(nextButton.getText().equals("Finish")) {
107                 nextButton.setText("Next");
108             }
109             stepCounter = stepCounter - 1;
110             if (stepCounter < 1) {
111                 stepCounter = 1;
112             }
113             showNextPanel("step" + stepCounter);
114         }
115     }
116 
117     /**
118      * Common functionality to show next panel.
119      * @param panelName
120      */
121     private void showNextPanel(String panelName) {
122          CardLayout cl = (CardLayout) centerPanel.getLayout();
123          cl.show(centerPanel, panelName);
124     }
125 
126     private void doImport() {
127           IImportService service = (IImportService) SpringUtil.getApplicationContext().getBean(IServicesConstants.LOGBOOK_IMPORT_SERVICE);
128             try
129             {
130                 service.processAirportFile(new File(step1Panel.getFilePathName()));
131             }
132             catch (ServiceException se)
133             {
134                 JOptionPane.showMessageDialog(this, "Unable to import the entire file " + step1Panel.getFilePathName() + " \n The reason is: " + se.getMessage(), "Failure to Import" , JOptionPane.ERROR_MESSAGE);
135             }
136     }
137 
138 
139 
140 }