@@ -139,6 +139,13 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite | |||
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()) { | |||
@@ -154,22 +161,12 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite | |||
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); | |||
} | |||
@@ -205,7 +202,6 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite | |||
} | |||
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 | |||
@@ -214,10 +210,7 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite | |||
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); | |||
} | |||
} | |||
@@ -305,6 +298,23 @@ public class VRadioButtonGroup extends FocusableFlowPanelComposite | |||
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); | |||
} | |||
} |
@@ -85,7 +85,6 @@ public class RadioButtonGroupConnector | |||
} | |||
@OnStateChange("readOnly") | |||
@SuppressWarnings("deprecation") | |||
void updateWidgetReadOnly() { | |||
getWidget().setEnabled(isEnabled() && !isReadOnly()); | |||
} |
@@ -41,6 +41,16 @@ public class RadioButtonGroupElement extends AbstractSingleSelectElement { | |||
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(); |
@@ -0,0 +1,21 @@ | |||
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); | |||
} | |||
} |
@@ -0,0 +1,21 @@ | |||
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); | |||
} | |||
} |
@@ -0,0 +1,46 @@ | |||
/* | |||
* 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")); | |||
}); | |||
} | |||
} |
@@ -0,0 +1,31 @@ | |||
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")); | |||
}); | |||
} | |||
} |