<commit_before>/*
 * 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.drools.guvnor.client.widgets.tables;

import com.google.gwt.core.client.GWT;
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.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.resources.client.ImageResource.ImageOptions;
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
 * <code>pageSize</code> rows are always rendered even if the "last" page has
 * less than <code>pageSize</code> rows remain. Forked not sub-classed as 
 * GWTs code is largely private and not open to extension :(
 */
public class GuvnorSimplePager extends AbstractPager {

    /**
     * A ClientBundle that provides images for this widget.
     */
    public static interface Resources
        extends
        ClientBundle {

        /**
         * The image used to skip ahead multiple pages.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerFastForward.png")
        ImageResource simplePagerFastForward();

        /**
         * The disabled "fast forward" image.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerFastForwardDisabled.png")
        ImageResource simplePagerFastForwardDisabled();

        /**
         * The image used to go to the first page.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerFirstPage.png")
        ImageResource simplePagerFirstPage();

        /**
         * The disabled first page image.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerFirstPageDisabled.png")
        ImageResource simplePagerFirstPageDisabled();

        /**
         * The image used to go to the last page.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerLastPage.png")
        ImageResource simplePagerLastPage();

        /**
         * The disabled last page image.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerLastPageDisabled.png")
        ImageResource simplePagerLastPageDisabled();

        /**
         * The image used to go to the next page.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerNextPage.png")
        ImageResource simplePagerNextPage();

        /**
         * The disabled next page image.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerNextPageDisabled.png")
        ImageResource simplePagerNextPageDisabled();

        /**
         * The image used to go to the previous page.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerPreviousPage.png")
        ImageResource simplePagerPreviousPage();

        /**
         * The disabled previous page image.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerPreviousPageDisabled.png")
        ImageResource simplePagerPreviousPageDisabled();

        /**
         * The styles used in this widget.
         */
        @Source("../../resources/css/SimplePager.css")
        Style simplePagerStyle();
    }

    /**
     * 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 {@link 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) {
            super( resEnabled );
            this.resEnabled = resEnabled;
            this.resDisabled = resDiabled;
            this.styleDisabled = disabledStyle;
        }

        public boolean isDisabled() {
            return disabled;
        }

        @Override
        public void onBrowserEvent(Event event) {
            // Ignore events if disabled.
            if ( disabled ) {
                return;
            }

            super.onBrowserEvent( event );
        }

        public void setDisabled(boolean isDisabled) {
            if ( this.disabled == isDisabled ) {
                return;
            }

            this.disabled = isDisabled;
            if ( disabled ) {
                setResource( resDisabled );
                getElement().getParentElement().addClassName( styleDisabled );
            } else {
                setResource( resEnabled );
                getElement().getParentElement().removeClassName( styleDisabled );
            }
        }
    }

    private static int       DEFAULT_FAST_FORWARD_ROWS = 1000;
    private static Resources DEFAULT_RESOURCES;

    private static Resources getDefaultResources() {
        if ( DEFAULT_RESOURCES == null ) {
            DEFAULT_RESOURCES = GWT.create( Resources.class );
        }
        return DEFAULT_RESOURCES;
    }

    private final ImageButton fastForward;

    private final int         fastForwardRows;

    private final ImageButton firstPage;

    /**
     * We use an {@link 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 {@link Resources} used by this widget.
     */
    private final Resources   resources;

    /**<commit_msg>GUVNOR-1735: Include 'Last page' button on pager. Set 'Fast Forward' page size to 100.<commit_after>/*
 * 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.drools.guvnor.client.widgets.tables;

import com.google.gwt.core.client.GWT;
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.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.resources.client.ImageResource.ImageOptions;
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
 * <code>pageSize</code> rows are always rendered even if the "last" page has
 * less than <code>pageSize</code> rows remain. Forked not sub-classed as 
 * GWTs code is largely private and not open to extension :(
 */
public class GuvnorSimplePager extends AbstractPager {

    /**
     * A ClientBundle that provides images for this widget.
     */
    public static interface Resources
        extends
        ClientBundle {

        /**
         * The image used to skip ahead multiple pages.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerFastForward.png")
        ImageResource simplePagerFastForward();

        /**
         * The disabled "fast forward" image.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerFastForwardDisabled.png")
        ImageResource simplePagerFastForwardDisabled();

        /**
         * The image used to go to the first page.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerFirstPage.png")
        ImageResource simplePagerFirstPage();

        /**
         * The disabled first page image.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerFirstPageDisabled.png")
        ImageResource simplePagerFirstPageDisabled();

        /**
         * The image used to go to the last page.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerLastPage.png")
        ImageResource simplePagerLastPage();

        /**
         * The disabled last page image.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerLastPageDisabled.png")
        ImageResource simplePagerLastPageDisabled();

        /**
         * The image used to go to the next page.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerNextPage.png")
        ImageResource simplePagerNextPage();

        /**
         * The disabled next page image.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerNextPageDisabled.png")
        ImageResource simplePagerNextPageDisabled();

        /**
         * The image used to go to the previous page.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerPreviousPage.png")
        ImageResource simplePagerPreviousPage();

        /**
         * The disabled previous page image.
         */
        @ImageOptions(flipRtl = true)
        @Source("../../resources/images/simplepager/simplePagerPreviousPageDisabled.png")
        ImageResource simplePagerPreviousPageDisabled();

        /**
         * The styles used in this widget.
         */
        @Source("../../resources/css/SimplePager.css")
        Style simplePagerStyle();
    }

    /**
     * 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 {@link 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) {
            super( resEnabled );
            this.resEnabled = resEnabled;
            this.resDisabled = resDiabled;
            this.styleDisabled = disabledStyle;
        }

        public boolean isDisabled() {
            return disabled;
        }

        @Override
        public void onBrowserEvent(Event event) {
            // Ignore events if disabled.
            if ( disabled ) {
                return;
            }

            super.onBrowserEvent( event );
        }

        public void setDisabled(boolean isDisabled) {
            if ( this.disabled == isDisabled ) {
                return;
            }

            this.disabled = isDisabled;
            if ( disabled ) {
                setResource( resDisabled );
                getElement().getParentElement().addClassName( styleDisabled );
            } else {
                setResource( resEnabled );
                getElement().getParentElement().removeClassName( styleDisabled );
            }
        }
    }

    private static int       DEFAULT_FAST_FORWARD_ROWS = 100;
    private static Resources DEFAULT_RESOURCES;

    private static Resources getDefaultResources() {
        if ( DEFAULT_RESOURCES == null ) {
            DEFAULT_RESOURCES = GWT.create( Resources.class );
        }
        return DEFAULT_RESOURCES;
    }

    private final ImageButton fastForward;

    private final int         fastForwardRows;

    private final ImageButton firstPage;

    /**
     * We use an {@link 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 {@link Resources} used by this widget.
     */
    private final Resources   resources;

    /**
{'fraction_non_alphanumeric': 0.09516933578367026, 'fraction_numerical': 0.0022315568390653714, 'mean_word_length': 2.1605476042314873, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '423987', 'n_tokens_mistral': 4045, 'n_tokens_neox': 3865, 'n_words': 1061}