Skip to content

Commit

Permalink
Clean up old Grid selection models
Browse files Browse the repository at this point in the history
This patch removes old Grid selection model APIs
in favor of the new common SelectionModel API.

Change-Id: Iab8f2921930a575012c7da6226811d14a7145271
  • Loading branch information
Teemu Suo-Anttila committed Sep 6, 2016
1 parent 2cafb5f commit fbb55ac
Show file tree
Hide file tree
Showing 24 changed files with 407 additions and 1,068 deletions.
Expand Up @@ -33,7 +33,7 @@ public abstract class AbstractListingConnector

private DataSource<JsonObject> dataSource = null;

private SelectionModel<String> selectionModel = null;
private SelectionModel<JsonObject> selectionModel = null;

@Override
public void setDataSource(DataSource<JsonObject> dataSource) {
Expand All @@ -47,15 +47,15 @@ public DataSource<JsonObject> getDataSource() {

/**
* Sets the selection model to use. Passing {@code null} disables selection.
*
*
* @param selectionModel
* the selection model or null to disable
*/
public void setSelectionModel(SelectionModel<String> selectionModel) {
public void setSelectionModel(SelectionModel<JsonObject> selectionModel) {
this.selectionModel = selectionModel;
}

public SelectionModel<String> getSelectionModel() {
public SelectionModel<JsonObject> getSelectionModel() {
return selectionModel;
}
}
Expand Up @@ -31,10 +31,13 @@
import com.vaadin.client.data.DataSource;
import com.vaadin.client.ui.SimpleManagedLayout;
import com.vaadin.client.widget.grid.selection.ClickSelectHandler;
import com.vaadin.client.widget.grid.selection.SpaceSelectHandler;
import com.vaadin.client.widget.grid.sort.SortEvent;
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.selection.SelectionModel;
import com.vaadin.shared.data.selection.SelectionModel.Single;
import com.vaadin.shared.data.sort.SortDirection;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.grid.GridServerRpc;
Expand All @@ -56,6 +59,8 @@ public class GridConnector extends AbstractListingConnector
private Map<Column<?, JsonObject>, String> columnToIdMap = new HashMap<>();
/* Child component list for HasComponentsConnector */
private List<ComponentConnector> childComponents;
private SpaceSelectHandler<JsonObject> spaceSelectHandler;
private ClickSelectHandler<JsonObject> clickSelectHandler;

@Override
public Grid<JsonObject> getWidget() {
Expand All @@ -66,17 +71,32 @@ public Grid<JsonObject> getWidget() {
protected void init() {
super.init();

new ClickSelectHandler<>(getWidget());
// Default selection style is space key.
spaceSelectHandler = new SpaceSelectHandler<JsonObject>(getWidget());
getWidget().addSortHandler(this::handleSortEvent);

layout();
}

@Override
public void setDataSource(DataSource<JsonObject> dataSource) {
super.setDataSource(dataSource);
getWidget().setDataSource(dataSource);
}

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

/**
* Adds a column to the Grid widget. For each column a communication id
* stored for client to server communication.
Expand Down Expand Up @@ -112,6 +132,12 @@ public void onUnregister() {
super.onUnregister();

columnToIdMap.clear();
removeClickHandler();

if (spaceSelectHandler != null) {
spaceSelectHandler.removeHandler();
spaceSelectHandler = null;
}
}

@Override
Expand Down Expand Up @@ -177,4 +203,11 @@ public HandlerRegistration addConnectorHierarchyChangeHandler(
public GridState getState() {
return (GridState) super.getState();
}

private void removeClickHandler() {
if (clickSelectHandler != null) {
clickSelectHandler.removeHandler();
clickSelectHandler = null;
}
}
}
@@ -1,12 +1,12 @@
/*
* Copyright 2000-2016 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
Expand All @@ -18,19 +18,22 @@
import com.vaadin.client.ServerConnector;
import com.vaadin.client.connectors.AbstractListingConnector;
import com.vaadin.client.extensions.AbstractExtensionConnector;
import com.vaadin.shared.data.DataCommunicatorConstants;
import com.vaadin.shared.data.selection.SelectionModel;

import elemental.json.JsonObject;

/**
* The client-side connector for selection extensions.
*
*
* @author Vaadin Ltd.
*
*
* @since
*/
public abstract class AbstractSelectionConnector extends
AbstractExtensionConnector {
public abstract class AbstractSelectionConnector
extends AbstractExtensionConnector {

private SelectionModel<String> model = null;
private SelectionModel<JsonObject> model = null;

@Override
protected void extend(ServerConnector target) {
Expand All @@ -45,10 +48,10 @@ protected void extend(ServerConnector target) {

/**
* Creates a selection model object to be used by the Connector.
*
*
* @return created selection model
*/
protected abstract SelectionModel<String> createSelectionModel();
protected abstract SelectionModel<JsonObject> createSelectionModel();

@Override
public AbstractListingConnector getParent() {
Expand All @@ -57,10 +60,41 @@ public AbstractListingConnector getParent() {

/**
* Returns the client-side selection model associated with this connector.
*
*
* @return the selection model in use
*/
protected SelectionModel<String> getSelectionModel() {
protected SelectionModel<JsonObject> getSelectionModel() {
return model;
}

/**
* Gets the selected state from a given json object. This is a helper method
* for selection model connectors.
*
* @param item
* a json object
* @return {@code true} if the json object is marked as selected;
* {@code false} if not
*/
public static boolean isItemSelected(JsonObject item) {
return item.hasKey(DataCommunicatorConstants.SELECTED)
&& item.getBoolean(DataCommunicatorConstants.SELECTED);
}

/**
* Gets the item key from given json object. This is a helper method for
* selection model connectors.
*
* @param item
* a json object
* @return item key; {@code null} if there is no key
*/
public static String getKey(JsonObject item) {
if (item.hasKey(DataCommunicatorConstants.KEY)) {
return item.getString(DataCommunicatorConstants.KEY);
} else {
return null;
}
}

}
@@ -1,12 +1,12 @@
/*
* Copyright 2000-2016 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
Expand All @@ -23,48 +23,48 @@
import com.vaadin.shared.data.selection.SelectionServerRpc;
import com.vaadin.shared.ui.Connect;

import elemental.json.JsonObject;

/**
* A connector for single selection extensions.
*
*
* @author Vaadin Ltd.
*/
@Connect(com.vaadin.data.selection.SingleSelection.class)
public class SingleSelectionConnector extends AbstractSelectionConnector {

private static class SingleSelection implements
SelectionModel.Single<String> {
private static class SingleSelection
implements SelectionModel.Single<JsonObject> {

private String value;
private SelectionServerRpc rpc;

SingleSelection(SelectionServerRpc rpc) {
this.rpc = rpc;
}

@Override
public void select(String item) {
if (item != null && !item.equals(value)) {
rpc.select(item);
value = item;
public void select(JsonObject item) {
if (!isSelected(item)) {
rpc.select(getKey(item));
}
}

@Override
public void deselect(String item) {
if (item != null && item.equals(value)) {
rpc.deselect(item);
value = null;
public void deselect(JsonObject item) {
if (isSelected(item)) {
rpc.deselect(getKey(item));
}
}

@Override
public boolean isSelected(String item) {
return value != null && value.equals(item);
public boolean isSelected(JsonObject item) {
return isItemSelected(item);
}

@Override
public Optional<String> getSelectedItem() {
return Optional.ofNullable(value);
public Optional<JsonObject> getSelectedItem() {
throw new UnsupportedOperationException(
"A client-side selection model does not know the full selection");
}
}

Expand All @@ -85,7 +85,7 @@ protected void extend(ServerConnector target) {
}

@Override
protected SelectionModel<String> createSelectionModel() {
protected SelectionModel<JsonObject> createSelectionModel() {
return new SingleSelection(getRpcProxy(SelectionServerRpc.class));
}
}
Expand Up @@ -27,6 +27,7 @@
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.vaadin.client.WidgetUtil;
import com.vaadin.client.widget.grid.selection.SelectionModelWithSelectionColumn;
import com.vaadin.client.widgets.Grid;

/**
Expand Down Expand Up @@ -625,8 +626,8 @@ public double getFrozenColumnsWidth() {
private int getRealFrozenColumnCount() {
if (grid.getFrozenColumnCount() < 0) {
return 0;
} else if (grid.getSelectionModel()
.getSelectionColumnRenderer() != null) {
} else if (grid
.getSelectionModel() instanceof SelectionModelWithSelectionColumn) {
// includes the selection column
return grid.getFrozenColumnCount() + 1;
} else {
Expand Down
Expand Up @@ -452,7 +452,7 @@ public SelectAllHandler<T> getSelectAllHandler() {
return new SelectAllHandler<T>() {
@Override
public void onSelectAll(SelectAllEvent<T> event) {
event.getSelectionModel().select(asList());
asList().forEach(event.getSelectionModel()::select);
}
};
}
Expand All @@ -461,5 +461,4 @@ private Stream<DataChangeHandler> getHandlers() {
Set<DataChangeHandler> copy = new LinkedHashSet<>(changeHandlers);
return copy.stream();
}

}
Expand Up @@ -16,7 +16,7 @@
package com.vaadin.client.widget.grid.events;

import com.google.gwt.event.shared.GwtEvent;
import com.vaadin.client.widget.grid.selection.SelectionModel;
import com.vaadin.shared.data.selection.SelectionModel;

/**
* A select all event, fired by the Grid when it needs all rows in data source
Expand Down

0 comments on commit fbb55ac

Please sign in to comment.