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");
}
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");
}
+ 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.
*
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;
public class OptionGroup extends AbstractSelect implements
FieldEvents.BlurNotifier, FieldEvents.FocusNotifier {
+ private Set<Object> disabledItemIds = new HashSet<Object>();
+
public OptionGroup() {
super();
}
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);
}
+ 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;
+ }
}
--- /dev/null
+<?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>
--- /dev/null
+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;
+ }
+}