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 /client/src | |
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 'client/src')
6 files changed, 359 insertions, 62 deletions
diff --git a/client/src/com/vaadin/client/connectors/GridConnector.java b/client/src/com/vaadin/client/connectors/GridConnector.java index 92b6296efa..e076faeaaa 100644 --- a/client/src/com/vaadin/client/connectors/GridConnector.java +++ b/client/src/com/vaadin/client/connectors/GridConnector.java @@ -43,7 +43,6 @@ import com.vaadin.client.ui.AbstractHasComponentsConnector; import com.vaadin.client.ui.SimpleManagedLayout; import com.vaadin.client.ui.grid.EditorRowHandler; import com.vaadin.client.ui.grid.Grid; -import com.vaadin.client.ui.grid.Grid.CellStyleGenerator; import com.vaadin.client.ui.grid.Grid.FooterCell; import com.vaadin.client.ui.grid.Grid.FooterRow; import com.vaadin.client.ui.grid.Grid.HeaderCell; @@ -61,6 +60,10 @@ import com.vaadin.client.ui.grid.selection.SelectionModelSingle; import com.vaadin.client.ui.grid.sort.SortEvent; import com.vaadin.client.ui.grid.sort.SortHandler; import com.vaadin.client.ui.grid.sort.SortOrder; +import com.vaadin.client.widget.grid.CellReference; +import com.vaadin.client.widget.grid.CellStyleGenerator; +import com.vaadin.client.widget.grid.RowReference; +import com.vaadin.client.widget.grid.RowStyleGenerator; import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.grid.EditorRowClientRpc; import com.vaadin.shared.ui.grid.EditorRowServerRpc; @@ -93,30 +96,41 @@ public class GridConnector extends AbstractHasComponentsConnector implements private static final class CustomCellStyleGenerator implements CellStyleGenerator<JSONObject> { @Override - public String getStyle(Grid<JSONObject> grid, JSONObject row, - int rowIndex, GridColumn<?, JSONObject> column, int columnIndex) { - if (column == null) { - JSONValue styleValue = row.get(GridState.JSONKEY_ROWSTYLE); - if (styleValue != null) { - return styleValue.isString().stringValue(); - } else { - return null; - } + public String getStyle(CellReference<JSONObject> cellReference) { + JSONValue cellstyles = cellReference.getRow().get( + GridState.JSONKEY_CELLSTYLES); + if (cellstyles == null) { + return null; + } + + CustomGridColumn c = (CustomGridColumn) cellReference.getColumn(); + + JSONObject cellStylesObject = cellstyles.isObject(); + assert cellStylesObject != null; + + JSONValue styleValue = cellStylesObject.get(c.id); + if (styleValue != null) { + return styleValue.isString().stringValue(); } else { - JSONValue cellstyles = row.get(GridState.JSONKEY_CELLSTYLES); - if (cellstyles == null) { - return null; - } + return null; + } + } - CustomGridColumn c = (CustomGridColumn) column; - JSONValue styleValue = cellstyles.isObject().get(c.id); - if (styleValue != null) { - return styleValue.isString().stringValue(); - } else { - return null; - } + } + + private static final class CustomRowStyleGenerator implements + RowStyleGenerator<JSONObject> { + @Override + public String getStyle(RowReference<JSONObject> rowReference) { + JSONValue styleValue = rowReference.getRow().get( + GridState.JSONKEY_ROWSTYLE); + if (styleValue != null) { + return styleValue.isString().stringValue(); + } else { + return null; } } + } /** @@ -725,6 +739,15 @@ public class GridConnector extends AbstractHasComponentsConnector implements } } + @OnStateChange("hasRowStyleGenerator") + private void onRowStyleGeneratorChange() { + if (getState().hasRowStyleGenerator) { + getWidget().setRowStyleGenerator(new CustomRowStyleGenerator()); + } else { + getWidget().setRowStyleGenerator(null); + } + } + @OnStateChange("selectedKeys") private void updateSelectionFromState() { boolean changed = false; diff --git a/client/src/com/vaadin/client/ui/grid/Grid.java b/client/src/com/vaadin/client/ui/grid/Grid.java index d19deaef4d..a930fcdc66 100644 --- a/client/src/com/vaadin/client/ui/grid/Grid.java +++ b/client/src/com/vaadin/client/ui/grid/Grid.java @@ -101,6 +101,10 @@ import com.vaadin.client.ui.grid.sort.Sort; import com.vaadin.client.ui.grid.sort.SortEvent; import com.vaadin.client.ui.grid.sort.SortHandler; import com.vaadin.client.ui.grid.sort.SortOrder; +import com.vaadin.client.widget.grid.CellReference; +import com.vaadin.client.widget.grid.CellStyleGenerator; +import com.vaadin.client.widget.grid.RowReference; +import com.vaadin.client.widget.grid.RowStyleGenerator; import com.vaadin.shared.ui.grid.GridColumnState; import com.vaadin.shared.ui.grid.GridConstants; import com.vaadin.shared.ui.grid.GridStaticCellType; @@ -155,37 +159,6 @@ public class Grid<T> extends ResizeComposite implements HasSelectionChangeHandlers<T>, SubPartAware, DeferredWorker { /** - * Callback interface for generating custom style names for data rows and - * cells. - * - * @see Grid#setCellStyleGenerator(CellStyleGenerator) - */ - public interface CellStyleGenerator<T> { - - /** - * 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. - * - * @param grid - * the source grid - * @param row - * the data object of the target row - * @param rowIndex - * the index of the row - * @param column - * the column of the cell, <code>null</code> when getting a - * row style - * @param columnIndex - * the index of the column, -1 when getting a 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<T> grid, T row, int rowIndex, - GridColumn<?, T> column, int columnIndex); - } - - /** * Abstract base class for Grid header and footer sections. * * @param <ROWTYPE> @@ -3042,14 +3015,16 @@ public class Grid<T> extends ResizeComposite implements boolean isEvenIndex = (row.getRow() % 2 == 0); setStyleName(rowElement, rowStripeStyleName, isEvenIndex); + rowReference.set(rowIndex, rowData); + if (hasData) { setStyleName(rowElement, rowSelectedStyleName, isSelected(rowData)); - if (cellStyleGenerator != null) { + if (rowStyleGenerator != null) { try { - String rowStylename = cellStyleGenerator.getStyle( - Grid.this, rowData, rowIndex, null, -1); + String rowStylename = rowStyleGenerator + .getStyle(rowReference); setCustomStyleName(rowElement, rowStylename); } catch (RuntimeException e) { getLogger().log( @@ -3080,9 +3055,9 @@ public class Grid<T> extends ResizeComposite implements if (hasData && cellStyleGenerator != null) { try { - String generatedStyle = cellStyleGenerator.getStyle( - Grid.this, rowData, rowIndex, column, - cell.getColumn()); + cellReference.set(cell.getColumn(), column); + String generatedStyle = cellStyleGenerator + .getStyle(cellReference); setCustomStyleName(cell.getElement(), generatedStyle); } catch (RuntimeException e) { getLogger().log( @@ -4598,6 +4573,9 @@ public class Grid<T> extends ResizeComposite implements private Point rowEventTouchStartingPoint; private CellStyleGenerator<T> cellStyleGenerator; + private RowStyleGenerator<T> rowStyleGenerator; + private RowReference<T> rowReference = new RowReference<T>(this); + private CellReference<T> cellReference = new CellReference<T>(rowReference); private boolean handleHeaderDefaultRowEvent(Event event, RowContainer container, final Cell cell) { @@ -5375,8 +5353,7 @@ public class Grid<T> extends ResizeComposite implements } /** - * 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 @@ -5388,8 +5365,7 @@ public class Grid<T> extends ResizeComposite implements } /** - * 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 @@ -5398,6 +5374,28 @@ public class Grid<T> extends ResizeComposite implements return cellStyleGenerator; } + /** + * 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<T> rowStyleGenerator) { + this.rowStyleGenerator = rowStyleGenerator; + refreshBody(); + } + + /** + * 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<T> getRowStyleGenerator() { + return rowStyleGenerator; + } + private static void setCustomStyleName(Element element, String styleName) { assert element != null; diff --git a/client/src/com/vaadin/client/widget/grid/CellReference.java b/client/src/com/vaadin/client/widget/grid/CellReference.java new file mode 100644 index 0000000000..66aa40f702 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/CellReference.java @@ -0,0 +1,109 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.client.widget.grid; + +import com.vaadin.client.ui.grid.Grid; +import com.vaadin.client.ui.grid.GridColumn; + +/** + * 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. + * + * @param <T> + * the type of the row object containing this cell + */ +public class CellReference<T> { + private int columnIndex; + private GridColumn<?, T> column; + private final RowReference<T> rowReference; + + public CellReference(RowReference<T> rowReference) { + this.rowReference = rowReference; + } + + /** + * Sets the identifying information for this cell. + * + * @param rowReference + * a reference to the row that contains this cell + * @param columnIndex + * the index of the column + * @param column + * the column object + */ + public void set(int columnIndex, GridColumn<?, T> column) { + this.columnIndex = columnIndex; + this.column = column; + } + + /** + * Gets the grid that contains the referenced cell. + * + * @return the grid that contains referenced cell + */ + public Grid<T> getGrid() { + return rowReference.getGrid(); + } + + /** + * Gets the row index of the row. + * + * @return the index of the row + */ + public int getRowIndex() { + return rowReference.getRowIndex(); + } + + /** + * Gets the row data object. + * + * @return the row object + */ + public T getRow() { + return rowReference.getRow(); + } + + /** + * Gets the index of the column. + * + * @return the index of the column + */ + public int getColumnIndex() { + return columnIndex; + } + + /** + * Gets the column objects. + * + * @return the column object + */ + public GridColumn<?, T> getColumn() { + return column; + } + + /** + * Gets the value of the cell. + * + * @return the value of the cell + */ + public Object getValue() { + return getColumn().getValue(getRow()); + } +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/CellStyleGenerator.java b/client/src/com/vaadin/client/widget/grid/CellStyleGenerator.java new file mode 100644 index 0000000000..079d3c521b --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/CellStyleGenerator.java @@ -0,0 +1,39 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.client.widget.grid; + +import com.vaadin.client.ui.grid.Grid; + +/** + * Callback interface for generating custom style names for cells + * + * @param <T> + * the row type of the target grid + * + * @see Grid#setCellStyleGenerator(CellStyleGenerator) + */ +public interface CellStyleGenerator<T> { + + /** + * Called by Grid to generate a style name for a column element. + * + * @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 abstract String getStyle(CellReference<T> cellReference); +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/RowReference.java b/client/src/com/vaadin/client/widget/grid/RowReference.java new file mode 100644 index 0000000000..8dd836cb77 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/RowReference.java @@ -0,0 +1,87 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.client.widget.grid; + +import com.vaadin.client.ui.grid.Grid; + +/** + * 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. + * + * @param <T> + * the row object type + */ +public class RowReference<T> { + private final Grid<T> grid; + + private int rowIndex; + private T row; + + /** + * Creates a new row reference for the given grid. + * + * @param grid + * the grid that the row belongs to + */ + public RowReference(Grid<T> grid) { + this.grid = grid; + } + + /** + * Sets the identifying information for this row. + * + * @param rowIndex + * the index of the row + * @param row + * the row object + */ + public void set(int rowIndex, T row) { + this.rowIndex = rowIndex; + this.row = row; + } + + /** + * Gets the grid that contains the referenced row. + * + * @return the grid that contains referenced row + */ + public Grid<T> getGrid() { + return grid; + } + + /** + * Gets the row index of the row. + * + * @return the index of the row + */ + public int getRowIndex() { + return rowIndex; + } + + /** + * Gets the row data object. + * + * @return the row object + */ + public T getRow() { + return row; + } + +}
\ No newline at end of file diff --git a/client/src/com/vaadin/client/widget/grid/RowStyleGenerator.java b/client/src/com/vaadin/client/widget/grid/RowStyleGenerator.java new file mode 100644 index 0000000000..ba2d6bc238 --- /dev/null +++ b/client/src/com/vaadin/client/widget/grid/RowStyleGenerator.java @@ -0,0 +1,41 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.client.widget.grid; + +import java.io.Serializable; + +import com.vaadin.client.ui.grid.Grid; + +/** + * Callback interface for generating custom style names for data rows + * + * @param <T> + * the row type of the target grid + * + * @see Grid#setRowStyleGenerator(RowStyleGenerator) + */ +public interface RowStyleGenerator<T> 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 abstract String getStyle(RowReference<T> rowReference); +}
\ No newline at end of file |