488 lines
14 KiB
Java
488 lines
14 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.widgets.ckancontentmoderator.client.ui.table;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.Comparator;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
import org.gcube.portlets.widgets.ckancontentmoderator.client.ContentModeratorWidgetConstants;
|
|
import org.gcube.portlets.widgets.ckancontentmoderator.client.resources.ContentModeratorWidgetResources;
|
|
import org.gcube.portlets.widgets.ckancontentmoderator.client.ui.SelectionItem;
|
|
import org.gcube.portlets.widgets.ckancontentmoderator.shared.CatalogueDataset;
|
|
|
|
import com.github.gwtbootstrap.client.ui.Pagination;
|
|
import com.google.gwt.cell.client.ButtonCell;
|
|
import com.google.gwt.cell.client.DateCell;
|
|
import com.google.gwt.core.shared.GWT;
|
|
import com.google.gwt.event.shared.HandlerManager;
|
|
import com.google.gwt.i18n.client.DateTimeFormat;
|
|
import com.google.gwt.resources.client.ImageResource;
|
|
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;
|
|
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;
|
|
import com.google.gwt.user.client.ui.Label;
|
|
import com.google.gwt.view.client.AbstractDataProvider;
|
|
import com.google.gwt.view.client.ListDataProvider;
|
|
|
|
// TODO: Auto-generated Javadoc
|
|
/**
|
|
* The Class ItemsTable.
|
|
*
|
|
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
|
*
|
|
* Jun 15, 2021
|
|
* @param <T> the generic type
|
|
*/
|
|
public class ItemsTable<T extends CatalogueDataset> extends AbstractItemsCellTable<T> implements SelectionItem {
|
|
|
|
// private Column<T, ImageResource> icon;
|
|
private TextColumn<T> type;
|
|
private TextColumn<T> name;
|
|
private TextColumn<T> title;
|
|
private TextColumn<T> author;
|
|
public DateTimeFormat dtformat = DateTimeFormat.getFormat("dd MMM hh:mm aaa yyyy");
|
|
public ImageResource info = ContentModeratorWidgetResources.ICONS.infoSquare();
|
|
|
|
private AbstractDataProvider<T> dataProvider;
|
|
|
|
/**
|
|
* The Enum DISPLAY_FIELD.
|
|
*
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Feb 1, 2016
|
|
*/
|
|
public static enum DISPLAY_FIELD {
|
|
TYPE, NAME, TITLE, AUTHOR, CREATED
|
|
};
|
|
|
|
private List<DISPLAY_FIELD> displayFields;
|
|
private Column<T, Date> dateColumn;
|
|
// private List<String> displayProperties;
|
|
// private boolean showGcubeItemsInfo;
|
|
private DISPLAY_FIELD startSortByColumn;
|
|
private boolean isAsyncronusTable;
|
|
|
|
|
|
/**
|
|
* Instantiates a new items table.
|
|
*
|
|
* @param eventBus the event bus
|
|
* @param fields the fields
|
|
* @param startSortByColumn the start sort by column
|
|
*/
|
|
public ItemsTable(HandlerManager eventBus, DISPLAY_FIELD[] fields,
|
|
DISPLAY_FIELD startSortByColumn) {
|
|
this.eventBus = eventBus;
|
|
this.startSortByColumn = startSortByColumn;
|
|
setDisplayFields(fields);
|
|
}
|
|
|
|
/**
|
|
* Update items.
|
|
*
|
|
* @param items the items
|
|
* @param removeOldItems the remove old items
|
|
*/
|
|
/*
|
|
* (non-Javadoc)
|
|
*
|
|
* @see
|
|
* org.gcube.portlets.widgets.wsexplorer.client.view.grid.AbstractItemsCellTable
|
|
* #updateItems(java.util.List, boolean)
|
|
*/
|
|
public void updateItems(List<T> items, boolean removeOldItems) {
|
|
super.updateItems(items, removeOldItems);
|
|
}
|
|
|
|
/**
|
|
* Adds the items.
|
|
*
|
|
* @param items the items
|
|
*/
|
|
public void addItems(List<T> items) {
|
|
super.addItems(items);
|
|
}
|
|
|
|
/**
|
|
* Inits the table.
|
|
*
|
|
* @param pager the pager
|
|
* @param pagination the pagination
|
|
* @param dataProvider the data provider
|
|
*/
|
|
/*
|
|
* (non-Javadoc)
|
|
*
|
|
* @see org.gcube.portlets.admin.gcubereleases.client.view.AbstractPackageTable#
|
|
* initTable(com.google.gwt.user.cellview.client.AbstractCellTable,
|
|
* com.google.gwt.user.cellview.client.SimplePager,
|
|
* com.github.gwtbootstrap.client.ui.Pagination)
|
|
*/
|
|
@Override
|
|
public void initTable(final SimplePager pager, final Pagination pagination, AbstractDataProvider<T> dataProvider) {
|
|
this.dataProvider = dataProvider;
|
|
initAbstractTable(eventBus, fireEventOnClick, dataProvider, ContentModeratorWidgetConstants.ITEMS_PER_PAGE);
|
|
this.dataProvider.addDataDisplay(sortedCellTable);
|
|
|
|
// this.isAsyncronusTable = dataProvider instanceof
|
|
// AbstractDataProvider?true:false;
|
|
this.isAsyncronusTable = dataProvider instanceof ListDataProvider ? false : true;
|
|
|
|
// sortedCellTable.setEmptyTableWidget(new
|
|
// Label(WorkspaceExplorerConstants.WORKSPACE_FOLDER_EMPTY_MESSAGE));
|
|
setEmptyTableMessage(ContentModeratorWidgetConstants.NO_DATA);
|
|
|
|
if (this.displayFields.contains(DISPLAY_FIELD.TYPE)) {
|
|
|
|
// NAME
|
|
type = new TextColumn<T>() {
|
|
@Override
|
|
public String getValue(T object) {
|
|
if (object == null)
|
|
return "";
|
|
return ((CatalogueDataset) object).getType();
|
|
}
|
|
|
|
// ADDING TOOLTIP
|
|
@Override
|
|
public void render(com.google.gwt.cell.client.Cell.Context context, T object, SafeHtmlBuilder sb) {
|
|
if (object == null)
|
|
return;
|
|
sb.appendHtmlConstant("<div title=\"" + ((CatalogueDataset) object).getType() + "\">");
|
|
super.render(context, object, sb);
|
|
sb.appendHtmlConstant("</div>");
|
|
};
|
|
};
|
|
|
|
sortedCellTable.addColumn(name, "Type", true);
|
|
|
|
if (!isAsyncronusTable) {
|
|
Comparator<T> c = new Comparator<T>() {
|
|
@Override
|
|
public int compare(T o1, T o2) {
|
|
return ((CatalogueDataset) o1).getType().compareTo(((CatalogueDataset) o2).getType());
|
|
}
|
|
};
|
|
|
|
sortedCellTable.setComparator(type, c);
|
|
}
|
|
|
|
}
|
|
|
|
// if (this.displayFields.contains(DISPLAY_FIELD.ICON)) {
|
|
// // ICONS
|
|
// ImageResourceCell iconResourceCell = new ImageResourceCell() {
|
|
// public Set<String> getConsumedEvents() {
|
|
// HashSet<String> events = new HashSet<String>();
|
|
// events.add("click");
|
|
// return events;
|
|
// }
|
|
// };
|
|
//
|
|
// icon = new Column<T, ImageResource>(iconResourceCell) {
|
|
// @Override
|
|
// public ImageResource getValue(T dataObj) {
|
|
// return Util.getImage(dataObj);
|
|
// }
|
|
//
|
|
// /*
|
|
// * (non-Javadoc)
|
|
// *
|
|
// * @see
|
|
// * com.google.gwt.user.cellview.client.Column#render(com.google.gwt.cell.client.
|
|
// * Cell.Context, java.lang.Object,
|
|
// * com.google.gwt.safehtml.shared.SafeHtmlBuilder)
|
|
// */
|
|
// @Override
|
|
// public void render(Context context, T object, SafeHtmlBuilder sb) {
|
|
// super.render(context, object, sb);
|
|
//
|
|
// }
|
|
// };
|
|
//// icon.setSortable(false);
|
|
// sortedCellTable.addColumn(icon, "", false);
|
|
// sortedCellTable.setColumnWidth(icon, 32.0, Unit.PX);
|
|
// }
|
|
|
|
if (this.displayFields.contains(DISPLAY_FIELD.NAME)) {
|
|
|
|
// NAME
|
|
name = new TextColumn<T>() {
|
|
@Override
|
|
public String getValue(T object) {
|
|
if (object == null)
|
|
return "";
|
|
return ((CatalogueDataset) object).getName();
|
|
}
|
|
|
|
// ADDING TOOLTIP
|
|
@Override
|
|
public void render(com.google.gwt.cell.client.Cell.Context context, T object, SafeHtmlBuilder sb) {
|
|
if (object == null)
|
|
return;
|
|
sb.appendHtmlConstant("<div title=\"" + ((CatalogueDataset) object).getName() + "\">");
|
|
super.render(context, object, sb);
|
|
sb.appendHtmlConstant("</div>");
|
|
};
|
|
};
|
|
|
|
sortedCellTable.addColumn(name, "Name", true);
|
|
|
|
if (!isAsyncronusTable) {
|
|
Comparator<T> c = new Comparator<T>() {
|
|
@Override
|
|
public int compare(T o1, T o2) {
|
|
return ((CatalogueDataset) o1).getName().compareTo(((CatalogueDataset) o2).getName());
|
|
}
|
|
};
|
|
|
|
sortedCellTable.setComparator(name, c);
|
|
}
|
|
|
|
}
|
|
|
|
if (this.displayFields.contains(DISPLAY_FIELD.TITLE)) {
|
|
|
|
title = new TextColumn<T>() {
|
|
@Override
|
|
public String getValue(T object) {
|
|
if (object == null)
|
|
return "";
|
|
return ((CatalogueDataset) object).getTitle() != null ? ((CatalogueDataset) object).getTitle() : "";
|
|
}
|
|
};
|
|
|
|
sortedCellTable.addColumn(title, "Title", true);
|
|
|
|
if (!isAsyncronusTable) {
|
|
Comparator<T> c = new Comparator<T>() {
|
|
@Override
|
|
public int compare(T o1, T o2) {
|
|
return ((CatalogueDataset) o1).getTitle().compareTo(((CatalogueDataset) o2).getTitle());
|
|
}
|
|
};
|
|
sortedCellTable.setComparator(title, c);
|
|
}
|
|
|
|
}
|
|
|
|
if (this.displayFields.contains(DISPLAY_FIELD.AUTHOR)) {
|
|
|
|
author = new TextColumn<T>() {
|
|
@Override
|
|
public String getValue(T object) {
|
|
if (object == null)
|
|
return "";
|
|
return ((CatalogueDataset) object).getAuthor() != null ? ((CatalogueDataset) object).getAuthor()
|
|
: "";
|
|
}
|
|
};
|
|
|
|
sortedCellTable.addColumn(author, "Title", true);
|
|
|
|
if (!isAsyncronusTable) {
|
|
Comparator<T> c = new Comparator<T>() {
|
|
@Override
|
|
public int compare(T o1, T o2) {
|
|
return ((CatalogueDataset) o1).getAuthor().compareTo(((CatalogueDataset) o2).getAuthor());
|
|
}
|
|
};
|
|
sortedCellTable.setComparator(author, c);
|
|
}
|
|
|
|
}
|
|
|
|
if (this.displayFields.contains(DISPLAY_FIELD.AUTHOR)) {
|
|
|
|
DateCell date = new DateCell(dtformat);
|
|
dateColumn = new Column<T, Date>(date) {
|
|
|
|
@Override
|
|
public Date getValue(T object) {
|
|
if (object == null)
|
|
return null;
|
|
|
|
return new Date(((CatalogueDataset) object).getCreated());
|
|
}
|
|
};
|
|
sortedCellTable.addColumn(dateColumn, "Created", true);
|
|
|
|
if (!isAsyncronusTable) {
|
|
Comparator<T> c = new Comparator<T>() {
|
|
@Override
|
|
public int compare(T o1, T o2) {
|
|
if (o1 == null || o1.getCreated() == 0)
|
|
return -1;
|
|
|
|
if (o2 == null || o2.getCreated() == 0)
|
|
return 1;
|
|
|
|
Date d1 = new Date(((CatalogueDataset) o1).getCreated());
|
|
Date d2 = new Date(((CatalogueDataset) o2).getCreated());
|
|
|
|
// GWT.log(d1.toString() + "is after "+d2.toString() +" ? "+d2.after(d1));
|
|
|
|
if (d1.after(d2))
|
|
return 1;
|
|
else
|
|
return -1;
|
|
}
|
|
};
|
|
GWT.log("date colum sortable");
|
|
sortedCellTable.setComparator(dateColumn, c);
|
|
}
|
|
|
|
}
|
|
|
|
GWT.log("startSortByColumn: " + startSortByColumn);
|
|
|
|
if (startSortByColumn != null)
|
|
switch (startSortByColumn) {
|
|
case NAME:
|
|
if (this.displayFields.contains(DISPLAY_FIELD.NAME)) {
|
|
sortedCellTable.setInitialSortColumn(name);
|
|
}
|
|
break;
|
|
case TITLE:
|
|
if (this.displayFields.contains(DISPLAY_FIELD.TITLE)) {
|
|
sortedCellTable.setInitialSortColumn(title);
|
|
}
|
|
break;
|
|
case AUTHOR:
|
|
if (this.displayFields.contains(DISPLAY_FIELD.AUTHOR)) {
|
|
sortedCellTable.setDefaultSortOrder(dateColumn, false); // sorts ascending on first click
|
|
sortedCellTable.setInitialSortColumn(dateColumn);
|
|
GWT.log("sortedCellTable: " + sortedCellTable);
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
/*
|
|
* final SingleSelectionModel<Item> selectionModel = new
|
|
* SingleSelectionModel<Item>(); selectionModel.addSelectionChangeHandler(new
|
|
* Handler() {
|
|
*
|
|
* @Override public void onSelectionChange(SelectionChangeEvent event) { } });
|
|
*/
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*
|
|
* @param fields the new display fields
|
|
*/
|
|
public void setDisplayFields(DISPLAY_FIELD[] fields) {
|
|
this.displayFields = fields != null && fields.length > 0 ? Arrays.asList(fields)
|
|
: Arrays.asList(DISPLAY_FIELD.values());
|
|
}
|
|
|
|
/**
|
|
* Reset columns table.
|
|
*/
|
|
public void reInitColumnsTable() {
|
|
int count = sortedCellTable.getColumnCount();
|
|
for (int i = 0; i < count; i++) {
|
|
sortedCellTable.removeColumn(0);
|
|
}
|
|
initTable(null, null, dataProvider);
|
|
}
|
|
|
|
/**
|
|
* Gets the display fields.
|
|
*
|
|
* @return the displayFields
|
|
*/
|
|
public List<DISPLAY_FIELD> getDisplayFields() {
|
|
return displayFields;
|
|
}
|
|
|
|
/**
|
|
* The Class ButtonImageCell.
|
|
*
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Feb 1, 2016
|
|
*/
|
|
public class ButtonImageCell extends ButtonCell {
|
|
|
|
/**
|
|
* Render.
|
|
*
|
|
* @param context the context
|
|
* @param value the value
|
|
* @param sb the sb
|
|
*/
|
|
/*
|
|
* (non-Javadoc)
|
|
*
|
|
* @see
|
|
* com.google.gwt.cell.client.AbstractSafeHtmlCell#render(com.google.gwt.cell.
|
|
* client.Cell.Context, java.lang.Object,
|
|
* com.google.gwt.safehtml.shared.SafeHtmlBuilder)
|
|
*/
|
|
@Override
|
|
public void render(com.google.gwt.cell.client.Cell.Context context, String value, SafeHtmlBuilder sb) {
|
|
SafeHtml html = SafeHtmlUtils.fromTrustedString(new Image(value).toString());
|
|
sb.append(html);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets the selected item.
|
|
*
|
|
* @return the selected item
|
|
*/
|
|
/*
|
|
* (non-Javadoc)
|
|
*
|
|
* @see org.gcube.portlets.widgets.wsexplorer.client.notification.
|
|
* SelectionItemHandler#getSelectionItem()
|
|
*/
|
|
@Override
|
|
public T getSelectedItem() {
|
|
return ssm.getSelectedObject();
|
|
}
|
|
|
|
/**
|
|
* Sets the empty table message.
|
|
*
|
|
* @param msg the new empty table message
|
|
*/
|
|
public void setEmptyTableMessage(String msg) {
|
|
msg = msg != null ? msg : ContentModeratorWidgetConstants.NO_DATA;
|
|
if (sortedCellTable != null)
|
|
sortedCellTable.setEmptyTableWidget(new Label(msg));
|
|
}
|
|
}
|