diff options
author | Leif Åstrand <leif@vaadin.com> | 2014-12-04 15:43:48 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <teemusa@vaadin.com> | 2014-12-04 14:35:39 +0000 |
commit | ca061119cabe564c5d2205865d95a5e5ed496c6a (patch) | |
tree | 261d7e569353c3315c5b3ba90f1d558bf74711d6 /server/src/com | |
parent | c817f2c578839db70011889533f4c666638685cf (diff) | |
download | vaadin-framework-ca061119cabe564c5d2205865d95a5e5ed496c6a.tar.gz vaadin-framework-ca061119cabe564c5d2205865d95a5e5ed496c6a.zip |
Add server-side CellStyleGenerator (#13334)
Change-Id: Id12f1135673d93fddd0a59d26b1c546a0ef0ee1d
Diffstat (limited to 'server/src/com')
-rw-r--r-- | server/src/com/vaadin/data/RpcDataProviderExtension.java | 51 | ||||
-rw-r--r-- | server/src/com/vaadin/ui/Grid.java | 59 |
2 files changed, 109 insertions, 1 deletions
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 @@ -201,6 +201,37 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, } /** + * 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 + * <code>null</code>, otherwise a cell style is generated. + * <p> + * The returned style name is prefixed so that the actual style for + * cells will be <tt>v-grid-cell-content-[style name]</tt>, and the row + * style will be <tt>v-grid-row-[style name]</tt>. + * + * @param grid + * the source grid + * @param itemId + * the itemId of the target row + * @param propertyId + * the propertyId of the target cell, <code>null</code> when + * getting row style + * @return the style name to add to this cell or row element, or + * <code>null</code> to not set any style + */ + public abstract String getStyle(Grid grid, Object itemId, + Object propertyId); + } + + /** * Abstract base class for Grid header and footer sections. * * @param <ROWTYPE> @@ -1941,6 +1972,8 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, private EditorRow editorRow; + private CellStyleGenerator cellStyleGenerator; + /** * <code>true</code> if Grid is using the internal IndexedContainer created * in Grid() constructor, or <code>false</code> 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 <code>null</code> 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 <code>null</code> if no generator is + * set + */ + public CellStyleGenerator getCellStyleGenerator() { + return cellStyleGenerator; + } } |