Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011 JBoss 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 org.uberfire.ext.widgets.common.client.tables;
 
 import  com.google.gwt.event.dom.client.ClickEvent;
 import  com.google.gwt.event.dom.client.ClickHandler;
 import  com.google.gwt.i18n.client.NumberFormat;
 import  com.google.gwt.resources.client.CssResource;
 import  com.google.gwt.resources.client.ImageResource;
 import  com.google.gwt.user.cellview.client.AbstractPager;
 import  com.google.gwt.user.cellview.client.SimplePager;
 import  com.google.gwt.user.client.Event;
 import  com.google.gwt.user.client.ui.HTML;
 import  com.google.gwt.user.client.ui.HasVerticalAlignment;
 import  com.google.gwt.user.client.ui.HorizontalPanel;
 import  com.google.gwt.user.client.ui.Image;
 import  com.google.gwt.view.client.HasRows;
 import  com.google.gwt.view.client.Range;
Essentially a fork of GWT's SimplePager that maintains a set page size and displays page numbers and total pages more elegantly. SimplePager will ensure pageSize rows are always rendered even if the "last" page has less than pageSize rows remain. Forked not sub-classed as GWTs code is largely private and not open to extension :(
 
 public class UberfireSimplePager extends AbstractPager {

    
Styles used by this widget.
 
     public static interface Style
             extends
             CssResource {

        
Applied to buttons.
 
         String button();

        
Applied to disabled buttons.
 
         String disabledButton();

        
Applied to the details text.
 
         String pageDetails();
     }

    
The location of the text relative to the paging buttons.
 
     public static enum TextLocation {
         CENTER, LEFT, RIGHT
     }

    
An Image that acts as a button.
 
     private static class ImageButton extends Image {
 
         private boolean disabled;
         private final ImageResource resDisabled;
         private final ImageResource resEnabled;
         private final String styleDisabled;
 
         public ImageButton( ImageResource resEnabled,
                             ImageResource resDiabled,
                             String disabledStyle ) {
             superresEnabled );
             this. = resEnabled;
             this. = resDiabled;
             this. = disabledStyle;
         }
 
         public boolean isDisabled() {
             return ;
         }
 
         @Override
         public void onBrowserEvent( Event event ) {
             // Ignore events if disabled.
            if (  ) {
                return;
            }
            super.onBrowserEvent( event );
        }
        public void setDisabledboolean isDisabled ) {
            if ( this. == isDisabled ) {
                return;
            }
            this. = isDisabled;
            if (  ) {
                setResource(  );
                getElement().getParentElement().addClassName(  );
            } else {
                setResource(  );
                getElement().getParentElement().removeClassName(  );
            }
        }
    }
    private static int DEFAULT_FAST_FORWARD_ROWS = 100;
        if (  == null ) {
        }
        return ;
    }
    private final ImageButton fastForward;
    private final int fastForwardRows;
    private final ImageButton firstPage;

    
We use an HTML so we can embed the loading image.
    private final HTML label = new HTML();
    private final ImageButton lastPage;
    private final ImageButton nextPage;
    private final ImageButton prevPage;

    
The UberfireSimplePagerResources used by this widget.
    private final UberfireSimplePagerResources resources;

    
The Style used by this widget.
    private final Style style;
    //Page size is normally derieved from the visibleRange
    private int pageSize = 10;

    
Construct a SimplePager with the default text location.
    public UberfireSimplePager() {
        this. );
    }

    
Construct a SimplePager with the specified text location.

Parameters:
location the location of the text relative to the buttons
    public UberfireSimplePagerTextLocation location ) {
        thislocation,
              getDefaultResources(),
              true,
              ,
              true );
    }

    
Construct a SimplePager with the specified resources.

Parameters:
location the location of the text relative to the buttons
resources the UberfireSimplePagerResources to use
showFastForwardButton if true, show a fast-forward button that advances by a larger increment than a single page
fastForwardRows the number of rows to jump when fast forwarding
showLastPageButton if true, show a button to go the the last page
    public UberfireSimplePagerTextLocation location,
                                UberfireSimplePagerResources resources,
                                boolean showFastForwardButton,
                                final int fastForwardRows,
                                boolean showLastPageButton ) {
        this. = resources;
        this. = fastForwardRows;
        this. = resources.simplePagerStyle();
        this..ensureInjected();
        // Create the buttons.
        String disabledStyle = .disabledButton();
         = new ImageButtonresources.simplePagerFirstPage(),
                                     resources.simplePagerFirstPageDisabled(),
                                     disabledStyle );
        .addClickHandlernew ClickHandler() {
            public void onClick( ClickEvent event ) {
                firstPage();
            }
        } );
         = new ImageButtonresources.simplePagerNextPage(),
                                    resources.simplePagerNextPageDisabled(),
                                    disabledStyle );
        .addClickHandlernew ClickHandler() {
            public void onClick( ClickEvent event ) {
                nextPage();
            }
        } );
         = new ImageButtonresources.simplePagerPreviousPage(),
                                    resources.simplePagerPreviousPageDisabled(),
                                    disabledStyle );
        .addClickHandlernew ClickHandler() {
            public void onClick( ClickEvent event ) {
                previousPage();
            }
        } );
        if ( showLastPageButton ) {
             = new ImageButtonresources.simplePagerLastPage(),
                                        resources.simplePagerLastPageDisabled(),
                                        disabledStyle );
            .addClickHandlernew ClickHandler() {
                public void onClick( ClickEvent event ) {
                    lastPage();
                }
            } );
        } else {
             = null;
        }
        if ( showFastForwardButton ) {
             = new ImageButtonresources.simplePagerFastForward(),
                                           resources.simplePagerFastForwardDisabled(),
                                           disabledStyle );
            .addClickHandlernew ClickHandler() {
                public void onClick( ClickEvent event ) {
                    setPagegetPage() + getFastForwardPages() );
                }
            } );
        } else {
             = null;
        }
        // Construct the widget.
        HorizontalPanel layout = new HorizontalPanel();
        layout.setVerticalAlignment( HasVerticalAlignment.ALIGN_MIDDLE );
        initWidget( layout );
        if ( location == . ) {
            layout.add(  );
        }
        layout.add(  );
        layout.add(  );
        if ( location == . ) {
            layout.add(  );
        }
        layout.add(  );
        if ( showFastForwardButton ) {
            layout.add(  );
        }
        if ( showLastPageButton ) {
            layout.add(  );
        }
        if ( location == . ) {
            layout.add(  );
        }
        // Add style names to the cells.
        .getElement().getParentElement().addClassName( .button() );
        .getElement().getParentElement().addClassName( .button() );
        .getElement().getParentElement().addClassName( .pageDetails() );
        .getElement().getParentElement().addClassName( .button() );
        if ( showFastForwardButton ) {
            .getElement().getParentElement().addClassName( .button() );
        }
        if ( showLastPageButton ) {
            .getElement().getParentElement().addClassName( .button() );
        }
        // Disable the buttons by default.
        setDisplaynull );
    }
    // We want pageSize to remain constant
    @Override
    public int getPageSize() {
        return ;
    }
    // Page forward by an exact size rather than the number of visible
    // rows as is in the norm in the underlying implementation
    @Override
    public void nextPage() {
        if ( getDisplay() != null ) {
            Range range = getDisplay().getVisibleRange();
            setPageStartrange.getStart()
                                  + getPageSize() );
        }
    }
    // Page back by an exact size rather than the number of visible rows
    // as is in the norm in the underlying implementation
    @Override
    public void previousPage() {
        if ( getDisplay() != null ) {
            Range range = getDisplay().getVisibleRange();
            setPageStartrange.getStart()
                                  - getPageSize() );
        }
    }
    @Override
    public void setDisplay( HasRows display ) {
        // Enable or disable all buttons.
        boolean disableButtons = ( display == null );
        setFastForwardDisableddisableButtons );
        setNextPageButtonsDisableddisableButtons );
        setPrevPageButtonsDisableddisableButtons );
        super.setDisplay( display );
    }
    @Override
    public void setPageint index ) {
        super.setPage( index );
    }
    @Override
    public void setPageSizeint pageSize ) {
        this. = pageSize;
        super.setPageSize( pageSize );
    }
    // Override so the last page is shown with a number of rows less
    // than the pageSize rather than always showing the pageSize number
    // of rows and possibly repeating rows on the last and penultimate
    // page
    @Override
    public void setPageStartint index ) {
        if (getDisplay() != null) {
          Range range = getDisplay().getVisibleRange();
          int displayPageSize = range.getLength();
          if (!isRangeLimited() && getDisplay().isRowCountExact()) {
            index = Math.min(indexgetDisplay().getRowCount() - displayPageSize);
          }
          index = Math.max(0, index);
          if (index != range.getStart()) {
            getDisplay().setVisibleRange(indexdisplayPageSize);
          }
        }  
    }

    
Let the page know that the table is loading. Call this method to clear all data from the table and hide the current range when new data is being loaded into the table.
    public void startLoading() {
        getDisplay().setRowCount( 0,
                                  true );
        .setHTML( "" );
    }

    
Get the number of pages to fast forward based on the current page size.

Returns:
the number of pages to fast forward
    private int getFastForwardPages() {
        int pageSize = getPageSize();
        return pageSize > 0 ?  / pageSize : 0;
    }

    
Enable or disable the fast forward button.

Parameters:
disabled true to disable, false to enable
    private void setFastForwardDisabledboolean disabled ) {
        if (  == null ) {
            return;
        }
        if ( disabled ) {
            .setResource( .simplePagerFastForwardDisabled() );
            .getElement().getParentElement().addClassName(
                    .disabledButton() );
        } else {
            .setResource( .simplePagerFastForward() );
            .getElement().getParentElement().removeClassName(
                    .disabledButton() );
        }
        //The one line change to GWT's SimplePager code!
        .setDisableddisabled );
    }

    
Enable or disable the next page buttons.

Parameters:
disabled true to disable, false to enable
    private void setNextPageButtonsDisabledboolean disabled ) {
        .setDisableddisabled );
        if (  != null ) {
            .setDisableddisabled );
        }
    }

    
Enable or disable the previous page buttons.

Parameters:
disabled true to disable, false to enable
    private void setPrevPageButtonsDisabledboolean disabled ) {
        .setDisableddisabled );
        .setDisableddisabled );
    }
    // Override to display "0 of 0" when there are no records (otherwise
    // you get "1-1 of 0") and "1 of 1" when there is only one record
    // (otherwise you get "1-1 of 1"). Not internationalised (but
    // neither is SimplePager)
    protected String createText() {
        NumberFormat formatter = NumberFormat.getFormat( "#,###" );
        HasRows display = getDisplay();
        Range range = display.getVisibleRange();
        int pageStart = range.getStart() + 1;
        int pageSize = range.getLength();
        int dataSize = display.getRowCount();
        int endIndex = Math.mindataSize,
                                 pageStart
                                         + pageSize
                                         - 1 );
        endIndex = Math.maxpageStart,
                             endIndex );
        boolean exact = display.isRowCountExact();
        if ( dataSize == 0 ) {
            return "0 of 0";
        } else if ( pageStart == endIndex ) {
            return formatter.format( pageStart )
                    + " of "
                    + formatter.format( dataSize );
        }
        return formatter.format( pageStart )
                + "-"
                + formatter.format( endIndex )
                + ( exact ? " of " : " of over " )
                + formatter.format( dataSize );
    }
    @Override
    protected void onRangeOrRowCountChanged() {
        HasRows display = getDisplay();
        .setText( createText() );
        // Update the prev and first buttons.
        // Update the next and last buttons.
        if ( isRangeLimited() || !display.isRowCountExact() ) {
            setNextPageButtonsDisabled( !hasNextPage() );
            setFastForwardDisabled( !hasNextPagesgetFastForwardPages() ) );
        }
    }

    
Check if the next button is disabled. Visible for testing.
    boolean isNextButtonDisabled() {
        return .isDisabled();
    }

    
Check if the previous button is disabled. Visible for testing.
    boolean isPreviousButtonDisabled() {
        return .isDisabled();
    }
New to GrepCode? Check out our FAQ X