Statistics
| Branch: | Tag: | Revision:

root / src / com / google / gwt / user / cellview / client / GssSimplePager.java @ 63366925

History | View | Annotate | Download (15.4 kB)

1 ab3fae7b Christos Stathis
/*
2 63366925 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 ab3fae7b Christos Stathis
 *
4 63366925 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 63366925 Christos Stathis
 * without modification, are permitted provided that the following
6 63366925 Christos Stathis
 * conditions are met:
7 ab3fae7b Christos Stathis
 *
8 63366925 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 63366925 Christos Stathis
 *      disclaimer.
11 ab3fae7b Christos Stathis
 *
12 63366925 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 63366925 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 63366925 Christos Stathis
 *      provided with the distribution.
16 63366925 Christos Stathis
 *
17 63366925 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 63366925 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 63366925 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 63366925 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 63366925 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 63366925 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 63366925 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 63366925 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 63366925 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 63366925 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 63366925 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 63366925 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 63366925 Christos Stathis
 *
30 63366925 Christos Stathis
 * The views and conclusions contained in the software and
31 63366925 Christos Stathis
 * documentation are those of the authors and should not be
32 63366925 Christos Stathis
 * interpreted as representing official policies, either expressed
33 63366925 Christos Stathis
 * or implied, of GRNET S.A.
34 ab3fae7b Christos Stathis
 */
35 ab3fae7b Christos Stathis
package com.google.gwt.user.cellview.client;
36 ab3fae7b Christos Stathis
37 ab3fae7b Christos Stathis
import com.google.gwt.core.client.GWT;
38 ab3fae7b Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
39 ab3fae7b Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
40 ab3fae7b Christos Stathis
import com.google.gwt.i18n.client.NumberFormat;
41 ab3fae7b Christos Stathis
import com.google.gwt.resources.client.ClientBundle;
42 ab3fae7b Christos Stathis
import com.google.gwt.resources.client.CssResource;
43 ab3fae7b Christos Stathis
import com.google.gwt.resources.client.ImageResource;
44 ab3fae7b Christos Stathis
import com.google.gwt.resources.client.ImageResource.ImageOptions;
45 ab3fae7b Christos Stathis
import com.google.gwt.uibinder.client.UiConstructor;
46 ab3fae7b Christos Stathis
import com.google.gwt.user.client.ui.HTML;
47 ab3fae7b Christos Stathis
import com.google.gwt.user.client.ui.HasVerticalAlignment;
48 ab3fae7b Christos Stathis
import com.google.gwt.user.client.ui.HorizontalPanel;
49 ab3fae7b Christos Stathis
import com.google.gwt.user.client.ui.Image;
50 ab3fae7b Christos Stathis
import com.google.gwt.view.client.HasRows;
51 ab3fae7b Christos Stathis
import com.google.gwt.view.client.Range;
52 ab3fae7b Christos Stathis
53 ab3fae7b Christos Stathis
/**
54 ab3fae7b Christos Stathis
 * A pager for controlling a {@link HasRows} that only supports simple page
55 ab3fae7b Christos Stathis
 * navigation.
56 ab3fae7b Christos Stathis
 *
57 ab3fae7b Christos Stathis
 * <p>
58 ab3fae7b Christos Stathis
 * <h3>Example</h3>
59 ab3fae7b Christos Stathis
 * {@example com.google.gwt.examples.cellview.SimplePagerExample}
60 ab3fae7b Christos Stathis
 * </p>
61 ab3fae7b Christos Stathis
 */
62 ab3fae7b Christos Stathis
public class GssSimplePager extends AbstractPager {
63 ab3fae7b Christos Stathis
64 ab3fae7b Christos Stathis
  /**
65 ab3fae7b Christos Stathis
   * A ClientBundle that provides images for this widget.
66 ab3fae7b Christos Stathis
   */
67 ab3fae7b Christos Stathis
  public static interface Resources extends ClientBundle {
68 ab3fae7b Christos Stathis
69 ab3fae7b Christos Stathis
    /**
70 ab3fae7b Christos Stathis
     * The image used to skip ahead multiple pages.
71 ab3fae7b Christos Stathis
     */
72 ab3fae7b Christos Stathis
    @ImageOptions(flipRtl = true)
73 ab3fae7b Christos Stathis
    ImageResource simplePagerFastForward();
74 ab3fae7b Christos Stathis
75 ab3fae7b Christos Stathis
    /**
76 ab3fae7b Christos Stathis
     * The disabled "fast forward" image.
77 ab3fae7b Christos Stathis
     */
78 ab3fae7b Christos Stathis
    @ImageOptions(flipRtl = true)
79 ab3fae7b Christos Stathis
    ImageResource simplePagerFastForwardDisabled();
80 ab3fae7b Christos Stathis
81 ab3fae7b Christos Stathis
    /**
82 ab3fae7b Christos Stathis
     * The image used to go to the first page.
83 ab3fae7b Christos Stathis
     */
84 ab3fae7b Christos Stathis
    @ImageOptions(flipRtl = true)
85 ab3fae7b Christos Stathis
    ImageResource simplePagerFirstPage();
86 ab3fae7b Christos Stathis
87 ab3fae7b Christos Stathis
    /**
88 ab3fae7b Christos Stathis
     * The disabled first page image.
89 ab3fae7b Christos Stathis
     */
90 ab3fae7b Christos Stathis
    @ImageOptions(flipRtl = true)
91 ab3fae7b Christos Stathis
    ImageResource simplePagerFirstPageDisabled();
92 ab3fae7b Christos Stathis
93 ab3fae7b Christos Stathis
    /**
94 ab3fae7b Christos Stathis
     * The image used to go to the last page.
95 ab3fae7b Christos Stathis
     */
96 ab3fae7b Christos Stathis
    @ImageOptions(flipRtl = true)
97 ab3fae7b Christos Stathis
    ImageResource simplePagerLastPage();
98 ab3fae7b Christos Stathis
99 ab3fae7b Christos Stathis
    /**
100 ab3fae7b Christos Stathis
     * The disabled last page image.
101 ab3fae7b Christos Stathis
     */
102 ab3fae7b Christos Stathis
    @ImageOptions(flipRtl = true)
103 ab3fae7b Christos Stathis
    ImageResource simplePagerLastPageDisabled();
104 ab3fae7b Christos Stathis
105 ab3fae7b Christos Stathis
    /**
106 ab3fae7b Christos Stathis
     * The image used to go to the next page.
107 ab3fae7b Christos Stathis
     */
108 ab3fae7b Christos Stathis
    @ImageOptions(flipRtl = true)
109 ab3fae7b Christos Stathis
    ImageResource simplePagerNextPage();
110 ab3fae7b Christos Stathis
111 ab3fae7b Christos Stathis
    /**
112 ab3fae7b Christos Stathis
     * The disabled next page image.
113 ab3fae7b Christos Stathis
     */
114 ab3fae7b Christos Stathis
    @ImageOptions(flipRtl = true)
115 ab3fae7b Christos Stathis
    ImageResource simplePagerNextPageDisabled();
116 ab3fae7b Christos Stathis
117 ab3fae7b Christos Stathis
    /**
118 ab3fae7b Christos Stathis
     * The image used to go to the previous page.
119 ab3fae7b Christos Stathis
     */
120 ab3fae7b Christos Stathis
    @ImageOptions(flipRtl = true)
121 ab3fae7b Christos Stathis
    ImageResource simplePagerPreviousPage();
122 ab3fae7b Christos Stathis
123 ab3fae7b Christos Stathis
    /**
124 ab3fae7b Christos Stathis
     * The disabled previous page image.
125 ab3fae7b Christos Stathis
     */
126 ab3fae7b Christos Stathis
    @ImageOptions(flipRtl = true)
127 ab3fae7b Christos Stathis
    ImageResource simplePagerPreviousPageDisabled();
128 ab3fae7b Christos Stathis
129 ab3fae7b Christos Stathis
    /**
130 ab3fae7b Christos Stathis
     * The styles used in this widget.
131 ab3fae7b Christos Stathis
     */
132 ab3fae7b Christos Stathis
    @Source("SimplePager.css")
133 ab3fae7b Christos Stathis
    Style simplePagerStyle();
134 ab3fae7b Christos Stathis
  }
135 ab3fae7b Christos Stathis
136 ab3fae7b Christos Stathis
  /**
137 ab3fae7b Christos Stathis
   * Styles used by this widget.
138 ab3fae7b Christos Stathis
   */
139 ab3fae7b Christos Stathis
  public static interface Style extends CssResource {
140 ab3fae7b Christos Stathis
141 ab3fae7b Christos Stathis
    /**
142 ab3fae7b Christos Stathis
     * Applied to buttons.
143 ab3fae7b Christos Stathis
     */
144 ab3fae7b Christos Stathis
    String button();
145 ab3fae7b Christos Stathis
146 ab3fae7b Christos Stathis
    /**
147 ab3fae7b Christos Stathis
     * Applied to disabled buttons.
148 ab3fae7b Christos Stathis
     */
149 ab3fae7b Christos Stathis
    String disabledButton();
150 ab3fae7b Christos Stathis
151 ab3fae7b Christos Stathis
    /**
152 ab3fae7b Christos Stathis
     * Applied to the details text.
153 ab3fae7b Christos Stathis
     */
154 ab3fae7b Christos Stathis
    String pageDetails();
155 ab3fae7b Christos Stathis
  }
156 ab3fae7b Christos Stathis
157 ab3fae7b Christos Stathis
  /**
158 ab3fae7b Christos Stathis
   * The location of the text relative to the paging buttons.
159 ab3fae7b Christos Stathis
   */
160 ab3fae7b Christos Stathis
  public static enum TextLocation {
161 ab3fae7b Christos Stathis
    CENTER, LEFT, RIGHT;
162 ab3fae7b Christos Stathis
  }
163 ab3fae7b Christos Stathis
164 ab3fae7b Christos Stathis
  private static int DEFAULT_FAST_FORWARD_ROWS = 1000;
165 ab3fae7b Christos Stathis
  private static Resources DEFAULT_RESOURCES;
166 ab3fae7b Christos Stathis
167 ab3fae7b Christos Stathis
  private static Resources getDefaultResources() {
168 ab3fae7b Christos Stathis
    if (DEFAULT_RESOURCES == null) {
169 ab3fae7b Christos Stathis
      DEFAULT_RESOURCES = GWT.create(Resources.class);
170 ab3fae7b Christos Stathis
    }
171 ab3fae7b Christos Stathis
    return DEFAULT_RESOURCES;
172 ab3fae7b Christos Stathis
  }
173 ab3fae7b Christos Stathis
174 ab3fae7b Christos Stathis
  private final Image fastForward;
175 ab3fae7b Christos Stathis
176 ab3fae7b Christos Stathis
  private final int fastForwardRows;
177 ab3fae7b Christos Stathis
178 ab3fae7b Christos Stathis
  private final Image firstPage;
179 ab3fae7b Christos Stathis
180 ab3fae7b Christos Stathis
  /**
181 ab3fae7b Christos Stathis
   * We use an {@link HTML} so we can embed the loading image.
182 ab3fae7b Christos Stathis
   */
183 ab3fae7b Christos Stathis
  private final HTML label = new HTML();
184 ab3fae7b Christos Stathis
185 ab3fae7b Christos Stathis
  private final Image lastPage;
186 ab3fae7b Christos Stathis
187 ab3fae7b Christos Stathis
  /**
188 ab3fae7b Christos Stathis
   * Set to true when the next and last buttons are disabled.
189 ab3fae7b Christos Stathis
   */
190 ab3fae7b Christos Stathis
  private boolean nextDisabled;
191 ab3fae7b Christos Stathis
192 ab3fae7b Christos Stathis
  private final Image nextPage;
193 ab3fae7b Christos Stathis
194 ab3fae7b Christos Stathis
  /**
195 ab3fae7b Christos Stathis
   * Set to true when the prev and first buttons are disabled.
196 ab3fae7b Christos Stathis
   */
197 ab3fae7b Christos Stathis
  private boolean prevDisabled;
198 ab3fae7b Christos Stathis
199 ab3fae7b Christos Stathis
  private final Image prevPage;
200 ab3fae7b Christos Stathis
201 ab3fae7b Christos Stathis
  /**
202 ab3fae7b Christos Stathis
   * The {@link Resources} used by this widget.
203 ab3fae7b Christos Stathis
   */
204 ab3fae7b Christos Stathis
  private final Resources resources;
205 ab3fae7b Christos Stathis
206 ab3fae7b Christos Stathis
  /**
207 ab3fae7b Christos Stathis
   * The {@link Style} used by this widget.
208 ab3fae7b Christos Stathis
   */
209 ab3fae7b Christos Stathis
  private final Style style;
210 ab3fae7b Christos Stathis
211 ab3fae7b Christos Stathis
  /**
212 ab3fae7b Christos Stathis
   * Construct a {@link GssSimplePager} with the default text location.
213 ab3fae7b Christos Stathis
   */
214 ab3fae7b Christos Stathis
  public GssSimplePager() {
215 ab3fae7b Christos Stathis
    this(TextLocation.CENTER);
216 ab3fae7b Christos Stathis
  }
217 ab3fae7b Christos Stathis
218 ab3fae7b Christos Stathis
  /**
219 ab3fae7b Christos Stathis
   * Construct a {@link GssSimplePager} with the specified text location.
220 ab3fae7b Christos Stathis
   *
221 ab3fae7b Christos Stathis
   * @param location the location of the text relative to the buttons
222 ab3fae7b Christos Stathis
   */
223 ab3fae7b Christos Stathis
  @UiConstructor
224 ab3fae7b Christos Stathis
  // Hack for Google I/O demo
225 ab3fae7b Christos Stathis
  public GssSimplePager(TextLocation location) {
226 ab3fae7b Christos Stathis
    this(location, getDefaultResources(), false, DEFAULT_FAST_FORWARD_ROWS,
227 ab3fae7b Christos Stathis
        true);
228 ab3fae7b Christos Stathis
  }
229 ab3fae7b Christos Stathis
230 ab3fae7b Christos Stathis
  /**
231 ab3fae7b Christos Stathis
   * Construct a {@link GssSimplePager} with the specified resources.
232 ab3fae7b Christos Stathis
   *
233 ab3fae7b Christos Stathis
   * @param location the location of the text relative to the buttons
234 ab3fae7b Christos Stathis
   * @param resources the {@link Resources} to use
235 ab3fae7b Christos Stathis
   * @param showFastForwardButton if true, show a fast-forward button that
236 ab3fae7b Christos Stathis
   *          advances by a larger increment than a single page
237 ab3fae7b Christos Stathis
   * @param fastForwardRows the number of rows to jump when fast forwarding
238 ab3fae7b Christos Stathis
   * @param showLastPageButton if true, show a button to go the the last page
239 ab3fae7b Christos Stathis
   */
240 ab3fae7b Christos Stathis
  public GssSimplePager(TextLocation location, Resources resources,
241 ab3fae7b Christos Stathis
      boolean showFastForwardButton, final int fastForwardRows,
242 ab3fae7b Christos Stathis
      boolean showLastPageButton) {
243 ab3fae7b Christos Stathis
    this.resources = resources;
244 ab3fae7b Christos Stathis
    this.fastForwardRows = fastForwardRows;
245 ab3fae7b Christos Stathis
    this.style = resources.simplePagerStyle();
246 ab3fae7b Christos Stathis
    this.style.ensureInjected();
247 ab3fae7b Christos Stathis
248 ab3fae7b Christos Stathis
    // Create the buttons.
249 ab3fae7b Christos Stathis
    firstPage = new Image(resources.simplePagerFirstPage());
250 ab3fae7b Christos Stathis
    firstPage.addClickHandler(new ClickHandler() {
251 ab3fae7b Christos Stathis
      public void onClick(ClickEvent event) {
252 ab3fae7b Christos Stathis
        firstPage();
253 ab3fae7b Christos Stathis
      }
254 ab3fae7b Christos Stathis
    });
255 ab3fae7b Christos Stathis
    nextPage = new Image(resources.simplePagerNextPage());
256 ab3fae7b Christos Stathis
    nextPage.addClickHandler(new ClickHandler() {
257 ab3fae7b Christos Stathis
      public void onClick(ClickEvent event) {
258 ab3fae7b Christos Stathis
        nextPage();
259 ab3fae7b Christos Stathis
      }
260 ab3fae7b Christos Stathis
    });
261 ab3fae7b Christos Stathis
    prevPage = new Image(resources.simplePagerPreviousPage());
262 ab3fae7b Christos Stathis
    prevPage.addClickHandler(new ClickHandler() {
263 ab3fae7b Christos Stathis
      public void onClick(ClickEvent event) {
264 ab3fae7b Christos Stathis
        previousPage();
265 ab3fae7b Christos Stathis
      }
266 ab3fae7b Christos Stathis
    });
267 ab3fae7b Christos Stathis
    if (showLastPageButton) {
268 ab3fae7b Christos Stathis
      lastPage = new Image(resources.simplePagerLastPage());
269 ab3fae7b Christos Stathis
      lastPage.addClickHandler(new ClickHandler() {
270 ab3fae7b Christos Stathis
        public void onClick(ClickEvent event) {
271 ab3fae7b Christos Stathis
          lastPage();
272 ab3fae7b Christos Stathis
        }
273 ab3fae7b Christos Stathis
      });
274 ab3fae7b Christos Stathis
    } else {
275 ab3fae7b Christos Stathis
      lastPage = null;
276 ab3fae7b Christos Stathis
    }
277 ab3fae7b Christos Stathis
    if (showFastForwardButton) {
278 ab3fae7b Christos Stathis
      fastForward = new Image(resources.simplePagerFastForward());
279 ab3fae7b Christos Stathis
      fastForward.addClickHandler(new ClickHandler() {
280 ab3fae7b Christos Stathis
        public void onClick(ClickEvent event) {
281 ab3fae7b Christos Stathis
          setPage(getPage() + getFastForwardPages());
282 ab3fae7b Christos Stathis
        }
283 ab3fae7b Christos Stathis
      });
284 ab3fae7b Christos Stathis
    } else {
285 ab3fae7b Christos Stathis
      fastForward = null;
286 ab3fae7b Christos Stathis
    }
287 ab3fae7b Christos Stathis
288 ab3fae7b Christos Stathis
    // Construct the widget.
289 ab3fae7b Christos Stathis
    HorizontalPanel layout = new HorizontalPanel();
290 ab3fae7b Christos Stathis
    layout.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
291 ab3fae7b Christos Stathis
    initWidget(layout);
292 ab3fae7b Christos Stathis
    if (location == TextLocation.RIGHT) {
293 ab3fae7b Christos Stathis
      layout.add(label);
294 ab3fae7b Christos Stathis
    }
295 ab3fae7b Christos Stathis
    layout.add(firstPage);
296 ab3fae7b Christos Stathis
    layout.add(prevPage);
297 ab3fae7b Christos Stathis
    if (location == TextLocation.CENTER) {
298 ab3fae7b Christos Stathis
      layout.add(label);
299 ab3fae7b Christos Stathis
    }
300 ab3fae7b Christos Stathis
    layout.add(nextPage);
301 ab3fae7b Christos Stathis
    if (showFastForwardButton) {
302 ab3fae7b Christos Stathis
      layout.add(fastForward);
303 ab3fae7b Christos Stathis
    }
304 ab3fae7b Christos Stathis
    if (showLastPageButton) {
305 ab3fae7b Christos Stathis
      layout.add(lastPage);
306 ab3fae7b Christos Stathis
    }
307 ab3fae7b Christos Stathis
    if (location == TextLocation.LEFT) {
308 ab3fae7b Christos Stathis
      layout.add(label);
309 ab3fae7b Christos Stathis
    }
310 ab3fae7b Christos Stathis
311 ab3fae7b Christos Stathis
    // Add style names to the cells.
312 ab3fae7b Christos Stathis
    firstPage.getElement().getParentElement().addClassName(style.button());
313 ab3fae7b Christos Stathis
    prevPage.getElement().getParentElement().addClassName(style.button());
314 ab3fae7b Christos Stathis
    label.getElement().getParentElement().addClassName(style.pageDetails());
315 ab3fae7b Christos Stathis
    nextPage.getElement().getParentElement().addClassName(style.button());
316 ab3fae7b Christos Stathis
    if (showFastForwardButton) {
317 ab3fae7b Christos Stathis
      fastForward.getElement().getParentElement().addClassName(style.button());
318 ab3fae7b Christos Stathis
    }
319 ab3fae7b Christos Stathis
    if (showLastPageButton) {
320 ab3fae7b Christos Stathis
      lastPage.getElement().getParentElement().addClassName(style.button());
321 ab3fae7b Christos Stathis
    }
322 ab3fae7b Christos Stathis
323 ab3fae7b Christos Stathis
    // Disable the buttons by default.
324 ab3fae7b Christos Stathis
    setDisplay(null);
325 ab3fae7b Christos Stathis
  }
326 ab3fae7b Christos Stathis
327 ab3fae7b Christos Stathis
  @Override
328 ab3fae7b Christos Stathis
  public void firstPage() {
329 ab3fae7b Christos Stathis
    super.firstPage();
330 ab3fae7b Christos Stathis
  }
331 ab3fae7b Christos Stathis
332 ab3fae7b Christos Stathis
  @Override
333 ab3fae7b Christos Stathis
  public int getPage() {
334 ab3fae7b Christos Stathis
    return super.getPage();
335 ab3fae7b Christos Stathis
  }
336 ab3fae7b Christos Stathis
337 ab3fae7b Christos Stathis
  @Override
338 ab3fae7b Christos Stathis
  public int getPageCount() {
339 ab3fae7b Christos Stathis
    return super.getPageCount();
340 ab3fae7b Christos Stathis
  }
341 ab3fae7b Christos Stathis
342 ab3fae7b Christos Stathis
  @Override
343 ab3fae7b Christos Stathis
  public boolean hasNextPage() {
344 ab3fae7b Christos Stathis
    return super.hasNextPage();
345 ab3fae7b Christos Stathis
  }
346 ab3fae7b Christos Stathis
347 ab3fae7b Christos Stathis
  @Override
348 ab3fae7b Christos Stathis
  public boolean hasNextPages(int pages) {
349 ab3fae7b Christos Stathis
    return super.hasNextPages(pages);
350 ab3fae7b Christos Stathis
  }
351 ab3fae7b Christos Stathis
352 ab3fae7b Christos Stathis
  @Override
353 ab3fae7b Christos Stathis
  public boolean hasPage(int index) {
354 ab3fae7b Christos Stathis
    return super.hasPage(index);
355 ab3fae7b Christos Stathis
  }
356 ab3fae7b Christos Stathis
357 ab3fae7b Christos Stathis
  @Override
358 ab3fae7b Christos Stathis
  public boolean hasPreviousPage() {
359 ab3fae7b Christos Stathis
    return super.hasPreviousPage();
360 ab3fae7b Christos Stathis
  }
361 ab3fae7b Christos Stathis
362 ab3fae7b Christos Stathis
  @Override
363 ab3fae7b Christos Stathis
  public boolean hasPreviousPages(int pages) {
364 ab3fae7b Christos Stathis
    return super.hasPreviousPages(pages);
365 ab3fae7b Christos Stathis
  }
366 ab3fae7b Christos Stathis
367 ab3fae7b Christos Stathis
  @Override
368 ab3fae7b Christos Stathis
  public void lastPage() {
369 ab3fae7b Christos Stathis
    super.lastPage();
370 ab3fae7b Christos Stathis
  }
371 ab3fae7b Christos Stathis
372 ab3fae7b Christos Stathis
  @Override
373 ab3fae7b Christos Stathis
  public void lastPageStart() {
374 ab3fae7b Christos Stathis
    super.lastPageStart();
375 ab3fae7b Christos Stathis
  }
376 ab3fae7b Christos Stathis
377 ab3fae7b Christos Stathis
  @Override
378 ab3fae7b Christos Stathis
  public void nextPage() {
379 ab3fae7b Christos Stathis
    super.nextPage();
380 ab3fae7b Christos Stathis
  }
381 ab3fae7b Christos Stathis
382 ab3fae7b Christos Stathis
  @Override
383 ab3fae7b Christos Stathis
  public void previousPage() {
384 ab3fae7b Christos Stathis
    super.previousPage();
385 ab3fae7b Christos Stathis
  }
386 ab3fae7b Christos Stathis
387 ab3fae7b Christos Stathis
  @Override
388 ab3fae7b Christos Stathis
  public void setDisplay(HasRows display) {
389 ab3fae7b Christos Stathis
    // Enable or disable all buttons.
390 ab3fae7b Christos Stathis
    boolean disableButtons = (display == null);
391 ab3fae7b Christos Stathis
    setFastForwardDisabled(disableButtons);
392 ab3fae7b Christos Stathis
    setNextPageButtonsDisabled(disableButtons);
393 ab3fae7b Christos Stathis
    setPrevPageButtonsDisabled(disableButtons);
394 ab3fae7b Christos Stathis
    super.setDisplay(display);
395 ab3fae7b Christos Stathis
  }
396 ab3fae7b Christos Stathis
397 ab3fae7b Christos Stathis
  @Override
398 ab3fae7b Christos Stathis
  public void setPage(int index) {
399 ab3fae7b Christos Stathis
    super.setPage(index);
400 ab3fae7b Christos Stathis
  }
401 ab3fae7b Christos Stathis
402 ab3fae7b Christos Stathis
  @Override
403 ab3fae7b Christos Stathis
  public void setPageSize(int pageSize) {
404 ab3fae7b Christos Stathis
    super.setPageSize(pageSize);
405 ab3fae7b Christos Stathis
  }
406 ab3fae7b Christos Stathis
407 ab3fae7b Christos Stathis
  @Override
408 ab3fae7b Christos Stathis
  public void setPageStart(int index) {
409 ab3fae7b Christos Stathis
    super.setPageStart(index);
410 ab3fae7b Christos Stathis
  }
411 ab3fae7b Christos Stathis
412 ab3fae7b Christos Stathis
  /**
413 ab3fae7b Christos Stathis
   * Let the page know that the table is loading. Call this method to clear all
414 ab3fae7b Christos Stathis
   * data from the table and hide the current range when new data is being
415 ab3fae7b Christos Stathis
   * loaded into the table.
416 ab3fae7b Christos Stathis
   */
417 ab3fae7b Christos Stathis
  public void startLoading() {
418 ab3fae7b Christos Stathis
    getDisplay().setRowCount(0, true);
419 ab3fae7b Christos Stathis
    label.setHTML("");
420 ab3fae7b Christos Stathis
  }
421 ab3fae7b Christos Stathis
422 ab3fae7b Christos Stathis
  /**
423 ab3fae7b Christos Stathis
   * Get the text to display in the pager that reflects the state of the pager.
424 ab3fae7b Christos Stathis
   *
425 ab3fae7b Christos Stathis
   * @return the text
426 ab3fae7b Christos Stathis
   */
427 ab3fae7b Christos Stathis
  protected String createText() {
428 ab3fae7b Christos Stathis
    // Default text is 1 based.
429 ab3fae7b Christos Stathis
    NumberFormat formatter = NumberFormat.getFormat("#,###");
430 ab3fae7b Christos Stathis
    HasRows display = getDisplay();
431 ab3fae7b Christos Stathis
    Range range = display.getVisibleRange();
432 ab3fae7b Christos Stathis
    int pageStart = range.getStart() + 1;
433 ab3fae7b Christos Stathis
    int pageSize = range.getLength();
434 ab3fae7b Christos Stathis
    int dataSize = display.getRowCount();
435 ab3fae7b Christos Stathis
    int endIndex = Math.min(dataSize, pageStart + pageSize - 1);
436 ab3fae7b Christos Stathis
    endIndex = Math.max(pageStart, endIndex);
437 ab3fae7b Christos Stathis
    boolean exact = display.isRowCountExact();
438 ab3fae7b Christos Stathis
    return formatter.format(pageStart) + "-" + formatter.format(endIndex)
439 ab3fae7b Christos Stathis
        + (exact ? " of " : " of over ") + formatter.format(dataSize);
440 ab3fae7b Christos Stathis
  }
441 ab3fae7b Christos Stathis
442 ab3fae7b Christos Stathis
  @Override
443 ab3fae7b Christos Stathis
  protected void onRangeOrRowCountChanged() {
444 ab3fae7b Christos Stathis
    HasRows display = getDisplay();
445 ab3fae7b Christos Stathis
    label.setText(createText());
446 ab3fae7b Christos Stathis
447 ab3fae7b Christos Stathis
    // Update the prev and first buttons.
448 ab3fae7b Christos Stathis
    setPrevPageButtonsDisabled(!hasPreviousPage());
449 ab3fae7b Christos Stathis
450 ab3fae7b Christos Stathis
    // Update the next and last buttons.
451 ab3fae7b Christos Stathis
    if (isRangeLimited() || !display.isRowCountExact()) {
452 ab3fae7b Christos Stathis
      setNextPageButtonsDisabled(!hasNextPage());
453 ab3fae7b Christos Stathis
      setFastForwardDisabled(!hasNextPages(getFastForwardPages()));
454 ab3fae7b Christos Stathis
    }
455 ab3fae7b Christos Stathis
  }
456 ab3fae7b Christos Stathis
457 ab3fae7b Christos Stathis
  /**
458 ab3fae7b Christos Stathis
   * Check if the next button is disabled. Visible for testing.
459 ab3fae7b Christos Stathis
   */
460 ab3fae7b Christos Stathis
  boolean isNextButtonDisabled() {
461 ab3fae7b Christos Stathis
    return nextDisabled;
462 ab3fae7b Christos Stathis
  }
463 ab3fae7b Christos Stathis
464 ab3fae7b Christos Stathis
  /**
465 ab3fae7b Christos Stathis
   * Check if the previous button is disabled. Visible for testing.
466 ab3fae7b Christos Stathis
   */
467 ab3fae7b Christos Stathis
  boolean isPreviousButtonDisabled() {
468 ab3fae7b Christos Stathis
    return prevDisabled;
469 ab3fae7b Christos Stathis
  }
470 ab3fae7b Christos Stathis
471 ab3fae7b Christos Stathis
  /**
472 ab3fae7b Christos Stathis
   * Get the number of pages to fast forward based on the current page size.
473 ab3fae7b Christos Stathis
   *
474 ab3fae7b Christos Stathis
   * @return the number of pages to fast forward
475 ab3fae7b Christos Stathis
   */
476 ab3fae7b Christos Stathis
  private int getFastForwardPages() {
477 ab3fae7b Christos Stathis
    int pageSize = getPageSize();
478 ab3fae7b Christos Stathis
    return pageSize > 0 ? fastForwardRows / pageSize : 0;
479 ab3fae7b Christos Stathis
  }
480 ab3fae7b Christos Stathis
481 ab3fae7b Christos Stathis
  /**
482 ab3fae7b Christos Stathis
   * Enable or disable the fast forward button.
483 ab3fae7b Christos Stathis
   *
484 ab3fae7b Christos Stathis
   * @param disabled true to disable, false to enable
485 ab3fae7b Christos Stathis
   */
486 ab3fae7b Christos Stathis
  private void setFastForwardDisabled(boolean disabled) {
487 ab3fae7b Christos Stathis
    if (fastForward == null) {
488 ab3fae7b Christos Stathis
      return;
489 ab3fae7b Christos Stathis
    }
490 ab3fae7b Christos Stathis
    if (disabled) {
491 ab3fae7b Christos Stathis
      fastForward.setResource(resources.simplePagerFastForwardDisabled());
492 ab3fae7b Christos Stathis
      fastForward.getElement().getParentElement().addClassName(
493 ab3fae7b Christos Stathis
          style.disabledButton());
494 ab3fae7b Christos Stathis
    } else {
495 ab3fae7b Christos Stathis
      fastForward.setResource(resources.simplePagerFastForward());
496 ab3fae7b Christos Stathis
      fastForward.getElement().getParentElement().removeClassName(
497 ab3fae7b Christos Stathis
          style.disabledButton());
498 ab3fae7b Christos Stathis
    }
499 ab3fae7b Christos Stathis
  }
500 ab3fae7b Christos Stathis
501 ab3fae7b Christos Stathis
  /**
502 ab3fae7b Christos Stathis
   * Enable or disable the next page buttons.
503 ab3fae7b Christos Stathis
   *
504 ab3fae7b Christos Stathis
   * @param disabled true to disable, false to enable
505 ab3fae7b Christos Stathis
   */
506 ab3fae7b Christos Stathis
  private void setNextPageButtonsDisabled(boolean disabled) {
507 ab3fae7b Christos Stathis
    if (disabled == nextDisabled) {
508 ab3fae7b Christos Stathis
      return;
509 ab3fae7b Christos Stathis
    }
510 ab3fae7b Christos Stathis
511 ab3fae7b Christos Stathis
    nextDisabled = disabled;
512 ab3fae7b Christos Stathis
    if (disabled) {
513 ab3fae7b Christos Stathis
      nextPage.setResource(resources.simplePagerNextPageDisabled());
514 ab3fae7b Christos Stathis
      nextPage.getElement().getParentElement().addClassName(
515 ab3fae7b Christos Stathis
          style.disabledButton());
516 ab3fae7b Christos Stathis
      if (lastPage != null) {
517 ab3fae7b Christos Stathis
        lastPage.setResource(resources.simplePagerLastPageDisabled());
518 ab3fae7b Christos Stathis
        lastPage.getElement().getParentElement().addClassName(
519 ab3fae7b Christos Stathis
            style.disabledButton());
520 ab3fae7b Christos Stathis
      }
521 ab3fae7b Christos Stathis
    } else {
522 ab3fae7b Christos Stathis
      nextPage.setResource(resources.simplePagerNextPage());
523 ab3fae7b Christos Stathis
      nextPage.getElement().getParentElement().removeClassName(
524 ab3fae7b Christos Stathis
          style.disabledButton());
525 ab3fae7b Christos Stathis
      if (lastPage != null) {
526 ab3fae7b Christos Stathis
        lastPage.setResource(resources.simplePagerLastPage());
527 ab3fae7b Christos Stathis
        lastPage.getElement().getParentElement().removeClassName(
528 ab3fae7b Christos Stathis
            style.disabledButton());
529 ab3fae7b Christos Stathis
      }
530 ab3fae7b Christos Stathis
    }
531 ab3fae7b Christos Stathis
  }
532 ab3fae7b Christos Stathis
533 ab3fae7b Christos Stathis
  /**
534 ab3fae7b Christos Stathis
   * Enable or disable the previous page buttons.
535 ab3fae7b Christos Stathis
   *
536 ab3fae7b Christos Stathis
   * @param disabled true to disable, false to enable
537 ab3fae7b Christos Stathis
   */
538 ab3fae7b Christos Stathis
  private void setPrevPageButtonsDisabled(boolean disabled) {
539 ab3fae7b Christos Stathis
    if (disabled == prevDisabled) {
540 ab3fae7b Christos Stathis
      return;
541 ab3fae7b Christos Stathis
    }
542 ab3fae7b Christos Stathis
543 ab3fae7b Christos Stathis
    prevDisabled = disabled;
544 ab3fae7b Christos Stathis
    if (disabled) {
545 ab3fae7b Christos Stathis
      firstPage.setResource(resources.simplePagerFirstPageDisabled());
546 ab3fae7b Christos Stathis
      firstPage.getElement().getParentElement().addClassName(
547 ab3fae7b Christos Stathis
          style.disabledButton());
548 ab3fae7b Christos Stathis
      prevPage.setResource(resources.simplePagerPreviousPageDisabled());
549 ab3fae7b Christos Stathis
      prevPage.getElement().getParentElement().addClassName(
550 ab3fae7b Christos Stathis
          style.disabledButton());
551 ab3fae7b Christos Stathis
    } else {
552 ab3fae7b Christos Stathis
      firstPage.setResource(resources.simplePagerFirstPage());
553 ab3fae7b Christos Stathis
      firstPage.getElement().getParentElement().removeClassName(
554 ab3fae7b Christos Stathis
          style.disabledButton());
555 ab3fae7b Christos Stathis
      prevPage.setResource(resources.simplePagerPreviousPage());
556 ab3fae7b Christos Stathis
      prevPage.getElement().getParentElement().removeClassName(
557 ab3fae7b Christos Stathis
          style.disabledButton());
558 ab3fae7b Christos Stathis
    }
559 ab3fae7b Christos Stathis
  }
560 ab3fae7b Christos Stathis
}