litesoft
@ 948
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
/* * Copyright 2008 Google Inc. * * Licensed 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. */ package com.google.gwt.gen2.table.client; import com.google.gwt.core.client.*; import com.google.gwt.event.dom.client.*; import com.google.gwt.gen2.table.event.client.*; import com.google.gwt.user.client.ui.*; /** * A panel that wraps a {@link PagingScrollTable} and includes options to * manipulate the page. * <p/> * <h3>CSS Style Rules</h3> * <p/> * <ul class="css"> * <p/> * <li>.gwt-PagingOptions { applied to the entire widget }</li> * <p/> * <li>.gwt-PagingOptions .errorMessage { applied to the error message }</li> * <p/> * <li>.pagingOptionsFirstPage { the first page button }</li> * <p/> * <li>.pagingOptionsLastPage { the last page button }</li> * <p/> * <li>.pagingOptionsNextPage { the next page button }</li> * <p/> * <li>.pagingOptionsPreviousPage { the previous page button }</li> * <p/> * </ul> */ public class PagingOptions extends Composite { /** * An {@link com.google.gwt.user.client.ui.ImageBundle} that provides images * for {@link PagingOptions}. */ public static interface PagingOptionsImages extends ImageBundle { /** * An image used to navigate to the first page. * * @return a prototype of this image */ AbstractImagePrototype pagingOptionsFirstPage(); /** * An image used to navigate to the last page. * * @return a prototype of this image */ AbstractImagePrototype pagingOptionsLastPage(); /** * An image used to navigate to the next page. * * @return a prototype of this image */ AbstractImagePrototype pagingOptionsNextPage(); /** * An image used to navigate to the previous page. * * @return a prototype of this image */ AbstractImagePrototype pagingOptionsPrevPage(); } /** * The default style name. */ public static final String DEFAULT_STYLENAME = "gwt-PagingOptions"; public static final String STYLENAME_PREFIX = "pagingOptions"; /** * The label used to display errors. */ private HTML errorLabel; /** * Goto first page button. */ private Image firstImage; /** * Goto last page button. */ private Image lastImage; /** * The loading image. */ private Image loadingImage; /** * Goto next page button. */ private Image nextImage; /** * The HTML field that contains the number of pages. */ private HTML numPagesLabel; /** * Goto previous page button. */ private Image prevImage; /** * The box where the user can select the current page. */ private TextBox curPageBox = new TextBox(); /** * The table being affected. */ private PagingScrollTable<?> table; /** * Constructor. * * @param table the table being ad */ public PagingOptions( PagingScrollTable<?> table ) { this( table, GWT.<PagingOptionsImages>create( PagingOptionsImages.class ) ); } /** * Constructor. * * @param table the table being affected * @param images the images to use */ public PagingOptions( PagingScrollTable<?> table, PagingOptionsImages images ) { this.table = table; // Create the main widget HorizontalPanel hPanel = new HorizontalPanel(); initWidget( hPanel ); hPanel.setVerticalAlignment( HasVerticalAlignment.ALIGN_MIDDLE ); setStyleName( DEFAULT_STYLENAME ); // Create the paging image buttons createPageButtons( images ); // Create the current page box createCurPageBox(); // Create the page count label numPagesLabel = new HTML(); // Create the loading image loadingImage = new Image( GWT.getModuleBaseURL() + "scrollTableLoading.gif" ); loadingImage.setVisible( false ); // Create the error label errorLabel = new HTML(); errorLabel.setStylePrimaryName( "errorMessage" ); // Add the widgets to the panel hPanel.add( createSpacer() ); hPanel.add( firstImage ); hPanel.add( createSpacer() ); hPanel.add( prevImage ); hPanel.add( createSpacer() ); hPanel.add( curPageBox ); hPanel.add( createSpacer() ); hPanel.add( numPagesLabel ); hPanel.add( createSpacer() ); hPanel.add( nextImage ); hPanel.add( createSpacer() ); hPanel.add( lastImage ); hPanel.add( createSpacer() ); hPanel.add( loadingImage ); hPanel.add( errorLabel ); // Add handlers to the table table.addPageLoadHandler( new PageLoadHandler() { public void onPageLoad( PageLoadEvent event ) { loadingImage.setVisible( false ); errorLabel.setHTML( "" ); } } ); table.addPageChangeHandler( new PageChangeHandler() { public void onPageChange( PageChangeEvent event ) { curPageBox.setText( (event.getNewPage() + 1) + "" ); loadingImage.setVisible( true ); errorLabel.setHTML( "" ); } } ); table.addPagingFailureHandler( new PagingFailureHandler() { public void onPagingFailure( PagingFailureEvent event ) { loadingImage.setVisible( false ); errorLabel.setHTML( event.getException().getMessage() ); } } ); table.addPageCountChangeHandler( new PageCountChangeHandler() { public void onPageCountChange( PageCountChangeEvent event ) { setPageCount( event.getNewPageCount() ); } } ); setPageCount( table.getPageCount() ); } /** * @return the {@link PagingScrollTable}. */ public PagingScrollTable<?> getPagingScrollTable() { return table; } /** * Create a widget that can be used to add space. * * @return a spacer widget */ protected Widget createSpacer() { return new HTML( " " ); } /** * Create a box that holds the current page. */ private void createCurPageBox() { // Setup the widget curPageBox.setWidth( "3em" ); curPageBox.setText( "1" ); curPageBox.setTextAlignment( TextBoxBase.ALIGN_RIGHT ); // Disallow non-numeric pages KeyPressHandler handler = new KeyPressHandler() { public void onKeyPress( KeyPressEvent event ) { char charCode = event.getCharCode(); if ( charCode == KeyCodes.KEY_ENTER ) { PagingOptions.this.table.gotoPage( getPagingBoxValue(), false ); } else if ( !Character.isDigit( charCode ) && (charCode != KeyCodes.KEY_TAB) && (charCode != KeyCodes.KEY_BACKSPACE) && (charCode != KeyCodes.KEY_DELETE) && (charCode != KeyCodes.KEY_ENTER) && (charCode != KeyCodes.KEY_HOME) && (charCode != KeyCodes.KEY_END) && (charCode != KeyCodes.KEY_LEFT) && (charCode != KeyCodes.KEY_UP) && (charCode != KeyCodes.KEY_RIGHT) && (charCode != KeyCodes.KEY_DOWN) ) { curPageBox.cancelKey(); } } }; // Add the handler curPageBox.addKeyPressHandler( handler ); } /** * Create a paging image buttons. * * @param images the images to use */ private void createPageButtons( PagingOptionsImages images ) { // Create the images firstImage = images.pagingOptionsFirstPage().createImage(); firstImage.addStyleName( STYLENAME_PREFIX + "FirstPage" ); prevImage = images.pagingOptionsPrevPage().createImage(); prevImage.addStyleName( STYLENAME_PREFIX + "PreviousPage" ); nextImage = images.pagingOptionsNextPage().createImage(); nextImage.addStyleName( STYLENAME_PREFIX + "NextPage" ); lastImage = images.pagingOptionsLastPage().createImage(); lastImage.addStyleName( STYLENAME_PREFIX + "LastPage" ); // Create the listener ClickHandler handler = new ClickHandler() { public void onClick( ClickEvent event ) { Object source = event.getSource(); if ( source == firstImage ) { table.gotoFirstPage(); } else if ( source == lastImage ) { table.gotoLastPage(); } else if ( source == nextImage ) { table.gotoNextPage(); } else if ( source == prevImage ) { table.gotoPreviousPage(); } } }; // Add the listener to each image firstImage.addClickHandler( handler ); prevImage.addClickHandler( handler ); nextImage.addClickHandler( handler ); lastImage.addClickHandler( handler ); } /** * Get the value of in the page box. If the value is invalid, it will be set * to 1 automatically. * * @return the value in the page box */ private int getPagingBoxValue() { int page = 0; try { page = Integer.parseInt( curPageBox.getText() ) - 1; } catch ( NumberFormatException e ) { // This will catch an empty box curPageBox.setText( "1" ); } // Replace values less than 1 if ( page < 1 ) { curPageBox.setText( "1" ); page = 0; } // Return the 0 based page, not the 1 based visible value return page; } /** * Set the page count. * * @param pageCount the current page count */ private void setPageCount( int pageCount ) { if ( pageCount < 0 ) { numPagesLabel.setHTML( "" ); lastImage.setVisible( false ); } else { numPagesLabel.setHTML( "of " + pageCount ); numPagesLabel.setVisible( true ); lastImage.setVisible( true ); } } } |