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.IServicesConstants;
6   import org.wcb.model.service.IImportService;
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 ImportWizard extends JPanel implements ActionListener {
44  
45      private JPanel centerPanel;
46      private Step1ofImportWizard step1Panel;
47      private Step2ofImportWizard step2Panel;
48      private CompletionofWizard completionPanel;
49      private JButton backButton;
50      private JButton nextButton;
51      private int stepCounter;
52      private int MAX_STEPS=2;
53  
54      public ImportWizard () {
55          initComponents();
56          stepCounter = 1;
57      }
58  
59      private void initComponents() {
60          ImageIcon titleIcon = UIHelper.getIcon("org/wcb/resources/gui/importpref_wiz.gif");
61          JLabel wizardLabel = new JLabel(titleIcon);
62          JCloudPane wizard = new JCloudPane();
63          wizard.add(wizardLabel);
64  
65          backButton = new JButton("Back");
66          nextButton = new JButton("Next");
67          backButton.addActionListener(this);
68          nextButton.addActionListener(this);
69  
70          step1Panel = new Step1ofImportWizard();
71          step2Panel = new Step2ofImportWizard();
72  
73          completionPanel = new CompletionofWizard();
74          completionPanel.setTextToLabel("You have successfully imported data into your Log book!");
75          centerPanel = new JPanel(new CardLayout());
76          centerPanel.add(step1Panel, "step1");
77          centerPanel.add(step2Panel, "step2");
78  
79          centerPanel.add(completionPanel,"completion");
80  
81          JPanel buttonPanel = new JPanel();
82          buttonPanel.add(backButton);
83          buttonPanel.add(nextButton);
84  
85          setLayout(new BorderLayout());
86          add(wizardLabel, BorderLayout.WEST);
87          add(centerPanel, BorderLayout.CENTER);
88          add(buttonPanel, BorderLayout.SOUTH);
89  
90      }
91  
92      public void actionPerformed(ActionEvent evt) {
93          Object src = evt.getSource();
94          if (src.equals(nextButton)) {
95              if(nextButton.getText().equals("Finish")) {
96                  doImport();
97                  showNextPanel("completion");
98                  nextButton.setEnabled(false);
99                  backButton.setEnabled(false);
100             } else {
101                 stepCounter = stepCounter + 1;
102                 if(stepCounter >= MAX_STEPS) {
103                     stepCounter = MAX_STEPS;
104                     nextButton.setText("Finish");
105                 }
106                 showNextPanel("step" + stepCounter);
107             }
108         }
109         if (src.equals(backButton)) {
110             if(nextButton.getText().equals("Finish")) {
111                 nextButton.setText("Next");
112             }
113             stepCounter = stepCounter - 1;
114             if (stepCounter < 1) {
115                 stepCounter = 1;
116             }
117             showNextPanel("step" + stepCounter);
118         }
119     }
120 
121     /**
122      * Common functionality to show next panel.
123      * @param panelName
124      */
125     private void showNextPanel(String panelName) {
126          CardLayout cl = (CardLayout) centerPanel.getLayout();
127          cl.show(centerPanel, panelName);
128     }
129 
130     private void doImport() {
131           IImportService service = (IImportService) SpringUtil.getApplicationContext().getBean(IServicesConstants.LOGBOOK_IMPORT_SERVICE);
132             try
133             {
134                 service.processFile(new File(step1Panel.getFilePathName()), step2Panel.getForm().getValueObject());
135                 JOptionPane.showMessageDialog(this, "Completed Import!" + " \n You may need to restart application to see new entries. ", "Success Importing new Logbook Entries" , JOptionPane.INFORMATION_MESSAGE);
136             }
137             catch (ServiceException se)
138             {
139                 JOptionPane.showMessageDialog(this, "Unable to import the entire file " + step1Panel.getFilePathName() + " \n The reason is: " + se.getMessage(), "Failure to Import" , JOptionPane.ERROR_MESSAGE);
140             }
141     }
142 
143 
144 
145 }