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 org.apache.wicket.markup.html.form.Form;
020import org.apache.wicket.markup.html.panel.Panel;
021
022/**
023 * The default bar of button components for wizards. This should be good for 90% of the cases. If
024 * not, override {@link Wizard#newButtonBar(String)} and provide your own.
025 * <p>
026 * The button bar holds the {@link PreviousButton previous}, [@link NextButton next},
027 * {@link LastButton last}, [@link CancelButton cancel} and {@link FinishButton finish} buttons. The
028 * {@link LastButton last button} is off by default. You can turn it on by having the wizard model
029 * return true for {@link IWizardModel#isLastVisible() the is last visible method}.
030 * </p>
031 * 
032 * @author Eelco Hillenius
033 */
034public class WizardButtonBar extends Panel
035{
036        private static final long serialVersionUID = 1L;
037
038        private final IWizard wizard;
039
040        /**
041         * Construct.
042         * 
043         * @param id
044         *            The component id
045         * @param wizard
046         *            The containing wizard
047         */
048        public WizardButtonBar(final String id, final IWizard wizard)
049        {
050                super(id);
051                
052                this.wizard = wizard;
053        }
054        
055        @Override
056        protected void onInitialize()
057        {
058                super.onInitialize();
059
060                add(newPreviousButton("previous", wizard));
061                add(newNextButton("next", wizard));
062                add(newLastButton("last", wizard));
063                add(newCancelButton("cancel", wizard));
064                add(newFinishButton("finish", wizard));
065        }
066
067        @Override
068        protected void onBeforeRender()
069        {
070                super.onBeforeRender();
071
072                WizardButton button = getDefaultButton(wizard.getWizardModel());
073                if (button != null) {
074                        Form<?> form = button.getForm();
075                        if (form != null) {
076                                form.setDefaultButton(button);
077                        }
078                }
079        }
080
081        public WizardButton getDefaultButton(final IWizardModel model)
082        {
083                if (model.isNextAvailable())
084                {
085                        return (WizardButton)get("next");
086                }
087                else if (model.isLastAvailable())
088                {
089                        return (WizardButton)get("last");
090                }
091                else if (model.isLastStep(model.getActiveStep()))
092                {
093                        return (WizardButton)get("finish");
094                }
095                return null;
096        }
097        
098        /**
099         * Creates a new button for {@link IWizardModel#previous()}.
100         * 
101         * @param id the button's id
102         * @param wizard the {@link IWizard}
103         * @return a new {@code PreviousButton}
104         */
105        protected WizardButton newPreviousButton(final String id, final IWizard wizard)
106        {
107                return new PreviousButton(id, wizard);
108        }
109
110        /**
111         * Creates a new button for {@link IWizardModel#next()}.
112         * 
113         * @param id the button's id
114         * @param wizard the {@link IWizard}
115         * @return a new {@code NextButton}
116         */
117        protected WizardButton newNextButton(final String id, final IWizard wizard)
118        {
119                return new NextButton(id, wizard);
120        }
121
122        /**
123         * Creates a new button for {@link IWizardModel#last()}.
124         * 
125         * @param id the button's id
126         * @param wizard the {@link IWizard}
127         * @return a new {@code LastButton}
128         */
129        protected WizardButton newLastButton(final String id, final IWizard wizard)
130        {
131                return new LastButton(id, wizard);
132        }
133
134        /**
135         * Creates a new button for {@link IWizardModel#cancel()}.
136         * 
137         * @param id the button's id
138         * @param wizard the {@link IWizard}
139         * @return a new {@code CancelButton}
140         */
141        protected WizardButton newCancelButton(final String id, final IWizard wizard)
142        {
143                return new CancelButton(id, wizard);
144        }
145
146        /**
147         * Creates a new button for {@link IWizardModel#finish()}.
148         * 
149         * @param id the button's id
150         * @param wizard the {@link IWizard}
151         * @return a new button
152         */
153        protected WizardButton newFinishButton(final String id, final IWizard wizard)
154        {
155                return new FinishButton(id, wizard);
156        }
157}