Change-Id: I299ad824c7154d08286ba9597e2ac8ee01791988feature/vaadin8-book-vol2
@@ -40,7 +40,7 @@ public class ColumnConnector extends AbstractExtensionConnector { | |||
@Override | |||
public JsonValue getValue(JsonObject row) { | |||
return row.getObject(DataProviderConstants.DATA).get( | |||
getConnectorId()); | |||
getState().communicationId); | |||
} | |||
}; | |||
getParent().addColumn(column); |
@@ -30,10 +30,9 @@ public class Column<T, V> extends AbstractExtension implements | |||
private Function<T, V> getter; | |||
Column(String caption, Function<T, V> getter, Grid<T> grid) { | |||
Column(String caption, Function<T, V> getter) { | |||
this.getter = getter; | |||
getState().caption = caption; | |||
extend(grid); | |||
} | |||
@Override | |||
@@ -43,7 +42,7 @@ public class Column<T, V> extends AbstractExtension implements | |||
} | |||
JsonObject obj = jsonObject.getObject(DataProviderConstants.DATA); | |||
// TODO: Renderers | |||
obj.put(getConnectorId(), getter.apply(data).toString()); | |||
obj.put(getState(false).communicationId, getter.apply(data).toString()); | |||
} | |||
@Override | |||
@@ -51,6 +50,18 @@ public class Column<T, V> extends AbstractExtension implements | |||
} | |||
public ColumnState getState() { | |||
return (ColumnState) super.getState(); | |||
return getState(true); | |||
} | |||
public ColumnState getState(boolean markAsDirty) { | |||
return (ColumnState) super.getState(markAsDirty); | |||
} | |||
void extend(Grid<T> grid) { | |||
super.extend(grid); | |||
} | |||
void setCommunicationId(String key) { | |||
getState().communicationId = key; | |||
} | |||
} |
@@ -15,33 +15,38 @@ | |||
*/ | |||
package com.vaadin.tokka.ui.components.grid; | |||
import java.util.LinkedHashMap; | |||
import java.util.Map; | |||
import java.util.HashSet; | |||
import java.util.Set; | |||
import java.util.function.Function; | |||
import com.vaadin.tokka.server.communication.data.DataSource; | |||
import com.vaadin.tokka.server.communication.data.KeyMapper; | |||
import com.vaadin.tokka.server.communication.data.SingleSelection; | |||
import com.vaadin.tokka.ui.components.AbstractListing; | |||
public class Grid<T> extends AbstractListing<T> { | |||
private DataSource<T, ?> dataSource; | |||
private Map<String, Column<T, ?>> columns = new LinkedHashMap<>(); | |||
private KeyMapper<Column<T, ?>> columnKeys = new KeyMapper<>(); | |||
private Set<Column<T, ?>> columnSet = new HashSet<>(); | |||
public Grid() { | |||
setSelectionModel(new SingleSelection<T>()); | |||
} | |||
public <V> Column<T, V> addColumn(String caption, Function<T, V> getter) { | |||
Column<T, V> c = new Column<T, V>(caption, getter, this); | |||
columns.put(c.getConnectorId(), c); | |||
Column<T, V> c = new Column<T, V>(caption, getter); | |||
c.extend(this); | |||
c.setCommunicationId(columnKeys.key(c)); | |||
columnSet.add(c); | |||
addDataGenerator(c); | |||
return c; | |||
} | |||
public void removeColumn(Column<T, ?> column) { | |||
if (columns.containsValue(column)) { | |||
removeDataGenerator(columns.remove(column.getConnectorId())); | |||
if (columnSet.remove(column)) { | |||
columnKeys.remove(column); | |||
removeDataGenerator(column); | |||
column.remove(); | |||
} | |||
} |
@@ -0,0 +1,12 @@ | |||
package com.vaadin.tokka.ui.components.grid; | |||
import org.junit.Test; | |||
public class GridTest { | |||
@Test | |||
public void testAddColumnWithoutAttach() { | |||
Grid<String> grid = new Grid<>(); | |||
grid.addColumn("Length", String::length); | |||
} | |||
} |
@@ -20,4 +20,5 @@ import com.vaadin.shared.communication.SharedState; | |||
public class ColumnState extends SharedState { | |||
public String caption; | |||
public String communicationId; | |||
} |