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
019
020import org.apache.wicket.util.io.IClusterable;
021import org.apache.wicket.markup.html.basic.Label;
022import org.apache.wicket.markup.html.navigation.paging.IPageableItems;
023import org.apache.wicket.model.Model;
024import org.apache.wicket.model.StringResourceModel;
025
026
027/**
028 * Label that provides Showing x to y of z message given for a DataTable. The message can be
029 * overridden using the <code>NavigatorLabel</code> property key, the default message is used is of
030 * the format <code>Showing ${from} to ${to} of ${of}</code>. The message can also be configured
031 * pragmatically by setting it as the model object of the label.
032 * 
033 * @author Igor Vaynberg (ivaynberg)
034 * 
035 */
036public class NavigatorLabel extends Label
037{
038        private static final long serialVersionUID = 1L;
039
040        /**
041         * Construct.
042         * 
043         * @param id
044         * @param pageable
045         */
046        public NavigatorLabel(final String id, final IPageableItems pageable)
047        {
048                super(id);
049                setDefaultModel(new StringResourceModel("NavigatorLabel", this,
050                        new Model<>(new LabelModelObject(pageable))));
051        }
052
053        public static class LabelModelObject implements IClusterable
054        {
055                private static final long serialVersionUID = 1L;
056                private final IPageableItems pageable;
057
058                /**
059                 * Construct.
060                 * 
061                 * @param table
062                 */
063                public LabelModelObject(final IPageableItems table)
064                {
065                        pageable = table;
066                }
067
068                /**
069                 * @return "z" in "Showing x to y of z"
070                 */
071                public long getOf()
072                {
073                        return pageable.getItemCount();
074                }
075
076                /**
077                 * @return "x" in "Showing x to y of z"
078                 */
079                public long getFrom()
080                {
081                        if (getOf() == 0)
082                        {
083                                return 0;
084                        }
085                        return pageable.getCurrentPage() * pageable.getItemsPerPage() + 1;
086                }
087
088                /**
089                 * @return "y" in "Showing x to y of z"
090                 */
091                public long getTo()
092                {
093                        if (getOf() == 0)
094                        {
095                                return 0;
096                        }
097                        return Math.min(getOf(), getFrom() + pageable.getItemsPerPage() - 1);
098                }
099        }
100}