diff options
author | Artur Signell <artur.signell@itmill.com> | 2010-06-15 07:09:03 +0000 |
---|---|---|
committer | Artur Signell <artur.signell@itmill.com> | 2010-06-15 07:09:03 +0000 |
commit | 3c468800e265a1897d74986f0f2c0092720769a0 (patch) | |
tree | f4e094b36dfaa4f472f624db74728bb4e88e5d51 /src/com | |
parent | 2497bb21873ce07fccc595faa2a7538215f5ec2c (diff) | |
download | vaadin-framework-3c468800e265a1897d74986f0f2c0092720769a0.tar.gz vaadin-framework-3c468800e265a1897d74986f0f2c0092720769a0.zip |
Merged fix, sample and tests for "#3720 - Disable Items for OptionGroup" from /branches/6.4-minor-features
svn changeset:13659/svn branch:6.4
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/vaadin/ui/AbstractSelect.java | 44 | ||||
-rw-r--r-- | src/com/vaadin/ui/OptionGroup.java | 88 |
2 files changed, 109 insertions, 23 deletions
diff --git a/src/com/vaadin/ui/AbstractSelect.java b/src/com/vaadin/ui/AbstractSelect.java index e8ebdc6724..60dfea0da2 100644 --- a/src/com/vaadin/ui/AbstractSelect.java +++ b/src/com/vaadin/ui/AbstractSelect.java @@ -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. * diff --git a/src/com/vaadin/ui/OptionGroup.java b/src/com/vaadin/ui/OptionGroup.java index d8baf0bf5c..cc74ecf07a 100644 --- a/src/com/vaadin/ui/OptionGroup.java +++ b/src/com/vaadin/ui/OptionGroup.java @@ -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(); } @@ -48,6 +52,15 @@ public class OptionGroup extends AbstractSelect implements } @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; + } } |