]> source.dussan.org Git - vaadin-framework.git/commitdiff
Merged fix, sample and tests for "#3720 - Disable Items for OptionGroup" from /branch...
authorArtur Signell <artur.signell@itmill.com>
Tue, 15 Jun 2010 07:09:03 +0000 (07:09 +0000)
committerArtur Signell <artur.signell@itmill.com>
Tue, 15 Jun 2010 07:09:03 +0000 (07:09 +0000)
svn changeset:13659/svn branch:6.4

src/com/vaadin/ui/AbstractSelect.java
src/com/vaadin/ui/OptionGroup.java
tests/src/com/vaadin/tests/components/optiongroup/DisabledOptionGroupItems.html [new file with mode: 0644]
tests/src/com/vaadin/tests/components/optiongroup/DisabledOptionGroupItems.java [new file with mode: 0644]

index e8ebdc6724d2432d439a23b005c196a40ba24fa9..60dfea0da264725c66440bfc0770140e166b2e7a 100644 (file)
@@ -316,22 +316,12 @@ public abstract class AbstractSelect extends AbstractField implements
         final Collection ids = getItemIds();
         if (isNullSelectionAllowed() && getNullSelectionItemId() != null
                 && !ids.contains(getNullSelectionItemId())) {
-            // Gets the option attribute values
             final Object id = getNullSelectionItemId();
-            final String key = itemIdMapper.key(id);
-            final String caption = getItemCaption(id);
-            final Resource icon = getItemIcon(id);
             // Paints option
             target.startTag("so");
-            if (icon != null) {
-                target.addAttribute("icon", icon);
-            }
-            target.addAttribute("caption", caption);
-            target.addAttribute("nullselection", true);
-            target.addAttribute("key", key);
+            paintItem(target, id);
             if (isSelected(id)) {
-                target.addAttribute("selected", true);
-                selectedKeys[keyIndex++] = key;
+                selectedKeys[keyIndex++] = itemIdMapper.key(id);
             }
             target.endTag("so");
         }
@@ -348,21 +338,11 @@ public abstract class AbstractSelect extends AbstractField implements
                 continue;
             }
             final String key = itemIdMapper.key(id);
-            final String caption = getItemCaption(id);
             // add listener for each item, to cause repaint if an item changes
             getCaptionChangeListener().addNotifierForItem(id);
-            final Resource icon = getItemIcon(id); // Paints the option
             target.startTag("so");
-            if (icon != null) {
-                target.addAttribute("icon", icon);
-            }
-            target.addAttribute("caption", caption);
-            if (id != null && id.equals(getNullSelectionItemId())) {
-                target.addAttribute("nullselection", true);
-            }
-            target.addAttribute("key", key);
+            paintItem(target, id);
             if (isSelected(id) && keyIndex < selectedKeys.length) {
-                target.addAttribute("selected", true);
                 selectedKeys[keyIndex++] = key;
             }
             target.endTag("so");
@@ -378,6 +358,24 @@ public abstract class AbstractSelect extends AbstractField implements
 
     }
 
+    protected void paintItem(PaintTarget target, Object itemId)
+            throws PaintException {
+        final String key = itemIdMapper.key(itemId);
+        final String caption = getItemCaption(itemId);
+        final Resource icon = getItemIcon(itemId);
+        if (icon != null) {
+            target.addAttribute("icon", icon);
+        }
+        target.addAttribute("caption", caption);
+        if (itemId != null && itemId.equals(getNullSelectionItemId())) {
+            target.addAttribute("nullselection", true);
+        }
+        target.addAttribute("key", key);
+        if (isSelected(itemId)) {
+            target.addAttribute("selected", true);
+        }
+    }
+
     /**
      * Invoked when the value of a variable has changed.
      * 
index d8baf0bf5ce85039ab24b4a64f2714c108e1f172..cc74ecf07abd718189cc4c38a72ef65fda310a50 100644 (file)
@@ -5,7 +5,9 @@
 package com.vaadin.ui;
 
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 
 import com.vaadin.data.Container;
 import com.vaadin.event.FieldEvents;
@@ -25,6 +27,8 @@ import com.vaadin.terminal.gwt.client.ui.VOptionGroup;
 public class OptionGroup extends AbstractSelect implements
         FieldEvents.BlurNotifier, FieldEvents.FocusNotifier {
 
+    private Set<Object> disabledItemIds = new HashSet<Object>();
+
     public OptionGroup() {
         super();
     }
@@ -47,6 +51,15 @@ public class OptionGroup extends AbstractSelect implements
         super.paintContent(target);
     }
 
+    @Override
+    protected void paintItem(PaintTarget target, Object itemId)
+            throws PaintException {
+        super.paintItem(target, itemId);
+        if (!isItemEnabled(itemId)) {
+            target.addAttribute("disabled", true);
+        }
+    }
+
     @Override
     public void changeVariables(Object source, Map<String, Object> variables) {
         super.changeVariables(source, variables);
@@ -78,4 +91,79 @@ public class OptionGroup extends AbstractSelect implements
 
     }
 
+    protected void setValue(Object newValue, boolean repaintIsNotNeeded) {
+        if (repaintIsNotNeeded) {
+            /*
+             * Check that value from changeVariables() doesn't contain unallowed
+             * selections: In the multi select mode, the user has selected or
+             * deselected a disabled item. In the single select mode, the user
+             * has selected a disabled item.
+             */
+            if (isMultiSelect()) {
+                Set currentValueSet = (Set) getValue();
+                Set newValueSet = (Set) newValue;
+                for (Object itemId : currentValueSet) {
+                    if (!isItemEnabled(itemId) && !newValueSet.contains(itemId)) {
+                        requestRepaint();
+                        return;
+                    }
+                }
+                for (Object itemId : newValueSet) {
+                    if (!isItemEnabled(itemId)
+                            && !currentValueSet.contains(itemId)) {
+                        requestRepaint();
+                        return;
+                    }
+                }
+            } else {
+                if (newValue == null) {
+                    newValue = getNullSelectionItemId();
+                }
+                if (!isItemEnabled(newValue)) {
+                    requestRepaint();
+                    return;
+                }
+            }
+        }
+        super.setValue(newValue, repaintIsNotNeeded);
+    }
+
+    /**
+     * Sets an item disabled or enabled. In the multiselect mode, a disabled
+     * item cannot be selected or deselected by the user. In the single
+     * selection mode, a disable item cannot be selected.
+     * 
+     * However, programmatical selection or deselection of an disable item is
+     * possible. By default, items are enabled.
+     * 
+     * @param itemId
+     *            the id of the item to be disabled or enabled
+     * @param enabled
+     *            if true the item is enabled, otherwise the item is disabled
+     */
+    public void setItemEnabled(Object itemId, boolean enabled) {
+        if (itemId != null) {
+            if (enabled) {
+                disabledItemIds.remove(itemId);
+            } else {
+                disabledItemIds.add(itemId);
+            }
+            requestRepaint();
+        }
+    }
+
+    /**
+     * Returns true if the item is enabled.
+     * 
+     * @param itemId
+     *            the id of the item to be checked
+     * @return true if the item is enabled, false otherwise
+     * @see #setItemEnabled(Object, boolean)
+     */
+    public boolean isItemEnabled(Object itemId) {
+        if (itemId != null) {
+            return !disabledItemIds.contains(itemId);
+        }
+        return true;
+    }
 }
diff --git a/tests/src/com/vaadin/tests/components/optiongroup/DisabledOptionGroupItems.html b/tests/src/com/vaadin/tests/components/optiongroup/DisabledOptionGroupItems.html
new file mode 100644 (file)
index 0000000..4841dff
--- /dev/null
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head profile="http://selenium-ide.openqa.org/profiles/test-case">
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+<link rel="selenium.base" href="" />
+<title>DisabledOptionGroupItems</title>
+</head>
+<body>
+<table cellpadding="1" cellspacing="1" border="1">
+<thead>
+<tr><td rowspan="1" colspan="3">DisabledOptionGroupItems</td></tr>
+</thead><tbody>
+<tr>
+       <td>open</td>
+       <td>/run/com.vaadin.tests.components.optiongroup.DisabledOptionGroupItems</td>
+       <td></td>
+</tr>
+<tr>
+       <td>waitForVaadin</td>
+       <td></td>
+       <td></td>
+</tr>
+<tr>
+       <td>screenCapture</td>
+       <td></td>
+       <td></td>
+</tr>
+
+</tbody></table>
+</body>
+</html>
diff --git a/tests/src/com/vaadin/tests/components/optiongroup/DisabledOptionGroupItems.java b/tests/src/com/vaadin/tests/components/optiongroup/DisabledOptionGroupItems.java
new file mode 100644 (file)
index 0000000..0b065da
--- /dev/null
@@ -0,0 +1,106 @@
+package com.vaadin.tests.components.optiongroup;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import com.vaadin.tests.components.ComponentTestCase;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.CheckBox;
+import com.vaadin.ui.Component;
+import com.vaadin.ui.OptionGroup;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.Button.ClickListener;
+
+public class DisabledOptionGroupItems extends ComponentTestCase<OptionGroup> {
+
+    private static final List<String> cities = Arrays.asList(new String[] {
+            "Berlin", "Brussels", "Helsinki", "Madrid", "Oslo", "Paris",
+            "Stockholm" });
+
+    private static final String NULL_SELECTION_ID = "Berlin";
+
+    @Override
+    protected void setup() {
+        super.setup();
+
+        OptionGroup og = createOptionGroup("");
+        og.setItemEnabled("Helsinki", false);
+        og.setItemEnabled("Paris", false);
+        og.setValue(Arrays.asList("Helsinki"));
+        og.setNullSelectionAllowed(true);
+        og.setNullSelectionItemId(NULL_SELECTION_ID);
+        addTestComponent(og);
+
+        og = createOptionGroup("");
+        og.setMultiSelect(true);
+        og.setValue(Arrays.asList("Helsinki"));
+        og.setNullSelectionAllowed(true);
+        og.setItemEnabled("Helsinki", false);
+        og.setItemEnabled("Paris", false);
+        addTestComponent(og);
+
+    }
+
+    private OptionGroup createOptionGroup(String caption) {
+        OptionGroup og = new OptionGroup(caption, cities);
+        og.setImmediate(true);
+        return og;
+    }
+
+    @Override
+    protected List<Component> createActions() {
+        ArrayList<Component> actions = new ArrayList<Component>();
+
+        CheckBox enabled = new CheckBox("Enabled", new Button.ClickListener() {
+            public void buttonClick(ClickEvent event) {
+                setEnabled(event.getButton().booleanValue());
+            }
+        });
+        enabled.setValue(true);
+        enabled.setImmediate(true);
+        actions.add(enabled);
+
+        CheckBox readonly = new CheckBox("Readonly",
+                new Button.ClickListener() {
+                    public void buttonClick(ClickEvent event) {
+                        setReadOnly(event.getButton().booleanValue());
+                    }
+                });
+        readonly.setValue(false);
+        readonly.setImmediate(true);
+        actions.add(readonly);
+
+        Button toggleDisabledItems = new Button("Toggle disabled items",
+                new ClickListener() {
+                    public void buttonClick(ClickEvent event) {
+                        for (OptionGroup og : getTestComponents()) {
+                            for (Object itemId : og.getItemIds()) {
+                                og.setItemEnabled(itemId, !og
+                                        .isItemEnabled(itemId));
+                            }
+                        }
+                    }
+                });
+        actions.add(toggleDisabledItems);
+
+        Button toggleSelectionMode = new Button("Toggle selection mode",
+                new ClickListener() {
+                    public void buttonClick(ClickEvent event) {
+                        for (OptionGroup og : getTestComponents()) {
+                            if (og.isMultiSelect()) {
+                                og.setMultiSelect(false);
+                                og.setNullSelectionItemId(NULL_SELECTION_ID);
+                            } else {
+                                og.setNullSelectionItemId(null);
+                                og.setMultiSelect(true);
+                            }
+
+                        }
+                    }
+                });
+        actions.add(toggleSelectionMode);
+
+        return actions;
+    }
+}