View Javadoc

1   /**
2    * 2008, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos
3    * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt
4    */
5   package pt.digitalis.dif.presentation.views.jsp.taglibs.objects;
6   
7   import java.util.List;
8   import java.util.Map;
9   
10  import pt.digitalis.dif.controller.http.HTTPConstants;
11  import pt.digitalis.dif.controller.interfaces.IDIFRequest;
12  import pt.digitalis.dif.presentation.views.jsp.taglibs.AbstractDIFTag;
13  import pt.digitalis.dif.presentation.views.jsp.taglibs.layout.Wizard;
14  import pt.digitalis.dif.presentation.views.jsp.taglibs.objects.beans.WizardStepItem;
15  
16  /**
17   * Helper class that defines a wizard to be rendered on an HTML page
18   * 
19   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
20   * @created Jan 22, 2009
21   */
22  public class WizardDefinition {
23  
24      /** The wizard's form navigation action parameter hidden field */
25      public static final String HTML_ACTION_PARAMETER = "_wiz_nav_action";
26  
27      /** A parameter to control if the wizard step has executed */
28      public static final String HTML_EXECUTION_STATE_PARAMETER = "_wiz_executed_step";
29  
30      /** The wizard's form active step parameter hidden field */
31      public static final String HTML_STEP_PARAMETER = "_wiz_step";
32  
33      /** The wizard's form navigation go to step parameter hidden field */
34      public static final String HTML_TO_STEP_PARAMETER = "_wiz_nav_tostep";
35  
36      /** The index layout rendered in the left of the wizard container */
37      public static final String LEFT_INDEX_LAYOUT = "left";
38  
39      /** The index layout rendered in the top of the wizard container */
40      public static final String TOP_INDEX_LAYOUT = "top";
41  
42      /** Max chars that the top index may contain. More will force a dynamic shortening of the step names */
43      public static final int TOP_INDEX_MAX_CHARS = 100;
44  
45      /**
46       * Max chars that the top index may contain in a single title. More will force a truncate operation on these
47       * specific titles
48       */
49      public static final int TOP_INDEX_SINGLE_MAX_CHARS = 30;
50  
51      /**
52       * @param request
53       *            the current stage dif request
54       * @param wizardName
55       *            the wizard/form name
56       * @param hasIndex
57       *            if the wizard has an index page
58       * @param canNavigate
59       *            if the wizard can navigate from the present step
60       * @param wizardDef
61       *            a wizard definition object instance if available
62       * @return the current active step number
63       */
64      static public int getWizardActiveStepNumber(IDIFRequest request, String wizardName, boolean hasIndex,
65              boolean canNavigate, WizardDefinition wizardDef)
66      {
67          int wizardStep;
68  
69          Object stepParameter = request.getParameter(HTML_STEP_PARAMETER);
70          Object actionParameter = request.getParameter(HTML_ACTION_PARAMETER);
71          Object formParameter = request.getParameter(HTTPConstants.FORM_SUBMIT_NAME);
72          Object stageParameter = request.getParameter(HTTPConstants.FORM_SUBMIT_STAGE);
73          Object directStepNavParameter = request.getParameter(HTML_TO_STEP_PARAMETER);
74  
75          if (stepParameter == null && wizardDef != null)
76          {
77              // No step given, infer step from the stage...
78              for (int i = 0; i < wizardDef.getItems().size(); i++)
79              {
80                  if (request.getStage().equals(wizardDef.getItems().get(i).getStageId()))
81                  {
82                      // Found wizard step for the given stage...
83                      actionParameter = request.getParameter(WizardDefinition.HTML_ACTION_PARAMETER);
84  
85                      stepParameter = ++i;
86  
87                      break;
88                  }
89              }
90  
91              // Inferred the first stage from the wizard stage list
92              // Since there is an index page we set it instead if no forms have been submitted
93              if (wizardDef.getHasIndexPage() && stepParameter != null && (Integer) stepParameter == 2)
94              {
95                  Object submittedStage = request.getParameter(HTTPConstants.FORM_SUBMIT_STAGE);
96  
97                  if (submittedStage == null || request.getStage().equalsIgnoreCase(submittedStage.toString()))
98                      stepParameter = 1;
99              }
100 
101             request.addParameter(WizardDefinition.HTML_STEP_PARAMETER, stepParameter);
102             request.addParameter(WizardDefinition.HTML_TO_STEP_PARAMETER, stepParameter);
103             request.addParameter(HTTPConstants.FORM_SUBMIT_NAME, wizardDef.getName());
104             request.addParameter(HTTPConstants.FORM_SUBMIT_STAGE, request.getStage());
105 
106             stepParameter = request.getParameter(HTML_STEP_PARAMETER);
107             actionParameter = request.getParameter(HTML_ACTION_PARAMETER);
108             formParameter = wizardDef.getName();
109             stageParameter = request.getStage();
110         }
111 
112         if (stepParameter == null || formParameter == null || !wizardName.equalsIgnoreCase(formParameter.toString())
113                 || stageParameter == null || !request.getStage().equalsIgnoreCase(stageParameter.toString()))
114             wizardStep = 1;
115         else
116         {
117             wizardStep = Integer.parseInt(stepParameter.toString());
118 
119             if (canNavigate || (hasIndex && wizardStep == 1))
120             {
121                 if (directStepNavParameter != null)
122                 {
123                     wizardStep = Integer.parseInt(directStepNavParameter.toString().split(" ")[0]);
124 
125                 }
126                 else if (actionParameter != null)
127                 {
128                     String action = actionParameter.toString();
129                     Map<String, String> messages = AbstractDIFTag.getTagMessages(Wizard.class, request.getSession()
130                             .getLanguage());
131 
132                     if (action.equalsIgnoreCase(messages.get("previous")) && wizardStep > 1)
133                         wizardStep--;
134                     // FIXME: Viegas: Wizard implementation - Must determine the number of steps
135                     else if (action.equalsIgnoreCase(messages.get("next")) && wizardStep < 90)
136                         wizardStep++;
137                 }
138             }
139         }
140 
141         return wizardStep;
142     }
143 
144     /** The current wizard step */
145     private Integer activeStep;
146 
147     /** If the finish button is enabled */
148     private Boolean canFinish;
149 
150     /** The description of the wizard. For the index page */
151     private String description;
152 
153     /** the layout of the inner form */
154     private String formLayout;
155 
156     /** if there should exist an auto generated wizard index page */
157     private Boolean hasIndexPage;
158 
159     /** the wizard content fixed height. Forces inner scroll when the content exceeds it */
160     private Integer height;
161 
162     /** the layout of the index when visible */
163     private String indexLayout;
164 
165     /** The list of items of the current wizard */
166     private List<WizardStepItem> items;
167 
168     /** The name (id) of the wizard */
169     private String name;
170 
171     /** set required indicator active or inactive */
172     private boolean noRequiredIndicator = false;
173 
174     /** Readonly wizard */
175     private Boolean readonly = false;
176 
177     /** if the index should be showed on all pages */
178     private Boolean showIndex;
179 
180     /** Turn on or off the titlePrefix */
181     private boolean showTitlePrefix = true;
182 
183     /** Submit title message */
184     private String submitTitle;
185 
186     /** The title of the wizard */
187     private String title;
188 
189     /** Customize the title prefix message */
190     private String titlePrefix;
191 
192     /** If T a top navigation will be added to the bottom one */
193     private Boolean topNavigation = false;
194 
195     /** IF the wizard should render it's inner components inside it's form */
196     private Boolean wrapWithForm = true;
197 
198     /**
199      * Default constructor
200      * 
201      * @param name
202      * @param items
203      */
204     public WizardDefinition(String name, List<WizardStepItem> items)
205     {
206         this.items = items;
207         this.name = name;
208         this.activeStep = null;
209     }
210 
211     /**
212      * Default constructor
213      * 
214      * @param name
215      * @param items
216      * @param activeStep
217      */
218     public WizardDefinition(String name, List<WizardStepItem> items, int activeStep)
219     {
220         this.items = items;
221         this.name = name;
222         this.activeStep = activeStep;
223     }
224 
225     /**
226      * @return the activeStep
227      */
228     public int getActiveStep()
229     {
230         return activeStep;
231     }
232 
233     /**
234      * @return the canFinish
235      */
236     public Boolean getCanFinish()
237     {
238         if (canFinish == null)
239             return (getActiveStep() == items.size());
240         else
241             return canFinish;
242     }
243 
244     /**
245      * @return the description
246      */
247     public String getDescription()
248     {
249         return description;
250     }
251 
252     /**
253      * Inspector for the 'formLayout' attribute.
254      * 
255      * @return the formLayout value
256      */
257     public String getFormLayout()
258     {
259         return formLayout;
260     }
261 
262     /**
263      * @return the hasIndexPage
264      */
265     public Boolean getHasIndexPage()
266     {
267         if (hasIndexPage == null)
268             return getDescription() != null;
269         else
270             return hasIndexPage;
271     }
272 
273     /**
274      * Inspector for the 'height' attribute.
275      * 
276      * @return the height value
277      */
278     public Integer getHeight()
279     {
280         return height;
281     }
282 
283     /**
284      * @return the indexLayout
285      */
286     public String getIndexLayout()
287     {
288         if (indexLayout == null)
289             return LEFT_INDEX_LAYOUT;
290 
291         return indexLayout;
292     }
293 
294     /**
295      * @return the items
296      */
297     public List<WizardStepItem> getItems()
298     {
299         return items;
300     }
301 
302     /**
303      * @return the name
304      */
305     public String getName()
306     {
307         return name;
308     }
309 
310     /**
311      * Inspector for the 'readonly' attribute.
312      * 
313      * @return the readonly value
314      */
315     public Boolean getReadonly()
316     {
317         return readonly;
318     }
319 
320     /**
321      * @return the showIndex
322      */
323     public Boolean getShowIndex()
324     {
325         if (showIndex == null)
326             return false;
327         else
328             return showIndex;
329     }
330 
331     /**
332      * Inspector for the 'submitTitle' attribute.
333      * 
334      * @return the submitTitle value
335      */
336     public String getSubmitTitle()
337     {
338         return submitTitle;
339     }
340 
341     /**
342      * @return the title
343      */
344     public String getTitle()
345     {
346         return title;
347     }
348 
349     /**
350      * Inspector for the 'titlePrefix' attribute.
351      * 
352      * @return the titlePrefix value
353      */
354     public String getTitlePrefix()
355     {
356         return titlePrefix;
357     }
358 
359     /**
360      * Inspector for the 'topNavigation' attribute.
361      * 
362      * @return the topNavigation value
363      */
364     public Boolean getTopNavigation()
365     {
366         return topNavigation;
367     }
368 
369     /**
370      * Inspector for the 'wrapWithForm' attribute.
371      * 
372      * @return the wrapWithForm value
373      */
374     public Boolean getWrapWithForm()
375     {
376         return wrapWithForm;
377     }
378 
379     /**
380      * Inspector for the 'noRequiredIndicator' attribute.
381      * 
382      * @return the noRequiredIndicator value
383      */
384     public boolean isNoRequiredIndicator()
385     {
386         return noRequiredIndicator;
387     }
388 
389     /**
390      * Inspector for the 'showTitlePrefix' attribute.
391      * 
392      * @return the showTitlePrefix value
393      */
394     public boolean isShowTitlePrefix()
395     {
396         return showTitlePrefix;
397     }
398 
399     /**
400      * @param activeStep
401      *            the activeStep to set
402      */
403     public void setActiveStep(int activeStep)
404     {
405         this.activeStep = activeStep;
406     }
407 
408     /**
409      * @param canFinish
410      *            the canFinish to set
411      */
412     public void setCanFinish(Boolean canFinish)
413     {
414         this.canFinish = canFinish;
415     }
416 
417     /**
418      * @param description
419      *            the description to set
420      */
421     public void setDescription(String description)
422     {
423         this.description = description;
424     }
425 
426     /**
427      * Modifier for the 'formLayout' attribute.
428      * 
429      * @param formLayout
430      *            the new formLayout value to set
431      */
432     public void setFormLayout(String formLayout)
433     {
434         this.formLayout = formLayout;
435     }
436 
437     /**
438      * @param hasIndexPage
439      *            the hasIndexPage to set
440      */
441     public void setHasIndexPage(Boolean hasIndexPage)
442     {
443         this.hasIndexPage = hasIndexPage;
444     }
445 
446     /**
447      * Modifier for the 'height' attribute.
448      * 
449      * @param height
450      *            the new height value to set
451      */
452     public void setHeight(Integer height)
453     {
454         this.height = height;
455     }
456 
457     /**
458      * @param indexLayout
459      *            the indexLayout to set
460      */
461     public void setIndexLayout(String indexLayout)
462     {
463         this.indexLayout = indexLayout;
464     }
465 
466     /**
467      * @param items
468      *            the items to set
469      */
470     public void setItems(List<WizardStepItem> items)
471     {
472         this.items = items;
473     }
474 
475     /**
476      * @param name
477      *            the name to set
478      */
479     public void setName(String name)
480     {
481         this.name = name;
482     }
483 
484     /**
485      * Modifier for the 'noRequiredIndicator' attribute.
486      * 
487      * @param noRequiredIndicator
488      *            the new noRequiredIndicator value to set
489      */
490     public void setNoRequiredIndicator(boolean noRequiredIndicator)
491     {
492         this.noRequiredIndicator = noRequiredIndicator;
493     }
494 
495     /**
496      * Modifier for the 'readonly' attribute.
497      * 
498      * @param readonly
499      *            the new readonly value to set
500      */
501     public void setReadonly(Boolean readonly)
502     {
503         this.readonly = readonly;
504     }
505 
506     /**
507      * @param showIndex
508      *            the showIndex to set
509      */
510     public void setShowIndex(Boolean showIndex)
511     {
512         this.showIndex = showIndex;
513     }
514 
515     /**
516      * Modifier for the 'showTitlePrefix' attribute.
517      * 
518      * @param showTitlePrefix
519      *            the new showTitlePrefix value to set
520      */
521     public void setShowTitlePrefix(boolean showTitlePrefix)
522     {
523         this.showTitlePrefix = showTitlePrefix;
524     }
525 
526     /**
527      * Modifier for the 'submitTitle' attribute.
528      * 
529      * @param submitTitle
530      *            the new submitTitle value to set
531      */
532     public void setSubmitTitle(String submitTitle)
533     {
534         this.submitTitle = submitTitle;
535     }
536 
537     /**
538      * @param title
539      *            the title to set
540      */
541     public void setTitle(String title)
542     {
543         this.title = title;
544     }
545 
546     /**
547      * Modifier for the 'titlePrefix' attribute.
548      * 
549      * @param titlePrefix
550      *            the new titlePrefix value to set
551      */
552     public void setTitlePrefix(String titlePrefix)
553     {
554         this.titlePrefix = titlePrefix;
555     }
556 
557     /**
558      * Modifier for the 'topNavigation' attribute.
559      * 
560      * @param topNavigation
561      *            the new topNavigation value to set
562      */
563     public void setTopNavigation(Boolean topNavigation)
564     {
565         this.topNavigation = topNavigation;
566     }
567 
568     /**
569      * Modifier for the 'wrapWithForm' attribute.
570      * 
571      * @param wrapWithForm
572      *            the new wrapWithForm value to set
573      */
574     public void setWrapWithForm(Boolean wrapWithForm)
575     {
576         this.wrapWithForm = wrapWithForm;
577     }
578 }