From ca061119cabe564c5d2205865d95a5e5ed496c6a Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Thu, 4 Dec 2014 15:43:48 +0200 Subject: Add server-side CellStyleGenerator (#13334) Change-Id: Id12f1135673d93fddd0a59d26b1c546a0ef0ee1d --- .../com/vaadin/data/RpcDataProviderExtension.java | 51 ++++++++++++++++++- server/src/com/vaadin/ui/Grid.java | 59 ++++++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) (limited to 'server/src/com') diff --git a/server/src/com/vaadin/data/RpcDataProviderExtension.java b/server/src/com/vaadin/data/RpcDataProviderExtension.java index ffef7e5b9e..d607879aa5 100644 --- a/server/src/com/vaadin/data/RpcDataProviderExtension.java +++ b/server/src/com/vaadin/data/RpcDataProviderExtension.java @@ -47,6 +47,7 @@ import com.vaadin.shared.data.DataRequestRpc; import com.vaadin.shared.ui.grid.GridState; import com.vaadin.shared.ui.grid.Range; import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.CellStyleGenerator; import com.vaadin.ui.Grid.Column; import com.vaadin.ui.components.grid.Renderer; @@ -718,7 +719,6 @@ public class RpcDataProviderExtension extends AbstractExtension { Grid grid = getGrid(); - int i = 0; for (Object propertyId : propertyIds) { Column column = grid.getColumn(propertyId); @@ -733,9 +733,42 @@ public class RpcDataProviderExtension extends AbstractExtension { final JsonObject rowObject = Json.createObject(); rowObject.put(GridState.JSONKEY_DATA, rowData); rowObject.put(GridState.JSONKEY_ROWKEY, keyMapper.getKey(itemId)); + + CellStyleGenerator cellStyleGenerator = grid.getCellStyleGenerator(); + if (cellStyleGenerator != null) { + setGeneratedStyles(cellStyleGenerator, rowObject, propertyIds, + itemId); + } + return rowObject; } + private void setGeneratedStyles(CellStyleGenerator generator, + JsonObject rowObject, Collection propertyIds, Object itemId) { + Grid grid = getGrid(); + + JsonObject cellStyles = null; + for (Object propertyId : propertyIds) { + String style = generator.getStyle(grid, itemId, propertyId); + if (style != null) { + if (cellStyles == null) { + cellStyles = Json.createObject(); + } + + String columnKey = columnKeys.key(propertyId); + cellStyles.put(columnKey, style); + } + } + if (cellStyles != null) { + rowObject.put(GridState.JSONKEY_CELLSTYLES, cellStyles); + } + + String rowStyle = generator.getStyle(grid, itemId, null); + if (rowStyle != null) { + rowObject.put(GridState.JSONKEY_ROWSTYLE, rowStyle); + } + } + @Override protected DataProviderState getState() { return (DataProviderState) super.getState(); @@ -813,6 +846,22 @@ public class RpcDataProviderExtension extends AbstractExtension { rpc.setRowData(index, rowArray.toJson()); } + /** + * Pushes a new version of all the rows in the active cache range. + */ + public void refreshCache() { + if (!clientInitialized) { + return; + } + + int firstRow = activeRowHandler.activeRange.getStart(); + int numberOfRows = activeRowHandler.activeRange.length(); + + List itemIds = RpcDataProviderExtension.this.container.getItemIds( + firstRow, numberOfRows); + pushRows(firstRow, itemIds); + } + @Override public void setParent(ClientConnector parent) { super.setParent(parent); diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index 9732aa4612..992c666709 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -200,6 +200,37 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, protected abstract SelectionModel createModel(); } + /** + * Callback interface for generating custom style names for data rows and + * cells. + * + * @see Grid#setCellStyleGenerator(CellStyleGenerator) + */ + public interface CellStyleGenerator extends Serializable { + + /** + * Called by Grid to generate a style name for a row or cell element. + * Row styles are generated when the column parameter is + * null, otherwise a cell style is generated. + *

+ * The returned style name is prefixed so that the actual style for + * cells will be v-grid-cell-content-[style name], and the row + * style will be v-grid-row-[style name]. + * + * @param grid + * the source grid + * @param itemId + * the itemId of the target row + * @param propertyId + * the propertyId of the target cell, null when + * getting row style + * @return the style name to add to this cell or row element, or + * null to not set any style + */ + public abstract String getStyle(Grid grid, Object itemId, + Object propertyId); + } + /** * Abstract base class for Grid header and footer sections. * @@ -1941,6 +1972,8 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, private EditorRow editorRow; + private CellStyleGenerator cellStyleGenerator; + /** * true if Grid is using the internal IndexedContainer created * in Grid() constructor, or false if the user has set their @@ -3551,4 +3584,30 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, EditorRowClientRpc getEditorRowRpc() { return getRpcProxy(EditorRowClientRpc.class); } + + /** + * Sets the cell style generator that is used for generating styles for rows + * and cells. + * + * @param cellStyleGenerator + * the cell style generator to set, or null to + * remove a previously set generator + */ + public void setCellStyleGenerator(CellStyleGenerator cellStyleGenerator) { + this.cellStyleGenerator = cellStyleGenerator; + getState().hasCellStyleGenerator = (cellStyleGenerator != null); + + datasourceExtension.refreshCache(); + } + + /** + * Gets the cell style generator that is used for generating styles for rows + * and cells. + * + * @return the cell style generator, or null if no generator is + * set + */ + public CellStyleGenerator getCellStyleGenerator() { + return cellStyleGenerator; + } } -- cgit v1.2.3