1   /*
2    * Created on Aug 12, 2007
3    * 
4    * Copyright 2005 CafeSip.org 
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License"); 
7    * you may not use this file except in compliance with the License. 
8    * You may obtain a copy of the License at 
9    *
10   *	http://www.apache.org/licenses/LICENSE-2.0 
11   *
12   * Unless required by applicable law or agreed to in writing, software 
13   * distributed under the License is distributed on an "AS IS" BASIS, 
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
15   * See the License for the specific language governing permissions and 
16   * limitations under the License.
17   *
18   */
19  package org.cafesip.gwtcomp.examples.client;
20  
21  import java.util.HashMap;
22  
23  import org.cafesip.gwtcomp.client.ui.MessageBar;
24  import org.cafesip.gwtcomp.client.ui.WizardPanel;
25  import org.cafesip.gwtcomp.client.ui.WizardPanelListener;
26  import org.cafesip.gwtcomp.client.utils.ContextUtil;
27  import org.cafesip.gwtcomp.client.utils.FileUploadUtil;
28  import org.cafesip.gwtcomp.client.utils.HTMLHelper;
29  
30  import com.google.gwt.event.dom.client.ClickEvent;
31  import com.google.gwt.event.dom.client.ClickHandler;
32  import com.google.gwt.user.client.ui.FlowPanel;
33  import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
34  import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
35  
36  /**
37   * @author Amit Chatterjee
38   * 
39   */
40  public class WizardPanelSample extends FlowPanel
41  {
42      private static final String PANEL_UPLOAD = "upload";
43  
44      private static final String PANEL_INFO = "info";
45  
46      private static final String PANEL_SUMMARY = "summary";
47  
48      protected static final String ACTION_FILE_UPLOAD = ContextUtil
49              .getContextURL()
50              + "/gwtcomp/fileUploadServlet";
51  
52      private WizardPanel wizardPanel;
53  
54      private PersonalInfoPanel piPanel;
55  
56      private ImageUploadPanel fuPanel;
57  
58      private SummaryPanel suPanel;
59  
60      public WizardPanelSample()
61      {
62          super();
63          setWidth("100%");
64  
65          init();
66      }
67  
68      protected void init()
69      {
70          wizardPanel = new WizardPanel(WizardPanelSample.ACTION_FILE_UPLOAD);
71          wizardPanel.getPanel().setWidth("100%");
72          add(wizardPanel);
73          wizardPanel.setSize("100%", "100%");
74  
75          wizardPanel.getTitleBar().setHelpUrl("http://www.cafesip.org/");
76          wizardPanel.getTitleBar().setText("Example Wizard");
77  
78          wizardPanel.getMessageBar().setMessage(
79                  "Please provide the following information");
80  
81          piPanel = new PersonalInfoPanel(wizardPanel);
82          wizardPanel.addPanel(PANEL_INFO, piPanel);
83          piPanel.setWidth("100%");
84  
85          fuPanel = new ImageUploadPanel(wizardPanel);
86          wizardPanel.addPanel(PANEL_UPLOAD, fuPanel);
87          fuPanel.setWidth("100%");
88  
89          suPanel = new SummaryPanel(this);
90          wizardPanel.addPanel(PANEL_SUMMARY, suPanel);
91          suPanel.setWidth("100%");
92  
93          wizardPanel.addPanelListener(new WizardPanelListener()
94          {
95              public void onPageSwitch(String name)
96              {
97                  handleSwitch(name);
98              }
99          });
100 
101         // display the panel
102         wizardPanel.displayPanel(PANEL_INFO);
103 
104         wizardPanel.getNextButton().addClickHandler(new ClickHandler()
105         {
106 
107             public void onClick(ClickEvent event)
108             {
109                 wizardPanel.displayNextPanel();
110             }
111         });
112 
113         wizardPanel.getPreviousButton().addClickHandler(new ClickHandler()
114         {
115 
116             public void onClick(ClickEvent event)
117             {
118                 wizardPanel.displayPreviousPanel();
119             }
120         });
121 
122         wizardPanel.getFinishButton().addClickHandler(new ClickHandler()
123         {
124 
125             public void onClick(ClickEvent event)
126             {
127                 submitForm();
128             }
129         });
130 
131         wizardPanel.getCancelButton().addClickHandler(new ClickHandler()
132         {
133 
134             public void onClick(ClickEvent event)
135             {
136                 resetWizard();
137             }
138         });
139 
140         wizardPanel.getFormPanel().addSubmitCompleteHandler(
141                 new SubmitCompleteHandler()
142                 {
143 
144                     public void onSubmitComplete(SubmitCompleteEvent event)
145                     {
146 
147                         if (event.getResults() == null)
148                         {
149                             wizardPanel
150                                     .getMessageBar()
151                                     .setMessage(
152                                             "An error was encountered."
153                                                     + " Please see server logs for details.",
154                                             MessageBar.SEVERITY_ERROR);
155                         }
156                         else
157                         {
158                             FileUploadUtil result = new FileUploadUtil(event
159                                     .getResults());
160                             if (result.getStatus() == 200)
161                             {
162                                 wizardPanel
163                                         .getMessageBar()
164                                         .setMessage(
165                                                 "Your request has been submitted to the server.");
166                                 resetWizard();
167                             }
168                             else
169                             {
170                                 wizardPanel.getMessageBar().setMessage(
171                                         "An error was encountered: "
172                                                 + result.getReason() + ".",
173                                         MessageBar.SEVERITY_ERROR);
174                             }
175                         }
176 
177                     }
178                 });
179 
180         wizardPanel.getFinishButton().setEnabled(false);
181     }
182 
183     private void submitForm()
184     {
185         wizardPanel.getFormPanel().submit();
186     }
187 
188     private void handleSwitch(String name)
189     {
190         if (name.equals(PANEL_SUMMARY))
191         {
192             suPanel.setProperties();
193             wizardPanel.getFinishButton().setEnabled(true);
194         }
195         else
196         {
197             wizardPanel.getFinishButton().setEnabled(false);
198         }
199     }
200 
201     /**
202      * @return Returns the wizardPanel.
203      */
204     public WizardPanel getWizardPanel()
205     {
206         return wizardPanel;
207     }
208 
209     /**
210      * @param wizardPanel
211      *            The wizardPanel to set.
212      */
213     public void setWizardPanel(WizardPanel wizardPanel)
214     {
215         this.wizardPanel = wizardPanel;
216     }
217 
218     protected HashMap<String, String> getEnteredFields()
219     {
220         HashMap<String, String> map = new HashMap<String, String>();
221 
222         map.put("Name", formatText(piPanel.getName().getText()));
223         map.put("Address", formatText(piPanel.getAddress().getText()));
224         map.put("City", formatText(piPanel.getCities().getItemText(
225                 piPanel.getCities().getSelectedIndex())));
226         map.put("County", formatText(piPanel.getCounties().getItemText(
227                 piPanel.getCounties().getSelectedIndex())));
228 
229         map.put("Caption", formatText(fuPanel.getCaption().getText()));
230         map.put("Description", formatText(fuPanel.getDescription().getText()));
231         map.put("File Name", formatText(fuPanel.getUpload().getFilename()));
232         return map;
233     }
234 
235     private String formatText(String text)
236     {
237         return (text == null) || (text.trim().length() == 0) ? HTMLHelper
238                 .italics("Not entered") : text;
239     }
240 
241     private void resetWizard()
242     {
243         piPanel.getName().setText("");
244         piPanel.getEmail().setText("");
245         piPanel.getAddress().setText("");
246         piPanel.getCities().setSelectedIndex(0);
247         piPanel.getCounties().setSelectedIndex(0);
248 
249         fuPanel.getCaption().setText("");
250         fuPanel.getDescription().setText("");
251         fuPanel.getUpload().setName("");
252 
253         wizardPanel.displayPanel(PANEL_INFO);
254         wizardPanel.getFinishButton().setEnabled(false);
255     }
256 }