workspace-explorer/src/main/java/org/gcube/portlets/widgets/wsexplorer/client/view/WorkspaceExplorerPaginated....

280 lines
8.6 KiB
Java

/**
*
*/
package org.gcube.portlets.widgets.wsexplorer.client.view;
import java.util.ArrayList;
import java.util.List;
import org.gcube.portlets.widgets.wsexplorer.client.Util;
import org.gcube.portlets.widgets.wsexplorer.client.WorkspaceExplorerConstants;
import org.gcube.portlets.widgets.wsexplorer.client.view.grid.SortedCellTable;
import org.gcube.portlets.widgets.wsexplorer.client.view.grid.ItemsTable.DISPLAY_FIELD;
import org.gcube.portlets.widgets.wsexplorer.shared.FilterCriteria;
import org.gcube.portlets.widgets.wsexplorer.shared.Item;
import org.gcube.portlets.widgets.wsexplorer.shared.ItemType;
import com.github.gwtbootstrap.client.ui.constants.AlertType;
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.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.Range;
/**
* The Class WorkspaceExplorerPaginated.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Jul 5, 2017
*/
public class WorkspaceExplorerPaginated extends WorkspaceExplorer{
private VerticalPanel vPanel = new VerticalPanel();
private static WorkspaceExplorerPaginated INSTANCE;
//private ListDataProvider<Item> dataProvider = new ListDataProvider<Item>();
private static MyCustomDataProvider<Item> dataProvider = new MyCustomDataProvider<Item>();
protected boolean loadGcubeProperties = false;
/**
* Instantiates a new workspace explorer paginated.
*
* @param eventBus the event bus
* @param filterCriteria the filter criteria
* @param showableTypes the showable types
* @param selectableTypes the selectable types
* @param displayProperties the display properties
* @param showGcubeInfo the show gcube info
* @param sortByColumn the sort by column
* @param fields the fields
*/
public WorkspaceExplorerPaginated(
HandlerManager eventBus, FilterCriteria filterCriteria,
ItemType[] showableTypes, ItemType[] selectableTypes,
List<String> displayProperties, boolean showGcubeInfo,
DISPLAY_FIELD sortByColumn, DISPLAY_FIELD[] fields) {
super(eventBus, filterCriteria, showableTypes, selectableTypes, displayProperties, showGcubeInfo, sortByColumn, dataProvider);
INSTANCE = this;
}
/**
* Gets the asycn data provider.
*
* @return the asycn data provider
*/
public AsyncDataProvider<Item> getAsycnDataProvider(){
return (AsyncDataProvider<Item>) getCellTable().getDataProvider();
//return new MyCustomDataProvider<Item>();
}
/**
* Gets the cell tale.
*
* @return the cell tale
*/
public SortedCellTable<Item> getCellTable(){
return getItTables().getCellTable();
}
/**
* Load folder.
*
* @param item the item
* @param loadGcubeProperties the load gcube properties
* @param startIdx the start idx
* @param limit the limit
* @throws Exception the exception
*/
public void loadFolder(final Item item, final boolean loadGcubeProperties, final int startIdx, final int limit) throws Exception {
GWT.log("loading folder data");
//super.loadFolder(item, loadGcubeProperties);
this.loadGcubeProperties = loadGcubeProperties;
setLoading();
if(!item.isFolder())
throw new Exception("Item is not a folder");
if(item.getId()==null || item.getId().isEmpty())
throw new Exception("Item id is null or empty");
// we make a copy of showable types
final List<ItemType> showableTypesParam = new ArrayList<ItemType>(showableTypes);
// we get sure that folders are displayed
for (ItemType folder : Util.FOLDERS) {
if (!showableTypesParam.contains(folder))
showableTypesParam.add(folder);
}
final boolean purgeEmpyFolders = !showEmptyFolders;
if(getDisplayingFolderItem()==null || getDisplayingFolderItem().getId()!=item.getId()){
WorkspaceExplorerConstants.workspaceNavigatorService.getFolderChildrenCount(item, new AsyncCallback<Integer>() {
@Override
public void onFailure(Throwable caught) {
}
@Override
public void onSuccess(Integer result) {
GWT.log("Folder Children count: "+result);
getAsycnDataProvider().updateRowCount(result, false);
//final Range range = display.getVisibleRange();
perfomGetFolderChildren(item, loadGcubeProperties, startIdx, limit, purgeEmpyFolders, showableTypesParam);
}
});
}else
perfomGetFolderChildren(item, loadGcubeProperties, startIdx, limit, purgeEmpyFolders, showableTypesParam);
}
/**
* Perfom get folder children.
*
* @param item the item
* @param loadGcubeProperties the load gcube properties
* @param startIdx the start idx
* @param limit the limit
* @param purgeEmpyFolders the purge empy folders
* @param showableTypesParam the showable types param
*/
private void perfomGetFolderChildren(final Item item, boolean loadGcubeProperties, final int startIdx, int limit, boolean purgeEmpyFolders, List<ItemType> showableTypesParam){
GWT.log("loading workspace folder by item id from server: "+item.getId());
WorkspaceExplorerConstants.workspaceNavigatorService.getFolder(item, showableTypesParam, purgeEmpyFolders, filterCriteria, loadGcubeProperties, startIdx, limit, new AsyncCallback<Item>() {
@Override
public void onFailure(Throwable caught) {
Window.alert(caught.getMessage());
setAlert(caught.getMessage(), AlertType.ERROR);
GWT.log("Error loading workspace folder from server",caught);
}
@Override
public void onSuccess(Item result) {
if(item.getName()==null || item.getName().isEmpty())
item.setName(result.getName());
GWT.log("Returned "+result.getChildren().size() +" children");
//getItTables().updateItems(result.getChildren(), true);
getAsycnDataProvider().updateRowData(startIdx, result.getChildren());
getCellTable().setPageSize(result.getChildren().size()+1);
//cellList.setVisibleRange(startIdx, result.getChildren().size());
getCellTable().redraw();
GWT.log("cellList size: "+getCellTable().getRowCount());
setDisplayingFolderItem(result);
}
});
}
/**
* Inits the pagination.
*
* @param itemsPerPage the items per page
*/
public void initPagination(int itemsPerPage){
//dataProvider.updateRowCount(100, true);
// Add the cellList to the dataProvider.
//asyncDataProvider.addDataDisplay(cellTable);
// Create paging controls.
SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
SimplePager pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
pager.setPageSize(itemsPerPage);
pager.setDisplay(getCellTable());
vPanel.add(getCellTable());
vPanel.add(pager);
}
/**
* Gets the pager panel.
*
* @return the pager panel
*/
public VerticalPanel getPagerPanel(){
return vPanel;
}
/**
* A custom {@link AsyncDataProvider}.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Jul 5, 2017
* @param <T> the generic type
*/
public static class MyCustomDataProvider<T> extends AsyncDataProvider<T> {
/**
* {@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<T> display) {
// Get the new range.
final Range range = display.getVisibleRange();
// We are creating fake data. Normally, the data will come
// from a
// server.
// int start = range.getStart();
// int length = range.getLength();
// List<Item> newData = new ArrayList<Item>();
// for (int i = start; i < start + length; i++) {
// newData.add(new Item("Item "+i, "Item "+i, false));
// }
// // Push the data to the displays. AsyncDataProvider will
// // only update
// // displays that are within range of the data.
// updateRowData(start, newData);
int start = range.getStart();
int length = range.getLength();
GWT.log("Range changed: "+start +" "+length);
// try {
// GWT.log("qui");
// INSTANCE.loadFolder(INSTANCE.getItemSelected(), INSTANCE.loadGcubeProperties, start, length);
// GWT.log("qua");
// }
// catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// Item item = new Item(WorkspaceExplorerPaginated.this.getF, folderName, true);
// WorkspaceExplorerPaginated.this.loadFolder(WorkspaceExplorerPaginated.this.getItemSelected(), super.get,start, length);
}
/**
*
*/
public MyCustomDataProvider() {
// TODO Auto-generated constructor stub
}
}
}