From 15f5564155785a76c70991fe9a3a4e8643813a28 Mon Sep 17 00:00:00 2001 From: Gleb Gorelov Date: Fri, 3 Feb 2017 15:11:48 +0400 Subject: [PATCH] PL-8569 Add support for descriptions providers in DataGrid --- .../cuba/gui/components/DataGrid.java | 79 ++++++++++++++++++- .../cuba/web/gui/components/WebDataGrid.java | 62 ++++++++++++++- 2 files changed, 138 insertions(+), 3 deletions(-) diff --git a/modules/gui/src/com/haulmont/cuba/gui/components/DataGrid.java b/modules/gui/src/com/haulmont/cuba/gui/components/DataGrid.java index 67a8183de3..46b19ed27b 100644 --- a/modules/gui/src/com/haulmont/cuba/gui/components/DataGrid.java +++ b/modules/gui/src/com/haulmont/cuba/gui/components/DataGrid.java @@ -493,10 +493,10 @@ public interface DataGrid * Called by {@link DataGrid} to get a style for cell. * * @param entity an entity instance represented by the current row - * @param property data item property identifier + * @param columnId id of the DataGrid column * @return style name or null to apply the default */ - String getStyleName(E entity, @Nullable String property); + String getStyleName(E entity, String columnId); } /** @@ -511,6 +511,81 @@ public interface DataGrid */ void removeCellStyleProvider(CellStyleProvider styleProvider); + /** + * A callback interface for generating optional descriptions (tooltips) for + * DataGrid cells. If a cell has both a {@link RowDescriptionProvider row + * description} and a cell description, the latter has precedence. + */ + interface CellDescriptionProvider { + + /** + * Called by DataGrid to generate a description (tooltip) for a cell. The + * description may contain HTML markup. + * + * @param entity an entity instance represented by the current row + * @param columnId id of the DataGrid column + * @return the cell description or {@code null} for no description + */ + String getDescription(E entity, String columnId); + } + + /** + * Returns the {@code CellDescriptionProvider} instance used to generate + * descriptions (tooltips) for DataGrid cells. + * + * @return the description provider or {@code null} if no provider is set + */ + CellDescriptionProvider getCellDescriptionProvider(); + + /** + * Sets the {@code CellDescriptionProvider} instance for generating + * optional descriptions (tooltips) for individual DataGrid cells. If a + * {@link RowDescriptionProvider} is also set, the row description it + * generates is displayed for cells for which {@code provider} returns null. + * + * @param provider the description provider to use or {@code null} to remove a + * previously set provider if any + */ + void setCellDescriptionProvider(CellDescriptionProvider provider); + + /** + * A callback interface for generating optional descriptions (tooltips) for + * DataGrid rows. If a description is generated for a row, it is used for + * all the cells in the row for which a {@link CellDescriptionProvider cell + * description} is not generated. + */ + interface RowDescriptionProvider { + + /** + * Called by DataGrid to generate a description (tooltip) for a row. The + * description may contain HTML markup. + * + * @param entity an entity instance represented by the current row + * @return the row description or {@code null} for no description + */ + String getDescription(E entity); + } + + /** + * Returns the {@code RowDescriptionProvider} instance used to generate + * descriptions (tooltips) for DataGrid rows + * + * @return the description provider or {@code} null if no provider is set + */ + RowDescriptionProvider getRowDescriptionProvider(); + + /** + * Sets the {@code RowDescriptionProvider} instance for generating + * optional descriptions (tooltips) for DataGrid rows. If a + * {@link CellDescriptionProvider} is also set, the row description + * generated by {@code provider} is used for cells for which the cell + * description provider returns null. + * + * @param provider the description provider to use or {@code null} to remove a + * previously set provider if any + */ + void setRowDescriptionProvider(RowDescriptionProvider provider); + abstract class AbstractDataGridEvent extends EventObject { public AbstractDataGridEvent(DataGrid component) { diff --git a/modules/web/src/com/haulmont/cuba/web/gui/components/WebDataGrid.java b/modules/web/src/com/haulmont/cuba/web/gui/components/WebDataGrid.java index 57576f1efe..9804349eff 100644 --- a/modules/web/src/com/haulmont/cuba/web/gui/components/WebDataGrid.java +++ b/modules/web/src/com/haulmont/cuba/web/gui/components/WebDataGrid.java @@ -111,6 +111,9 @@ public class WebDataGrid extends WebAbstractComponent rowStyleProviders; protected List cellStyleProviders; + protected RowDescriptionProvider rowDescriptionProvider; + protected CellDescriptionProvider cellDescriptionProvider; + protected Security security = AppBeans.get(Security.NAME); protected MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME); @@ -1536,6 +1539,59 @@ public class WebDataGrid extends WebAbstractComponent provider) { + this.cellDescriptionProvider = provider; + + if (provider != null) { + component.setCellDescriptionGenerator(createCellDescriptionGenerator()); + } else { + component.setCellDescriptionGenerator(null); + } + } + + protected Grid.CellDescriptionGenerator createCellDescriptionGenerator() { + return cell -> { + //noinspection unchecked + E item = (E) datasource.getItem(cell.getItemId()); + Column column = getColumnByPropertyId(cell.getPropertyId()); + if (column == null) { + throw new RuntimeException("Column not found for propertyId: " + cell.getPropertyId()); + } + return cellDescriptionProvider.getDescription(item, column.getId()); + }; + } + + @Override + public RowDescriptionProvider getRowDescriptionProvider() { + return rowDescriptionProvider; + } + + @Override + public void setRowDescriptionProvider(RowDescriptionProvider provider) { + this.rowDescriptionProvider = provider; + + if (provider != null) { + component.setRowDescriptionGenerator(createRowDescriptionGenerator()); + } else { + component.setRowDescriptionGenerator(null); + } + } + + protected Grid.RowDescriptionGenerator createRowDescriptionGenerator() { + return row -> { + //noinspection unchecked + E item = (E) datasource.getItem(row.getItemId()); + + return rowDescriptionProvider.getDescription(item); + }; + } + @Override public Column addGeneratedColumn(String columnId, ColumnGenerator generator) { return addGeneratedColumn(columnId, generator, columnsOrder.size()); @@ -1853,7 +1909,11 @@ public class WebDataGrid extends WebAbstractComponent