517 lines
14 KiB
Java
517 lines
14 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.user.geoportaldataentry.client.ui.table;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Comparator;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
import org.gcube.application.geoportalcommon.ConvertToDataViewModel;
|
|
import org.gcube.application.geoportalcommon.shared.products.ConcessioneDV;
|
|
import org.gcube.portlets.user.geoportaldataentry.client.events.ShowItemEvent;
|
|
|
|
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.cell.client.FieldUpdater;
|
|
import com.google.gwt.core.shared.GWT;
|
|
import com.google.gwt.dom.client.Style.Unit;
|
|
import com.google.gwt.event.shared.HandlerManager;
|
|
import com.google.gwt.i18n.client.DateTimeFormat;
|
|
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;
|
|
import com.google.gwt.view.client.MultiSelectionModel;
|
|
import com.google.gwt.view.client.SelectionModel;
|
|
import com.google.gwt.view.client.SingleSelectionModel;
|
|
|
|
/**
|
|
* 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 ConcessioneDV> extends AbstractItemsCellTable<T> {
|
|
|
|
private static final int ITEMS_PER_PAGE = 10;
|
|
private static final String NO_DATA = "No data";
|
|
private TextColumn<T> name;
|
|
private TextColumn<T> introduction;
|
|
private TextColumn<T> author;
|
|
public static DateTimeFormat dtformat = DateTimeFormat.getFormat(ConvertToDataViewModel.DATE_FORMAT);
|
|
|
|
private AbstractDataProvider<T> dataProvider;
|
|
|
|
/**
|
|
* The Enum DISPLAY_FIELD.
|
|
*
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Feb 1, 2016
|
|
*/
|
|
public static enum DISPLAY_FIELD {
|
|
NAME, INTRO, AUTHOR, PROJECT_START_DATE, PROJECT_END_DATE
|
|
};
|
|
|
|
private List<DISPLAY_FIELD> displayFields;
|
|
private Column<T, Date> startProjectDateColumn;
|
|
private Column<T, Date> endProjectDateColumn;
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
@Override
|
|
public void initTable(final SimplePager pager, final Pagination pagination, AbstractDataProvider<T> dataProvider) {
|
|
this.dataProvider = dataProvider;
|
|
this.theSelectionModel = new SingleSelectionModel<T>();
|
|
initAbstractTable(eventBus, fireEventOnClick, dataProvider, theSelectionModel, ITEMS_PER_PAGE);
|
|
this.dataProvider.addDataDisplay(sortedCellTable);
|
|
|
|
this.isAsyncronusTable = dataProvider instanceof ListDataProvider ? false : true;
|
|
setEmptyTableMessage(NO_DATA);
|
|
|
|
// final CheckboxCell cellCheckBox = new CheckboxCell(true, false);
|
|
// Column<T, Boolean> checkColumn = new Column<T, Boolean>(cellCheckBox) {
|
|
// @Override
|
|
// public Boolean getValue(T object) {
|
|
// // Get the value from the selection model.
|
|
// return theSelectionModel.isSelected(object);
|
|
// }
|
|
//
|
|
// @Override
|
|
// public void render(Context context, T object, SafeHtmlBuilder sb) {
|
|
// super.render(context, object, sb);
|
|
// GWT.log("added checkbox: " + cellCheckBox + " to object: " + object);
|
|
// }
|
|
//
|
|
// };
|
|
//
|
|
// sortedCellTable.addColumn(checkColumn, "", false);
|
|
// sortedCellTable.setColumnWidth(checkColumn, 40, Unit.PX);
|
|
|
|
ButtonCell previewButton = new ButtonCell();
|
|
Column<T, String> showdItemColumn = new Column<T, String>(previewButton) {
|
|
public String getValue(T object) {
|
|
return "Show";
|
|
}
|
|
};
|
|
|
|
showdItemColumn.setFieldUpdater(new FieldUpdater<T, String>() {
|
|
@Override
|
|
public void update(int index, T object, String value) {
|
|
GWT.log("clicked show");
|
|
eventBus.fireEvent(new ShowItemEvent<T>(Arrays.asList(object)));
|
|
}
|
|
});
|
|
sortedCellTable.addColumn(showdItemColumn);
|
|
sortedCellTable.setColumnWidth(showdItemColumn, 80, 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 ((ConcessioneDV) object).getNome();
|
|
}
|
|
|
|
// 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 introduction=\"" + ((ConcessioneDV) object).getNome() + "\">");
|
|
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 ((ConcessioneDV) o1).getNome().compareTo(((ConcessioneDV) o2).getNome());
|
|
}
|
|
};
|
|
|
|
sortedCellTable.setComparator(name, c);
|
|
}
|
|
|
|
}
|
|
|
|
if (this.displayFields.contains(DISPLAY_FIELD.INTRO)) {
|
|
|
|
introduction = new TextColumn<T>() {
|
|
@Override
|
|
public String getValue(T object) {
|
|
if (object == null)
|
|
return "";
|
|
return ((ConcessioneDV) object).getIntroduzione() != null
|
|
? ((ConcessioneDV) object).getIntroduzione()
|
|
: "";
|
|
}
|
|
};
|
|
|
|
sortedCellTable.addColumn(introduction, "Intro", true);
|
|
|
|
if (!isAsyncronusTable) {
|
|
Comparator<T> c = new Comparator<T>() {
|
|
@Override
|
|
public int compare(T o1, T o2) {
|
|
return ((ConcessioneDV) o1).getIntroduzione().compareTo(((ConcessioneDV) o2).getIntroduzione());
|
|
}
|
|
};
|
|
sortedCellTable.setComparator(introduction, c);
|
|
}
|
|
|
|
}
|
|
|
|
if (this.displayFields.contains(DISPLAY_FIELD.AUTHOR)) {
|
|
|
|
author = new TextColumn<T>() {
|
|
@Override
|
|
public String getValue(T object) {
|
|
if (object == null)
|
|
return "";
|
|
|
|
String toDisplay = toDisplayAuthors(((ConcessioneDV) object).getAuthors());
|
|
return toDisplay;
|
|
}
|
|
};
|
|
|
|
sortedCellTable.addColumn(author, "Author/s", true);
|
|
|
|
if (!isAsyncronusTable) {
|
|
Comparator<T> c = new Comparator<T>() {
|
|
@Override
|
|
public int compare(T o1, T o2) {
|
|
String toDisplay1 = toDisplayAuthors(((ConcessioneDV) o1).getAuthors());
|
|
String toDisplay2 = toDisplayAuthors(((ConcessioneDV) o2).getAuthors());
|
|
return toDisplay1.compareTo(toDisplay2);
|
|
}
|
|
};
|
|
sortedCellTable.setComparator(author, c);
|
|
}
|
|
|
|
}
|
|
|
|
if (this.displayFields.contains(DISPLAY_FIELD.PROJECT_START_DATE)) {
|
|
|
|
DateCell date = new DateCell(dtformat);
|
|
startProjectDateColumn = new Column<T, Date>(date) {
|
|
|
|
@Override
|
|
public Date getValue(T object) {
|
|
if (object == null)
|
|
return null;
|
|
|
|
return (((ConcessioneDV) object).getDataInizioProgetto());
|
|
}
|
|
};
|
|
sortedCellTable.addColumn(startProjectDateColumn, "Project Start Date", true);
|
|
|
|
if (!isAsyncronusTable) {
|
|
Comparator<T> c = new Comparator<T>() {
|
|
@Override
|
|
public int compare(T o1, T o2) {
|
|
if (o1 == null)
|
|
return -1;
|
|
|
|
if (o2 == null)
|
|
return 1;
|
|
|
|
Date d1 = (((ConcessioneDV) o1).getDataInizioProgetto());
|
|
Date d2 = (((ConcessioneDV) o2).getDataInizioProgetto());
|
|
|
|
// 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(startProjectDateColumn, c);
|
|
}
|
|
|
|
}
|
|
|
|
if (this.displayFields.contains(DISPLAY_FIELD.PROJECT_END_DATE)) {
|
|
|
|
DateCell date = new DateCell(dtformat);
|
|
endProjectDateColumn = new Column<T, Date>(date) {
|
|
|
|
@Override
|
|
public Date getValue(T object) {
|
|
if (object == null)
|
|
return null;
|
|
|
|
return (((ConcessioneDV) object).getDataFineProgetto());
|
|
}
|
|
};
|
|
sortedCellTable.addColumn(startProjectDateColumn, "Project End Date", true);
|
|
|
|
if (!isAsyncronusTable) {
|
|
Comparator<T> c = new Comparator<T>() {
|
|
@Override
|
|
public int compare(T o1, T o2) {
|
|
if (o1 == null)
|
|
return -1;
|
|
|
|
if (o2 == null)
|
|
return 1;
|
|
|
|
Date d1 = (((ConcessioneDV) o1).getDataFineProgetto());
|
|
Date d2 = (((ConcessioneDV) o2).getDataFineProgetto());
|
|
|
|
// 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(endProjectDateColumn, c);
|
|
}
|
|
|
|
}
|
|
|
|
GWT.log("startSortByColumn: " + startSortByColumn);
|
|
|
|
if (startSortByColumn != null)
|
|
switch (startSortByColumn) {
|
|
case NAME:
|
|
if (this.displayFields.contains(DISPLAY_FIELD.NAME)) {
|
|
sortedCellTable.setInitialSortColumn(name);
|
|
}
|
|
break;
|
|
case INTRO:
|
|
if (this.displayFields.contains(DISPLAY_FIELD.INTRO)) {
|
|
sortedCellTable.setInitialSortColumn(introduction);
|
|
}
|
|
break;
|
|
case AUTHOR:
|
|
if (this.displayFields.contains(DISPLAY_FIELD.AUTHOR)) {
|
|
sortedCellTable.setInitialSortColumn(author);
|
|
}
|
|
break;
|
|
case PROJECT_START_DATE:
|
|
if (this.displayFields.contains(DISPLAY_FIELD.PROJECT_START_DATE)) {
|
|
sortedCellTable.setDefaultSortOrder(startProjectDateColumn, false); // sorts ascending on first
|
|
// click
|
|
sortedCellTable.setInitialSortColumn(startProjectDateColumn);
|
|
GWT.log("sortedCellTable: " + sortedCellTable);
|
|
}
|
|
break;
|
|
case PROJECT_END_DATE:
|
|
if (this.displayFields.contains(DISPLAY_FIELD.PROJECT_START_DATE)) {
|
|
sortedCellTable.setDefaultSortOrder(endProjectDateColumn, false); // sorts ascending on first click
|
|
sortedCellTable.setInitialSortColumn(endProjectDateColumn);
|
|
GWT.log("sortedCellTable: " + sortedCellTable);
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
// /**
|
|
// * To date format string.
|
|
// *
|
|
// * @param dateTime the date time
|
|
// * @return the string
|
|
// */
|
|
// public static Date toDisplayDate(Date dateTime) {
|
|
//
|
|
// if(dateTime==null)
|
|
// return null;
|
|
//
|
|
// return new Date(dtformat.format(dateTime));
|
|
// }
|
|
|
|
private String toDisplayAuthors(List<String> authors) {
|
|
String toDisplay = "";
|
|
if (authors == null)
|
|
return toDisplay;
|
|
|
|
for (String author : authors) {
|
|
toDisplay += author + "; ";
|
|
}
|
|
return toDisplay;
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public List<T> getSelectedItems() {
|
|
if (theSelectionModel instanceof SingleSelectionModel) {
|
|
T selected = ((SingleSelectionModel<T>) theSelectionModel).getSelectedObject();
|
|
if (selected != null) {
|
|
return Arrays.asList(selected);
|
|
}
|
|
|
|
} else if (theSelectionModel instanceof MultiSelectionModel) {
|
|
Set<T> selected = ((MultiSelectionModel<T>) theSelectionModel).getSelectedSet();
|
|
if (selected != null) {
|
|
return new ArrayList<T>(selected);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Sets the empty table message.
|
|
*
|
|
* @param msg the new empty table message
|
|
*/
|
|
public void setEmptyTableMessage(String msg) {
|
|
msg = msg != null ? msg : NO_DATA;
|
|
if (sortedCellTable != null)
|
|
sortedCellTable.setEmptyTableWidget(new Label(msg));
|
|
}
|
|
|
|
/**
|
|
* Gets the selection model.
|
|
*
|
|
* @return the selection model
|
|
*/
|
|
public SelectionModel<T> getSelectionModel() {
|
|
return theSelectionModel;
|
|
}
|
|
|
|
}
|