001    package org.apache.myfaces.tobago.model;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.slf4j.Logger;
021    import org.slf4j.LoggerFactory;
022    
023    import javax.faces.event.ActionEvent;
024    import java.util.ArrayList;
025    import java.util.List;
026    
027    public class Wizard {
028    
029      private static final Logger LOG = LoggerFactory.getLogger(Wizard.class);
030    
031      private int index;
032    
033      private List<WizardStep> course;
034    
035      public Wizard() {
036        reset();
037      }
038    
039      public void next(ActionEvent event) {
040        LOG.debug("next: " + event);
041    
042        index++;
043      }
044    
045      public void gotoStep(ActionEvent event) {
046        Object step = (event.getComponent().getAttributes().get("step"));
047        if (step instanceof Integer) {
048          index = (Integer) step;
049        } else { // todo: The JSP Tag uses String in the moment
050          index = Integer.parseInt((String) step);
051        }
052    
053        LOG.debug("gotoStep: " + index);
054      }
055    
056      public String previous() {
057        String outcome = getPreviousStep().getOutcome();
058        if (index > 0) {
059          index--;
060        } else {
061          LOG.error("Previous not available!");
062        }
063    
064        LOG.debug("gotoStep: " + index);
065        return outcome;
066      }
067    
068      public final boolean isPreviousAvailable() {
069        return getIndex() > 0;
070      }
071    
072      public final void finish(ActionEvent event) {
073        if (LOG.isDebugEnabled()) {
074          LOG.debug("finish");
075        }
076    
077        reset();
078      }
079    
080      public final void cancel(ActionEvent event) {
081        if (LOG.isDebugEnabled()) {
082          LOG.debug("cancel");
083        }
084        reset();
085      }
086    
087      public final int getIndex() {
088        return index;
089      }
090    
091      /**
092       * Helper method to reset attributes
093       */
094      public void reset() {
095        index = 0;
096        course = new ArrayList<WizardStep>();
097      }
098    
099      public List<WizardStep> getCourse() {
100        return course;
101      }
102    
103      public int getSize() {
104        return course.size();
105      }
106    
107      public void register() {
108    
109        if (index == course.size()) { // this is a new page
110          course.add(new WizardStep(index));
111        } else if (index < course.size()) {
112          course.set(index, new WizardStep(index));
113        } else {
114          throw new IllegalStateException("Index too large for course: index="
115              + index + " course.size()=" + course.size());
116        }
117        if (LOG.isInfoEnabled()) {
118          LOG.info("course: " + course);
119        }
120      }
121    
122      public WizardStep getPreviousStep() {
123        if (index > 0) {
124          return course.get(index - 1);
125        } else {
126          return null;
127        }
128      }
129      public WizardStep getCurrentStep() {
130          return course.get(index);
131      }
132    
133      public void removeForwardSteps() {
134        // todo
135        LOG.error("Not implemented yet");
136      }
137    }