All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.knightsoftnet.mtwidgets.client.ui.widget.PageNumberWithArrowsWidget Maven / Gradle / Ivy

Go to download

A set of widgets and handlers using server calls for gwt applications using gwt-bean-validators-spring-gwtp.

There is a newer version: 2.2.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You 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 de.knightsoftnet.mtwidgets.client.ui.widget;

import de.knightsoftnet.validators.client.decorators.ExtendedValueBoxEditor;
import de.knightsoftnet.validators.client.editor.ValueBoxEditor;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Widget;

import org.gwtproject.editor.client.Editor;
import org.gwtproject.editor.client.IsEditor;
import org.gwtproject.editor.client.TakesValue;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

/**
 * Input widget for page numbers used for pagination.
 *
 * @author Manfred Tremmel
 *
 */
public class PageNumberWithArrowsWidget extends Composite implements Editor,
    IsEditor>, HasValue, TakesValue {

  interface Binder extends UiBinder {
  }

  private static Binder uiBinder = GWT.create(Binder.class);

  @UiField
  Anchor paginationPrev;

  @UiField
  PageNumberListBox pageNumber;

  @UiField
  Anchor paginationNext;

  private int numPages;
  private int pageSize;
  private Pageable value;
  private boolean valueChangeHandlerInitialized;
  private final ValueBoxEditor editor;

  /**
   * constructor.
   */
  public PageNumberWithArrowsWidget() {
    super();
    numPages = -1;
    pageSize = 20;
    editor = new ExtendedValueBoxEditor<>(this, null);
    initWidget(uiBinder.createAndBindUi(this));
    pageNumber.addValueChangeHandler(value -> setValue(PageRequest.of(value.getValue(), pageSize)));
  }

  @Override
  public final void setValue(final Pageable newValue) {
    setValue(newValue, false);
  }

  @Override
  public final void setValue(final Pageable value, final boolean fireEvents) {
    this.value = value;
    int newPage = value == null ? 0 : value.getPageNumber();
    if (newPage < 0) {
      newPage = 0;
    }
    if (newPage >= numPages) {
      newPage = numPages - 1;
    }
    pageNumber.setValue(Integer.valueOf(newPage), fireEvents);
    paginationPrev.setEnabled(newPage > 0);
    paginationPrev.getElement().getStyle().setOpacity(paginationPrev.isEnabled() ? 1.0 : 0.4);
    paginationNext.setEnabled(newPage < numPages - 1);
    paginationNext.getElement().getStyle().setOpacity(paginationNext.isEnabled() ? 1.0 : 0.4);
  }

  @Override
  public Pageable getValue() {
    return numPages < 0 ? value : PageRequest.of(pageNumber.getValue(), pageSize);
  }

  @Override
  public HandlerRegistration addValueChangeHandler(final ValueChangeHandler handler) {
    if (!valueChangeHandlerInitialized) {
      ensureDomEventHandlers();
      valueChangeHandlerInitialized = true;
    }
    return this.addHandler(handler, ValueChangeEvent.getType());
  }

  protected void ensureDomEventHandlers() {
    final ValueChangeHandler changeHandler =
        event -> ValueChangeEvent.fire(this, getValue());
    pageNumber.addValueChangeHandler(changeHandler);
  }

  /**
   * set number pages, for each page a entry is created.
   *
   * @param numPages number of pages
   */
  public void setNumPages(final int numPages) {
    this.numPages = numPages;
    if (this.numPages < 1) {
      this.numPages = 1;
    }
    pageNumber.setNumPages(this.numPages);
    setValue(value, false);
  }

  public int getPageSize() {
    return pageSize;
  }

  public void setPageSize(final int pageSize) {
    this.pageSize = pageSize;
  }

  /**
   * pressed minus button.
   *
   * @param event click event
   */
  @UiHandler("paginationPrev")
  public void pressedMinusButton(final ClickEvent event) {
    final Integer newValue;
    if (pageNumber.getValue() == null) {
      newValue = Integer.valueOf(0);
    } else {
      newValue = Integer.valueOf(pageNumber.getValue().intValue() - 1);
    }
    setValue(PageRequest.of(newValue, pageSize), true);
  }

  /**
   * pressed plus button.
   *
   * @param event click event
   */
  @UiHandler("paginationNext")
  public void pressedPlusButton(final ClickEvent event) {
    final Integer newValue;
    if (pageNumber.getValue() == null) {
      newValue = Integer.valueOf(numPages);
    } else {
      newValue = Integer.valueOf(pageNumber.getValue().intValue() + 1);
    }
    setValue(PageRequest.of(newValue, pageSize), true);
  }

  @Override
  public ValueBoxEditor asEditor() {
    return editor;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy