aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2013-01-02 14:16:31 +0000
committerVaadin Code Review <review@vaadin.com>2013-01-02 14:16:31 +0000
commit83e6f7acf0df5ddd23063d9acb9bff158f98d0e0 (patch)
tree175dcc03cd0412de8f259bc8c9dc2de8576010d1
parent93b35b7895748db6074480a9a19a00073b520a63 (diff)
parent52ccef6debd4e26b8249d1a00569f8b8b7e611cb (diff)
downloadvaadin-framework-83e6f7acf0df5ddd23063d9acb9bff158f98d0e0.tar.gz
vaadin-framework-83e6f7acf0df5ddd23063d9acb9bff158f98d0e0.zip
Merge "Merge of (#9986) to Vaadin 7."
-rw-r--r--server/src/com/vaadin/ui/AbstractSelect.java59
-rw-r--r--uitest/src/com/vaadin/tests/components/table/ValueAfterClearingContainer.java121
2 files changed, 170 insertions, 10 deletions
diff --git a/server/src/com/vaadin/ui/AbstractSelect.java b/server/src/com/vaadin/ui/AbstractSelect.java
index ac80dfbb74..5a674d4b89 100644
--- a/server/src/com/vaadin/ui/AbstractSelect.java
+++ b/server/src/com/vaadin/ui/AbstractSelect.java
@@ -17,6 +17,7 @@
package com.vaadin.ui;
import java.io.Serializable;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EventObject;
@@ -25,6 +26,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
+import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -896,6 +898,42 @@ public abstract class AbstractSelect extends AbstractField<Object> implements
}
/**
+ * Checks that the current selection is valid, i.e. the selected item ids
+ * exist in the container. Updates the selection if one or several selected
+ * item ids are no longer available in the container.
+ */
+ @SuppressWarnings("unchecked")
+ public void sanitizeSelection() {
+ Object value = getValue();
+ if (value == null) {
+ return;
+ }
+
+ boolean changed = false;
+
+ if (isMultiSelect()) {
+ Collection<Object> valueAsCollection = (Collection<Object>) value;
+ List<Object> newSelection = new ArrayList<Object>(
+ valueAsCollection.size());
+ for (Object subValue : valueAsCollection) {
+ if (containsId(subValue)) {
+ newSelection.add(subValue);
+ } else {
+ changed = true;
+ }
+ }
+ if (changed) {
+ setValue(newSelection);
+ }
+ } else {
+ if (!containsId(value)) {
+ setValue(null);
+ }
+ }
+
+ }
+
+ /**
* Removes the property from all items. Removes a property with given id
* from all the items in the container.
*
@@ -943,11 +981,11 @@ public abstract class AbstractSelect extends AbstractField<Object> implements
if (items != null) {
if (items instanceof Container.ItemSetChangeNotifier) {
((Container.ItemSetChangeNotifier) items)
- .removeListener(this);
+ .removeItemSetChangeListener(this);
}
if (items instanceof Container.PropertySetChangeNotifier) {
((Container.PropertySetChangeNotifier) items)
- .removeListener(this);
+ .removePropertySetChangeListener(this);
}
}
@@ -960,11 +998,12 @@ public abstract class AbstractSelect extends AbstractField<Object> implements
// Adds listeners
if (items != null) {
if (items instanceof Container.ItemSetChangeNotifier) {
- ((Container.ItemSetChangeNotifier) items).addListener(this);
+ ((Container.ItemSetChangeNotifier) items)
+ .addItemSetChangeListener(this);
}
if (items instanceof Container.PropertySetChangeNotifier) {
((Container.PropertySetChangeNotifier) items)
- .addListener(this);
+ .addPropertySetChangeListener(this);
}
}
@@ -1829,7 +1868,7 @@ public abstract class AbstractSelect extends AbstractField<Object> implements
}
if (i instanceof Item.PropertySetChangeNotifier) {
((Item.PropertySetChangeNotifier) i)
- .addListener(getCaptionChangeListener());
+ .addPropertySetChangeListener(getCaptionChangeListener());
captionChangeNotifiers.add(i);
}
Collection<?> pids = i.getItemPropertyIds();
@@ -1839,7 +1878,7 @@ public abstract class AbstractSelect extends AbstractField<Object> implements
if (p != null
&& p instanceof Property.ValueChangeNotifier) {
((Property.ValueChangeNotifier) p)
- .addListener(getCaptionChangeListener());
+ .addValueChangeListener(getCaptionChangeListener());
captionChangeNotifiers.add(p);
}
}
@@ -1851,7 +1890,7 @@ public abstract class AbstractSelect extends AbstractField<Object> implements
getItemCaptionPropertyId());
if (p != null && p instanceof Property.ValueChangeNotifier) {
((Property.ValueChangeNotifier) p)
- .addListener(getCaptionChangeListener());
+ .addValueChangeListener(getCaptionChangeListener());
captionChangeNotifiers.add(p);
}
break;
@@ -1862,7 +1901,7 @@ public abstract class AbstractSelect extends AbstractField<Object> implements
getItemIconPropertyId());
if (p != null && p instanceof Property.ValueChangeNotifier) {
((Property.ValueChangeNotifier) p)
- .addListener(getCaptionChangeListener());
+ .addValueChangeListener(getCaptionChangeListener());
captionChangeNotifiers.add(p);
}
}
@@ -1874,10 +1913,10 @@ public abstract class AbstractSelect extends AbstractField<Object> implements
Object notifier = it.next();
if (notifier instanceof Item.PropertySetChangeNotifier) {
((Item.PropertySetChangeNotifier) notifier)
- .removeListener(getCaptionChangeListener());
+ .removePropertySetChangeListener(getCaptionChangeListener());
} else {
((Property.ValueChangeNotifier) notifier)
- .removeListener(getCaptionChangeListener());
+ .removeValueChangeListener(getCaptionChangeListener());
}
}
captionChangeNotifiers.clear();
diff --git a/uitest/src/com/vaadin/tests/components/table/ValueAfterClearingContainer.java b/uitest/src/com/vaadin/tests/components/table/ValueAfterClearingContainer.java
new file mode 100644
index 0000000000..93e7cafa99
--- /dev/null
+++ b/uitest/src/com/vaadin/tests/components/table/ValueAfterClearingContainer.java
@@ -0,0 +1,121 @@
+package com.vaadin.tests.components.table;
+
+import com.vaadin.data.Property.ValueChangeEvent;
+import com.vaadin.data.Property.ValueChangeListener;
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.tests.util.Log;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.CheckBox;
+import com.vaadin.ui.Notification;
+import com.vaadin.ui.Table;
+
+public class ValueAfterClearingContainer extends TestBase {
+
+ private static final String PROPERTY_ID = "property";
+
+ private Log log = new Log(5);
+ private final Table table = new Table();
+
+ @Override
+ protected void setup() {
+ table.setSelectable(true);
+ table.addContainerProperty(PROPERTY_ID, Integer.class, null);
+ table.setImmediate(true);
+ table.addValueChangeListener(new ValueChangeListener() {
+
+ public void valueChange(ValueChangeEvent event) {
+ log.log("Value changed to " + event.getProperty().getValue());
+ }
+ });
+ addComponent(log);
+
+ addComponent(table);
+ final CheckBox multiselect = new CheckBox("Multiselect");
+ multiselect.setImmediate(true);
+ multiselect.addValueChangeListener(new ValueChangeListener() {
+
+ public void valueChange(ValueChangeEvent event) {
+ Boolean value = multiselect.getValue();
+ table.setMultiSelect(value == null ? false : value);
+ }
+ });
+ addComponent(multiselect);
+ addComponent(new Button("Add table items", new Button.ClickListener() {
+ public void buttonClick(ClickEvent event) {
+ if (!table.getItemIds().isEmpty()) {
+ Notification.show("Only possible when the table is empty");
+ return;
+ } else {
+ for (int i = 0; i < 5; i++) {
+ table.addItem(new Object[] { Integer.valueOf(i) },
+ Integer.valueOf(i));
+ }
+ }
+ }
+ }));
+
+ addComponent(new Button("Show value", new Button.ClickListener() {
+ public void buttonClick(ClickEvent event) {
+ log.log("Table selection: " + table.getValue());
+ }
+ }));
+
+ addComponent(new Button("Remove items from table",
+ new Button.ClickListener() {
+ public void buttonClick(ClickEvent event) {
+ table.removeAllItems();
+ }
+ }));
+
+ addComponent(new Button("Remove items from container",
+ new Button.ClickListener() {
+ public void buttonClick(ClickEvent event) {
+ table.getContainerDataSource().removeAllItems();
+ }
+ }));
+ addComponent(new Button("Remove items from container and sanitize",
+ new Button.ClickListener() {
+ public void buttonClick(ClickEvent event) {
+ table.getContainerDataSource().removeAllItems();
+ table.sanitizeSelection();
+ }
+ }));
+ addComponent(new Button("Remove selected item from table",
+ new Button.ClickListener() {
+ public void buttonClick(ClickEvent event) {
+ Object selection = table.getValue();
+ if (selection == null) {
+ Notification.show("There is no selection");
+ return;
+ } else {
+ table.removeItem(selection);
+ }
+ }
+ }));
+ addComponent(new Button("Remove selected item from container",
+ new Button.ClickListener() {
+ public void buttonClick(ClickEvent event) {
+ Object selection = table.getValue();
+ if (selection == null) {
+ Notification.show("There is no selection");
+ return;
+ } else {
+ table.getContainerDataSource()
+ .removeItem(selection);
+ }
+ }
+ }));
+ }
+
+ @Override
+ protected String getDescription() {
+ return "Table value should be cleared when the selected item is removed from the container.";
+ }
+
+ @Override
+ protected Integer getTicketNumber() {
+ return Integer.valueOf(9986);
+ }
+
+}