import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.vaadin.client.widget.grid.sort.SortOrder;
import com.vaadin.client.widgets.Grid;
import com.vaadin.client.widgets.Grid.Column;
+import com.vaadin.shared.data.DataCommunicatorConstants;
import com.vaadin.shared.data.selection.SelectionModel;
-import com.vaadin.shared.data.selection.SelectionModel.Single;
+import com.vaadin.shared.data.selection.SelectionServerRpc;
import com.vaadin.shared.data.sort.SortDirection;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.grid.GridConstants;
super.init();
// Default selection style is space key.
- spaceSelectHandler = new SpaceSelectHandler<JsonObject>(getWidget());
+ spaceSelectHandler = new SpaceSelectHandler<>(getWidget());
+ clickSelectHandler = new ClickSelectHandler<>(getWidget());
getWidget().addSortHandler(this::handleSortEvent);
getWidget().setRowStyleGenerator(rowRef -> {
JsonObject json = rowRef.getRow();
/* Item click events */
getWidget().addBodyClickHandler(itemClickHandler);
getWidget().addBodyDoubleClickHandler(itemClickHandler);
+ getWidget().setSelectionModel(new SelectionModel.Single<JsonObject>() {
+
+ @Override
+ public void select(JsonObject item) {
+ getRpcProxy(SelectionServerRpc.class)
+ .select(item.getString(DataCommunicatorConstants.KEY));
+ }
+
+ @Override
+ public void deselect(JsonObject item) {
+ getRpcProxy(SelectionServerRpc.class).deselect(
+ item.getString(DataCommunicatorConstants.KEY));
+ }
+
+ @Override
+ public Optional<JsonObject> getSelectedItem() {
+ throw new UnsupportedOperationException(
+ "Selected item not known on the client side");
+ }
+
+ @Override
+ public boolean isSelected(JsonObject item) {
+ return item.hasKey(DataCommunicatorConstants.SELECTED)
+ && item.getBoolean(DataCommunicatorConstants.SELECTED);
+ }
+ });
layout();
}
@Override
public void setSelectionModel(SelectionModel<JsonObject> selectionModel) {
- removeClickHandler();
-
- super.setSelectionModel(selectionModel);
- getWidget().setSelectionModel(selectionModel);
-
- if (selectionModel instanceof Single) {
- // Single selection should be moved by a click.
- clickSelectHandler = new ClickSelectHandler<>(getWidget());
- }
+ throw new UnsupportedOperationException(
+ "Cannot set a selection model for GridConnector");
}
/**
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;
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;
* @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
*
* @param <T>
*/
- public static abstract class AbstractGridExtension<T>
+ public abstract static class AbstractGridExtension<T>
extends AbstractListingExtension<T> {
@Override
}
}
+ 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<>();
* Constructor for the {@link Grid} component.
*/
public Grid() {
- setSelectionModel(new SingleSelection<>(this));
+ setSelectionModel(new SingleSelection());
registerRpc(new GridServerRpcImpl());
detailsManager = new DetailsManager<>();
addExtension(detailsManager);
*/
package com.vaadin.shared.ui;
-import com.vaadin.shared.AbstractComponentState;
-
/**
* Shared state for {@code AbstractSingleSelect}.
*
*
* @since 8.0
*/
-public class AbstractSingleSelectState extends AbstractComponentState {
+public class AbstractSingleSelectState extends TabIndexState {
/**
* The key of the currently selected item or {@code null} if no item is
import com.vaadin.shared.annotations.DelegateToWidget;
import com.vaadin.shared.data.sort.SortDirection;
-import com.vaadin.shared.ui.TabIndexState;
+import com.vaadin.shared.ui.AbstractSingleSelectState;
/**
* The shared state for the {@link com.vaadin.ui.Grid} component.
* @since 8.0
* @author Vaadin Ltd
*/
-public class GridState extends TabIndexState {
+public class GridState extends AbstractSingleSelectState {
/**
* The default value for height-by-rows for both GWT widgets
import java.util.stream.Stream;
import com.vaadin.annotations.Widgetset;
-import com.vaadin.data.selection.SingleSelection;
import com.vaadin.server.VaadinRequest;
import com.vaadin.shared.Registration;
import com.vaadin.shared.ui.grid.HeightMode;
grid.addColumn("Small Random", data -> data.getSmallRandom() / 5d,
new ProgressBarRenderer());
- ((SingleSelection<DataObject>) grid.getSelectionModel())
- .addSelectionListener(e -> log("Selected: " + e.getValue()));
+ grid.addSelectionListener(e -> log("Selected: " + e.getValue()));
layout.addComponent(createMenu());
layout.addComponent(grid);