001    package org.gwtbootstrap3.client.ui;
002    
003    /*
004     * #%L
005     * GwtBootstrap3
006     * %%
007     * Copyright (C) 2013 - 2014 GwtBootstrap3
008     * %%
009     * Licensed under the Apache License, Version 2.0 (the "License");
010     * you may not use this file except in compliance with the License.
011     * You may obtain a copy of the License at
012     * 
013     *      http://www.apache.org/licenses/LICENSE-2.0
014     * 
015     * Unless required by applicable law or agreed to in writing, software
016     * distributed under the License is distributed on an "AS IS" BASIS,
017     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018     * See the License for the specific language governing permissions and
019     * limitations under the License.
020     * #L%
021     */
022    
023    import org.gwtbootstrap3.client.ui.base.HasPaginationSize;
024    import org.gwtbootstrap3.client.ui.base.HasResponsiveness;
025    import org.gwtbootstrap3.client.ui.base.helper.StyleHelper;
026    import org.gwtbootstrap3.client.ui.constants.IconType;
027    import org.gwtbootstrap3.client.ui.constants.PaginationSize;
028    import org.gwtbootstrap3.client.ui.constants.Styles;
029    import org.gwtbootstrap3.client.ui.html.UnorderedList;
030    
031    import com.google.gwt.event.dom.client.ClickEvent;
032    import com.google.gwt.event.dom.client.ClickHandler;
033    import com.google.gwt.user.cellview.client.SimplePager;
034    
035    /**
036     * Support for Bootstrap pagination (http://getbootstrap.com/components/#pagination)
037     *
038     * @author Joshua Godi
039     */
040    public class Pagination extends UnorderedList implements HasResponsiveness, HasPaginationSize {
041    
042        public Pagination() {
043            setStyleName(Styles.PAGINATION);
044        }
045    
046        public Pagination(final PaginationSize paginationSize) {
047            this();
048            setPaginationSize(paginationSize);
049        }
050    
051        @Override
052        public void setPaginationSize(final PaginationSize paginationSize) {
053            StyleHelper.addUniqueEnumStyleName(this, PaginationSize.class, paginationSize);
054        }
055    
056        @Override
057        public PaginationSize getPaginationSize() {
058            return PaginationSize.fromStyleName(getStyleName());
059        }
060    
061        public AnchorListItem addPreviousLink() {
062            final AnchorListItem listItem = new AnchorListItem();
063            listItem.setIcon(IconType.ANGLE_DOUBLE_LEFT);
064            insert(listItem, 0);
065            return listItem;
066        }
067    
068        public AnchorListItem addNextLink() {
069            final AnchorListItem listItem = new AnchorListItem();
070            listItem.setIcon(IconType.ANGLE_DOUBLE_RIGHT);
071            add(listItem);
072            return listItem;
073        }
074    
075        /**
076         * This will help to rebuild the Pagination based on the data inside the SimplePager passed in.
077         * <p/>
078         * Make sure to all this after adding/remove data from any of the grid to ensure that this stays
079         * current with the SimplePager.
080         * <p/>
081         * ex.
082         * dataProvider.getList().addAll(newData);
083         * pagination.rebuild(mySimplePager);
084         *
085         * @param pager the SimplePager of the CellTable/DataGrid
086         */
087        public void rebuild(final SimplePager pager) {
088            clear();
089    
090            if (pager.getPageCount() == 0) {
091                return;
092            }
093    
094            final AnchorListItem prev = addPreviousLink();
095            prev.addClickHandler(new ClickHandler() {
096                @Override
097                public void onClick(final ClickEvent event) {
098                    pager.previousPage();
099                }
100            });
101            prev.setEnabled(pager.hasPreviousPage());
102    
103            for (int i = 0; i < pager.getPageCount(); i++) {
104                final int display = i + 1;
105                final AnchorListItem page = new AnchorListItem(String.valueOf(display));
106                page.addClickHandler(new ClickHandler() {
107                    @Override
108                    public void onClick(final ClickEvent event) {
109                        pager.setPage(display - 1);
110                    }
111                });
112    
113                if (i == pager.getPage()) {
114                    page.setActive(true);
115                }
116    
117                add(page);
118            }
119    
120            final AnchorListItem next = addNextLink();
121            next.addClickHandler(new ClickHandler() {
122                @Override
123                public void onClick(final ClickEvent event) {
124                    pager.nextPage();
125                }
126            });
127            next.setEnabled(pager.hasNextPage());
128        }
129    }