private void updateItem(RadioButton button, JsonObject item,
boolean requireInitialization) {
+ if (requireInitialization) {
+ getWidget().add(button);
+ button.setStyleName("v-radiobutton");
+ button.addStyleName(CLASSNAME_OPTION);
+ button.addClickHandler(this);
+ }
+
String itemHtml = item
.getString(ListingJsonConstants.JSONKEY_ITEM_VALUE);
if (!isHtmlContentAllowed()) {
button.setHTML(itemHtml);
boolean optionEnabled = !item
.getBoolean(ListingJsonConstants.JSONKEY_ITEM_DISABLED);
- boolean enabled = optionEnabled && !isReadonly() && isEnabled();
- button.setEnabled(enabled);
- // #9258 apply the v-disabled class when disabled for UX
- button.setStyleName(StyleConstants.DISABLED,
- !isEnabled() || !optionEnabled);
+ updateItemEnabled(button, optionEnabled);
updateItemSelection(button,
item.getBoolean(ListingJsonConstants.JSONKEY_ITEM_SELECTED));
String key = item.getString(DataCommunicatorConstants.KEY);
- if (requireInitialization) {
- getWidget().add(button);
- button.setStyleName("v-radiobutton");
- button.addStyleName(CLASSNAME_OPTION);
- button.addClickHandler(this);
- }
optionsToItems.put(button, item);
keyToOptions.put(key, button);
}
}
protected void updateEnabledState() {
- boolean radioButtonEnabled = isEnabled() && !isReadonly();
// sets options enabled according to the widget's enabled,
// readonly and each options own enabled
for (Map.Entry<RadioButton, JsonObject> entry : optionsToItems
JsonObject value = entry.getValue();
boolean optionEnabled = !value
.getBoolean(ListingJsonConstants.JSONKEY_ITEM_DISABLED);
- radioButton.setEnabled(radioButtonEnabled && optionEnabled);
- // #9258 apply the v-disabled class when disabled for UX
- radioButton.setStyleName(StyleConstants.DISABLED,
- !isEnabled() || !optionEnabled);
+ updateItemEnabled(radioButton, optionEnabled);
}
}
protected void updateItemSelection(RadioButton radioButton, boolean value) {
radioButton.setValue(value);
radioButton.setStyleName(CLASSNAME_OPTION_SELECTED, value);
+ }
+ /**
+ * Updates the enabled state of a radio button.
+ *
+ * @param radioButton
+ * the radio button to update
+ * @param value
+ * {@code true} if enabled; {@code false} if not
+ *
+ * @since
+ */
+ protected void updateItemEnabled(RadioButton radioButton, boolean value) {
+ boolean enabled = value && !isReadonly() && isEnabled();
+ radioButton.setEnabled(enabled);
+ // #9258 apply the v-disabled class when disabled for UX
+ boolean hasDisabledStyle = !isEnabled() || !value;
+ radioButton.setStyleName(StyleConstants.DISABLED, hasDisabledStyle);
}
}
}
@OnStateChange("readOnly")
- @SuppressWarnings("deprecation")
void updateWidgetReadOnly() {
getWidget().setEnabled(isEnabled() && !isReadOnly());
}
return optionTexts;
}
+ /**
+ * Gets the list of option elements for this check box group.
+ *
+ * @return list of option elements
+ * @since
+ */
+ public List<WebElement> getOptionElements() {
+ return findElements(bySelectOption);
+ }
+
public void selectByText(String text) throws ReadOnlyException {
if (isReadOnly()) {
throw new ReadOnlyException();
--- /dev/null
+package com.vaadin.tests.components.checkboxgroup;
+
+import com.vaadin.annotations.Widgetset;
+import com.vaadin.server.SerializablePredicate;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.CheckBoxGroup;
+
+@Widgetset("com.vaadin.DefaultWidgetSet")
+public class CheckBoxGroupItemDisabled extends AbstractTestUI {
+
+ public static final SerializablePredicate<Integer> ENABLED_PROVIDER = i -> i != 3;
+
+ @Override
+ protected void setup(VaadinRequest request) {
+ CheckBoxGroup<Integer> cbg = new CheckBoxGroup<>();
+ cbg.setItems(1, 2, 3, 4);
+ cbg.setItemEnabledProvider(ENABLED_PROVIDER);
+ addComponent(cbg);
+ }
+}
--- /dev/null
+package com.vaadin.tests.components.radiobuttongroup;
+
+import com.vaadin.annotations.Widgetset;
+import com.vaadin.server.SerializablePredicate;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.RadioButtonGroup;
+
+@Widgetset("com.vaadin.DefaultWidgetSet")
+public class RadioButtonGroupItemDisabled extends AbstractTestUI {
+
+ public static final SerializablePredicate<Integer> ENABLED_PROVIDER = i -> i != 3;
+
+ @Override
+ protected void setup(VaadinRequest request) {
+ RadioButtonGroup<Integer> rbg = new RadioButtonGroup<>();
+ rbg.setItems(1, 2, 3, 4);
+ rbg.setItemEnabledProvider(ENABLED_PROVIDER);
+ addComponent(rbg);
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2000-2016 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.components.checkboxgroup;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.junit.Test;
+import org.openqa.selenium.WebElement;
+
+import com.vaadin.testbench.elements.CheckBoxGroupElement;
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+public class CheckBoxGroupItemDisabledTest extends MultiBrowserTest {
+
+ @Test
+ public void itemDisabledOnInit() {
+ openTestURL();
+ List<WebElement> options = $(CheckBoxGroupElement.class).first()
+ .getOptionElements();
+ options.stream().forEach(option -> {
+ Integer value = Integer.parseInt(option.getText());
+ boolean disabled = !CheckBoxGroupItemDisabled.ENABLED_PROVIDER
+ .test(value);
+ assertEquals(
+ "Unexpected status of v-disabled stylename for item "
+ + value,
+ disabled,
+ option.getAttribute("class").contains("v-disabled"));
+ });
+ }
+}
--- /dev/null
+package com.vaadin.tests.components.radiobuttongroup;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.junit.Test;
+import org.openqa.selenium.WebElement;
+
+import com.vaadin.testbench.elements.RadioButtonGroupElement;
+import com.vaadin.tests.tb3.MultiBrowserTest;
+
+public class RadioButtonGroupItemDisabledTest extends MultiBrowserTest {
+
+ @Test
+ public void itemDisabledOnInit() {
+ openTestURL();
+ List<WebElement> options = $(RadioButtonGroupElement.class).first()
+ .getOptionElements();
+ options.stream().forEach(option -> {
+ Integer value = Integer.parseInt(option.getText());
+ boolean disabled = !RadioButtonGroupItemDisabled.ENABLED_PROVIDER
+ .test(value);
+ assertEquals(
+ "Unexpected status of v-disabled stylename for item "
+ + value,
+ disabled,
+ option.getAttribute("class").contains("v-disabled"));
+ });
+ }
+}