Search in sources :

Example 1 with AbstractWSelectList

use of com.github.bordertech.wcomponents.AbstractWSelectList 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 2 with AbstractWSelectList

use of com.github.bordertech.wcomponents.AbstractWSelectList 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)

Aggregations

AbstractWSelectList (com.github.bordertech.wcomponents.AbstractWSelectList)2 WRadioButton (com.github.bordertech.wcomponents.WRadioButton)2 RadioButtonGroup (com.github.bordertech.wcomponents.RadioButtonGroup)1 WDateField (com.github.bordertech.wcomponents.WDateField)1 WNumberField (com.github.bordertech.wcomponents.WNumberField)1 SimpleDateFormat (java.text.SimpleDateFormat)1