Paginator.java 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
M
min 已提交
17
package org.apache.dubbo.admin.governance.util;
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

import java.io.Serializable;

/**
 * TODO Comment of Paginator
 *
 */
public class Paginator implements Serializable, Cloneable {

    private static final long serialVersionUID = 3688506614705500726L;

    // The default number of items per page; default is 10
    int itemsPerPage = 10;

    // Sliding window default size; default: 7
    int sliderSize = 7;

    // The current page.
    int currentPage;

    // The current page.
    String path;

    // total mumber of items
    int totalItems;

    // total number of pages
    int totalPage;

    /**
     * The most simple paging constructor.
     *
     * @param currentPage
     * @param totalItems
     * @param path
     */
    public Paginator(int currentPage, int totalItems, String path) {
        initPagination(currentPage, totalItems, 0, 0, path);
    }

    public Paginator(String currentPage, int totalItems, String path) {
        int currentPageTemp = 1;
        if (!(currentPage == null || currentPage.equals(""))) {
            currentPageTemp = Integer.parseInt(currentPage);
        }
        initPagination(currentPageTemp, totalItems, 0, 0, path);
    }

    /**
     * Complete paging constructor.
     *
     * @param currentPageT
     * @param totalItemsT
     * @param sliderSizeT
     * @param itemsPerPageT
     * @param path
     */
    public void initPagination(int currentPageT, int totalItemsT, int sliderSizeT, int itemsPerPageT, String path) {
        this.totalItems = (totalItemsT > 0) ? totalItemsT : 0;
        this.sliderSize = (sliderSizeT > 0) ? sliderSizeT : sliderSize;
        this.itemsPerPage = (itemsPerPageT > 0) ? itemsPerPageT : itemsPerPage;
        this.totalPage = totalItems / itemsPerPage + (totalItems % itemsPerPage == 0 ? 0 : 1);
        this.currentPage = (currentPageT > 0) ? currentPageT : 1;
        this.currentPage = currentPage < totalPage ? currentPage : totalPage;
        this.currentPage = (currentPage == 0) ? 1 : currentPage;
        this.path = path;
    }

    public int getItemsPerPage() {
        return this.itemsPerPage;
    }

    /**
     * Get a sliding window of fixed size, and the current page should lie in the middle of the sliding window.
     * For example: a total of 13 pages, the current page is page 5, a size of 5 sliding window should consists of 3,4,5,6,7, page 5 is placed in the middle. If the current page is 12, the return page number should be 9, 10, 11, 12, 13.
     *
     * @return An array containing page numbers, or an empty array if the specified sliding window size is less than 1 or the total number of pages is zero.
     */
    public int[] getSlider() {
        int width = sliderSize;
        if ((totalItems < 1)) {
            return new int[0];

        } else {
            if (width > totalPage) {
                width = totalPage;
            }

            int[] slider = new int[width];

            int startPage = currentPage - ((width - 1) / 2);

            if (startPage < 1) {
                startPage = 1;
            }

            if (((startPage + width) - 1) > totalPage) {
                startPage = totalPage - width + 1;
            }

            for (int i = 0; i < width; i++) {
                slider[i] = startPage + i;
            }
            return slider;
        }
    }

    /**
     * Construction pagination toolbar
     */
    public String getPaginatorBar() {

        StringBuffer str = new StringBuffer("<div class=\"page\">");
        str.append("<script type=\"text/javascript\">function gotoPage(page){window.location.href=\"/" + path
                + "/pages/\" + page;}</script>");

        // generate flip section
        // The total number of records
        str.append("total items: " + this.totalItems + "&nbsp;&nbsp;");

        // 2. Pages: current page / total pages
        str.append("page " + this.currentPage + " of " + this.totalPage + "nbsp;&nbsp;");

        // 3. Home, Previous
        if (this.currentPage > 1) {
            str.append("<a class=\"prev\" href=\"#\" onclick=\"gotoPage(1);\">Home</a>");
            str.append("<a class=\"prev\" href=\"#\" onclick=\"gotoPage(" + (this.currentPage - 1) + ");\">Previous</a>");
        } else {
            str.append("<a class=\"prev\" href=\"#\">Home</a>");
            str.append("<a class=\"prev\" href=\"#\">Previous</a>");
        }

        // 4. Activity block
        int[] slider = getSlider();
        for (int i = 0; i < slider.length; i++) {
            if (slider[i] == this.currentPage) {
                str.append("<a class=\"num current_num\" href=\"#\">");
            } else {
                str.append("<a class=\"num\" href=\"#\" onclick=\"gotoPage(" + slider[i] + ");\">");
            }
            str.append(slider[i] + "</a>");
        }

        // 5. Next page
        if (this.currentPage < this.totalPage) {
            str.append("<a class=\"prev\" href=\"#\" onclick=\"gotoPage(" + (this.currentPage + 1) + ");\">");
        } else {
            str.append("<a class=\"prev\" href=\"#\">");
        }
        str.append("Next</a>&nbsp;&nbsp;");

        // 6. Jump section
        str.append("jump to page ");
        str.append("<SELECT size=1 onchange=\"gotoPage(this.value);\">");
        for (int i = 1; i < this.totalPage + 1; i++) {
            if (i == this.currentPage) {
                str.append("<OPTION value=" + i + " selected>" + i + "</OPTION>");
            } else {
                str.append("<OPTION value=" + i + ">" + i + "</OPTION>");
            }
        }
        str.append("</SELECT>");

        // 7. Implicit conditions
        str.append("</div>");
        return str.toString();
    }

    /**
     * Get the initial record
     *
     * @return
     */
    public int getStartIndex() {
        return (this.currentPage - 1) * this.itemsPerPage + 1;
    }

}