From cef3e1ca64302cfa40e7eb3e287cd65785662d1c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Johannes=20Dahlstr=C3=B6m?= Date: Thu, 8 Sep 2016 21:28:07 +0300 Subject: [PATCH] Remove unused SingleSelection Change-Id: I33afc94a581d77984f459b0cbd0ff7fe49df8e79 --- .../selection/SingleSelectionConnector.java | 92 -------- .../data/selection/SingleSelection.java | 144 ------------- .../com/vaadin/data/SingleSelectionTest.java | 203 ------------------ .../com/vaadin/ui/AbstractListingTest.java | 5 +- 4 files changed, 2 insertions(+), 442 deletions(-) delete mode 100644 client/src/main/java/com/vaadin/client/connectors/selection/SingleSelectionConnector.java delete mode 100644 server/src/main/java/com/vaadin/data/selection/SingleSelection.java delete mode 100644 server/src/test/java/com/vaadin/data/SingleSelectionTest.java 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 deleted file mode 100644 index 875a70412c..0000000000 --- a/client/src/main/java/com/vaadin/client/connectors/selection/SingleSelectionConnector.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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; - -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 { - - private SelectionServerRpc rpc; - - SingleSelection(SelectionServerRpc rpc) { - this.rpc = rpc; - } - - @Override - public void select(JsonObject item) { - if (!isSelected(item)) { - rpc.select(getKey(item)); - } - } - - @Override - public void deselect(JsonObject item) { - if (isSelected(item)) { - rpc.deselect(getKey(item)); - } - } - - @Override - public boolean isSelected(JsonObject item) { - return isItemSelected(item); - } - - @Override - public Optional getSelectedItem() { - throw new UnsupportedOperationException( - "A client-side selection model does not know the full selection"); - } - } - - 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 SingleSelection createSelectionModel() { - return new SingleSelection(getRpcProxy(SelectionServerRpc.class)); - } -} diff --git a/server/src/main/java/com/vaadin/data/selection/SingleSelection.java b/server/src/main/java/com/vaadin/data/selection/SingleSelection.java deleted file mode 100644 index 81260e0cc2..0000000000 --- a/server/src/main/java/com/vaadin/data/selection/SingleSelection.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * 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.data.selection; - -import java.lang.reflect.Method; -import java.util.Objects; -import java.util.Optional; - -import com.vaadin.event.selection.SingleSelectionChange; -import com.vaadin.event.selection.SingleSelectionListener; -import com.vaadin.shared.Registration; -import com.vaadin.shared.data.selection.SelectionModel.Single; -import com.vaadin.shared.data.selection.SelectionServerRpc; -import com.vaadin.ui.AbstractListing; -import com.vaadin.util.ReflectTools; - -/** - * A {@code SelectionModel} for selecting a single value. Implements - * {@code Extension} to provide the communication logic for single selection for - * the listing it extends. - * - * @author Vaadin Ltd. - * - * @param - * the type of the items to select - * - * @since 8.0 - */ -public class SingleSelection extends AbstractSelectionModel - implements Single { - - @Deprecated - private static final Method SELECTION_CHANGE_METHOD = ReflectTools - .findMethod(SingleSelectionListener.class, "accept", - SingleSelectionChange.class); - - /** - * Creates a new {@code SingleSelection} extending the given parent listing. - * - * @param parent - * the parent listing - */ - public SingleSelection( - AbstractListing> parent) { - registerRpc(new SelectionServerRpc() { - - @Override - public void select(String key) { - if (!Objects.equals(selectedItem, getData(key))) { - doSelect(getData(key), true); - } - } - - @Override - public void deselect(String key) { - if (Objects.equals(selectedItem, getData(key))) { - doSelect(null, true); - } - } - }); - extend(parent); - } - - private T selectedItem = null; - - @Override - public Optional getSelectedItem() { - return Optional.ofNullable(selectedItem); - } - - @Override - public void select(T item) { - doSelect(item, false); - } - - @Override - public void deselect(T value) { - if (Objects.equals(selectedItem, value)) { - doSelect(null, false); - } - } - - @Override - public void remove() { - if (selectedItem != null) { - refresh(selectedItem); - } - super.remove(); - } - - /** - * Adds a selection listener. The listener is called when the value of this - * {@code SingleSelection} is changed either by the user or - * programmatically. - * - * @param listener - * the value change listener, not null - * @return a registration for the listener - */ - public Registration addSelectionListener( - SingleSelectionListener listener) { - Objects.requireNonNull(listener, "listener cannot be null"); - addListener(SingleSelectionChange.class, listener, - SELECTION_CHANGE_METHOD); - return () -> removeListener(SingleSelectionChange.class, listener); - } - - /** - * Selects the given item or deselects the current one if given - * {@code null}. - * - * @param item - * the item to select or {@code null} to deselect - * @param userOriginated - * {@code true} if this event originates from the client, - * {@code false} otherwise. - */ - protected void doSelect(T item, boolean userOriginated) { - if (!Objects.equals(item, selectedItem)) { - if (selectedItem != null) { - refresh(selectedItem); - } - selectedItem = item; - if (selectedItem != null) { - refresh(selectedItem); - } - fireEvent(new SingleSelectionChange<>(getParent(), selectedItem, - userOriginated)); - } - } -} diff --git a/server/src/test/java/com/vaadin/data/SingleSelectionTest.java b/server/src/test/java/com/vaadin/data/SingleSelectionTest.java deleted file mode 100644 index 5afa15e454..0000000000 --- a/server/src/test/java/com/vaadin/data/SingleSelectionTest.java +++ /dev/null @@ -1,203 +0,0 @@ -/* - * Copyright 2000-2014 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.data; - -import com.vaadin.data.selection.SingleSelection; -import com.vaadin.server.ClientMethodInvocation; -import com.vaadin.server.data.DataCommunicator; -import com.vaadin.server.data.datasource.bov.Person; -import com.vaadin.shared.data.DataCommunicatorClientRpc; -import com.vaadin.shared.data.DataCommunicatorConstants; -import com.vaadin.shared.data.selection.SelectionModel; -import com.vaadin.ui.AbstractListing; -import elemental.json.JsonArray; -import elemental.json.JsonObject; -import org.junit.Before; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** - * Test for {@link com.vaadin.data.selection.SingleSelection} - * - * @author Vaadin Ltd - */ -public class SingleSelectionTest { - - - private SingleSelection selectionModel; - private List selectionChanges; - - private static class PersonListing extends AbstractListing> { - public PersonListing() { - SingleSelection singleSelection = new SingleSelection<>(this); - setSelectionModel(singleSelection); - - } - - public SingleSelection getSelectionModel() { - return (SingleSelection) super.getSelectionModel(); - } - } - - - @Before - public void initListing() { - listing = new PersonListing(); - listing.setItems(PERSON_A, PERSON_B, PERSON_C); - selectionModel = listing.getSelectionModel(); - selectionChanges = new ArrayList<>(); - selectionModel.addSelectionListener(event -> selectionChanges.add(event.getValue()) ); - } - - public static final Person PERSON_C = new Person("c", 3); - public static final Person PERSON_B = new Person("b", 2); - public static final Person PERSON_A = new Person("a", 1); - public static final String RPC_INTERFACE = DataCommunicatorClientRpc.class.getName(); - private PersonListing listing; - - @Test - public void communication() { - - selectionModel.select(PERSON_C); - - DataCommunicator dataCommunicator = listing.getDataCommunicator(); - dataCommunicator.beforeClientResponse(true); - List invocations = dataCommunicator.retrievePendingRpcCalls(); - assertEquals(2, invocations.size()); - - ClientMethodInvocation invocationZero = invocations.get(0); - assertEquals(RPC_INTERFACE, invocationZero.getInterfaceName()); - assertEquals("reset", invocationZero.getMethodName()); - - ClientMethodInvocation invocationOne = invocations.get(1); - assertEquals(RPC_INTERFACE, invocationOne.getInterfaceName()); - assertEquals("setData", invocationOne.getMethodName()); - JsonObject object = ((JsonArray) invocationOne.getParameters()[1]).getObject(2); - - assertTrue("Expected selected item", object.getBoolean(DataCommunicatorConstants.SELECTED)); - } - - @Test - public void select() { - - selectionModel.select(PERSON_B); - - assertTrue(selectionModel.getSelectedItem().isPresent()); - - assertEquals(PERSON_B, selectionModel.getSelectedItem().orElse(null)); - - assertFalse(selectionModel.isSelected(PERSON_A)); - assertTrue(selectionModel.isSelected(PERSON_B)); - assertFalse(selectionModel.isSelected(PERSON_C)); - - assertEquals(Collections.singleton(PERSON_B), selectionModel.getSelectedItems()); - - assertEquals(Arrays.asList(PERSON_B), selectionChanges); - } - - @Test - public void selectDeselect() { - - selectionModel.select(PERSON_B); - selectionModel.deselect(PERSON_B); - - assertFalse(selectionModel.getSelectedItem().isPresent()); - - assertFalse(selectionModel.isSelected(PERSON_A)); - assertFalse(selectionModel.isSelected(PERSON_B)); - assertFalse(selectionModel.isSelected(PERSON_C)); - - assertTrue(selectionModel.getSelectedItems().isEmpty()); - - assertEquals(Arrays.asList(PERSON_B,null), selectionChanges); - } - @Test - public void reselect() { - - selectionModel.select(PERSON_B); - selectionModel.select(PERSON_C); - - assertEquals(PERSON_C, selectionModel.getSelectedItem().orElse(null)); - - assertFalse(selectionModel.isSelected(PERSON_A)); - assertFalse(selectionModel.isSelected(PERSON_B)); - assertTrue(selectionModel.isSelected(PERSON_C)); - - assertEquals(Collections.singleton(PERSON_C), selectionModel.getSelectedItems()); - - assertEquals(Arrays.asList(PERSON_B, PERSON_C), selectionChanges); - } - - @Test - public void deselectNoOp() { - - selectionModel.select(PERSON_C); - selectionModel.deselect(PERSON_B); - - assertEquals(PERSON_C, selectionModel.getSelectedItem().orElse(null)); - - assertFalse(selectionModel.isSelected(PERSON_A)); - assertFalse(selectionModel.isSelected(PERSON_B)); - assertTrue(selectionModel.isSelected(PERSON_C)); - - assertEquals(Collections.singleton(PERSON_C), selectionModel.getSelectedItems()); - - assertEquals(Arrays.asList(PERSON_C), selectionChanges); - } - @Test - public void selectTwice() { - - selectionModel.select(PERSON_C); - selectionModel.select(PERSON_C); - - assertEquals(PERSON_C, selectionModel.getSelectedItem().orElse(null)); - - assertFalse(selectionModel.isSelected(PERSON_A)); - assertFalse(selectionModel.isSelected(PERSON_B)); - assertTrue(selectionModel.isSelected(PERSON_C)); - - assertEquals(Collections.singleton(PERSON_C), selectionModel.getSelectedItems()); - - assertEquals(Arrays.asList(PERSON_C), selectionChanges); - } - - @Test - public void deselectTwice() { - - selectionModel.select(PERSON_C); - selectionModel.deselect(PERSON_C); - selectionModel.deselect(PERSON_C); - - assertFalse(selectionModel.getSelectedItem().isPresent()); - - assertFalse(selectionModel.isSelected(PERSON_A)); - assertFalse(selectionModel.isSelected(PERSON_B)); - assertFalse(selectionModel.isSelected(PERSON_C)); - - assertTrue(selectionModel.getSelectedItems().isEmpty()); - - assertEquals(Arrays.asList(PERSON_C,null), selectionChanges); - } - -} diff --git a/server/src/test/java/com/vaadin/ui/AbstractListingTest.java b/server/src/test/java/com/vaadin/ui/AbstractListingTest.java index cbab429748..6367a44585 100644 --- a/server/src/test/java/com/vaadin/ui/AbstractListingTest.java +++ b/server/src/test/java/com/vaadin/ui/AbstractListingTest.java @@ -9,7 +9,6 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import com.vaadin.data.selection.SingleSelection; import com.vaadin.server.data.BackEndDataSource; import com.vaadin.server.data.DataSource; import com.vaadin.server.data.ListDataSource; @@ -21,10 +20,10 @@ import elemental.json.JsonObject; public class AbstractListingTest { private final class TestListing extends - AbstractListing> { + AbstractSingleSelect { protected TestListing() { - setSelectionModel(new SingleSelection<>(this)); + setSelectionModel(new SimpleSingleSelection()); } /** -- 2.39.5