Selaa lähdekoodia

Split CellStyleGenerator into separate cell and row style generators

(#13334)

Change-Id: If07018b6f74ff1a4c616705f61b6118647d64342
tags/7.4.0.beta1
Artur Signell 9 vuotta sitten
vanhempi
commit
c9fe985fc3

+ 44
- 21
client/src/com/vaadin/client/connectors/GridConnector.java Näytä tiedosto

@@ -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;

+ 39
- 41
client/src/com/vaadin/client/ui/grid/Grid.java Näytä tiedosto

@@ -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;
@@ -154,37 +158,6 @@ import com.vaadin.shared.util.SharedUtil;
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.
*
@@ -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;


+ 109
- 0
client/src/com/vaadin/client/widget/grid/CellReference.java Näytä tiedosto

@@ -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());
}
}

+ 39
- 0
client/src/com/vaadin/client/widget/grid/CellStyleGenerator.java Näytä tiedosto

@@ -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);
}

+ 87
- 0
client/src/com/vaadin/client/widget/grid/RowReference.java Näytä tiedosto

@@ -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;
}

}

+ 41
- 0
client/src/com/vaadin/client/widget/grid/RowStyleGenerator.java Näytä tiedosto

@@ -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);
}

+ 29
- 7
server/src/com/vaadin/data/RpcDataProviderExtension.java Näytä tiedosto

@@ -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");
}
}


+ 193
- 20
server/src/com/vaadin/ui/Grid.java Näytä tiedosto

@@ -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
@@ -3860,6 +4009,30 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier,
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 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.

+ 4
- 1
shared/src/com/vaadin/shared/ui/grid/GridState.java Näytä tiedosto

@@ -140,6 +140,9 @@ public class GridState extends AbstractComponentState {
/** The enabled state of the editor row */
public boolean editorRowEnabled = false;

/** Whether row data might contain generated styles */
/** Whether row data might contain generated row styles */
public boolean hasRowStyleGenerator;
/** Whether row data might contain generated cell styles */
public boolean hasCellStyleGenerator;

}

+ 69
- 42
uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java Näytä tiedosto

@@ -42,12 +42,15 @@ import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
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.FooterCell;
import com.vaadin.ui.Grid.HeaderCell;
import com.vaadin.ui.Grid.HeaderRow;
import com.vaadin.ui.Grid.MultiSelectionModel;
import com.vaadin.ui.Grid.RowReference;
import com.vaadin.ui.Grid.RowStyleGenerator;
import com.vaadin.ui.Grid.SelectionMode;
import com.vaadin.ui.renderer.DateRenderer;
import com.vaadin.ui.renderer.HtmlRenderer;
@@ -61,6 +64,12 @@ import com.vaadin.ui.renderer.NumberRenderer;
*/
public class GridBasicFeatures extends AbstractComponentTest<Grid> {

public static final String ROW_STYLE_GENERATOR_ROW_NUMBERS_FOR_3_OF_4 = "Row numbers for 3/4";
public static final String ROW_STYLE_GENERATOR_NONE = "None";
public static final String ROW_STYLE_GENERATOR_ROW_NUMBERS = "Row numbers";
public static final String CELL_STYLE_GENERATOR_NONE = "None";
public static final String CELL_STYLE_GENERATOR_PROPERTY_TO_STRING = "Property to string";
public static final String CELL_STYLE_GENERATOR_SPECIAL = "Special for 1/4 Column 1";
private static final int MANUALLY_FORMATTED_COLUMNS = 5;
public static final int COLUMNS = 12;
public static final int ROWS = 1000;
@@ -306,50 +315,68 @@ public class GridBasicFeatures extends AbstractComponentTest<Grid> {
}
});

LinkedHashMap<String, CellStyleGenerator> styleGenerators = new LinkedHashMap<String, CellStyleGenerator>();
styleGenerators.put("None", null);
styleGenerators.put("Row only", new CellStyleGenerator() {
@Override
public String getStyle(Grid grid, Object itemId, Object propertyId) {
if (propertyId == null) {
return "row" + itemId;
} else {
return null;
}
}
});
styleGenerators.put("Cell only", new CellStyleGenerator() {
@Override
public String getStyle(Grid grid, Object itemId, Object propertyId) {
if (propertyId == null) {
return null;
} else {
return propertyId.toString().replace(' ', '-');
}
}
});
styleGenerators.put("Combined", new CellStyleGenerator() {
@Override
public String getStyle(Grid grid, Object itemId, Object propertyId) {
int rowIndex = ((Integer) itemId).intValue();
if (propertyId == null) {
if (rowIndex % 4 == 0) {
return null;
} else {
return "row" + itemId;
LinkedHashMap<String, CellStyleGenerator> cellStyleGenerators = new LinkedHashMap<String, CellStyleGenerator>();
LinkedHashMap<String, RowStyleGenerator> rowStyleGenerators = new LinkedHashMap<String, RowStyleGenerator>();
rowStyleGenerators.put(ROW_STYLE_GENERATOR_NONE, null);
rowStyleGenerators.put(ROW_STYLE_GENERATOR_ROW_NUMBERS,
new RowStyleGenerator() {
@Override
public String getStyle(RowReference rowReference) {
return "row" + rowReference.getItemId();
}
} else {
if (rowIndex % 4 == 1) {
return null;
} else if (rowIndex % 4 == 3
&& "Column 1".equals(propertyId)) {
return null;
});
rowStyleGenerators.put(ROW_STYLE_GENERATOR_ROW_NUMBERS_FOR_3_OF_4,
new RowStyleGenerator() {
@Override
public String getStyle(RowReference rowReference) {
int rowIndex = ((Integer) rowReference.getItemId())
.intValue();

if (rowIndex % 4 == 0) {
return null;
} else {
return "row" + rowReference.getItemId();
}
}
return propertyId.toString().replace(' ', '_');
}
}
});
createSelectAction("Style generator", "State", styleGenerators, "None",
});
cellStyleGenerators.put(CELL_STYLE_GENERATOR_NONE, null);
cellStyleGenerators.put(CELL_STYLE_GENERATOR_PROPERTY_TO_STRING,
new CellStyleGenerator() {
@Override
public String getStyle(CellReference cellReference) {
return cellReference.getPropertyId().toString()
.replace(' ', '-');
}
});
cellStyleGenerators.put(CELL_STYLE_GENERATOR_SPECIAL,
new CellStyleGenerator() {
@Override
public String getStyle(CellReference cellReference) {
int rowIndex = ((Integer) cellReference.getItemId())
.intValue();
Object propertyId = cellReference.getPropertyId();
if (rowIndex % 4 == 1) {
return null;
} else if (rowIndex % 4 == 3
&& "Column 1".equals(propertyId)) {
return null;
}
return propertyId.toString().replace(' ', '_');
}
});

createSelectAction("Row style generator", "State", rowStyleGenerators,
CELL_STYLE_GENERATOR_NONE,
new Command<Grid, RowStyleGenerator>() {
@Override
public void execute(Grid grid, RowStyleGenerator generator,
Object data) {
grid.setRowStyleGenerator(generator);
}
});

createSelectAction("Cell style generator", "State",
cellStyleGenerators, CELL_STYLE_GENERATOR_NONE,
new Command<Grid, CellStyleGenerator>() {
@Override
public void execute(Grid grid,

+ 19
- 8
uitest/src/com/vaadin/tests/components/grid/basicfeatures/client/GridCellStyleGeneratorTest.java Näytä tiedosto

@@ -21,6 +21,7 @@ import org.junit.Test;
import com.vaadin.testbench.elements.GridElement.GridCellElement;
import com.vaadin.testbench.elements.GridElement.GridRowElement;
import com.vaadin.tests.components.grid.basicfeatures.GridBasicClientFeaturesTest;
import com.vaadin.tests.widgetset.client.grid.GridBasicClientFeaturesWidget;

public class GridCellStyleGeneratorTest extends GridBasicClientFeaturesTest {

@@ -28,7 +29,8 @@ public class GridCellStyleGeneratorTest extends GridBasicClientFeaturesTest {
public void testStyleNameGeneratorScrolling() throws Exception {
openTestURL();

selectStyleNameGenerator("Combined");
selectCellStyleNameGenerator(GridBasicClientFeaturesWidget.CELL_STYLE_GENERATOR_COL_INDEX);
selectRowStyleNameGenerator(GridBasicClientFeaturesWidget.ROW_STYLE_GENERATOR_ROW_INDEX);

GridRowElement row2 = getGridElement().getRow(2);
GridCellElement cell4_2 = getGridElement().getCell(4, 2);
@@ -49,7 +51,8 @@ public class GridCellStyleGeneratorTest extends GridBasicClientFeaturesTest {
public void testDisableStyleNameGenerator() throws Exception {
openTestURL();

selectStyleNameGenerator("Combined");
selectCellStyleNameGenerator(GridBasicClientFeaturesWidget.CELL_STYLE_GENERATOR_COL_INDEX);
selectRowStyleNameGenerator(GridBasicClientFeaturesWidget.ROW_STYLE_GENERATOR_ROW_INDEX);

// Just verify that change was effective
GridRowElement row2 = getGridElement().getRow(2);
@@ -59,7 +62,8 @@ public class GridCellStyleGeneratorTest extends GridBasicClientFeaturesTest {
Assert.assertTrue(hasCssClass(cell4_2, "4_2"));

// Disable the generator and check again
selectStyleNameGenerator("None");
selectCellStyleNameGenerator(GridBasicClientFeaturesWidget.CELL_STYLE_GENERATOR_NONE);
selectRowStyleNameGenerator(GridBasicClientFeaturesWidget.ROW_STYLE_GENERATOR_NONE);

Assert.assertFalse(hasCssClass(row2, "2"));
Assert.assertFalse(hasCssClass(cell4_2, "4_2"));
@@ -69,7 +73,8 @@ public class GridCellStyleGeneratorTest extends GridBasicClientFeaturesTest {
public void testChangeStyleNameGenerator() throws Exception {
openTestURL();

selectStyleNameGenerator("Combined");
selectCellStyleNameGenerator(GridBasicClientFeaturesWidget.CELL_STYLE_GENERATOR_COL_INDEX);
selectRowStyleNameGenerator(GridBasicClientFeaturesWidget.ROW_STYLE_GENERATOR_ROW_INDEX);

// Just verify that change was effective
GridRowElement row2 = getGridElement().getRow(2);
@@ -79,7 +84,8 @@ public class GridCellStyleGeneratorTest extends GridBasicClientFeaturesTest {
Assert.assertTrue(hasCssClass(cell4_2, "4_2"));

// Change the generator and check again
selectStyleNameGenerator("Cell only");
selectRowStyleNameGenerator(GridBasicClientFeaturesWidget.ROW_STYLE_GENERATOR_NONE);
selectCellStyleNameGenerator(GridBasicClientFeaturesWidget.CELL_STYLE_GENERATOR_SIMPLE);

// Old styles removed?
Assert.assertFalse(hasCssClass(row2, "2"));
@@ -93,7 +99,8 @@ public class GridCellStyleGeneratorTest extends GridBasicClientFeaturesTest {
public void testStyleNameGeneratorChangePrimary() throws Exception {
openTestURL();

selectStyleNameGenerator("Combined");
selectCellStyleNameGenerator(GridBasicClientFeaturesWidget.CELL_STYLE_GENERATOR_COL_INDEX);
selectRowStyleNameGenerator(GridBasicClientFeaturesWidget.ROW_STYLE_GENERATOR_ROW_INDEX);

// Just verify that change was effective
GridRowElement row2 = getGridElement().getRow(2);
@@ -114,7 +121,11 @@ public class GridCellStyleGeneratorTest extends GridBasicClientFeaturesTest {
Assert.assertFalse(hasCssClass(cell4_2, "v-escalator-cell-content-4_2"));
}

private void selectStyleNameGenerator(String name) {
selectMenuPath("Component", "State", "Style generator", name);
private void selectCellStyleNameGenerator(String name) {
selectMenuPath("Component", "State", "Cell style generator", name);
}

private void selectRowStyleNameGenerator(String name) {
selectMenuPath("Component", "State", "Row style generator", name);
}
}

+ 17
- 7
uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridCellStyleGeneratorTest.java Näytä tiedosto

@@ -20,6 +20,7 @@ import org.junit.Test;

import com.vaadin.testbench.elements.GridElement.GridCellElement;
import com.vaadin.testbench.elements.GridElement.GridRowElement;
import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures;
import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeaturesTest;

public class GridCellStyleGeneratorTest extends GridBasicFeaturesTest {
@@ -27,7 +28,8 @@ public class GridCellStyleGeneratorTest extends GridBasicFeaturesTest {
public void testStyleNameGeneratorScrolling() throws Exception {
openTestURL();

selectStyleNameGenerator("Combined");
selectRowStyleNameGenerator(GridBasicFeatures.ROW_STYLE_GENERATOR_ROW_NUMBERS_FOR_3_OF_4);
selectCellStyleNameGenerator(GridBasicFeatures.CELL_STYLE_GENERATOR_SPECIAL);

GridRowElement row2 = getGridElement().getRow(2);
GridCellElement cell3_2 = getGridElement().getCell(3, 2);
@@ -49,7 +51,8 @@ public class GridCellStyleGeneratorTest extends GridBasicFeaturesTest {
public void testDisableStyleNameGenerator() throws Exception {
openTestURL();

selectStyleNameGenerator("Combined");
selectRowStyleNameGenerator(GridBasicFeatures.ROW_STYLE_GENERATOR_ROW_NUMBERS_FOR_3_OF_4);
selectCellStyleNameGenerator(GridBasicFeatures.CELL_STYLE_GENERATOR_SPECIAL);

// Just verify that change was effective
GridRowElement row2 = getGridElement().getRow(2);
@@ -59,7 +62,8 @@ public class GridCellStyleGeneratorTest extends GridBasicFeaturesTest {
Assert.assertTrue(hasCssClass(cell3_2, "Column_2"));

// Disable the generator and check again
selectStyleNameGenerator("None");
selectRowStyleNameGenerator(GridBasicFeatures.ROW_STYLE_GENERATOR_NONE);
selectCellStyleNameGenerator(GridBasicFeatures.CELL_STYLE_GENERATOR_NONE);

Assert.assertFalse(hasCssClass(row2, "row2"));
Assert.assertFalse(hasCssClass(cell3_2, "Column_2"));
@@ -69,7 +73,8 @@ public class GridCellStyleGeneratorTest extends GridBasicFeaturesTest {
public void testChangeStyleNameGenerator() throws Exception {
openTestURL();

selectStyleNameGenerator("Combined");
selectRowStyleNameGenerator(GridBasicFeatures.ROW_STYLE_GENERATOR_ROW_NUMBERS_FOR_3_OF_4);
selectCellStyleNameGenerator(GridBasicFeatures.CELL_STYLE_GENERATOR_SPECIAL);

// Just verify that change was effective
GridRowElement row2 = getGridElement().getRow(2);
@@ -79,7 +84,8 @@ public class GridCellStyleGeneratorTest extends GridBasicFeaturesTest {
Assert.assertTrue(hasCssClass(cell3_2, "Column_2"));

// Change the generator and check again
selectStyleNameGenerator("Cell only");
selectRowStyleNameGenerator(GridBasicFeatures.ROW_STYLE_GENERATOR_NONE);
selectCellStyleNameGenerator(GridBasicFeatures.CELL_STYLE_GENERATOR_PROPERTY_TO_STRING);

// Old styles removed?
Assert.assertFalse(hasCssClass(row2, "row2"));
@@ -89,7 +95,11 @@ public class GridCellStyleGeneratorTest extends GridBasicFeaturesTest {
Assert.assertTrue(hasCssClass(cell3_2, "Column-2"));
}

private void selectStyleNameGenerator(String name) {
selectMenuPath("Component", "State", "Style generator", name);
private void selectRowStyleNameGenerator(String name) {
selectMenuPath("Component", "State", "Row style generator", name);
}

private void selectCellStyleNameGenerator(String name) {
selectMenuPath("Component", "State", "Cell style generator", name);
}
}

+ 77
- 52
uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java Näytä tiedosto

@@ -41,7 +41,6 @@ import com.vaadin.client.ui.grid.Cell;
import com.vaadin.client.ui.grid.EditorRowHandler;
import com.vaadin.client.ui.grid.FlyweightCell;
import com.vaadin.client.ui.grid.Grid;
import com.vaadin.client.ui.grid.Grid.CellStyleGenerator;
import com.vaadin.client.ui.grid.Grid.FooterRow;
import com.vaadin.client.ui.grid.Grid.HeaderRow;
import com.vaadin.client.ui.grid.Grid.SelectionMode;
@@ -68,6 +67,10 @@ import com.vaadin.client.ui.grid.renderers.HtmlRenderer;
import com.vaadin.client.ui.grid.renderers.NumberRenderer;
import com.vaadin.client.ui.grid.renderers.TextRenderer;
import com.vaadin.client.ui.grid.selection.SelectionModel.None;
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.tests.widgetset.client.grid.GridBasicClientFeaturesWidget.Data;

/**
@@ -78,6 +81,13 @@ import com.vaadin.tests.widgetset.client.grid.GridBasicClientFeaturesWidget.Data
*/
public class GridBasicClientFeaturesWidget extends
PureGWTTestApplication<Grid<List<Data>>> {
public static final String ROW_STYLE_GENERATOR_NONE = "None";
public static final String ROW_STYLE_GENERATOR_ROW_INDEX = "Row numbers";
public static final String ROW_STYLE_GENERATOR_EVERY_THIRD = "Every third";

public static final String CELL_STYLE_GENERATOR_NONE = "None";
public static final String CELL_STYLE_GENERATOR_SIMPLE = "Simple";
public static final String CELL_STYLE_GENERATOR_COL_INDEX = "Column index";

public static enum Renderers {
TEXT_RENDERER, HTML_RENDERER, NUMBER_RENDERER, DATE_RENDERER;
@@ -412,8 +422,10 @@ public class GridBasicClientFeaturesWidget extends
String[] selectionModePath = { "Component", "State", "Selection mode" };
String[] primaryStyleNamePath = { "Component", "State",
"Primary Stylename" };
String[] styleGeneratorNamePath = { "Component", "State",
"Style generator" };
String[] rowStyleGeneratorNamePath = { "Component", "State",
"Row style generator" };
String[] cellStyleGeneratorNamePath = { "Component", "State",
"Cell style generator" };

addMenuCommand("multi", new ScheduledCommand() {
@Override
@@ -488,84 +500,97 @@ public class GridBasicClientFeaturesWidget extends
}
}, "Component", "State");

addMenuCommand("None", new ScheduledCommand() {
addMenuCommand(ROW_STYLE_GENERATOR_NONE, new ScheduledCommand() {
@Override
public void execute() {
grid.setCellStyleGenerator(null);
grid.setRowStyleGenerator(null);
}
}, styleGeneratorNamePath);
}, rowStyleGeneratorNamePath);

addMenuCommand("Row only", new ScheduledCommand() {
addMenuCommand(ROW_STYLE_GENERATOR_EVERY_THIRD, new ScheduledCommand() {
@Override
public void execute() {
grid.setCellStyleGenerator(new CellStyleGenerator<List<Data>>() {
grid.setRowStyleGenerator(new RowStyleGenerator<List<Data>>() {

@Override
public String getStyle(Grid<List<Data>> grid,
List<Data> row, int rowIndex,
GridColumn<?, List<Data>> column, int columnIndex) {
if (column == null) {
if (rowIndex % 3 == 0) {
return "third";
} else {
// First manual col is integer
Integer value = (Integer) row.get(COLUMNS
- MANUALLY_FORMATTED_COLUMNS).value;
return value.toString();
}
public String getStyle(RowReference<List<Data>> rowReference) {
if (rowReference.getRowIndex() % 3 == 0) {
return "third";
} else {
return null;
// First manual col is integer
Integer value = (Integer) rowReference.getRow()
.get(COLUMNS - MANUALLY_FORMATTED_COLUMNS).value;
return value.toString();
}
}
});

}
}, styleGeneratorNamePath);
}, rowStyleGeneratorNamePath);

addMenuCommand("Cell only", new ScheduledCommand() {
addMenuCommand(ROW_STYLE_GENERATOR_ROW_INDEX, new ScheduledCommand() {
@Override
public void execute() {
grid.setCellStyleGenerator(new CellStyleGenerator<List<Data>>() {
grid.setRowStyleGenerator(new RowStyleGenerator<List<Data>>() {

@Override
public String getStyle(Grid<List<Data>> grid,
List<Data> row, int rowIndex,
GridColumn<?, List<Data>> column, int columnIndex) {
if (column == null) {
return null;
} else {
if (column == grid.getColumn(2)) {
return "two";
} else if (column == grid.getColumn(COLUMNS
- MANUALLY_FORMATTED_COLUMNS)) {
// First manual col is integer
Integer value = (Integer) column.getValue(row);
return value.toString();

} else {
return null;
}
}
public String getStyle(RowReference<List<Data>> rowReference) {
return Integer.toString(rowReference.getRowIndex());
}
});

}
}, rowStyleGeneratorNamePath);

addMenuCommand(CELL_STYLE_GENERATOR_NONE, new ScheduledCommand() {
@Override
public void execute() {
grid.setCellStyleGenerator(null);
}
}, styleGeneratorNamePath);
}, cellStyleGeneratorNamePath);

addMenuCommand("Combined", new ScheduledCommand() {
addMenuCommand(CELL_STYLE_GENERATOR_SIMPLE, new ScheduledCommand() {
@Override
public void execute() {
grid.setCellStyleGenerator(new CellStyleGenerator<List<Data>>() {

@Override
public String getStyle(Grid<List<Data>> grid,
List<Data> row, int rowIndex,
GridColumn<?, List<Data>> column, int columnIndex) {
if (column == null) {
return Integer.toString(rowIndex);
public String getStyle(
CellReference<List<Data>> cellReference) {
GridColumn<?, List<Data>> column = cellReference
.getColumn();
if (column == grid.getColumn(2)) {
return "two";
} else if (column == grid.getColumn(COLUMNS
- MANUALLY_FORMATTED_COLUMNS)) {
// First manual col is integer
Integer value = (Integer) column
.getValue(cellReference.getRow());
return value.toString();

} else {
return rowIndex + "_"
+ grid.getColumns().indexOf(column);
return null;
}
}
});
}
}, styleGeneratorNamePath);
}, cellStyleGeneratorNamePath);
addMenuCommand(CELL_STYLE_GENERATOR_COL_INDEX, new ScheduledCommand() {
@Override
public void execute() {
grid.setCellStyleGenerator(new CellStyleGenerator<List<Data>>() {

@Override
public String getStyle(
CellReference<List<Data>> cellReference) {
return cellReference.getRowIndex()
+ "_"
+ grid.getColumns().indexOf(
cellReference.getColumn());
}
});
}
}, cellStyleGeneratorNamePath);

for (int i = -1; i <= COLUMNS; i++) {
final int index = i;

Loading…
Peruuta
Tallenna