package org.gcube.portlets.user.geoportaldataentry.client.ui; import java.util.List; import org.gcube.application.geoportalcommon.shared.products.ConcessioneDV; import org.gcube.portlets.user.geoportaldataentry.client.ConstantsGeoPortalDataEntryApp.RECORD_TYPE; import org.gcube.portlets.user.geoportaldataentry.client.GeoPortalDataEntryApp; import org.gcube.portlets.user.geoportaldataentry.client.ui.table.ItemsTable; import org.gcube.portlets.user.geoportaldataentry.client.ui.table.ItemsTable.DISPLAY_FIELD; import org.gcube.portlets.user.geoportaldataentry.client.ui.table.SortedCellTable; import org.gcube.portlets.user.geoportaldataentry.client.ui.utils.LoaderIcon; import org.gcube.portlets.user.geoportaldataentry.shared.SearchedData; import com.google.gwt.core.client.GWT; import com.google.gwt.event.shared.HandlerManager; import com.google.gwt.user.cellview.client.SimplePager; import com.google.gwt.user.cellview.client.SimplePager.TextLocation; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.ui.FlowPanel; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.VerticalPanel; 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.MultiSelectionModel; import com.google.gwt.view.client.Range; import com.google.gwt.view.client.SelectionModel; import com.google.gwt.view.client.SingleSelectionModel; /** * The Class GeonaRecordsPaginatedView. * * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * * Aug 4, 2021 */ public class GeonaRecordsPaginatedView { private static final int ITEM_START_INDEX = 0; private static final int ITEMS_PER_PAGE = 30; private VerticalPanel vPanel = new VerticalPanel(); private FlowPanel pagerPanel = new FlowPanel(); private Boolean initClassFirstRangeChanged = false; private ItemsTable itemsTable; private MyCustomDataProvider dataProvider = new MyCustomDataProvider(); protected Widget orginalLoadingIndicator = null; private LoaderIcon loadingPanel = new LoaderIcon("Loading data..."); private int serverStartIndex; private HandlerManager eventBus; private RECORD_TYPE recordType; /** * Instantiates a new geona records paginated view. * * @param eventbus the eventbus * @param recordType the record type * @param displayFields the display fields * @param sortByField the sort by field */ public GeonaRecordsPaginatedView(HandlerManager eventbus, RECORD_TYPE recordType, DISPLAY_FIELD[] displayFields, DISPLAY_FIELD sortByField) { this.recordType = recordType; this.initClassFirstRangeChanged = true; this.eventBus = eventbus; itemsTable = new ItemsTable(eventbus, displayFields, sortByField); itemsTable.initTable(null, null, dataProvider); orginalLoadingIndicator = itemsTable.getCellTable().getLoadingIndicator(); initPagination(ITEMS_PER_PAGE); // loadNewPage(ITEM_START_INDEX, ITEMS_PER_PAGE, false); loadItemsForStatus(recordType); } /** * Gets the asycn data provider. * * @return the asycn data provider */ public AsyncDataProvider getAsycnDataProvider() { return (AsyncDataProvider) getCellTable().getDataProvider(); } /** * Gets the cell tale. * * @return the cell tale */ private SortedCellTable getCellTable() { return itemsTable.getCellTable(); } /** * Inits the pagination. * * @param itemsPerPage the items per page */ public void initPagination(int itemsPerPage) { SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class); SimplePager pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true); pager.setDisplay(getCellTable()); pager.setPageSize(itemsPerPage); pager.getElement().getStyle().setProperty("margin", "auto"); vPanel.add(loadingPanel); vPanel.add(getCellTable()); vPanel.getElement().addClassName("vPanel"); pagerPanel.add(pager); } /** * Gets the pager panel. * * @return the pager panel */ public VerticalPanel getCellPanel() { return vPanel; } /** * Set the panel in loading mode. * * @param show the show */ protected void showLoading(boolean show) { loadingPanel.setVisible(show); } /** * Gets the pager panel. * * @return the pager panel */ public FlowPanel getPagerPanel() { return pagerPanel; } /** * Load new page. * * @param startIdx the start idx * @param limit the limit * @param resetStore the reset store */ private void loadNewPage(final int startIdx, final int limit, final boolean resetStore) { // initFirstRangeChanged = resetStore; GWT.log("loadNewPage with parameters [startIdx: " + startIdx + ", limit: " + limit + ", resetStore:" + resetStore + "]"); // showLoading(true); int newStartIndex = startIdx; if (resetStore) { GWT.log("Cleaning all data..."); newStartIndex = 0; serverStartIndex = 0; GWT.log("Store reset performed start index is: " + newStartIndex); getAsycnDataProvider().updateRowCount(ITEMS_PER_PAGE, false); } loadItemsForStatus(newStartIndex, limit, serverStartIndex); } /** * Load items for status. * * @param recordType the record type */ public void loadItemsForStatus(RECORD_TYPE recordType) { this.recordType = recordType; getCellTable().setVisibleRangeAndClearData(new Range(ITEM_START_INDEX, ITEMS_PER_PAGE), false); loadNewPage(ITEM_START_INDEX, ITEMS_PER_PAGE, true); } /** * Sets the new page result. * * @param result the new new page result */ private void setNewPageResult(SearchedData result) { GWT.log("setNewPageResult: " + result); serverStartIndex = result.getServerEndIndex(); SelectionModel sm = getCellTable().getSelectionModel(); if (sm instanceof SingleSelectionModel) { SingleSelectionModel ssm = (SingleSelectionModel) sm; ssm.clear(); } else if (sm instanceof MultiSelectionModel) { MultiSelectionModel msm = (MultiSelectionModel) sm; msm.clear(); } getAsycnDataProvider().updateRowCount((int) result.getTotalItems(), true); getAsycnDataProvider().updateRowData(result.getClientStartIndex(), result.getData()); if (result.getData().size() == 0) { getCellTable().setLoadingIndicator(new Label("No data")); } else { getCellTable().setLoadingIndicator(orginalLoadingIndicator); } GWT.log("Updating row data startIndex: " + result.getClientStartIndex() + " children size: " + result.getData().size()); GWT.log("getAsycnDataProvider().getDataDisplays().size(): " + getCellTable().getRowCount()); if (result.isServerSearchFinished()) { GWT.log("Search finished!!!"); getAsycnDataProvider().updateRowCount(getCellTable().getRowCount(), true); } // initFirstRangeChanged = false; } /** * Load items for status. * * @param the generic type * @param offset the offset * @param limit the limit * @param serverIndex the server index */ private void loadItemsForStatus(int newStartIndex, int limit, int serverIndex) { showLoading(true); GWT.log("calling getDataForStatus with parameters [startIndex: " + newStartIndex + ", limit: " + limit + ", serverIndex:" + serverIndex + "]"); GeoPortalDataEntryApp.greetingService.getListConcessioni(newStartIndex, limit, new AsyncCallback() { @Override public void onFailure(Throwable caught) { showLoading(false); } @Override public void onSuccess(SearchedData result) { showLoading(false); setNewPageResult(result); } }); } /** * Select items. * * @param select the select * @param limitToPage the limit to page */ public void selectItems(boolean select, boolean limitToPage) { SortedCellTable table = getCellTable(); int rowSize = table.getVisibleItemCount(); for (int i = 0; i < rowSize; i++) { ConcessioneDV item = table.getVisibleItem(i); itemsTable.getSelectionModel().setSelected(item, select); } } /** * Gets the select items. * * @return the select items */ public List getSelectItems() { return itemsTable.getSelectedItems(); } /** * A custom {@link AsyncDataProvider}. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Jul 5, 2017 * @param the generic type */ public class MyCustomDataProvider extends AsyncDataProvider { /** * {@link #onRangeChanged(HasData)} is called when the table requests a new * range of data. You can push data back to the displays using * {@link #updateRowData(int, List)}. * * @param display the display */ @Override public void onRangeChanged(HasData display) { // Get the new range. final Range range = display.getVisibleRange(); int start = range.getStart(); int length = range.getLength(); if (initClassFirstRangeChanged) { GWT.log("initClassFirstRangeChanged is true.. returning"); initClassFirstRangeChanged = false; return; } GWT.log("Range changed: " + start + " " + length + " visible count: " + display.getVisibleItemCount()); loadNewPage(start, length, false); //eventBus.fireEvent(new TableRangeViewChangedEvent(start, length)); } } }