001/*
002 *  Copyright 2013 GWT-Bootstrap
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package com.github.gwtbootstrap.client.ui;
017
018import com.github.gwtbootstrap.client.ui.base.DivWidget;
019import com.github.gwtbootstrap.client.ui.base.Style;
020import com.github.gwtbootstrap.client.ui.base.StyleHelper;
021import com.github.gwtbootstrap.client.ui.base.UnorderedList;
022import com.github.gwtbootstrap.client.ui.resources.Bootstrap;
023import com.google.gwt.user.client.ui.Widget;
024import com.github.gwtbootstrap.client.ui.base.*;
025import com.github.gwtbootstrap.client.ui.constants.Device;
026import com.google.gwt.user.client.ui.*;
027
028import java.util.Iterator;
029
030//@formatter:off
031/**
032 * Multi-Page Pagination
033 *
034 * @since 2.0.4.0
035 * @author Dominik Mayer
036 * @author Danilo Reinert
037 */
038//@formatter:on
039public class Pagination extends Composite implements HasStyle, IsResponsive, HasId, InsertPanel.ForIsWidget, HasWidgets.ForIsWidget {
040
041    private final DivWidget paginationDiv = new DivWidget();
042    private UnorderedList list = new UnorderedList();
043
044    public static enum PaginationSize implements Style {
045        MINI,
046        SMALL,
047        NORMAL,
048        LARGE;
049
050        @Override
051        public String get() {
052            if(this != NORMAL) {
053                return "pagination-" + this.name().toLowerCase();
054            }
055            return "";
056        }
057    }
058
059    public Pagination() {
060        paginationDiv.setStyle(Bootstrap.Pagination.LEFT);
061        paginationDiv.add(list);
062        initWidget(paginationDiv);
063        setSize(PaginationSize.NORMAL);
064    }
065
066    public void setAlignment(String alignment) {
067        if (alignment.equalsIgnoreCase("right"))
068            setStyle(Bootstrap.Pagination.RIGHT);
069        else if (alignment.equalsIgnoreCase("centered"))
070            setStyle(Bootstrap.Pagination.CENTERED);
071        else
072            setStyle(Bootstrap.Pagination.LEFT);
073    }
074
075    public void setSize(PaginationSize size) {
076        StyleHelper.changeStyle(this, size, PaginationSize.class);
077    }
078
079    @Override
080    public void add(IsWidget w) {
081        list.add(w);
082    }
083
084    @Override
085    public boolean remove(IsWidget w) {
086        return paginationDiv.remove(w);
087    }
088
089    @Override
090    public void insert(IsWidget w, int beforeIndex) {
091        paginationDiv.insert(w, beforeIndex);
092    }
093
094    @Override
095    public int getWidgetIndex(IsWidget child) {
096        return paginationDiv.getWidgetIndex(child);
097    }
098
099    @Override
100    public Widget getWidget(int index) {
101        return list.getWidget(index);
102    }
103
104    @Override
105    public int getWidgetCount() {
106        return list.getWidgetCount();
107    }
108
109    @Override
110    public int getWidgetIndex(Widget child) {
111        return list.getWidgetIndex(child);
112    }
113
114    @Override
115    public boolean remove(int index) {
116        return list.remove(index);
117    }
118
119    /**
120     * {@inheritDoc}
121     */
122    public void setId(String id) {
123        paginationDiv.setId(id);
124    }
125
126    /**
127     * {@inheritDoc}
128     */
129    public String getId() {
130        return paginationDiv.getId();
131    }
132
133    /**
134     * {@inheritDoc}
135     */
136    public void setHideOn(Device device) {
137        paginationDiv.setHideOn(device);
138    }
139
140    /**
141     * {@inheritDoc}
142     */
143    public void setShowOn(Device device) {
144        paginationDiv.setShowOn(device);
145    }
146
147    /**
148     * {@inheritDoc}
149     */
150    public void removeStyle(Style style) {
151        paginationDiv.removeStyle(style);
152    }
153
154    /**
155     * {@inheritDoc}
156     */
157    public void addStyle(Style style) {
158        paginationDiv.addStyle(style);
159    }
160
161    /**
162     * {@inheritDoc}
163     */
164    public void setStyle(Style style) {
165        paginationDiv.setStyle(style);
166    }
167
168    /**
169     * Use setPullRight instead.
170     *
171     * @param pullRight
172     *            <code>true</code> if the widget should be aligned right.
173     */
174    @Deprecated
175    public void pullRight(boolean pullRight) {
176        paginationDiv.pullRight(pullRight);
177    }
178
179    /**
180     * Pulls the widget to the right side.
181     *
182     * @param pullRight
183     *            <code>true</code> if the widget should be aligned right.
184     */
185    public void setPullRight(boolean pullRight) {
186        paginationDiv.setPullRight(pullRight);
187    }
188
189    /**
190     * {@inheritDoc}
191     */
192    public void add(Widget w) {
193        list.add(w);
194    }
195
196    /**
197     * Removes all child widgets.
198     */
199    @Override
200    public void clear() {
201        list.clear();
202    }
203
204    /**
205     * Gets an iterator for the contained widgets. This iterator is required to
206     * implement {@link java.util.Iterator#remove()}.
207     */
208    @Override
209    public Iterator<Widget> iterator() {
210        return list.iterator();
211    }
212
213    /**
214     * Removes a child widget.
215     *
216     * @param w the widget to be removed
217     * @return <code>true</code> if the widget was present
218     */
219    @Override
220    public boolean remove(Widget w) {
221        return list.remove(w);
222    }
223
224    /**
225     * Inserts another widget into this one.
226     *
227     * @param w
228     *            the widget to be inserted
229     * @param beforeIndex
230     *            the index of the position before which it should be set
231     */
232    public void insert(Widget w, int beforeIndex) {
233        list.insert(w, beforeIndex);
234    }
235
236    /**
237     * Insert a NavLink correctly allocated in the Pagination widget
238     *
239     * @param page the number of the page displayed by the NavLink
240     * @return the created NavLink filled with the page number
241     */
242    public NavLink addPageLink(int page) {
243        final NavLink pageLink = new NavLink(String.valueOf(page));
244        insert(pageLink, page);
245        return pageLink;
246    }
247
248    /**
249     * Insert a NavLink displaying "<" at the most left position of the Pagination widget
250     *
251     * @return the created NavLink
252     */
253    public NavLink addPreviousLink() {
254        final NavLink pageLink = new NavLink("<");
255        insert(pageLink, 0);
256        return pageLink;
257    }
258
259    /**
260     * Insert a NavLink displaying ">" at the most right position of the Pagination widget
261     *
262     * @return the created NavLink
263     */
264    public NavLink addNextLink() {
265        final NavLink pageLink = new NavLink(">");
266        add(pageLink);
267        return pageLink;
268    }
269
270}