View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.model;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.faces.event.ActionEvent;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  public class Wizard {
30  
31    private static final Logger LOG = LoggerFactory.getLogger(Wizard.class);
32  
33    private int index;
34  
35    private List<WizardStep> course;
36  
37    public Wizard() {
38      reset();
39    }
40  
41    public void next(ActionEvent event) {
42      LOG.debug("next: " + event);
43  
44      index++;
45    }
46  
47    public void gotoStep(ActionEvent event) {
48      Object step = (event.getComponent().getAttributes().get("step"));
49      if (step instanceof Integer) {
50        index = (Integer) step;
51      } else { // todo: The JSP Tag uses String in the moment
52        index = Integer.parseInt((String) step);
53      }
54  
55      LOG.debug("gotoStep: " + index);
56    }
57  
58    public String previous() {
59      String outcome = getPreviousStep().getOutcome();
60      if (index > 0) {
61        index--;
62      } else {
63        LOG.error("Previous not available!");
64      }
65  
66      LOG.debug("gotoStep: " + index);
67      return outcome;
68    }
69  
70    public final boolean isPreviousAvailable() {
71      return getIndex() > 0;
72    }
73  
74    public final void finish(ActionEvent event) {
75      if (LOG.isDebugEnabled()) {
76        LOG.debug("finish");
77      }
78  
79      reset();
80    }
81  
82    public final void cancel(ActionEvent event) {
83      if (LOG.isDebugEnabled()) {
84        LOG.debug("cancel");
85      }
86      reset();
87    }
88  
89    public final int getIndex() {
90      return index;
91    }
92  
93    /**
94     * Helper method to reset attributes
95     */
96    public void reset() {
97      index = 0;
98      course = new ArrayList<WizardStep>();
99    }
100 
101   public List<WizardStep> getCourse() {
102     return course;
103   }
104 
105   public int getSize() {
106     return course.size();
107   }
108 
109   public void register() {
110 
111     if (index == course.size()) { // this is a new page
112       course.add(new WizardStep(index));
113     } else if (index < course.size()) {
114       course.set(index, new WizardStep(index));
115     } else {
116       throw new IllegalStateException("Index too large for course: index="
117           + index + " course.size()=" + course.size());
118     }
119     if (LOG.isInfoEnabled()) {
120       LOG.info("course: " + course);
121     }
122   }
123 
124   public WizardStep getPreviousStep() {
125     if (index > 0) {
126       return course.get(index - 1);
127     } else {
128       return null;
129     }
130   }
131   public WizardStep getCurrentStep() {
132       return course.get(index);
133   }
134 
135   public void removeForwardSteps() {
136     // todo
137     LOG.error("Not implemented yet");
138   }
139 }