From 3b0ed7a67553ca48e40f9c78da5655256a1fe5ea Mon Sep 17 00:00:00 2001 From: Denis Anisimov Date: Sun, 24 Aug 2014 14:48:03 +0300 Subject: Update selection after changes in underlying data source (#13580). Change-Id: I6354d85bd6bc37b1cbb69f388559278d5a163256 --- server/src/com/vaadin/ui/AbstractSelect.java | 19 +++++ .../TestAbstractSelectValueUpdate.java | 81 ++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 server/tests/src/com/vaadin/tests/server/component/abstractselect/TestAbstractSelectValueUpdate.java (limited to 'server') diff --git a/server/src/com/vaadin/ui/AbstractSelect.java b/server/src/com/vaadin/ui/AbstractSelect.java index b083db3183..2c4dd5be5d 100644 --- a/server/src/com/vaadin/ui/AbstractSelect.java +++ b/server/src/com/vaadin/ui/AbstractSelect.java @@ -1675,6 +1675,8 @@ public abstract class AbstractSelect extends AbstractField implements // Clears the item id mapping table itemIdMapper.removeAll(); + adjustSelection(); + // Notify all listeners fireItemSetChange(); } @@ -1712,6 +1714,23 @@ public abstract class AbstractSelect extends AbstractField implements markAsDirty(); } + /** + * Removes orphaned ids from selection. + */ + protected void adjustSelection() { + Object value = getValue(); + if (isMultiSelect() && (value instanceof Collection)) { + Collection collection = (Collection) value; + for (Object id : collection) { + if (!containsId(id)) { + unselect(id); + } + } + } else if (!containsId(value)) { + unselect(value); + } + } + /** * Implementation of item set change event. */ diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractselect/TestAbstractSelectValueUpdate.java b/server/tests/src/com/vaadin/tests/server/component/abstractselect/TestAbstractSelectValueUpdate.java new file mode 100644 index 0000000000..e81f6e09b6 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstractselect/TestAbstractSelectValueUpdate.java @@ -0,0 +1,81 @@ +/* + * 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.tests.server.component.abstractselect; + +import java.util.Collection; +import java.util.Collections; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.data.util.BeanItemContainer; +import com.vaadin.ui.AbstractSelect; + +public class TestAbstractSelectValueUpdate { + + @Test + public void removeItem_deleteItemFromUnderlyingContainer_selectValueIsUpdated() { + BeanItemContainer container = new BeanItemContainer( + Object.class); + Object item1 = new Object(); + Object item2 = new Object(); + container.addBean(item1); + container.addBean(item2); + TestSelect select = new TestSelect(); + select.setContainerDataSource(container); + + select.setValue(item1); + + Assert.assertNotNull("Value is null after selection", select.getValue()); + + container.removeItem(item1); + + Assert.assertNull("Value is not null after removal", select.getValue()); + } + + @Test + public void removeItem_multiselectSectionDeleteItemFromUnderlyingContainer_selectValueIsUpdated() { + BeanItemContainer container = new BeanItemContainer( + Object.class); + Object item1 = new Object(); + Object item2 = new Object(); + container.addBean(item1); + container.addBean(item2); + TestSelect select = new TestSelect(); + select.setMultiSelect(true); + select.setContainerDataSource(container); + + select.setValue(Collections.singletonList(item1)); + + checkSelectedItemsCount(select, 1); + + container.removeItem(item1); + + checkSelectedItemsCount(select, 0); + } + + private void checkSelectedItemsCount(TestSelect select, int count) { + Assert.assertNotNull("Selected value is null", select.getValue()); + Assert.assertTrue("Selected value is not a collection", + select.getValue() instanceof Collection); + Assert.assertEquals("Wrong number of selected items", + ((Collection) select.getValue()).size(), count); + } + + private class TestSelect extends AbstractSelect { + + } +} -- cgit v1.2.3