PL-8569 Add support for descriptions providers in DataGrid

This commit is contained in:
Gleb Gorelov 2017-02-03 15:11:48 +04:00
parent 159b3e17a5
commit 15f5564155
2 changed files with 138 additions and 3 deletions

View File

@ -493,10 +493,10 @@ public interface DataGrid<E extends Entity>
* 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<E extends Entity>
*/
void removeCellStyleProvider(CellStyleProvider<? super E> 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<E extends Entity> {
/**
* 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<E> 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<E extends Entity> {
/**
* 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<E> provider);
abstract class AbstractDataGridEvent extends EventObject {
public AbstractDataGridEvent(DataGrid component) {

View File

@ -111,6 +111,9 @@ public class WebDataGrid<E extends Entity> extends WebAbstractComponent<CubaGrid
protected List<RowStyleProvider> rowStyleProviders;
protected List<CellStyleProvider> cellStyleProviders;
protected RowDescriptionProvider<E> rowDescriptionProvider;
protected CellDescriptionProvider<E> cellDescriptionProvider;
protected Security security = AppBeans.get(Security.NAME);
protected MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
@ -1536,6 +1539,59 @@ public class WebDataGrid<E extends Entity> extends WebAbstractComponent<CubaGrid
}
}
@Override
public CellDescriptionProvider getCellDescriptionProvider() {
return cellDescriptionProvider;
}
@Override
public void setCellDescriptionProvider(CellDescriptionProvider<E> 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<E> 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<E, ?> generator) {
return addGeneratedColumn(columnId, generator, columnsOrder.size());
@ -1853,7 +1909,11 @@ public class WebDataGrid<E extends Entity> extends WebAbstractComponent<CubaGrid
Entity item = datasource.getItem(itemId);
String joinedStyle = null;
for (CellStyleProvider styleProvider : cellStyleProviders) {
String styleName = styleProvider.getStyleName(item, propertyId == null ? null : propertyId.toString());
Column column = getColumnByPropertyId(propertyId);
if (column == null) {
throw new RuntimeException("Column not found for propertyId: " + propertyId);
}
String styleName = styleProvider.getStyleName(item, column.getId());
if (styleName != null) {
if (joinedStyle == null) {
joinedStyle = styleName;