aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <teemusa@vaadin.com>2014-12-04 15:44:22 +0200
committerHenrik Paul <henrik@vaadin.com>2014-12-08 14:29:53 +0000
commit44c001568254102445c5e352e2e509f091ab1dec (patch)
treeb7877b4c54018b9e3686633c1b166e8eb939237d
parent71fc0accc54d16b9445d81e1c71a98b9c332b332 (diff)
downloadvaadin-framework-44c001568254102445c5e352e2e509f091ab1dec.tar.gz
vaadin-framework-44c001568254102445c5e352e2e509f091ab1dec.zip
Add getColumns function to Grid (#13334)
Since Grid on the server side does many things with properties this patch also adds a way to get the backing property id for a column. Change-Id: Ia78c611a28b566593c3291681904ac14cf0c48ee
-rw-r--r--server/src/com/vaadin/data/RpcDataProviderExtension.java33
-rw-r--r--server/src/com/vaadin/ui/Grid.java35
2 files changed, 47 insertions, 21 deletions
diff --git a/server/src/com/vaadin/data/RpcDataProviderExtension.java b/server/src/com/vaadin/data/RpcDataProviderExtension.java
index c63864f79b..c4a4e3f22b 100644
--- a/server/src/com/vaadin/data/RpcDataProviderExtension.java
+++ b/server/src/com/vaadin/data/RpcDataProviderExtension.java
@@ -709,34 +709,29 @@ public class RpcDataProviderExtension extends AbstractExtension {
}
private void pushRows(int firstRow, List<?> itemIds) {
- Collection<?> propertyIds = container.getContainerPropertyIds();
JsonArray rows = Json.createArray();
for (int i = 0; i < itemIds.size(); ++i) {
- rows.set(i, getRowData(propertyIds, itemIds.get(i)));
+ rows.set(i, getRowData(getGrid().getColumns(), itemIds.get(i)));
}
rpc.setRowData(firstRow, rows.toJson());
}
- private JsonValue getRowData(Collection<?> propertyIds, Object itemId) {
+ private JsonValue getRowData(Collection<Column> columns, Object itemId) {
Item item = container.getItem(itemId);
JsonObject rowData = Json.createObject();
Grid grid = getGrid();
- for (Object propertyId : propertyIds) {
- Column column = grid.getColumn(propertyId);
+ for (Column column : columns) {
+ Object propertyId = column.getColumnProperty();
- // TODO: Optimize this with Grid.getColumns() 04.12.2014 -Teemu
- if (column != null) {
- Object propertyValue = item.getItemProperty(propertyId)
- .getValue();
- JsonValue encodedValue = encodeValue(propertyValue,
- column.getRenderer(), column.getConverter(),
- grid.getLocale());
+ Object propertyValue = item.getItemProperty(propertyId).getValue();
+ JsonValue encodedValue = encodeValue(propertyValue,
+ column.getRenderer(), column.getConverter(),
+ grid.getLocale());
- rowData.put(columnKeys.key(propertyId), encodedValue);
- }
+ rowData.put(columnKeys.key(propertyId), encodedValue);
}
final JsonObject rowObject = Json.createObject();
@@ -745,19 +740,19 @@ public class RpcDataProviderExtension extends AbstractExtension {
CellStyleGenerator cellStyleGenerator = grid.getCellStyleGenerator();
if (cellStyleGenerator != null) {
- setGeneratedStyles(cellStyleGenerator, rowObject, propertyIds,
- itemId);
+ setGeneratedStyles(cellStyleGenerator, rowObject, columns, itemId);
}
return rowObject;
}
private void setGeneratedStyles(CellStyleGenerator generator,
- JsonObject rowObject, Collection<?> propertyIds, Object itemId) {
+ JsonObject rowObject, Collection<Column> columns, Object itemId) {
Grid grid = getGrid();
JsonObject cellStyles = null;
- for (Object propertyId : propertyIds) {
+ for (Column column : columns) {
+ Object propertyId = column.getColumnProperty();
String style = generator.getStyle(grid, itemId, propertyId);
if (style != null) {
if (cellStyles == null) {
@@ -849,7 +844,7 @@ public class RpcDataProviderExtension extends AbstractExtension {
* roundtrip.
*/
Object itemId = container.getIdByIndex(index);
- JsonValue row = getRowData(container.getContainerPropertyIds(), itemId);
+ JsonValue row = getRowData(getGrid().getColumns(), itemId);
JsonArray rowArray = Json.createArray();
rowArray.set(0, row);
rpc.setRowData(index, rowArray.toJson());
diff --git a/server/src/com/vaadin/ui/Grid.java b/server/src/com/vaadin/ui/Grid.java
index f12a774004..00d6bbc957 100644
--- a/server/src/com/vaadin/ui/Grid.java
+++ b/server/src/com/vaadin/ui/Grid.java
@@ -974,6 +974,11 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier,
*/
private final Grid grid;
+ /**
+ * Backing property for column
+ */
+ private final Object columnProperty;
+
private Converter<?, Object> converter;
/**
@@ -990,10 +995,13 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier,
* The grid this column belongs to. Should not be null.
* @param state
* the shared state of this column
+ * @param columnProperty
+ * the backing property id for this column
*/
- Column(Grid grid, GridColumnState state) {
+ Column(Grid grid, GridColumnState state, Object columnProperty) {
this.grid = grid;
this.state = state;
+ this.columnProperty = columnProperty;
internalSetRenderer(new TextRenderer());
}
@@ -1008,6 +1016,15 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier,
}
/**
+ * Return the property id for the backing property of this Column
+ *
+ * @return property id
+ */
+ public Object getColumnProperty() {
+ return columnProperty;
+ }
+
+ /**
* Returns the caption of the header. By default the header caption is
* the property id of the column.
*
@@ -2337,6 +2354,20 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier,
}
/**
+ * Returns a copy of currently configures columns in their current visual
+ * order in this Grid.
+ *
+ * @return unmodifiable copy of current columns in visual order
+ */
+ public Collection<Column> getColumns() {
+ List<Column> columns = new ArrayList<Grid.Column>();
+ for (String columnId : getState(false).columnOrder) {
+ columns.add(getColumnByColumnId(columnId));
+ }
+ return Collections.unmodifiableList(columns);
+ }
+
+ /**
* Adds a new Column to Grid. Also adds the property to container with data
* type String, if property for column does not exist in it. Default value
* for the new property is an empty String.
@@ -2481,7 +2512,7 @@ public class Grid extends AbstractComponent implements SelectionChangeNotifier,
GridColumnState columnState = new GridColumnState();
columnState.id = columnKeys.key(datasourcePropertyId);
- Column column = new Column(this, columnState);
+ Column column = new Column(this, columnState, datasourcePropertyId);
columns.put(datasourcePropertyId, column);
getState().columns.add(columnState);