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

de.knightsoftnet.mtwidgets.client.ui.widget.PageableList 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.3.2
Show newest version
package de.knightsoftnet.mtwidgets.client.ui.widget;

import de.knightsoftnet.gwtp.spring.shared.search.TableFieldDefinition;
import de.knightsoftnet.mtwidgets.client.ui.widget.features.HandlesSelectedEntry;
import de.knightsoftnet.mtwidgets.client.ui.widget.styling.WidgetResources;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
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.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.TakesValue;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Provider;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.gwtproject.editor.client.EditorDelegate;
import org.gwtproject.editor.client.ValueAwareEditor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Persistable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;

import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

import javax.inject.Inject;

/**
 * list with pages.
 *
 * @author Manfred Tremmel
 *
 * @param  type of the list entries
 */
public class PageableList> extends Composite
    implements ValueAwareEditor>, TakesValue>, HasValueChangeHandlers,
    HandlesSelectedEntry {

  interface Binder extends UiBinder> {
  }

  @UiField
  HTMLPanel header;

  @UiField
  PageableListEditor content;

  @UiField
  PageNumberWithArrowsWidget pageable;

  private final Provider> searchResultListProvider;

  /**
   * the default resources.
   */
  private final WidgetResources resources;

  private Map headerMap;

  private Page value;

  private Sort sort;

  private HandlesSelectedEntry parent;

  /**
   * Constructor for Pageable List.
   */
  @Inject
  public PageableList(final Binder binder, final WidgetResources resources,
      final Provider> searchResultListProvider) {
    super();
    this.resources = resources;
    this.sort = Sort.unsorted();
    this.resources.pageableStyle().ensureInjected();
    this.searchResultListProvider = searchResultListProvider;
    initWidget(binder.createAndBindUi(this));
    content.setParent(this);
  }

  @Ignore
  @UiFactory
  public PageableListEditor buildSearchResultList() {
    return searchResultListProvider.get();
  }

  @Override
  public void setDelegate(final EditorDelegate> delegate) {
    delegate.subscribe();
  }

  @Override
  public Page getValue() {
    return value;
  }

  public boolean hasEntries() {
    return value != null && !value.isEmpty();
  }

  @Override
  public void setValue(final Page value) {
    this.value = value;
    content.showList(value.getContent());
    pageable.setValue(value.getPageable());
    pageable.setNumPages(value.getTotalPages());
  }

  @Override
  public void flush() {
    // nothing to do
  }

  @Override
  public void onPropertyChange(final String... paths) {
    // nothing to do
  }

  @Override
  public HandlerRegistration addValueChangeHandler(final ValueChangeHandler handler) {
    return addHandler(handler, ValueChangeEvent.getType());
  }

  @Override
  public void handleSelectedEntry(final T entry) {
    parent.handleSelectedEntry(entry);
  }

  /**
   * pageable changed.
   *
   * @param event value change handler
   */
  @UiHandler("pageable")
  public void pageableChanged(final ValueChangeEvent event) {
    final Pageable newPageable =
        PageRequest.of(event.getValue().getPageNumber(), event.getValue().getPageSize(), sort);
    ValueChangeEvent.fire(this, newPageable);
  }

  /**
   * set table field definitions.
   *
   * @param tableFieldDefinitions collection of field definitions
   */
  public void setTableFieldDefinitions(
      final Collection> tableFieldDefinitions) {
    content.setTableFieldDefinitions(tableFieldDefinitions);
    headerMap = tableFieldDefinitions.stream() //
        .collect(Collectors.toMap(

            TableFieldDefinition::getFieldName, //
            definition -> {
              final HTMLPanel headerCell = new HTMLPanel(definition.getFieldDisplayName());
              definition.getStyles().forEach(headerCell::addStyleName);
              headerCell.addDomHandler(event -> {
                final Direction direction;
                if (StringUtils.contains(headerCell.getStyleName(),
                    resources.pageableStyle().headerSortUp())) {
                  direction = Direction.DESC;
                } else {
                  direction = Direction.ASC;
                }
                fireSortChanged(Sort.by(direction, definition.getFieldName()));
              }, ClickEvent.getType());
              header.add(headerCell);
              return headerCell;
            }));
  }

  /**
   * clear/reset sorting (no header is selectd).
   */
  public void clearSort() {
    headerMap.forEach((key, headerCell) -> {
      headerCell.setStyleName(resources.pageableStyle().headerSortUp(), false);
      headerCell.setStyleName(resources.pageableStyle().headerSortDown(), false);
    });
  }

  private void fireSortChanged(final Sort sort) {
    this.sort = ObjectUtils.defaultIfNull(sort, Sort.unsorted());
    final Pageable currentPageable = pageable.getValue();
    final Pageable newPageable =
        PageRequest.of(currentPageable.getPageNumber(), currentPageable.getPageSize(), this.sort);
    headerMap.forEach((key, headerCell) -> {
      final Order order = this.sort.getOrderFor(key);
      headerCell.setStyleName(resources.pageableStyle().headerSortUp(),
          order != null && order.isAscending());
      headerCell.setStyleName(resources.pageableStyle().headerSortDown(),
          order != null && order.isDescending());
    });
    ValueChangeEvent.fire(this, newPageable);
  }

  public void setParent(final HandlesSelectedEntry parent) {
    this.parent = parent;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy