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;
018
019import java.util.ArrayList;
020import java.util.List;
021
022/**
023 * Abstract wizard model that provides an implementation for handling {@link IWizardModelListener
024 * wizard model listeners} and provides base implementations of many methods. If you want to provide
025 * a custom implementation of {@link IWizardModel}, it is recommended you start by overriding this
026 * class.
027 * 
028 * @author eelcohillenius
029 */
030public abstract class AbstractWizardModel implements IWizardModel
031{
032
033        /**
034         * 
035         */
036        private static final long serialVersionUID = 1L;
037
038        /** Whether cancel functionality is available. */
039        private boolean cancelVisible = true;
040
041        /** Whether the last button should be shown at all; false by default. */
042        private boolean lastVisible = false;
043
044        /** Listeners for {@link IWizardModelListener model events}. */
045        private final List<IWizardModelListener> wizardModelListeners = new ArrayList<IWizardModelListener>(
046                1);
047
048        /**
049         * Construct.
050         */
051        public AbstractWizardModel()
052        {
053        }
054
055        /**
056         * Adds a wizard model listener.
057         * 
058         * @param listener
059         *            The listener to add
060         */
061        @Override
062        public final void addListener(final IWizardModelListener listener)
063        {
064                wizardModelListeners.add(listener);
065        }
066
067        /**
068         * This implementation just fires {@link IWizardModelListener#onCancel() a cancel event}. Though
069         * this isn't a very strong contract, it gives all the power to the user of this model.
070         * 
071         * @see IWizardModel#cancel()
072         */
073        @Override
074        public void cancel()
075        {
076                fireWizardCancelled();
077        }
078
079        /**
080         * This implementation just fires {@link IWizardModelListener#onFinish() a finish event}. Though
081         * this isn't a very strong contract, it gives all the power to the user of this model.
082         * 
083         * @see IWizardModel#finish()
084         */
085        @Override
086        public void finish()
087        {
088                fireWizardFinished();
089        }
090
091        /**
092         * Gets whether cancel functionality is available.
093         * 
094         * @return Whether cancel functionality is available
095         */
096        @Override
097        public boolean isCancelVisible()
098        {
099                return cancelVisible;
100        }
101
102        /**
103         * Checks if the last button should be displayed. This method should only return true if
104         * {@link IWizardModel#isLastAvailable} can return true at any point. Returning false will
105         * prevent the last button from appearing on the wizard at all.
106         * 
107         * @return <tt>true</tt> if the previous last should be displayed, <tt>false</tt> otherwise.
108         */
109        @Override
110        public boolean isLastVisible()
111        {
112                return lastVisible;
113        }
114
115        /**
116         * Removes a wizard model listener.
117         * 
118         * @param listener
119         *            The listener to remove
120         */
121        @Override
122        public final void removeListener(final IWizardModelListener listener)
123        {
124                wizardModelListeners.remove(listener);
125        }
126
127        /**
128         * Sets whether cancel functionality is available.
129         * 
130         * @param cancelVisible
131         *            Whether cancel functionality is available
132         */
133        public void setCancelVisible(final boolean cancelVisible)
134        {
135                this.cancelVisible = cancelVisible;
136        }
137
138        /**
139         * Configures if the last button should be displayed.
140         * 
141         * @param lastVisible
142         *            <tt>true</tt> to display the last button, <tt>false</tt> otherwise.
143         * @see #isLastVisible
144         */
145        public void setLastVisible(final boolean lastVisible)
146        {
147                this.lastVisible = lastVisible;
148        }
149
150        /**
151         * Notify listeners that the active step has changed.
152         * 
153         * @param step
154         *            The new step
155         */
156        protected final void fireActiveStepChanged(final IWizardStep step)
157        {
158                for (IWizardModelListener listener : wizardModelListeners)
159                {
160                        listener.onActiveStepChanged(step);
161                }
162        }
163
164        /**
165         * Notify listeners that the wizard is finished.
166         */
167        protected final void fireWizardCancelled()
168        {
169                for (IWizardModelListener listener : wizardModelListeners)
170                {
171                        listener.onCancel();
172                }
173        }
174
175        /**
176         * Notify listeners that the wizard is finished.
177         */
178        protected final void fireWizardFinished()
179        {
180                for (IWizardModelListener listener : wizardModelListeners)
181                {
182                        listener.onFinish();
183                }
184        }
185}