Search in sources :

Example 1 with WRadioButton

use of com.github.bordertech.wcomponents.WRadioButton in project wcomponents by BorderTech.

the class AbstractCompare method getCompareValue.

/**
 * Get the value to use in the compare.
 * <p>
 * It will return the same "value" the client would have used in its subordinate logic.
 * </p>
 * <p>
 * The compare value will either be (i) a date formatted String for WDateFields, (ii) a BigDecimal for WNumberFields
 * or (iii) a String value.
 * </p>
 *
 * @return the value to be used for the compare.
 */
protected Object getCompareValue() {
    // Date Compare (Use Date Formatted String - YYYY-MM-DD)
    if (trigger instanceof WDateField) {
        return value == null ? null : new SimpleDateFormat(INTERNAL_DATE_FORMAT).format(value);
    } else if (trigger instanceof WNumberField) {
        // Number Compare (Use Number Object)
        return value;
    } else if (trigger instanceof AbstractWSelectList) {
        // String Compare - List (Use the Option's Code)
        final AbstractWSelectList listTrigger = (AbstractWSelectList) trigger;
        final List<?> options = listTrigger.getOptions();
        // No options, just return the compare value (so that a test against null works correctly)
        if (options == null || options.isEmpty()) {
            return value == null ? null : value.toString();
        }
        // Check if the value is a valid option allowing for "Legacy" matching
        if (SelectListUtil.containsOptionWithMatching(options, value)) {
            Object option = SelectListUtil.getOptionWithMatching(options, value);
            String code = listTrigger.optionToCode(option);
            return code;
        }
        // Return the value as a String - Treat empty the same as null
        return (value == null || Util.empty(value.toString())) ? null : value.toString();
    } else if (trigger instanceof RadioButtonGroup && value instanceof WRadioButton) {
        // String Compare for RadioButtonGroup and value is WRadioButton (Use the button value)
        // Note - This is only for backward compatibility where projects have used a radio button
        // in the trigger. Projects should use the value expected, not the radio button.
        // If the radio button passed into the compare is used in a repeater, then this compare will not work.
        String data = ((WRadioButton) value).getValue();
        // Treat empty the same as null
        return Util.empty(data) ? null : data;
    } else {
        // Treat empty the same as null
        return (value == null || Util.empty(value.toString())) ? null : value.toString();
    }
}
Also used : AbstractWSelectList(com.github.bordertech.wcomponents.AbstractWSelectList) WRadioButton(com.github.bordertech.wcomponents.WRadioButton) WNumberField(com.github.bordertech.wcomponents.WNumberField) WDateField(com.github.bordertech.wcomponents.WDateField) RadioButtonGroup(com.github.bordertech.wcomponents.RadioButtonGroup) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with WRadioButton

use of com.github.bordertech.wcomponents.WRadioButton in project wcomponents by BorderTech.

the class WRadioButtonRenderer_Test method testDoPaint.

@Test
public void testDoPaint() throws IOException, SAXException, XpathException {
    RadioButtonGroup group = new RadioButtonGroup();
    WRadioButton button = group.addRadioButton(1);
    button.setVisible(true);
    assertSchemaMatch(button);
    assertXpathExists("//ui:radiobutton", button);
    assertXpathEvaluatesTo(button.getId(), "//ui:radiobutton/@id", button);
    assertXpathEvaluatesTo(button.getGroupName(), "//ui:radiobutton/@groupName", button);
    assertXpathEvaluatesTo(button.getValue(), "//ui:radiobutton/@value", button);
    assertXpathNotExists("//ui:radiobutton/@submitOnChange", button);
    // Check selected
    assertXpathNotExists("//ui:radiobutton/@selected", button);
    button.setSelected(true);
    assertSchemaMatch(button);
    assertXpathEvaluatesTo("true", "//ui:radiobutton/@selected", button);
    // Check disabled
    assertXpathNotExists("//ui:radiobutton/@disabled", button);
    button.setDisabled(true);
    assertSchemaMatch(button);
    assertXpathEvaluatesTo("true", "//ui:radiobutton/@disabled", button);
    // Check hidden
    assertXpathNotExists("//ui:radiobutton/@hidden", button);
    setFlag(button, ComponentModel.HIDE_FLAG, true);
    assertSchemaMatch(button);
    assertXpathEvaluatesTo("true", "//ui:radiobutton/@hidden", button);
    // Check required
    assertXpathNotExists("//ui:radiobutton/@required", button);
    group.setMandatory(true);
    assertSchemaMatch(button);
    assertXpathEvaluatesTo("true", "//ui:radiobutton/@required", button);
    // Check toolTip
    String toolTip = "WRadioButton_Test.testRenderedFormat.toolTip";
    button.setToolTip(toolTip);
    assertSchemaMatch(button);
    assertXpathEvaluatesTo(toolTip, "//ui:radiobutton/@toolTip", button);
    button.setToolTip(null);
    assertSchemaMatch(button);
    assertXpathNotExists("//ui:radiobutton/@toolTip", button);
    // Check submitOnChange
    group = new RadioButtonGroup();
    button = group.addRadioButton(1);
    group.setSubmitOnChange(true);
    assertSchemaMatch(button);
    assertXpathEvaluatesTo("true", "//ui:radiobutton/@submitOnChange", button);
}
Also used : WRadioButton(com.github.bordertech.wcomponents.WRadioButton) RadioButtonGroup(com.github.bordertech.wcomponents.RadioButtonGroup) Test(org.junit.Test)

Example 3 with WRadioButton

use of com.github.bordertech.wcomponents.WRadioButton in project wcomponents by BorderTech.

the class WRadioButtonRenderer_Test method testIsNullOption.

@Test
public void testIsNullOption() throws IOException, SAXException, XpathException {
    WPanel root = new WPanel();
    RadioButtonGroup group = new RadioButtonGroup();
    root.add(group);
    WRadioButton button1 = group.addRadioButton(null);
    WRadioButton button2 = group.addRadioButton("");
    WRadioButton button3 = group.addRadioButton("A");
    root.add(button1);
    root.add(button2);
    root.add(button3);
    assertSchemaMatch(root);
    assertXpathEvaluatesTo("true", "//ui:radiobutton/@isNull", button1);
    assertXpathEvaluatesTo("true", "//ui:radiobutton/@isNull", button2);
    assertXpathEvaluatesTo("", "//ui:radiobutton/@isNull", button3);
}
Also used : WRadioButton(com.github.bordertech.wcomponents.WRadioButton) WPanel(com.github.bordertech.wcomponents.WPanel) RadioButtonGroup(com.github.bordertech.wcomponents.RadioButtonGroup) Test(org.junit.Test)

Example 4 with WRadioButton

use of com.github.bordertech.wcomponents.WRadioButton in project wcomponents by BorderTech.

the class ByWComponent method findValue.

/**
 * Narrows down the search for a WebElement to find the appropriate value.
 *
 * @param current the current WebElement which was reached during a search.
 * @param component the component corresponding to the given WebElement.
 * @param context the context for the component.
 * @param value the value to search for.
 * @return the WebElement with the given value, or null if not found.
 */
public static WebElement findValue(final WebElement current, final WComponent component, final UIContext context, final Object value) {
    // If not narrowing down the search, just return the current element.
    if (value == null) {
        return current;
    }
    UIContextHolder.pushContext(context);
    try {
        if (component instanceof AbstractWSelectList) {
            AbstractWSelectList list = (AbstractWSelectList) component;
            List<?> options = list.getOptions();
            if (options != null) {
                for (int i = 0; i < options.size(); i++) {
                    Object option = options.get(i);
                    if (Util.equals(value, option) || Util.equals(value.toString(), list.getDesc(option, i))) {
                        return current.findElement(By.xpath(".//*[@value='" + list.getCode(option, i) + "']"));
                    }
                }
            }
            // Not found
            return null;
        } else if (component instanceof WRadioButton) {
            return value.equals(((WRadioButton) component).getValue()) ? current : null;
        } else {
            return current.findElement(By.xpath(".//*[@value='" + WebUtilities.encode(String.valueOf(value)) + "']"));
        }
    } finally {
        UIContextHolder.popContext();
    }
}
Also used : AbstractWSelectList(com.github.bordertech.wcomponents.AbstractWSelectList) WRadioButton(com.github.bordertech.wcomponents.WRadioButton)

Example 5 with WRadioButton

use of com.github.bordertech.wcomponents.WRadioButton in project wcomponents by BorderTech.

the class WLabelExample method addNullLabelExample.

/**
 * Example of when and how to use a null WLabel.
 */
private void addNullLabelExample() {
    add(new WHeading(HeadingLevel.H2, "How to use accessible null WLabels"));
    add(new ExplanatoryText("These examples shows how sometime a null WLabel is the right thing to do." + "\n This example uses a WFieldSet as the labelled component and it has its own \"label\"."));
    // We want to add a WFieldSet to a WFieldLayout but without an extra label.
    WFieldLayout fieldsFlat = new WFieldLayout();
    fieldsFlat.setMargin(new Margin(null, null, Size.XL, null));
    add(fieldsFlat);
    WFieldSet fs = new WFieldSet("Do you like Bananas?");
    fieldsFlat.addField((WLabel) null, fs);
    // now add the WRadioButtons to the WFieldSet using an inner WFieldLayout
    WFieldLayout innerLayout = new WFieldLayout(WFieldLayout.LAYOUT_STACKED);
    // The content will be a group of WRadioButtons
    RadioButtonGroup group1 = new RadioButtonGroup();
    WRadioButton rb1 = group1.addRadioButton(1);
    WRadioButton rb2 = group1.addRadioButton(2);
    // make the labels for the radio buttons
    WLabel rb1Label = new WLabel("", rb1);
    WImage labelImage = new WImage("/image/success.png", "I still like bananas");
    labelImage.setHtmlClass("wc-valign-bottom");
    rb1Label.add(labelImage);
    WLabel rb2Label = new WLabel("", rb2);
    labelImage = new WImage("/image/error.png", "I still dislike bananas");
    labelImage.setHtmlClass("wc-valign-bottom");
    rb2Label.add(labelImage);
    innerLayout.addField(rb1Label, rb1);
    innerLayout.addField(rb2Label, rb2);
    // add the content to the WFieldLayout - the order really doesn't matter.
    fs.add(group1);
    fs.add(innerLayout);
}
Also used : WFieldSet(com.github.bordertech.wcomponents.WFieldSet) WRadioButton(com.github.bordertech.wcomponents.WRadioButton) WFieldLayout(com.github.bordertech.wcomponents.WFieldLayout) RadioButtonGroup(com.github.bordertech.wcomponents.RadioButtonGroup) WImage(com.github.bordertech.wcomponents.WImage) ExplanatoryText(com.github.bordertech.wcomponents.examples.common.ExplanatoryText) WHeading(com.github.bordertech.wcomponents.WHeading) Margin(com.github.bordertech.wcomponents.Margin) WLabel(com.github.bordertech.wcomponents.WLabel)

Aggregations

WRadioButton (com.github.bordertech.wcomponents.WRadioButton)12 RadioButtonGroup (com.github.bordertech.wcomponents.RadioButtonGroup)9 Test (org.junit.Test)6 WLabel (com.github.bordertech.wcomponents.WLabel)3 AbstractWSelectList (com.github.bordertech.wcomponents.AbstractWSelectList)2 WDateField (com.github.bordertech.wcomponents.WDateField)2 WFieldLayout (com.github.bordertech.wcomponents.WFieldLayout)2 WFieldSet (com.github.bordertech.wcomponents.WFieldSet)2 WNumberField (com.github.bordertech.wcomponents.WNumberField)2 WTextField (com.github.bordertech.wcomponents.WTextField)2 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)2 Input (com.github.bordertech.wcomponents.Input)1 Labelable (com.github.bordertech.wcomponents.Labelable)1 Margin (com.github.bordertech.wcomponents.Margin)1 MultiInputComponent (com.github.bordertech.wcomponents.MultiInputComponent)1 SubordinateTarget (com.github.bordertech.wcomponents.SubordinateTarget)1 WCheckBox (com.github.bordertech.wcomponents.WCheckBox)1 WCheckBoxSelect (com.github.bordertech.wcomponents.WCheckBoxSelect)1 WComponent (com.github.bordertech.wcomponents.WComponent)1 WContainer (com.github.bordertech.wcomponents.WContainer)1