Added SortedCellTable

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/widgets/workspace-explorer@128989 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2016-06-06 12:48:11 +00:00
parent d2f5408c7b
commit ea54b0aabb
3 changed files with 418 additions and 76 deletions

View File

@ -3,7 +3,6 @@ package org.gcube.portlets.widgets.wsexplorer.client.view.grid;
import java.util.List;
import org.gcube.portlets.widgets.wsexplorer.client.event.ClickItemEvent;
import org.gcube.portlets.widgets.wsexplorer.client.resources.CellTableResources;
import com.github.gwtbootstrap.client.ui.CellTable;
import com.github.gwtbootstrap.client.ui.Pagination;
@ -33,8 +32,8 @@ import com.google.gwt.view.client.SingleSelectionModel;
*/
public abstract class AbstractItemsCellTable<T> {
protected CellTable<T> cellTable;
protected ListDataProvider<T> dataProvider = new ListDataProvider<>();
protected SortedCellTable<T> sortedCellTable;
// protected ListDataProvider<T> dataProvider = new ListDataProvider<>();
protected T itemContextMenu = null;
protected boolean showMoreInfo;
protected final SingleSelectionModel<T> ssm;
@ -58,19 +57,19 @@ public abstract class AbstractItemsCellTable<T> {
public AbstractItemsCellTable(HandlerManager eventBus, boolean fireOnClick) {
this.eventBus = eventBus;
this.showMoreInfo = fireOnClick;
cellTable = new CellTable<T>(1, CellTableResources.INSTANCE);
cellTable.addStyleName("table-explorer");
sortedCellTable = new SortedCellTable<T>();
sortedCellTable.addStyleName("table-explorer");
// cellTable.getElement().getStyle().setOverflow(Overflow.HIDDEN);
cellTable.addStyleName("table-explorer-vertical-middle");
cellTable.setStriped(true);
cellTable.setCondensed(true);
cellTable.setWidth("100%", true);
dataProvider.addDataDisplay(cellTable);
sortedCellTable.addStyleName("table-explorer-vertical-middle");
sortedCellTable.setStriped(true);
sortedCellTable.setCondensed(true);
sortedCellTable.setWidth("100%", true);
// dataProvider.addDataDisplay(sortedCellTable);
// initTable(cellTable, null, null);
cellTable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
sortedCellTable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
ssm = new SingleSelectionModel<>();
cellTable.setSelectionModel(ssm);
sortedCellTable.setSelectionModel(ssm);
ssm.addSelectionChangeHandler(new Handler() {
@Override
public void onSelectionChange(final SelectionChangeEvent event)
@ -85,7 +84,7 @@ public abstract class AbstractItemsCellTable<T> {
}
});
cellTable.addDomHandler(new DoubleClickHandler() {
sortedCellTable.addDomHandler(new DoubleClickHandler() {
@Override
public void onDoubleClick(final DoubleClickEvent event) {
@ -113,9 +112,9 @@ public abstract class AbstractItemsCellTable<T> {
menuWrapper.getElement().getStyle().setBorderStyle(BorderStyle.NONE);
menuWrapper.getElement().getStyle().setZIndex(10000);
menuWrapper.add(options);
cellTable.sinkEvents(Event.ONCONTEXTMENU);
sortedCellTable.sinkEvents(Event.ONCONTEXTMENU);
cellTable.addHandler(new ContextMenuHandler() {
sortedCellTable.addHandler(new ContextMenuHandler() {
@Override
public void onContextMenu(ContextMenuEvent event) {
/*
@ -153,16 +152,16 @@ public abstract class AbstractItemsCellTable<T> {
public void updateItems(List<T> items, boolean removeOldItems) {
if(removeOldItems){
dataProvider.getList().clear();
// dataProvider.getList().clear();
ssm.clear();
}
dataProvider.getList().addAll(items);
dataProvider.flush();
dataProvider.refresh();
sortedCellTable.setList(items);
// dataProvider.flush();
// dataProvider.refresh();
cellTable.setPageSize(items.size()+1);
cellTable.redraw();
sortedCellTable.setPageSize(items.size()+1);
sortedCellTable.redraw();
}
/**
@ -171,7 +170,7 @@ public abstract class AbstractItemsCellTable<T> {
* @return the cell tables
*/
public CellTable<T> getCellTable() {
return cellTable;
return sortedCellTable;
}
/**
@ -180,16 +179,6 @@ public abstract class AbstractItemsCellTable<T> {
* @return the data provider
*/
public ListDataProvider<T> getDataProvider() {
return dataProvider;
}
/**
* Sets the data provider.
*
* @param dataProvider
* the new data provider
*/
public void setDataProvider(ListDataProvider<T> dataProvider) {
this.dataProvider = dataProvider;
return sortedCellTable.getDataProvider();
}
}

View File

@ -21,6 +21,7 @@ import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.Cell.Context;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.ImageResourceCell;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.Style.Unit;
@ -31,7 +32,8 @@ import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.cellview.client.ColumnSortList.ColumnSortInfo;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Image;
@ -109,7 +111,7 @@ public class ItemsTable<T extends Item> extends AbstractItemsCellTable<T> implem
public void addItems(List<T> items) {
for (int i=0; i<items.size(); i++) {
dataProvider.getList().add(i, items.get(i));
getDataProvider().getList().add(i, items.get(i));
}
}
@ -119,7 +121,7 @@ public class ItemsTable<T extends Item> extends AbstractItemsCellTable<T> implem
*/
@Override
public void initTable(final SimplePager pager, final Pagination pagination) {
cellTable.setEmptyTableWidget(new Label("No data."));
sortedCellTable.setEmptyTableWidget(new Label("No data."));
if(this.displayFields.contains(DISPLAY_FIELD.ICON)){
//ICONS
@ -145,9 +147,9 @@ public class ItemsTable<T extends Item> extends AbstractItemsCellTable<T> implem
}
};
icon.setSortable(false);
cellTable.addColumn(icon, "");
cellTable.setColumnWidth(icon, 25.0, Unit.PX);
// icon.setSortable(false);
sortedCellTable.addColumn(icon, "", false);
sortedCellTable.setColumnWidth(icon, 25.0, Unit.PX);
}
if(this.displayFields.contains(DISPLAY_FIELD.NAME)){
@ -168,23 +170,29 @@ public class ItemsTable<T extends Item> extends AbstractItemsCellTable<T> implem
};
};
name.setSortable(true);
cellTable.addColumn(name, "Name");
// name.setSortable(true);
sortedCellTable.addColumn(name, "Name", true);
// double width = displayFields.size()>1?50:90;
// abstractCellTable.setColumnWidth(name, width, Unit.PCT);
ListHandler<T> nameColumnHandler = new ListHandler<>(dataProvider.getList());
nameColumnHandler.setComparator(name, new Comparator<T>() {
// ListHandler<T> nameColumnHandler = new ListHandler<>(dataProvider.getList());
// nameColumnHandler.setComparator(name, new Comparator<T>() {
// @Override
// public int compare(T o1, T o2) {
//
// return ((Item) o1).getName().compareTo(((Item) o2).getName());
// }
// });
Comparator<T> c = new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
return ((Item) o1).getName().compareTo(((Item) o2).getName());
}
});
};
cellTable.addColumnSortHandler(nameColumnHandler);
sortedCellTable.setComparator(name, c);
}
@ -197,19 +205,27 @@ public class ItemsTable<T extends Item> extends AbstractItemsCellTable<T> implem
}
};
owner.setSortable(true);
cellTable.addColumn(owner, "Owner");
// owner.setSortable(true);
sortedCellTable.addColumn(owner, "Owner", true);
// double width = displayFields.size()==2 ?90:40;
// abstractCellTable.setColumnWidth(owner, width, Unit.PCT);
ListHandler<T> ownerColumnHandler = new ListHandler<>(dataProvider.getList());
/*ListHandler<T> ownerColumnHandler = new ListHandler<>(dataProvider.getList());
ownerColumnHandler.setComparator(owner, new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
return ((Item) o1).getOwner().compareTo(((Item) o2).getOwner());
}
});
cellTable.addColumnSortHandler(ownerColumnHandler);
});*/
Comparator<T> c = new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
return ((Item) o1).getOwner().compareTo(((Item) o2).getOwner());
}
};
sortedCellTable.setComparator(owner, c);
// sortedCellTable.addColumnSortHandler(ownerColumnHandler);
}
@ -224,16 +240,50 @@ public class ItemsTable<T extends Item> extends AbstractItemsCellTable<T> implem
}
};
dateColumn.setSortable(true);
ListHandler<T> dateColumnHandler = new ListHandler<>(dataProvider.getList());
// dateColumn.setSortable(true);
/*ListHandler<T> dateColumnHandler = new ListHandler<>(dataProvider.getList());
dateColumnHandler.setComparator(dateColumn, new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
return ((Item) o1).getCreationDate().compareTo(((Item) o2).getCreationDate());
if(o1 == null || o1.getCreationDate()==null)
return -1;
if(o2 == null || o2.getCreationDate()==null)
return 1;
Date d1 = ((Item) o1).getCreationDate();
Date d2 = ((Item) o2).getCreationDate();
if(d2.after(d1))
return 1;
else
return -1;
}
});
cellTable.addColumnSortHandler(dateColumnHandler);
cellTable.addColumn(dateColumn, "Created");
});*/
Comparator<T> c = new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
if(o1 == null || o1.getCreationDate()==null)
return -1;
if(o2 == null || o2.getCreationDate()==null)
return 1;
Date d1 = ((Item) o1).getCreationDate();
Date d2 = ((Item) o2).getCreationDate();
GWT.log(d1.toString() + "is after "+d2.toString() +" ? "+d2.after(d1));
if(d1.after(d2))
return 1;
else
return -1;
}
};
sortedCellTable.addColumn(dateColumn, "Created", true);
sortedCellTable.setComparator(dateColumn,c);
}
if(displayProperties!=null){
@ -253,10 +303,10 @@ public class ItemsTable<T extends Item> extends AbstractItemsCellTable<T> implem
}
};
textColumn.setSortable(true);
cellTable.addColumn(textColumn, column);
// textColumn.setSortable(true);
sortedCellTable.addColumn(textColumn, column, true);
ListHandler<T> textColumnHandler = new ListHandler<>(dataProvider.getList());
/*ListHandler<T> textColumnHandler = new ListHandler<>(dataProvider.getList());
textColumnHandler.setComparator(textColumn, new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
@ -273,15 +323,39 @@ public class ItemsTable<T extends Item> extends AbstractItemsCellTable<T> implem
String v2 = e2.getGcubeProperties().get(column);
if(v1==null)
return -1;
if(v2==null)
return 1;
if(v2==null)
return -1;
return v1.compareToIgnoreCase(v2);
}
});
});*/
cellTable.addColumnSortHandler(textColumnHandler);
Comparator<T> c = new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
if(!(o1 instanceof Item))
return -1;
if(!(o2 instanceof Item))
return 1;
Item e1 = o1;
Item e2 = o2;
String v1 = e1.getGcubeProperties().get(column);
String v2 = e2.getGcubeProperties().get(column);
if(v1==null)
return 1;
if(v2==null)
return -1;
return v1.compareToIgnoreCase(v2);
}
};
sortedCellTable.setComparator(textColumn, c);
}
}
@ -324,24 +398,42 @@ public class ItemsTable<T extends Item> extends AbstractItemsCellTable<T> implem
}
};
showGcubeInfoClm.setSortable(false);
cellTable.addColumn(showGcubeInfoClm, "");
cellTable.setColumnWidth(showGcubeInfoClm, 32.0, Unit.PX);
// showGcubeInfoClm.setSortable(false);
sortedCellTable.addColumn(showGcubeInfoClm, "", false);
sortedCellTable.setColumnWidth(showGcubeInfoClm, 32.0, Unit.PX);
}
if(startSortByColumn!=null)
switch (startSortByColumn) {
case NAME:
if(this.displayFields.contains(DISPLAY_FIELD.NAME))
cellTable.getColumnSortList().push(name);
if(this.displayFields.contains(DISPLAY_FIELD.NAME)){
// sortedCellTable.setInitialSortColumn(name);
// sortedCellTable.getColumnSortList().clear();
// sortedCellTable.getColumnSortList().push(name);
// ColumnSortEvent.fire(sortedCellTable, sortedCellTable.getColumnSortList());
}
break;
case OWNER:
if(this.displayFields.contains(DISPLAY_FIELD.OWNER))
cellTable.getColumnSortList().push(owner);
if(this.displayFields.contains(DISPLAY_FIELD.OWNER)){
sortedCellTable.setInitialSortColumn(owner);
// sortedCellTable.getColumnSortList().clear();
// sortedCellTable.getColumnSortList().push(owner);
// ColumnSortEvent.fire(sortedCellTable, sortedCellTable.getColumnSortList());
}
break;
case CREATION_DATE:
if(this.displayFields.contains(DISPLAY_FIELD.CREATION_DATE))
cellTable.getColumnSortList().push(dateColumn);
if(this.displayFields.contains(DISPLAY_FIELD.CREATION_DATE)){
sortedCellTable.setDefaultSortOrder(dateColumn, false); // sorts ascending on first click
sortedCellTable.setInitialSortColumn(dateColumn);
// sortedCellTable.getColumnSortList().clear();
//// cellTable.getColumnSortList().push(dateColumn);
//// cellTable.getColumnSortList().push(dateColumn);
//
// int columnIndex = sortedCellTable.getColumnIndex(dateColumn);
// setSortedColumn(columnIndex, false);
// cellTable.getColumnSortList().push(dateColumn);
// ColumnSortEvent.fire(cellTable, cellTable.getColumnSortList());
}
break;
default:
break;
@ -355,6 +447,29 @@ public class ItemsTable<T extends Item> extends AbstractItemsCellTable<T> implem
});*/
}
/**
* Displays the appropriate sorted icon in the header of the column for the given index.
*
* @param columnIndex
* of the column to mark as sorted
* @param ascending
* <code>true</code> for ascending icon, <code>false</code> for descending icon
*/
public void setSortedColumn(int columnIndex, boolean ascending) {
GWT.log("Column index: "+columnIndex);
GWT.log("ascending: "+ascending);
Column<T, ?> column = sortedCellTable.getColumn(columnIndex);
if (column != null && column.isSortable()) {
ColumnSortInfo info = sortedCellTable.getColumnSortList().push(column);
// ColumnSortEvent.fire(cellTable, cellTable.getColumnSortList());
GWT.log("info.isAscending(): "+info.isAscending());
if (info.isAscending() != ascending) {
sortedCellTable.getColumnSortList().push(column);
ColumnSortEvent.fire(sortedCellTable, sortedCellTable.getColumnSortList());
}
}
}
/**
* Sets the display fields.
*
@ -378,9 +493,9 @@ public class ItemsTable<T extends Item> extends AbstractItemsCellTable<T> implem
* Reset columns table.
*/
public void reInitColumnsTable(){
int count = cellTable.getColumnCount();
int count = sortedCellTable.getColumnCount();
for(int i=0;i<count;i++){
cellTable.removeColumn(0);
sortedCellTable.removeColumn(0);
}
initTable(null, null);
}

View File

@ -0,0 +1,238 @@
/**
*
*/
package org.gcube.portlets.widgets.wsexplorer.client.view.grid;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.github.gwtbootstrap.client.ui.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.ColumnSortList.ColumnSortInfo;
import com.google.gwt.user.cellview.client.Header;
import com.google.gwt.view.client.ListDataProvider;
/**
* The Class SortedCellTable.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Jun 6, 2016
* @param <T> the generic type
*/
public class SortedCellTable<T> extends CellTable<T> {
/**
* To keep track of the currently sorted column
*/
private Column<T, ?> currentlySortedColumn;
/**
* Tells us which way to sort a column initially
*/
private Map<Column<T, ?>, Boolean> defaultSortOrderMap = new HashMap<Column<T, ?>, Boolean>();
/**
* Comparators associated with their columns
*/
private Map<Column<T, ?>, Comparator<T>> comparators = new HashMap<Column<T, ?>, Comparator<T>>();
/**
* Column to sort when the data provider's list is refreshed using
* {@link SortedCellTable#setList(List)}
*/
private Column<T, ?> initialSortColumn;
/**
* Data provider we will attach to this table
*/
private ListDataProvider<T> dataProvider;
/**
* Special column sorting handler that will allow us to do more controlled
* sorting
*/
ListHandler<T> columnSortHandler;
/**
* Instantiates a new sorted cell table.
*/
public SortedCellTable() {
super();
dataProvider = new ListDataProvider<T>();
dataProvider.addDataDisplay(this);
columnSortHandler = new ListHandler<T>(dataProvider.getList()) {
@Override
public void onColumnSort(ColumnSortEvent event) {
@SuppressWarnings("unchecked")
Column<T, ?> column = (Column<T, ?>) event.getColumn();
if (column == null) {
return;
}
if (column.equals(currentlySortedColumn)) {
// Default behavior
super.onColumnSort(event);
}
else {
// Initial sort; look up which direction we need
final Comparator<T> comparator = comparators.get(column);
if (comparator == null) {
return;
}
Boolean ascending = defaultSortOrderMap.get(column);
if (ascending == null || ascending) {
// Default behavior
super.onColumnSort(event);
}
else {
// Sort the column descending
Collections.sort(getList(), new Comparator<T>() {
public int compare(T o1, T o2) {
return -comparator.compare(o1, o2);
}
});
// Set the proper arrow in the header
getColumnSortList().push(new ColumnSortInfo(column, false));
}
currentlySortedColumn = column;
}
}
@Override
public void setComparator(Column<T, ?> column, Comparator<T> comparator) {
comparators.put(column, comparator);
super.setComparator(column, comparator);
}
};
addColumnSortHandler(columnSortHandler);
}
/**
* Adds a column to the table and sets its sortable state.
*
* @param column the column
* @param headerName the header name
* @param sortable the sortable
*/
public void addColumn(Column<T, ?> column, String headerName, boolean sortable) {
addColumn(column, headerName);
column.setSortable(sortable);
if (sortable) {
defaultSortOrderMap.put(column, true);
}
}
/* (non-Javadoc)
* @see com.google.gwt.user.cellview.client.AbstractCellTable#addColumn(com.google.gwt.user.cellview.client.Column, java.lang.String)
*/
public void addColumn(Column<T, ?> column, String headerName) {
super.addColumn(column, headerName);
}
/**
* Adds a column to the table and sets its sortable state.
*
* @param column the column
* @param header the header
* @param sortable the sortable
*/
public void addColumn(Column<T, ?> column, Header<?> header, boolean sortable) {
addColumn(column, header);
column.setSortable(sortable);
if (sortable) {
defaultSortOrderMap.put(column, true);
}
}
/**
* Sets the column to sort when the data list is reset using
* {@link SortedCellTable#setList(List)}.
*
* @param column the column
*/
public void setInitialSortColumn(Column<T, ?> column) {
initialSortColumn = column;
}
/**
* Sets a comparator to use when sorting the given column.
*
* @param column the column
* @param comparator the comparator
*/
public void setComparator(Column<T, ?> column, Comparator<T> comparator) {
columnSortHandler.setComparator(column, comparator);
}
/**
* Sets the sort order to use when this column is clicked and it was not
* previously sorted.
*
* @param column the column
* @param ascending the ascending
*/
public void setDefaultSortOrder(Column<T, ?> column, boolean ascending) {
defaultSortOrderMap.put(column, ascending);
}
/**
* Sets the table's data provider list and sorts the table based on the
* column given in {@link SortedCellTable#setInitialSortColumn(Column)}.
*
* @param list the new list
*/
public void setList(List<T> list) {
dataProvider.getList().clear();
if (list != null) {
/*for (T t : list) {
dataProvider.getList().add(t);
}*/
dataProvider.getList().addAll(list);
}
// Do a first-time sort based on which column was set in
// setInitialSortColumn()
if (initialSortColumn != null) {
Collections.sort(dataProvider.getList(), new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
return (defaultSortOrderMap.get(initialSortColumn) ? 1 : -1) * comparators.get(initialSortColumn).compare(o1, o2);
}
});
// Might as well get the little arrow on the header to make it
// official
getColumnSortList().push(
new ColumnSortInfo(
initialSortColumn,
defaultSortOrderMap.get(initialSortColumn)));
currentlySortedColumn = initialSortColumn;
}
}
/**
* @return the dataProvider
*/
public ListDataProvider<T> getDataProvider() {
return dataProvider;
}
}