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
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
123
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 }