WizardPanelSample.java
001 /*
002  * Created on Aug 12, 2007
003  
004  * Copyright 2005 CafeSip.org 
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License"); 
007  * you may not use this file except in compliance with the License. 
008  * You may obtain a copy of the License at 
009  *
010  *  http://www.apache.org/licenses/LICENSE-2.0 
011  *
012  * Unless required by applicable law or agreed to in writing, software 
013  * distributed under the License is distributed on an "AS IS" BASIS, 
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
015  * See the License for the specific language governing permissions and 
016  * limitations under the License.
017  *
018  */
019 package org.cafesip.gwtcomp.examples.client;
020 
021 import java.util.HashMap;
022 
023 import org.cafesip.gwtcomp.client.ui.MessageBar;
024 import org.cafesip.gwtcomp.client.ui.WizardPanel;
025 import org.cafesip.gwtcomp.client.ui.WizardPanelListener;
026 import org.cafesip.gwtcomp.client.utils.ContextUtil;
027 import org.cafesip.gwtcomp.client.utils.FileUploadUtil;
028 import org.cafesip.gwtcomp.client.utils.HTMLHelper;
029 
030 import com.google.gwt.user.client.ui.ClickListener;
031 import com.google.gwt.user.client.ui.FlowPanel;
032 import com.google.gwt.user.client.ui.FormHandler;
033 import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
034 import com.google.gwt.user.client.ui.FormSubmitEvent;
035 import com.google.gwt.user.client.ui.Widget;
036 
037 /**
038  @author Amit Chatterjee
039  
040  */
041 public class WizardPanelSample extends FlowPanel
042 {
043     private static final String PANEL_UPLOAD = "upload";
044 
045     private static final String PANEL_INFO = "info";
046 
047     private static final String PANEL_SUMMARY = "summary";
048 
049     protected static final String ACTION_FILE_UPLOAD = ContextUtil
050             .getContextURL()
051             "/gwtcomp/fileUploadServlet";
052 
053     private WizardPanel wizardPanel;
054 
055     private PersonalInfoPanel piPanel;
056 
057     private ImageUploadPanel fuPanel;
058 
059     private SummaryPanel suPanel;
060 
061     public WizardPanelSample()
062     {
063         super();
064         setWidth("100%");
065 
066         init();
067     }
068 
069     protected void init()
070     {
071         wizardPanel = new WizardPanel(WizardPanelSample.ACTION_FILE_UPLOAD);
072         wizardPanel.getPanel().setWidth("100%");
073         add(wizardPanel);
074         wizardPanel.setSize("100%""100%");
075 
076         wizardPanel.getTitleBar().setHelpUrl("http://www.cafesip.org/");
077         wizardPanel.getTitleBar().setText("Example Wizard");
078 
079         wizardPanel.getMessageBar().setMessage(
080                 "Please provide the following information");
081 
082         piPanel = new PersonalInfoPanel(wizardPanel);
083         wizardPanel.addPanel(PANEL_INFO, piPanel);
084         piPanel.setWidth("100%");
085 
086         fuPanel = new ImageUploadPanel(wizardPanel);
087         wizardPanel.addPanel(PANEL_UPLOAD, fuPanel);
088         fuPanel.setWidth("100%");
089 
090         suPanel = new SummaryPanel(this);
091         wizardPanel.addPanel(PANEL_SUMMARY, suPanel);
092         suPanel.setWidth("100%");
093 
094         wizardPanel.addPanelListener(new WizardPanelListener()
095         {
096             public void onPageSwitch(String name)
097             {
098                 handleSwitch(name);
099             }
100         });
101 
102         // display the panel
103         wizardPanel.displayPanel(PANEL_INFO);
104 
105         wizardPanel.getNextButton().addClickListener(new ClickListener()
106         {
107             public void onClick(Widget sender)
108             {
109                 wizardPanel.displayNextPanel();
110 
111             }
112         });
113 
114         wizardPanel.getPreviousButton().addClickListener(new ClickListener()
115         {
116             public void onClick(Widget sender)
117             {
118                 wizardPanel.displayPreviousPanel();
119             }
120         });
121 
122         wizardPanel.getFinishButton().addClickListener(new ClickListener()
123         {
124             public void onClick(Widget sender)
125             {
126                 submitForm();
127             }
128         });
129 
130         wizardPanel.getCancelButton().addClickListener(new ClickListener()
131         {
132             public void onClick(Widget sender)
133             {
134                 wizardPanel.getMessageBar().setMessage("Operation cancelled.");
135                 resetWizard();
136             }
137         });
138 
139         wizardPanel.getFormPanel().addFormHandler(new FormHandler()
140         {
141             public void onSubmit(FormSubmitEvent event)
142             {
143             }
144 
145             public void onSubmitComplete(FormSubmitCompleteEvent event)
146             {
147                 if (event.getResults() == null)
148                 {
149                     wizardPanel.getMessageBar().setMessage(
150                             "An error was encountered."
151                                     " Please see server logs for details.",
152                             MessageBar.SEVERITY_ERROR);
153                 }
154                 else
155                 {
156                     FileUploadUtil result = new FileUploadUtil(event
157                             .getResults());
158                     if (result.getStatus() == 200)
159                     {
160                         wizardPanel
161                                 .getMessageBar()
162                                 .setMessage(
163                                         "Your request has been submitted to the server.");
164                         resetWizard();
165                     }
166                     else
167                     {
168                         wizardPanel.getMessageBar().setMessage(
169                                 "An error was encountered: "
170                                         + result.getReason() ".",
171                                 MessageBar.SEVERITY_ERROR);
172                     }
173                 }
174             }
175         });
176 
177         wizardPanel.getFinishButton().setEnabled(false);
178     }
179 
180     private void submitForm()
181     {
182         wizardPanel.getFormPanel().submit();
183     }
184 
185     private void handleSwitch(String name)
186     {
187         if (name.equals(PANEL_SUMMARY))
188         {
189             suPanel.setProperties();
190             wizardPanel.getFinishButton().setEnabled(true);
191         }
192         else
193         {
194             wizardPanel.getFinishButton().setEnabled(false);
195         }
196     }
197 
198     /**
199      @return Returns the wizardPanel.
200      */
201     public WizardPanel getWizardPanel()
202     {
203         return wizardPanel;
204     }
205 
206     /**
207      @param wizardPanel
208      *            The wizardPanel to set.
209      */
210     public void setWizardPanel(WizardPanel wizardPanel)
211     {
212         this.wizardPanel = wizardPanel;
213     }
214 
215     protected HashMap getEnteredFields()
216     {
217         HashMap map = new HashMap();
218 
219         map.put("Name", formatText(piPanel.getName().getText()));
220         map.put("Address", formatText(piPanel.getAddress().getText()));
221         map.put("City", formatText(piPanel.getCities().getItemText(
222                 piPanel.getCities().getSelectedIndex())));
223         map.put("County", formatText(piPanel.getCounties().getItemText(
224                 piPanel.getCounties().getSelectedIndex())));
225 
226         map.put("Caption", formatText(fuPanel.getCaption().getText()));
227         map.put("Description", formatText(fuPanel.getDescription().getText()));
228         map.put("File Name", formatText(fuPanel.getUpload().getFilename()));
229         return map;
230     }
231 
232     private String formatText(String text)
233     {
234         return (text == null|| (text.trim().length() == 0? HTMLHelper
235                 .italics("Not entered": text;
236     }
237 
238     private void resetWizard()
239     {
240         piPanel.getName().setText("");
241         piPanel.getEmail().setText("");
242         piPanel.getAddress().setText("");
243         piPanel.getCities().setSelectedIndex(0);
244         piPanel.getCounties().setSelectedIndex(0);
245 
246         fuPanel.getCaption().setText("");
247         fuPanel.getDescription().setText("");
248         fuPanel.getUpload().setName("");
249 
250         wizardPanel.displayPanel(PANEL_INFO);
251         wizardPanel.getFinishButton().setEnabled(false);
252     }
253 }