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.markup.html.repeater.data.table;
018
019import org.apache.wicket.AttributeModifier;
020import org.apache.wicket.Component;
021import org.apache.wicket.markup.html.WebComponent;
022import org.apache.wicket.markup.html.WebMarkupContainer;
023import org.apache.wicket.markup.html.navigation.paging.IPageableItems;
024import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
025import org.apache.wicket.model.IModel;
026
027/**
028 * Toolbar that displays links used to navigate the pages of the datatable as well as a message
029 * about which rows are being displayed and their total number in the data table.
030 * 
031 * @author Igor Vaynberg (ivaynberg)
032 */
033public class NavigationToolbar extends AbstractToolbar
034{
035        private static final long serialVersionUID = 1L;
036
037        /**
038         * Constructor
039         * 
040         * @param table
041         *            data table this toolbar will be attached to
042         */
043        public NavigationToolbar(final DataTable<?, ?> table)
044        {
045                super(table);
046
047                WebMarkupContainer span = new WebMarkupContainer("span");
048                add(span);
049                span.add(AttributeModifier.replace("colspan", (IModel<String>) () -> String.valueOf(table.getColumns().size()).intern()));
050
051                span.add(newPagingNavigator("navigator", table));
052                Component complexLabel = newComplexNavigatorLabel("navigatorLabel", table);
053                if (complexLabel != null)
054                {
055                        span.add(complexLabel);
056                }
057                else
058                {
059                        span.add(newNavigatorLabel("navigatorLabel", table));
060                }
061        }
062
063        /**
064         * Factory method used to create the paging navigator that will be used by the datatable
065         * 
066         * @param navigatorId
067         *            component id the navigator should be created with
068         * @param table
069         *            dataview used by datatable
070         * @return paging navigator that will be used to navigate the data table
071         */
072        protected PagingNavigator newPagingNavigator(final String navigatorId,
073                final DataTable<?, ?> table)
074        {
075                return new PagingNavigator(navigatorId, table);
076        }
077
078        /**
079         * Factory method used to create the navigator label that will be used by the datatable.
080         * Use {@link NavigationToolbar#newComplexNavigatorLabel(String, IPageableItems)} instead if you
081         * want to override label with a more complex component.
082         * 
083         * @param navigatorId
084         *            component id navigator label should be created with
085         * @param table
086         *            DataTable used by datatable
087         * @return navigator label that will be used to navigate the data table
088         * 
089         */
090        protected WebComponent newNavigatorLabel(final String navigatorId, final DataTable<?, ?> table)
091        {
092                return new NavigatorLabel(navigatorId, table);
093        }
094
095        /**
096         * Factory method used to create the navigator component in place of label that will be used by the datatable.
097         * This method takes precedence over {@link NavigationToolbar#newNavigatorLabel(String, DataTable)}.
098         * By default it returns <code>null</code>.
099         *
100         * <strong>NOTE:</strong> This is just a HACK to not break API in wicket 9.x and support use case of a more
101         * complex component as label. In wicket 10.x we will simply change the return type of
102         * NavigationToolbar#newNavigatorLabel(String, DataTable).
103         *
104         * @param navigatorId
105         *            component id navigator label should be created with
106         * @param table
107         *            DataTable used by label
108         * @return navigator label that will be used to navigate the data table
109         *
110         */
111        protected Component newComplexNavigatorLabel(final String navigatorId, final IPageableItems table)
112        {
113                return null;
114        }
115
116        /** {@inheritDoc} */
117        @Override
118        protected void onConfigure()
119        {
120                super.onConfigure();
121                setVisible(getTable().getPageCount() > 1);
122        }
123}