/** * */ 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.application.geoportalcommon.shared.products.model.ValidationReportDV; import org.gcube.application.geoportalcommon.shared.products.model.ValidationReportDV.ValidationStatus; import org.gcube.portlets.user.geoportaldataentry.client.ConstantsGeoPortalDataEntryApp.ACTION_ON_ITEM; import org.gcube.portlets.user.geoportaldataentry.client.ConstantsGeoPortalDataEntryApp.RECORD_FIELD; import org.gcube.portlets.user.geoportaldataentry.client.events.ActionOnItemEvent; import org.gcube.portlets.user.geoportaldataentry.shared.SortFilter; import com.github.gwtbootstrap.client.ui.ButtonCell; import com.github.gwtbootstrap.client.ui.Pagination; import com.github.gwtbootstrap.client.ui.constants.IconType; import com.google.gwt.cell.client.Cell.Context; 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 the generic type */ public class ItemsTable extends AbstractItemsCellTable { private static final int ITEMS_PER_PAGE = 10; private static final String NO_DATA = "No data"; private TextColumn name; private TextColumn introduction; private TextColumn author; public static DateTimeFormat dtformat = DateTimeFormat.getFormat(ConvertToDataViewModel.DATE_FORMAT); private AbstractDataProvider dataProvider; private List displayFields; private TextColumn startEndProjectColumn; private TextColumn statusColumn; // private Column startProjectDateColumn; // private Column endProjectDateColumn; private boolean isAsyncronusTable; private TextColumn insertedBy; private Column createdColumn; private SortFilter currentSortFilter; /** * 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, RECORD_FIELD[] displayFields, SortFilter currentSortFilter) { this.eventBus = eventBus; this.currentSortFilter = currentSortFilter; setDisplayFields(displayFields); } /** * Adds the items. * * @param items the items */ public void addItems(List 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 dataProvider) { this.dataProvider = dataProvider; this.theSelectionModel = new SingleSelectionModel(); initAbstractTable(eventBus, fireEventOnClick, dataProvider, theSelectionModel, ITEMS_PER_PAGE); this.dataProvider.addDataDisplay(sortedCellTable); this.isAsyncronusTable = dataProvider instanceof ListDataProvider ? false : true; setEmptyTableMessage(NO_DATA); if (this.displayFields.contains(RECORD_FIELD.NAME)) { // NAME name = new TextColumn() { @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("
"); super.render(context, object, sb); sb.appendHtmlConstant("
"); }; }; sortedCellTable.addColumn(name, RECORD_FIELD.NAME.getDisplayName(), true); if (!isAsyncronusTable) { Comparator c = new Comparator() { @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() { @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 c = new Comparator() { @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() { @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 c = new Comparator() { @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() { @Override public String getValue(T object) { if (object == null) return ""; Date dS = (((ConcessioneDV) object).getDataInizioProgetto()); Date dE = (((ConcessioneDV) object).getDataFineProgetto()); return dtformat.format(dS) + " / " + dtformat.format(dE); } }; 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(dtformat); createdColumn = new Column(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 c = new Comparator() { @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() { @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 c = new Comparator() { @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.PUBLISHING_STATUS)) { statusColumn = new TextColumn() { @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(""); super.render(context, object, sb); sb.appendHtmlConstant(""); } }; sortedCellTable.addColumn(statusColumn, RECORD_FIELD.PUBLISHING_STATUS.getDisplayName(), false); sortedCellTable.setColumnWidth(statusColumn, 120, Unit.PX); } ButtonCell showOnMapButton = new ButtonCell(); showOnMapButton.setIcon(IconType.MAP_MARKER); MyToolTipColumn showOnMapColumn = new MyToolTipColumn(showOnMapButton, "Show on Map") { @Override public String getValue(T object) { return ""; } }; showOnMapColumn.setFieldUpdater(new FieldUpdater() { @Override public void update(int index, T object, String value) { GWT.log("clicked show"); eventBus.fireEvent(new ActionOnItemEvent(Arrays.asList(object), ACTION_ON_ITEM.SHOW_ON_MAP)); } }); sortedCellTable.addColumn(showOnMapColumn); sortedCellTable.setColumnWidth(showOnMapColumn, 50, Unit.PX); ButtonCell showReportRecordButton = new ButtonCell(); showReportRecordButton.setIcon(IconType.FILE_TEXT_ALT); MyToolTipColumn showReportRecordColumn = new MyToolTipColumn(showReportRecordButton, "Show Publication Report") { public String getValue(T object) { return ""; } }; showReportRecordColumn.setFieldUpdater(new FieldUpdater() { @Override public void update(int index, T object, String value) { eventBus.fireEvent(new ActionOnItemEvent(Arrays.asList(object), ACTION_ON_ITEM.SHOW_REPORT)); } }); sortedCellTable.addColumn(showReportRecordColumn); sortedCellTable.setColumnWidth(showReportRecordColumn, 50, Unit.PX); ButtonCell deleteRecordButton = new ButtonCell(); deleteRecordButton.setIcon(IconType.TRASH); MyToolTipColumn deleteRecordColumn = new MyToolTipColumn(deleteRecordButton, "Delete Project") { public String getValue(T object) { return ""; } }; deleteRecordColumn.setFieldUpdater(new FieldUpdater() { @Override public void update(int index, T object, String value) { eventBus.fireEvent(new ActionOnItemEvent(Arrays.asList(object), ACTION_ON_ITEM.REMOVE)); } }); sortedCellTable.addColumn(deleteRecordColumn); sortedCellTable.setColumnWidth(deleteRecordColumn, 50, Unit.PX); GWT.log("currentSortFilter: " + currentSortFilter); if (currentSortFilter != null) switch (currentSortFilter.getOrderByField()) { case NAME: if (this.displayFields.contains(RECORD_FIELD.NAME)) { sortedCellTable.setInitialSortColumn(name); } break; case INTRODUCTION: if (this.displayFields.contains(RECORD_FIELD.INTRODUCTION)) { sortedCellTable.setInitialSortColumn(introduction); } break; case AUTHOR: if (this.displayFields.contains(RECORD_FIELD.AUTHOR)) { sortedCellTable.setInitialSortColumn(author); } break; default: break; } } /** * To display authors. * * @param authors the authors * @return the string */ private String toDisplayAuthors(List 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 true for ascending icon, false * for descending icon */ public void setSortedColumn(int columnIndex, boolean ascending) { GWT.log("Column index: " + columnIndex); GWT.log("ascending: " + ascending); Column 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(RECORD_FIELD[] fields) { this.displayFields = fields != null && fields.length > 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 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 getSelectedItems() { if (theSelectionModel instanceof SingleSelectionModel) { T selected = ((SingleSelectionModel) theSelectionModel).getSelectedObject(); if (selected != null) { return Arrays.asList(selected); } } else if (theSelectionModel instanceof MultiSelectionModel) { Set selected = ((MultiSelectionModel) theSelectionModel).getSelectedSet(); if (selected != null) { return new ArrayList(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 getSelectionModel() { return theSelectionModel; } }