diff options
author | Artur Signell <artur@vaadin.com> | 2014-12-12 01:14:46 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <teemusa@vaadin.com> | 2014-12-12 14:41:58 +0000 |
commit | c9fe985fc34788d197417715d8d439c2ce0bc26b (patch) | |
tree | 65ef10961e662299b89443fc78fd2676f70fdab2 /server | |
parent | 643ccd9e6a9ca6bb1d4960aa1642fa790edc9ac1 (diff) | |
download | vaadin-framework-c9fe985fc34788d197417715d8d439c2ce0bc26b.tar.gz vaadin-framework-c9fe985fc34788d197417715d8d439c2ce0bc26b.zip |
Split CellStyleGenerator into separate cell and row style generators
(#13334)
Change-Id: If07018b6f74ff1a4c616705f61b6118647d64342
Diffstat (limited to 'server')
-rw-r--r-- | server/src/com/vaadin/data/RpcDataProviderExtension.java | 36 | ||||
-rw-r--r-- | server/src/com/vaadin/ui/Grid.java | 213 |
2 files changed, 222 insertions, 27 deletions
diff --git a/server/src/com/vaadin/data/RpcDataProviderExtension.java b/server/src/com/vaadin/data/RpcDataProviderExtension.java index f28d95f610..5e56643d6e 100644 --- a/server/src/com/vaadin/data/RpcDataProviderExtension.java +++ b/server/src/com/vaadin/data/RpcDataProviderExtension.java @@ -49,8 +49,11 @@ 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.CellReference; import com.vaadin.ui.Grid.CellStyleGenerator; import com.vaadin.ui.Grid.Column; +import com.vaadin.ui.Grid.RowReference; +import com.vaadin.ui.Grid.RowStyleGenerator; import com.vaadin.ui.renderer.Renderer; import elemental.json.Json; @@ -648,6 +651,9 @@ public class RpcDataProviderExtension extends AbstractExtension { /* Has client been initialized */ private boolean clientInitialized = false; + private RowReference rowReference; + private CellReference cellReference; + /** * Creates a new data provider using the given container. * @@ -753,22 +759,27 @@ public class RpcDataProviderExtension extends AbstractExtension { rowObject.put(GridState.JSONKEY_DATA, rowData); rowObject.put(GridState.JSONKEY_ROWKEY, keyMapper.getKey(itemId)); + rowReference.set(itemId); + CellStyleGenerator cellStyleGenerator = grid.getCellStyleGenerator(); if (cellStyleGenerator != null) { - setGeneratedStyles(cellStyleGenerator, rowObject, columns, itemId); + setGeneratedCellStyles(cellStyleGenerator, rowObject, columns); + } + RowStyleGenerator rowStyleGenerator = grid.getRowStyleGenerator(); + if (rowStyleGenerator != null) { + setGeneratedRowStyles(rowStyleGenerator, rowObject); } return rowObject; } - private void setGeneratedStyles(CellStyleGenerator generator, - JsonObject rowObject, Collection<Column> columns, Object itemId) { - Grid grid = getGrid(); - + private void setGeneratedCellStyles(CellStyleGenerator generator, + JsonObject rowObject, Collection<Column> columns) { JsonObject cellStyles = null; for (Column column : columns) { Object propertyId = column.getColumnProperty(); - String style = generator.getStyle(grid, itemId, propertyId); + cellReference.set(propertyId); + String style = generator.getStyle(cellReference); if (style != null) { if (cellStyles == null) { cellStyles = Json.createObject(); @@ -782,7 +793,11 @@ public class RpcDataProviderExtension extends AbstractExtension { rowObject.put(GridState.JSONKEY_CELLSTYLES, cellStyles); } - String rowStyle = generator.getStyle(grid, itemId, null); + } + + private void setGeneratedRowStyles(RowStyleGenerator generator, + JsonObject rowObject) { + String rowStyle = generator.getStyle(rowReference); if (rowStyle != null) { rowObject.put(GridState.JSONKEY_ROWSTYLE, rowStyle); } @@ -886,6 +901,13 @@ public class RpcDataProviderExtension extends AbstractExtension { .removeItemSetChangeListener(itemListener); } + } else if (parent instanceof Grid) { + Grid grid = (Grid) parent; + rowReference = new RowReference(grid); + cellReference = new CellReference(rowReference); + } else { + throw new IllegalStateException( + "Grid is the only accepted parent type"); } } diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java index 22bb38a8c0..1842e3e757 100644 --- a/server/src/com/vaadin/ui/Grid.java +++ b/server/src/com/vaadin/ui/Grid.java @@ -774,30 +774,180 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, } /** - * Callback interface for generating custom style names for data rows and - * cells. - * - * @see Grid#setCellStyleGenerator(CellStyleGenerator) + * A data class which contains information which identifies a row in a + * {@link Grid}. + * <p> + * Since this class follows the <code>Flyweight</code>-pattern any instance + * of this object is subject to change without the user knowing it and so + * should not be stored anywhere outside of the method providing these + * instances. */ - public interface CellStyleGenerator extends Serializable { + public static class RowReference implements Serializable { + private final Grid grid; + + private Object itemId; /** - * 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. + * Creates a new row reference for the given grid. * * @param grid - * the source grid + * the grid that the row belongs to + */ + public RowReference(Grid grid) { + this.grid = grid; + } + + /** + * Sets the identifying information for this row + * * @param itemId - * the itemId of the target row + * the item id of the row + */ + public void set(Object itemId) { + this.itemId = itemId; + } + + /** + * Gets the grid that contains the referenced row. + * + * @return the grid that contains referenced row + */ + public Grid getGrid() { + return grid; + } + + /** + * Gets the item id of the row. + * + * @return the item id of the row + */ + public Object getItemId() { + return itemId; + } + + /** + * Gets the item for the row. + * + * @return the item for the row + */ + public Item getItem() { + return grid.getContainerDataSource().getItem(itemId); + } + } + + /** + * A data class which contains information which identifies a cell in a + * {@link Grid}. + * <p> + * Since this class follows the <code>Flyweight</code>-pattern any instance + * of this object is subject to change without the user knowing it and so + * should not be stored anywhere outside of the method providing these + * instances. + */ + public static class CellReference implements Serializable { + private final RowReference rowReference; + + private Object propertyId; + + public CellReference(RowReference rowReference) { + this.rowReference = rowReference; + } + + /** + * Sets the identifying information for this cell + * * @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 + * the property id of the column + */ + public void set(Object propertyId) { + this.propertyId = propertyId; + } + + /** + * Gets the grid that contains the referenced cell. + * + * @return the grid that contains referenced cell + */ + public Grid getGrid() { + return rowReference.getGrid(); + } + + /** + * @return the property id of the column + */ + public Object getPropertyId() { + return propertyId; + } + + /** + * @return the property for the cell + */ + public Property<?> getProperty() { + return getItem().getItemProperty(propertyId); + } + + /** + * Gets the item id of the row of the cell. + * + * @return the item id of the row + */ + public Object getItemId() { + return rowReference.getItemId(); + } + + /** + * Gets the item for the row of the cell. + * + * @return the item for the row + */ + public Item getItem() { + return rowReference.getItem(); + } + + /** + * Gets the value of the cell. + * + * @return the value of the cell */ - public abstract String getStyle(Grid grid, Object itemId, - Object propertyId); + public Object getValue() { + return getProperty().getValue(); + } + } + + /** + * Callback interface for generating custom style names for data rows + * + * @see Grid#setRowStyleGenerator(RowStyleGenerator) + */ + public interface RowStyleGenerator extends Serializable { + + /** + * Called by Grid to generate a style name for a row + * + * @param rowReference + * The row to generate a style for + * @return the style name to add to this row, or {@code null} to not set + * any style + */ + public String getStyle(RowReference rowReference); + } + + /** + * Callback interface for generating custom style names for cells + * + * @see Grid#setCellStyleGenerator(CellStyleGenerator) + */ + public interface CellStyleGenerator extends Serializable { + + /** + * Called by Grid to generate a style name for a column + * + * @param cellReference + * The cell to generate a style for + * @return the style name to add to this cell, or {@code null} to not + * set any style + */ + public String getStyle(CellReference cellReference); } /** @@ -2248,6 +2398,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, private FieldGroup editorRowFieldGroup = new CustomFieldGroup(); private CellStyleGenerator cellStyleGenerator; + private RowStyleGenerator rowStyleGenerator; /** * <code>true</code> if Grid is using the internal IndexedContainer created @@ -3835,8 +3986,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, } /** - * Sets the cell style generator that is used for generating styles for rows - * and cells. + * Sets the style generator that is used for generating styles for cells * * @param cellStyleGenerator * the cell style generator to set, or <code>null</code> to @@ -3850,8 +4000,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, } /** - * Gets the cell style generator that is used for generating styles for rows - * and cells. + * Gets the style generator that is used for generating styles for cells * * @return the cell style generator, or <code>null</code> if no generator is * set @@ -3861,6 +4010,30 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier, } /** + * Sets the style generator that is used for generating styles for rows + * + * @param rowStyleGenerator + * the row style generator to set, or <code>null</code> to remove + * a previously set generator + */ + public void setRowStyleGenerator(RowStyleGenerator rowStyleGenerator) { + this.rowStyleGenerator = rowStyleGenerator; + getState().hasRowStyleGenerator = (rowStyleGenerator != null); + + datasourceExtension.refreshCache(); + } + + /** + * Gets the style generator that is used for generating styles for rows + * + * @return the row style generator, or <code>null</code> if no generator is + * set + */ + public RowStyleGenerator getRowStyleGenerator() { + return rowStyleGenerator; + } + + /** * Adds a row to the underlying container. The order of the parameters * should match the current visible column order. * <p> |