diff options
author | Johannes Dahlström <johannesd@vaadin.com> | 2016-08-24 11:26:12 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-08-31 13:08:47 +0000 |
commit | 277b1a5c2884291ea9312d555dc45545430c1d9c (patch) | |
tree | bb44b2af9da21f0ca10287e31c24b540df89aad2 /client | |
parent | ff3a48e73bb7318793746aa9c9ff1cf61b632c7f (diff) | |
download | vaadin-framework-277b1a5c2884291ea9312d555dc45545430c1d9c.tar.gz vaadin-framework-277b1a5c2884291ea9312d555dc45545430c1d9c.zip |
Implement SingleSelection on client and server
Change-Id: I48192de092c6b6e6be7ca2580720d2765962e167
Diffstat (limited to 'client')
5 files changed, 181 insertions, 1 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java b/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java index 06d3ecf97d..c00807ed2c 100644 --- a/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/AbstractListingConnector.java @@ -18,6 +18,7 @@ package com.vaadin.client.connectors; import com.vaadin.client.connectors.data.HasDataSource; import com.vaadin.client.data.DataSource; import com.vaadin.client.ui.AbstractComponentConnector; +import com.vaadin.shared.data.selection.SelectionModel; import com.vaadin.ui.AbstractListing; import elemental.json.JsonObject; @@ -32,6 +33,8 @@ public abstract class AbstractListingConnector private DataSource<JsonObject> dataSource = null; + private SelectionModel<String> selectionModel = null; + @Override public void setDataSource(DataSource<JsonObject> dataSource) { this.dataSource = dataSource; @@ -41,4 +44,18 @@ public abstract class AbstractListingConnector public DataSource<JsonObject> getDataSource() { return dataSource; } + + /** + * 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) { + this.selectionModel = selectionModel; + } + + public SelectionModel<String> getSelectionModel() { + return selectionModel; + } } diff --git a/client/src/main/java/com/vaadin/client/connectors/selection/AbstractSelectionConnector.java b/client/src/main/java/com/vaadin/client/connectors/selection/AbstractSelectionConnector.java new file mode 100644 index 0000000000..b295fb0cc3 --- /dev/null +++ b/client/src/main/java/com/vaadin/client/connectors/selection/AbstractSelectionConnector.java @@ -0,0 +1,66 @@ +/* + * 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 + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.client.connectors.selection; + +import com.vaadin.client.ServerConnector; +import com.vaadin.client.connectors.AbstractListingConnector; +import com.vaadin.client.extensions.AbstractExtensionConnector; +import com.vaadin.shared.data.selection.SelectionModel; + +/** + * The client-side connector for selection extensions. + * + * @author Vaadin Ltd. + * + * @since + */ +public abstract class AbstractSelectionConnector extends + AbstractExtensionConnector { + + private SelectionModel<String> model = null; + + @Override + protected void extend(ServerConnector target) { + if (!(target instanceof AbstractListingConnector)) { + throw new IllegalArgumentException( + "Cannot extend a connector that is not an " + + AbstractListingConnector.class.getSimpleName()); + } + model = createSelectionModel(); + ((AbstractListingConnector) target).setSelectionModel(model); + } + + /** + * Creates a selection model object to be used by the Connector. + * + * @return created selection model + */ + protected abstract SelectionModel<String> createSelectionModel(); + + @Override + public AbstractListingConnector getParent() { + return (AbstractListingConnector) super.getParent(); + } + + /** + * Returns the client-side selection model associated with this connector. + * + * @return the selection model in use + */ + protected SelectionModel<String> getSelectionModel() { + return model; + } +} diff --git a/client/src/main/java/com/vaadin/client/connectors/selection/SingleSelectionConnector.java b/client/src/main/java/com/vaadin/client/connectors/selection/SingleSelectionConnector.java new file mode 100644 index 0000000000..70844d1ae7 --- /dev/null +++ b/client/src/main/java/com/vaadin/client/connectors/selection/SingleSelectionConnector.java @@ -0,0 +1,91 @@ +/* + * 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 + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.client.connectors.selection; + +import java.util.Optional; + +import com.vaadin.client.ServerConnector; +import com.vaadin.client.connectors.AbstractListingConnector; +import com.vaadin.shared.data.selection.SelectionModel; +import com.vaadin.shared.data.selection.SelectionServerRpc; +import com.vaadin.shared.ui.Connect; + +/** + * 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 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; + } + } + + @Override + public void deselect(String item) { + if (item != null && item.equals(value)) { + rpc.deselect(item); + value = null; + } + } + + @Override + public boolean isSelected(String item) { + return value != null && value.equals(item); + } + + @Override + public Optional<String> getSelectedItem() { + return Optional.ofNullable(value); + } + } + + private AbstractListingConnector parent; + + @Override + public void onUnregister() { + super.onUnregister(); + if (parent.getSelectionModel() == getSelectionModel()) { + parent.setSelectionModel(null); + } + } + + @Override + protected void extend(ServerConnector target) { + super.extend(target); + parent = getParent(); + } + + @Override + protected SelectionModel<String> createSelectionModel() { + return new SingleSelection(getRpcProxy(SelectionServerRpc.class)); + } +} diff --git a/client/src/main/java/com/vaadin/client/extensions/AbstractExtensionConnector.java b/client/src/main/java/com/vaadin/client/extensions/AbstractExtensionConnector.java index fd7a109e29..d98b7d65b6 100644 --- a/client/src/main/java/com/vaadin/client/extensions/AbstractExtensionConnector.java +++ b/client/src/main/java/com/vaadin/client/extensions/AbstractExtensionConnector.java @@ -19,6 +19,12 @@ package com.vaadin.client.extensions; import com.vaadin.client.ServerConnector; import com.vaadin.client.ui.AbstractConnector; +/** + * An abstract implementation for extension connectors. + * + * @author Vaadin Ltd. + * @since 7.0 + */ public abstract class AbstractExtensionConnector extends AbstractConnector { boolean hasBeenAttached = false; diff --git a/client/src/main/java/com/vaadin/client/widget/grid/selection/HasSelectionHandlers.java b/client/src/main/java/com/vaadin/client/widget/grid/selection/HasSelectionHandlers.java index 5e5cc6263d..edb226fa4f 100644 --- a/client/src/main/java/com/vaadin/client/widget/grid/selection/HasSelectionHandlers.java +++ b/client/src/main/java/com/vaadin/client/widget/grid/selection/HasSelectionHandlers.java @@ -29,7 +29,7 @@ public interface HasSelectionHandlers<T> { * Register a selection change handler. * <p> * This handler is called whenever a - * {@link com.vaadin.ui.components.grid.selection.SelectionModel + * {@link com.vaadin.client.data.selection.components.grid.selection.SelectionModel * SelectionModel} detects a change in selection state. * * @param handler |