001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.extensions.wizard.dynamic;
018
019import java.util.Iterator;
020
021import org.apache.wicket.extensions.wizard.AbstractWizardModel;
022import org.apache.wicket.extensions.wizard.IWizardStep;
023import org.apache.wicket.extensions.wizard.WizardModel;
024
025/**
026 * Wizard model that is specialized on dynamic wizards. Unlike the default, static
027 * {@link WizardModel wizard model}, this model isn't very intelligent, but rather delegates much of
028 * the work and knowledge to the {@link IDynamicWizardStep dynamic wizard steps} it uses.
029 * 
030 * @author eelcohillenius
031 */
032public class DynamicWizardModel extends AbstractWizardModel
033{
034        private static final long serialVersionUID = 1L;
035
036        /**
037         * The current step. The only step that matters really,
038         */
039        private IDynamicWizardStep activeStep;
040
041        /**
042         * Remember the first step for resetting the wizard.
043         */
044        private final IDynamicWizardStep startStep;
045
046        /**
047         * Construct.
048         * 
049         * @param startStep
050         *            first step in the wizard
051         */
052        public DynamicWizardModel(final IDynamicWizardStep startStep)
053        {
054                this.startStep = startStep;
055        }
056
057        /**
058         * @see org.apache.wicket.extensions.wizard.IWizardModel#getActiveStep()
059         */
060        @Override
061        public IDynamicWizardStep getActiveStep()
062        {
063                return activeStep;
064        }
065
066        /**
067         * @return the step this wizard was constructed with (starts the wizard). Will be used for
068         *         resetting the wizard, unless you override {@link #reset()}.
069         */
070        public final IDynamicWizardStep getStartStep()
071        {
072                return startStep;
073        }
074
075        /**
076         * @see org.apache.wicket.extensions.wizard.IWizardModel#isLastAvailable()
077         */
078        @Override
079        public boolean isLastAvailable()
080        {
081                return activeStep.isLastAvailable();
082        }
083
084        /**
085         * @see org.apache.wicket.extensions.wizard.IWizardModel#isLastStep(org.apache.wicket.extensions.wizard.IWizardStep)
086         */
087        @Override
088        public boolean isLastStep(final IWizardStep step)
089        {
090                return ((IDynamicWizardStep)step).isLastStep();
091        }
092
093        /**
094         * @see org.apache.wicket.extensions.wizard.IWizardModel#isNextAvailable()
095         */
096        @Override
097        public boolean isNextAvailable()
098        {
099                return activeStep.isNextAvailable();
100        }
101
102        /**
103         * @see org.apache.wicket.extensions.wizard.IWizardModel#isPreviousAvailable()
104         */
105        @Override
106        public boolean isPreviousAvailable()
107        {
108                return activeStep.isPreviousAvailable();
109        }
110
111        @Override
112        public boolean isFinishAvailable()
113        {
114                return activeStep.isFinishAvailable();
115        }
116        
117        /**
118         * @see org.apache.wicket.extensions.wizard.IWizardModel#last()
119         */
120        @Override
121        public void last()
122        {
123                setActiveStep(activeStep.last());
124        }
125
126        /**
127         * @see org.apache.wicket.extensions.wizard.IWizardModel#next()
128         */
129        @Override
130        public void next()
131        {
132                setActiveStep(activeStep.next());
133        }
134
135        /**
136         * @see org.apache.wicket.extensions.wizard.IWizardModel#previous()
137         */
138        @Override
139        public void previous()
140        {
141                setActiveStep(activeStep.previous());
142        }
143
144        /**
145         * @see org.apache.wicket.extensions.wizard.IWizardModel#reset()
146         */
147        @Override
148        public void reset()
149        {
150                setActiveStep(startStep);
151        }
152
153        /**
154         * @see org.apache.wicket.extensions.wizard.IWizardModel#stepIterator()
155         */
156        @Override
157        public Iterator<IWizardStep> stepIterator()
158        {
159                return null;
160        }
161
162        /**
163         * Sets the active step.
164         * 
165         * @param step
166         *            the new active step step.
167         */
168        protected final void setActiveStep(final IDynamicWizardStep step)
169        {
170                if (step == null)
171                {
172                        throw new IllegalArgumentException("argument step must to be not null");
173                }
174
175                step.init(this);
176                activeStep = step;
177
178                fireActiveStepChanged(step);
179        }
180}