524 lines
17 KiB
Java
524 lines
17 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.user.geoportaldataentry.client.ui.table;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
import org.gcube.application.geoportalcommon.ConvertToDataViewModel;
|
|
import org.gcube.application.geoportalcommon.shared.ItemField;
|
|
import org.gcube.application.geoportalcommon.shared.products.ConcessioneDV;
|
|
import org.gcube.application.geoportalcommon.shared.products.model.ValidationReportDV;
|
|
import org.gcube.application.geoportalcommon.shared.products.model.ValidationReportDV.ValidationStatus;
|
|
|
|
import com.github.gwtbootstrap.client.ui.ButtonCell;
|
|
import com.github.gwtbootstrap.client.ui.Pagination;
|
|
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";
|
|
public static DateTimeFormat dtformat = DateTimeFormat.getFormat(ConvertToDataViewModel.DATE_FORMAT);
|
|
private AbstractDataProvider<T> dataProvider;
|
|
private List<ItemField> displayFields;
|
|
private boolean isAsyncronusTable;
|
|
|
|
/**
|
|
* Instantiates a new items table.
|
|
*
|
|
* @param eventBus the event bus
|
|
* @param displayFields the display fields
|
|
* @param startSortByColumn the start sort by column
|
|
*/
|
|
public ItemsTable(HandlerManager eventBus, List<ItemField> displayFields) {
|
|
this.eventBus = eventBus;
|
|
this.displayFields = displayFields;
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
|
|
int i = 0;
|
|
|
|
for (ItemField itemField : displayFields) {
|
|
String displayName = itemField.getDisplayName();
|
|
// NAME
|
|
TextColumn<T> col = new TextColumn<T>() {
|
|
@Override
|
|
public String getValue(T object) {
|
|
|
|
if (object == null)
|
|
return "";
|
|
|
|
if (displayName.equalsIgnoreCase("Name")) {
|
|
return ((ConcessioneDV) object).getNome();
|
|
} else if (displayName.equalsIgnoreCase("Introduction")) {
|
|
return ((ConcessioneDV) object).getIntroduzione();
|
|
} else if (displayName.equalsIgnoreCase("Author/s")) {
|
|
|
|
String toDisplay = "";
|
|
if (object.getAuthors() != null) {
|
|
toDisplay = toDisplayAuthors(((ConcessioneDV) object).getAuthors());
|
|
}
|
|
return toDisplay;
|
|
} else if (displayName.equalsIgnoreCase("Project Start/End Date")) {
|
|
|
|
Date dS = null;
|
|
Date dE = null;
|
|
if (object.getDataInizioProgetto() != null) {
|
|
dS = (((ConcessioneDV) object).getDataInizioProgetto());
|
|
}
|
|
|
|
if (object.getDataFineProgetto() != null) {
|
|
dE = (((ConcessioneDV) object).getDataFineProgetto());
|
|
}
|
|
|
|
String dateFormat = "";
|
|
if (dS != null) {
|
|
dateFormat += dtformat.format(dS);
|
|
}
|
|
|
|
dateFormat += " / ";
|
|
|
|
if (dE != null) {
|
|
dateFormat += dtformat.format(dE);
|
|
}
|
|
|
|
return dateFormat;
|
|
} else if (displayName.equalsIgnoreCase("Published with")) {
|
|
ValidationReportDV vr = ((ConcessioneDV) object).getValidationReport();
|
|
if (vr != null && vr.getStatus() != null)
|
|
return vr.getStatus().getLabel();
|
|
return "";
|
|
} else if (displayName.equalsIgnoreCase("Created")) {
|
|
Date cd = ((ConcessioneDV) object).getCreationTime();
|
|
return dtformat.format(cd);
|
|
} else if (displayName.equalsIgnoreCase("Created by")) {
|
|
return ((ConcessioneDV) object).getCreationUser();
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
// ADDING TOOLTIP
|
|
@Override
|
|
public void render(com.google.gwt.cell.client.Cell.Context context, T object, SafeHtmlBuilder sb) {
|
|
if (object == null)
|
|
return;
|
|
|
|
if (displayName.equalsIgnoreCase("Published with")) {
|
|
String value = getValue(object);
|
|
String color = "#000";
|
|
if (value.compareTo(ValidationStatus.PASSED.getLabel()) == 0) {
|
|
color = "#32CD32";
|
|
} else if (value.compareTo(ValidationStatus.WARNING.getLabel()) == 0) {
|
|
color = "#FF8000";
|
|
} else if (value.compareTo(ValidationStatus.ERROR.getLabel()) == 0) {
|
|
color = "red";
|
|
}
|
|
sb.appendHtmlConstant("<span style=\"color:" + color + "\";>");
|
|
super.render(context, object, sb);
|
|
sb.appendHtmlConstant("</span>");
|
|
} else
|
|
super.render(context, object, sb);
|
|
|
|
};
|
|
};
|
|
|
|
if(i==0) {
|
|
//name
|
|
sortedCellTable.setColumnWidth(col, 20, Unit.PCT);
|
|
}else if (i == 1) {
|
|
// intro
|
|
sortedCellTable.setColumnWidth(col, 30, Unit.PCT);
|
|
} else if (i == 2) {
|
|
sortedCellTable.setColumnWidth(col, 15, Unit.PCT);
|
|
}else if(i==displayFields.size()-1) {
|
|
sortedCellTable.setColumnWidth(col, 120, Unit.PX);
|
|
}
|
|
|
|
sortedCellTable.addColumn(col, itemField.getDisplayName(), true);
|
|
|
|
i++;
|
|
}
|
|
|
|
/*
|
|
* if (this.displayFields.contains(RECORD_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, RECORD_FIELD.NAME.getDisplayName(), 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(RECORD_FIELD.INTRODUCTION)) {
|
|
*
|
|
* 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,
|
|
* RECORD_FIELD.INTRODUCTION.getDisplayName(), 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); }
|
|
*
|
|
* // sortedCellTable.setColumnWidth(introduction, 100, Unit.PCT);
|
|
*
|
|
* }
|
|
*
|
|
* if (this.displayFields.contains(RECORD_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, RECORD_FIELD.AUTHOR.getDisplayName(),
|
|
* 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); }
|
|
*
|
|
* sortedCellTable.setColumnWidth(author, 220, Unit.PX);
|
|
*
|
|
* }
|
|
*
|
|
* if (this.displayFields.contains(RECORD_FIELD.PROJECT_START_END_DATE)) {
|
|
*
|
|
* startEndProjectColumn = new TextColumn<T>() {
|
|
*
|
|
* @Override public String getValue(T object) { if (object == null) return "";
|
|
*
|
|
* Date dS = null; Date dE = null; if(object.getDataInizioProgetto()!=null) { dS
|
|
* = (((ConcessioneDV) object).getDataInizioProgetto()); }
|
|
*
|
|
* if(object.getDataFineProgetto()!=null) { dE = (((ConcessioneDV)
|
|
* object).getDataFineProgetto()); }
|
|
*
|
|
* String dateFormat = ""; if(dS!=null) { dateFormat+=dtformat.format(dS); }
|
|
*
|
|
* dateFormat+=" / ";
|
|
*
|
|
* if(dE!=null) { dateFormat+=dtformat.format(dE); }
|
|
*
|
|
* return dateFormat; } };
|
|
*
|
|
* sortedCellTable.addColumn(startEndProjectColumn,
|
|
* RECORD_FIELD.PROJECT_START_END_DATE.getDisplayName(), false);
|
|
* sortedCellTable.setColumnWidth(startEndProjectColumn, 180, Unit.PX);
|
|
*
|
|
* }
|
|
*
|
|
* if (this.displayFields.contains(RECORD_FIELD.CREATED)) {
|
|
*
|
|
* DateCell date = new
|
|
* DateCell(DateTimeFormat.getFormat(ConvertToDataViewModel.DATE_FORMAT+" "
|
|
* +ConvertToDataViewModel.TIME_FORMAT)); createdColumn = new Column<T,
|
|
* Date>(date) {
|
|
*
|
|
* @Override public Date getValue(T object) { if (object == null) return null;
|
|
*
|
|
* return (((ConcessioneDV) object).getCreationTime()); } };
|
|
* sortedCellTable.addColumn(createdColumn,
|
|
* RECORD_FIELD.CREATED.getDisplayName(), 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).getCreationTime()); Date d2 =
|
|
* (((ConcessioneDV) o2).getCreationTime());
|
|
*
|
|
* // 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(createdColumn,
|
|
* c); }
|
|
*
|
|
* sortedCellTable.setColumnWidth(createdColumn, 150, Unit.PX);
|
|
*
|
|
* }
|
|
*
|
|
* if (this.displayFields.contains(RECORD_FIELD.CREATED_BY)) {
|
|
*
|
|
* // NAME insertedBy = new TextColumn<T>() {
|
|
*
|
|
* @Override public String getValue(T object) { if (object == null) return "";
|
|
* return ((ConcessioneDV) object).getCreationUser(); } };
|
|
*
|
|
* sortedCellTable.addColumn(insertedBy,
|
|
* RECORD_FIELD.CREATED_BY.getDisplayName(), true);
|
|
*
|
|
* if (!isAsyncronusTable) { Comparator<T> c = new Comparator<T>() {
|
|
*
|
|
* @Override public int compare(T o1, T o2) { return ((ConcessioneDV)
|
|
* o1).getCreationUser().compareTo(((ConcessioneDV) o2).getCreationUser()); } };
|
|
*
|
|
* sortedCellTable.setComparator(insertedBy, c); }
|
|
* sortedCellTable.setColumnWidth(insertedBy, 220, Unit.PX); }
|
|
*
|
|
* if (this.displayFields.contains(RECORD_FIELD.RECORD_STATUS)) {
|
|
*
|
|
* statusColumn = new TextColumn<T>() {
|
|
*
|
|
* @Override public String getValue(T object) { if (object == null) return "";
|
|
*
|
|
* ValidationReportDV vr = ((ConcessioneDV) object).getValidationReport(); if
|
|
* (vr != null && vr.getStatus() != null) return vr.getStatus().getLabel();
|
|
* return ""; }
|
|
*
|
|
* @Override public void render(Context context, T object, SafeHtmlBuilder sb) {
|
|
* // TODO Auto-generated method stub
|
|
*
|
|
* String value = getValue(object); String color = "#000"; if
|
|
* (value.compareTo(ValidationStatus.PASSED.getLabel()) == 0) { color =
|
|
* "#32CD32"; } else if (value.compareTo(ValidationStatus.WARNING.getLabel()) ==
|
|
* 0) { color = "#FF8000"; } else if
|
|
* (value.compareTo(ValidationStatus.ERROR.getLabel()) == 0) { color = "red"; }
|
|
* sb.appendHtmlConstant("<span style=\"color:" + color + "\";>");
|
|
* super.render(context, object, sb); sb.appendHtmlConstant("</span>");
|
|
*
|
|
* } };
|
|
*
|
|
* sortedCellTable.addColumn(statusColumn,
|
|
* RECORD_FIELD.RECORD_STATUS.getDisplayName(), false);
|
|
* sortedCellTable.setColumnWidth(statusColumn, 120, Unit.PX);
|
|
*
|
|
* }
|
|
*/
|
|
}
|
|
|
|
public void enableWriteOperations() {
|
|
|
|
}
|
|
|
|
/**
|
|
* To display authors.
|
|
*
|
|
* @param authors the authors
|
|
* @return the string
|
|
*/
|
|
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(List<ItemField> fields) {
|
|
// this.displayFields = fields != null && fields.size() > 0 ? Arrays.asList(fields)
|
|
// : Arrays.asList(RECORD_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<ItemField> 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;
|
|
}
|
|
|
|
}
|