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.Component;
020import org.apache.wicket.MarkupContainer;
021import org.apache.wicket.ajax.AjaxRequestTarget;
022import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
023import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
024import org.apache.wicket.markup.ComponentTag;
025
026/**
027 * A bar of buttons for wizards utilizing {@link AjaxFormSubmitBehavior}.
028 * 
029 * @see Wizard#newButtonBar(String)
030 * 
031 * @author svenmeier
032 */
033public class AjaxWizardButtonBar extends WizardButtonBar
034{
035        private static final long serialVersionUID = 1L;
036
037        /**
038         * Construct.
039         * 
040         * @param id
041         *            The component id
042         * @param wizard
043         *            The containing wizard
044         */
045        public AjaxWizardButtonBar(String id, Wizard wizard)
046        {
047                super(id, wizard);
048
049                wizard.setOutputMarkupId(true);
050        }
051
052        @Override
053        public MarkupContainer add(Component... childs)
054        {
055                for (Component component : childs)
056                {
057                        if (component instanceof WizardButton)
058                        {
059                                ajaxify((WizardButton)component);
060                        }
061                }
062                return super.add(childs);
063        }
064
065        private void ajaxify(final WizardButton button)
066        {
067                button.add(new AjaxFormSubmitBehavior("click")
068                {
069                        private static final long serialVersionUID = 1L;
070
071                        @Override
072                        protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
073                        {
074                                super.updateAjaxAttributes(attributes);
075
076                                AjaxWizardButtonBar.this.updateAjaxAttributes(attributes);
077                        }
078
079                        @Override
080                        public boolean getDefaultProcessing()
081                        {
082                                return button.getDefaultFormProcessing();
083                        }
084
085                        @Override
086                        protected void onSubmit(AjaxRequestTarget target)
087                        {
088                                target.add(findParent(Wizard.class));
089
090                                button.onSubmit();
091                        }
092
093                        @Override
094                        protected void onAfterSubmit(AjaxRequestTarget target)
095                        {
096                                button.onAfterSubmit();
097                        }
098
099                        @Override
100                        protected void onError(AjaxRequestTarget target)
101                        {
102                                target.add(findParent(Wizard.class));
103
104                                button.onError();
105                        }
106                        
107                        @Override
108                        protected void onComponentTag(ComponentTag tag)
109                        {
110                                // WICKET-5644 prevent non-Ajax submit (similar to AjaxButton WICKET-5594)
111                                tag.put("type", "button");
112                        }
113                });
114        }
115
116        /**
117         * Hook method to update Ajax attributes.
118         * 
119         * @param attributes
120         *            Ajax attributes
121         */
122        protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
123        {
124        }
125}