Package com.kepennar.application.client.ui

Source Code of com.kepennar.application.client.ui.EventsList$EventsListDataProvider

/**
*
*/
package com.kepennar.application.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
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.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.ColumnSortEvent.AsyncHandler;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.Range;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.inject.Inject;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.kepennar.application.client.requests.ApplicationRequestFactory;
import com.kepennar.application.client.requests.EventProxy;

/**
* @author r�cup_Moonroid
*
*/

public class EventsList extends Composite {

  private static ClientUiBinder uiBinder = GWT.create(ClientUiBinder.class);
  private final SingleSelectionModel<EventProxy> selectionModel = new SingleSelectionModel<EventProxy>();
  private final EventsListDataProvider asyncDataProvider = new EventsListDataProvider();
  private final ApplicationRequestFactory requestFactory;
 
  @UiField(provided = true)
  final CellTable<EventProxy> eventsTable = new CellTable<EventProxy>();
  @UiField
  final Button addButton = new Button();
  @UiField
  final Button deleteButton = new Button();
  @UiField
  final SimplePager eventsTablePager = new SimplePager();

  private static EventsList instance;

  interface ClientUiBinder extends UiBinder<Widget, EventsList> {
  }

  public static EventsList instance(ApplicationRequestFactory requestFactory) {
    if (instance == null) {
      instance = new EventsList(requestFactory);
    }
    return instance;
  }

  @Inject
  public EventsList(ApplicationRequestFactory requestFactory) {
   
    this.requestFactory = requestFactory;
   
    // Create name column.
    TextColumn<EventProxy> nameColumn = new TextColumn<EventProxy>() {
      @Override
      public String getValue(EventProxy event) {
        return event.getTitle();
      }
    };

    // Create description column.
    TextColumn<EventProxy> descriptionColumn = new TextColumn<EventProxy>() {
      @Override
      public String getValue(EventProxy event) {
        return event.getDescription();
      }
    };

    // Create description column.
    TextColumn<EventProxy> dateColumn = new TextColumn<EventProxy>() {
      @Override
      public String getValue(EventProxy event) {
        return event.getDateDebut().toString();
      }
    };

   

    // Add the columns.
    eventsTable.addColumn(nameColumn, "Name");
    eventsTable.addColumn(descriptionColumn, "Description");
    eventsTable.addColumn(dateColumn, "Date d�but");

    // Set the range to display. In this case, our visible range is smaller
    // than the data set.
    eventsTable.setVisibleRange(0, 3);
   
    // Set the width of each column.
    eventsTable.setColumnWidth(nameColumn, 25.0, Unit.PCT);
    eventsTable.setColumnWidth(descriptionColumn, 25.0, Unit.PCT);
    eventsTable.setColumnWidth(dateColumn, 50.0, Unit.PCT);

    eventsTablePager.setDisplay(eventsTable);
   
    // Model when row is selected
    eventsTable.setSelectionModel(selectionModel);

    // Connect the list to the data provider.
    asyncDataProvider.addDataDisplay(eventsTable);
   
    // Set the total row count. You might send an RPC request to determine
    // the total row count.
    //eventsTable.setRowCount(eventsList.size(), true);

    // Add a ColumnSortEvent.AsyncHandler to connect sorting to the
    // AsyncDataPRrovider.
    AsyncHandler columnSortHandler = new AsyncHandler(eventsTable);
    eventsTable.addColumnSortHandler(columnSortHandler);

    // We know that the data is sorted alphabetically by default.
    eventsTable.getColumnSortList().push(nameColumn);

    initWidget(uiBinder.createAndBindUi(this));

  }

  @UiHandler("addButton")
  void onAddButtonClick(ClickEvent event) {

   
  }

  @UiHandler("deleteButton")
  void onDeleteButtonClick(ClickEvent event) {
   
  }
 
 
  private class EventsListDataProvider extends AsyncDataProvider<EventProxy> {

    public EventsListDataProvider() {
      super();
    }
     @Override
        public void addDataDisplay(HasData<EventProxy> display) {
          super.addDataDisplay(display);
          // Request the count anytime a view is added.
          requestFactory.eventRequest().countEvents().fire(
              new Receiver<Long>() {
                @Override
                public void onSuccess(Long response) {
                  updateRowCount(response.intValue(), true);
                }
              });
     }

    @Override
    protected void onRangeChanged(HasData<EventProxy> display) {

      final Range range = display.getVisibleRange();

      requestFactory.eventRequest()
          .findEventEntries(range.getStart(), range.getLength())
          .fire(new Receiver<List<EventProxy>>() {
            @Override
            public void onSuccess(List<EventProxy> response) {
              updateRowData(0, response);
            }
          });

    }

   
  }

  /** GETTERS / SETTERS */
  /******* UI ********/
  public CellTable<EventProxy> getEventsTable() {
    return eventsTable;
  }

  public Button getAddButton() {
    return addButton;
  }

  public Button getDeleteButton() {
    return deleteButton;
  }

  public SimplePager getEventsTablePager() {
    return eventsTablePager;
  }
 
  public SingleSelectionModel<EventProxy> getSelectionModel() {
    return selectionModel;
  }

 
}
TOP

Related Classes of com.kepennar.application.client.ui.EventsList$EventsListDataProvider

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.