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.ajax.markup.html.tabs;
018
019import java.util.List;
020import java.util.Optional;
021
022import org.apache.wicket.ajax.AjaxRequestTarget;
023import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
024import org.apache.wicket.extensions.markup.html.tabs.ITab;
025import org.apache.wicket.extensions.markup.html.tabs.TabbedPanel;
026import org.apache.wicket.markup.html.WebMarkupContainer;
027import org.apache.wicket.model.IModel;
028
029
030/**
031 * Ajaxified version of the tabbed panel. Uses AjaxFallbackLink instead of regular wicket links so
032 * it can update itself inplace.
033 * 
034 * @author Igor Vaynberg (ivaynberg)
035 * 
036 * @param <T>
037 *            The type of panel to be used for this component's tabs. Just use {@link ITab} if you
038 *            have no special needs here.
039 */
040public class AjaxTabbedPanel<T extends ITab> extends TabbedPanel<T>
041{
042        private static final long serialVersionUID = 1L;
043
044        /**
045         * Constructor
046         * 
047         * @param id
048         * @param tabs
049         */
050        public AjaxTabbedPanel(final String id, final List<T> tabs)
051        {
052                this(id, tabs, null);
053        }
054
055        /**
056         * Constructor
057         * 
058         * @param id
059         * @param tabs
060         * @param model
061         *            model holding the index of the selected tab
062         */
063        public AjaxTabbedPanel(final String id, final List<T> tabs, IModel<Integer> model)
064        {
065                super(id, tabs, model);
066                setOutputMarkupId(true);
067
068                setVersioned(false);
069        }
070
071        @Override
072        protected WebMarkupContainer newLink(final String linkId, final int index)
073        {
074                return new AjaxFallbackLink<Void>(linkId)
075                {
076                        private static final long serialVersionUID = 1L;
077
078                        @Override
079                        public void onClick(Optional<AjaxRequestTarget> targetOptional)
080                        {
081                                setSelectedTab(index);
082                                targetOptional.ifPresent(target -> target.add(AjaxTabbedPanel.this));
083                                onAjaxUpdate(targetOptional);
084                        }
085                };
086        }
087
088        /**
089         * A template method that lets users add additional behavior when ajax update occurs. This
090         * method is called after the current tab has been set so access to it can be obtained via
091         * {@link #getSelectedTab()}.
092         * <p>
093         * <strong>Note</strong> Since an {@link AjaxFallbackLink} is used to back the ajax update the
094         * <code>target</code> argument can be null when the client browser does not support ajax and
095         * the fallback mode is used. See {@link AjaxFallbackLink} for details.
096         *
097         * @param target
098         *            ajax target used to update this component
099         */
100        protected void onAjaxUpdate(final Optional<AjaxRequestTarget> target)
101        {
102        }
103
104}