aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main
diff options
context:
space:
mode:
authorJohannes Dahlström <johannesd@vaadin.com>2016-09-08 21:26:35 +0300
committerVaadin Code Review <review@vaadin.com>2016-09-12 08:19:48 +0000
commit34dc467f08440e2b1a4e563ede205590002ac64e (patch)
tree3d3f787d2d251877b32c8119209791764c85d8db /server/src/main
parent78a5468279ddc442ac64d045f5fe4aa79ed9ef6e (diff)
downloadvaadin-framework-34dc467f08440e2b1a4e563ede205590002ac64e.tar.gz
vaadin-framework-34dc467f08440e2b1a4e563ede205590002ac64e.zip
Reimplement Grid selection model in terms of AbstractSingleSelection
Change-Id: Id21cb0df32e07bd01f90a29a814dcf0775067bfa
Diffstat (limited to 'server/src/main')
-rw-r--r--server/src/main/java/com/vaadin/ui/Grid.java48
1 files changed, 42 insertions, 6 deletions
diff --git a/server/src/main/java/com/vaadin/ui/Grid.java b/server/src/main/java/com/vaadin/ui/Grid.java
index c895ad08c3..705a9ca890 100644
--- a/server/src/main/java/com/vaadin/ui/Grid.java
+++ b/server/src/main/java/com/vaadin/ui/Grid.java
@@ -29,11 +29,11 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
+import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;
-import com.vaadin.data.selection.SingleSelection;
import com.vaadin.event.ConnectorEvent;
import com.vaadin.event.EventListener;
import com.vaadin.server.KeyMapper;
@@ -41,7 +41,6 @@ import com.vaadin.server.data.SortOrder;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.Registration;
import com.vaadin.shared.data.DataCommunicatorConstants;
-import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.shared.data.sort.SortDirection;
import com.vaadin.shared.ui.grid.ColumnState;
import com.vaadin.shared.ui.grid.GridConstants;
@@ -67,8 +66,7 @@ import elemental.json.JsonValue;
* @param <T>
* the grid bean type
*/
-public class Grid<T> extends AbstractListing<T, SelectionModel<T>>
- implements HasComponents {
+public class Grid<T> extends AbstractSingleSelect<T> implements HasComponents {
@Deprecated
private static final Method ITEM_CLICK_METHOD = ReflectTools
@@ -197,7 +195,7 @@ public class Grid<T> extends AbstractListing<T, SelectionModel<T>>
*
* @param <T>
*/
- public static abstract class AbstractGridExtension<T>
+ public abstract static class AbstractGridExtension<T>
extends AbstractListingExtension<T> {
@Override
@@ -776,6 +774,44 @@ public class Grid<T> extends AbstractListing<T, SelectionModel<T>>
}
}
+ private final class SingleSelection extends AbstractSingleSelection {
+ private T selectedItem = null;
+
+ SingleSelection() {
+ addDataGenerator((item, json) -> {
+ if (isSelected(item)) {
+ json.put(DataCommunicatorConstants.SELECTED, true);
+ }
+ });
+ }
+
+ @Override
+ public Optional<T> getSelectedItem() {
+ return Optional.ofNullable(selectedItem);
+ }
+
+ @Override
+ protected boolean isKeySelected(String key) {
+ return Objects.equals(key, getSelectedKey());
+ }
+
+ @Override
+ protected String getSelectedKey() {
+ return itemToKey(selectedItem);
+ }
+
+ @Override
+ protected void doSetSelectedKey(String key) {
+ if (selectedItem != null) {
+ getDataCommunicator().refresh(selectedItem);
+ }
+ selectedItem = keyToItem(key);
+ if (selectedItem != null) {
+ getDataCommunicator().refresh(selectedItem);
+ }
+ }
+ }
+
private KeyMapper<Column<T, ?>> columnKeys = new KeyMapper<>();
private Set<Column<T, ?>> columnSet = new LinkedHashSet<>();
private List<SortOrder<Column<T, ?>>> sortOrder = new ArrayList<>();
@@ -788,7 +824,7 @@ public class Grid<T> extends AbstractListing<T, SelectionModel<T>>
* Constructor for the {@link Grid} component.
*/
public Grid() {
- setSelectionModel(new SingleSelection<>(this));
+ setSelectionModel(new SingleSelection());
registerRpc(new GridServerRpcImpl());
detailsManager = new DetailsManager<>();
addExtension(detailsManager);