001/*
002 * #%L
003 * GwtMaterial
004 * %%
005 * Copyright (C) 2015 - 2017 GwtMaterialDesign
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 * 
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package gwt.material.design.client.ui.pager;
021
022import com.google.gwt.dom.client.Document;
023import com.google.gwt.event.logical.shared.ValueChangeHandler;
024import com.google.gwt.event.shared.HandlerRegistration;
025import com.google.gwt.user.client.ui.HasValue;
026import gwt.material.design.client.base.MaterialWidget;
027import gwt.material.design.client.base.constants.TableCssName;
028import gwt.material.design.client.ui.MaterialListValueBox;
029import gwt.material.design.client.ui.html.Span;
030
031/**
032 * Widget for building the page numbers selection - where user can change the page number into a listbox
033 *
034 * @author kevzlou7979
035 */
036public class PageNumberSelection extends MaterialWidget implements HasValue<Integer> {
037
038    private final MaterialDataPager pager;
039    protected Span pageLabel = new Span("Page");
040    protected MaterialListValueBox<Integer> listPages = new MaterialListValueBox<>();
041
042    public PageNumberSelection(MaterialDataPager pager) {
043        super(Document.get().createDivElement(), TableCssName.NUM_PAGE_PANEL);
044        this.pager = pager;
045    }
046
047    @Override
048    protected void onLoad() {
049        super.onLoad();
050
051        setGrid("s12 m4 l3");
052        setOffset("l3");
053
054
055        add(listPages);
056        add(pageLabel);
057
058        registerHandler(listPages.addValueChangeHandler(event -> pager.gotoPage(event.getValue())));
059    }
060
061    /**
062     * Re populate the total number of pages
063     */
064    public void updatePageNumber(int totalRows, int limit, int currentPage) {
065        listPages.clear();
066        int pages = (pager.isExcess()) ? (totalRows / limit) + 1 : totalRows / limit;
067        for (int i = 1; i <= pages; i++) {
068            listPages.addItem(i);
069        }
070        listPages.setSelectedIndex(currentPage - 1);
071    }
072
073    @Override
074    public Integer getValue() {
075        return listPages.getValue();
076    }
077
078    @Override
079    public void setValue(Integer value) {
080        listPages.setValue(value);
081    }
082
083    @Override
084    public void setValue(Integer value, boolean fireEvents) {
085        listPages.setValue(value, fireEvents);
086    }
087
088    @Override
089    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Integer> valueChangeHandler) {
090        return listPages.addValueChangeHandler(valueChangeHandler);
091    }
092
093    public void setSelectedIndex(int index) {
094        listPages.setSelectedIndex(index);
095    }
096
097    public int getSelectedIndex() {
098        return listPages.getSelectedIndex();
099    }
100}