Search in sources :

Example 1 with Input

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

the class AbstractWFieldIndicatorRenderer method doRender.

/**
 * Paints the given AbstractWFieldIndicator.
 *
 * @param component the WFieldErrorIndicator to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    AbstractWFieldIndicator fieldIndicator = (AbstractWFieldIndicator) component;
    XmlStringBuilder xml = renderContext.getWriter();
    WComponent validationTarget = fieldIndicator.getTargetComponent();
    // Diagnosables takes care of thieir own  messaging.
    if (validationTarget == null || (validationTarget instanceof Diagnosable && !(validationTarget instanceof Input))) {
        return;
    }
    if (validationTarget instanceof Input && !((Input) validationTarget).isReadOnly()) {
        return;
    }
    List<Diagnostic> diags = fieldIndicator.getDiagnostics();
    if (diags != null && !diags.isEmpty()) {
        xml.appendTagOpen("ui:fieldindicator");
        xml.appendAttribute("id", component.getId());
        xml.appendOptionalAttribute("track", component.isTracking(), "true");
        switch(fieldIndicator.getFieldIndicatorType()) {
            case INFO:
                xml.appendAttribute("type", "info");
                break;
            case WARN:
                xml.appendAttribute("type", "warn");
                break;
            case ERROR:
                xml.appendAttribute("type", "error");
                break;
            default:
                throw new SystemException("Cannot paint field indicator due to an invalid field indicator type: " + fieldIndicator.getFieldIndicatorType());
        }
        xml.appendAttribute("for", fieldIndicator.getRelatedFieldId());
        xml.appendClose();
        for (Diagnostic diag : diags) {
            xml.appendTag("ui:message");
            xml.appendEscaped(diag.getDescription());
            xml.appendEndTag("ui:message");
        }
        xml.appendEndTag("ui:fieldindicator");
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) AbstractWFieldIndicator(com.github.bordertech.wcomponents.validation.AbstractWFieldIndicator) Input(com.github.bordertech.wcomponents.Input) SystemException(com.github.bordertech.wcomponents.util.SystemException) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder) Diagnosable(com.github.bordertech.wcomponents.Diagnosable)

Example 2 with Input

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

the class AbstractSetMandatory_Test method testExecute.

@Test
public void testExecute() {
    // ---------------------
    // Valid Target (WInput) and TRUE Boolean Value
    SubordinateTarget target1 = new MyTarget();
    AbstractSetMandatory mandatory = new MyMandatory(target1, Boolean.TRUE);
    // Should be mandatory
    mandatory.execute();
    Assert.assertTrue("Target (Mandatable) should be mandatory", ((Mandatable) target1).isMandatory());
    // FALSE Boolean Value
    mandatory = new MyMandatory(target1, Boolean.FALSE);
    // Should not be mandatory
    mandatory.execute();
    Assert.assertFalse("Target (Mandatable) should not be mandatory", ((Mandatable) target1).isMandatory());
    // ---------------------
    // Valid Target (WField) and TRUE Boolean Value
    Input textArea = new WTextArea();
    WField target2 = new WFieldLayout().addField("test", textArea);
    mandatory = new MyMandatory(target2, Boolean.TRUE);
    // Should be mandatory
    mandatory.execute();
    Assert.assertTrue("Target (WField) should be mandatory", textArea.isMandatory());
    // FALSE Boolean Value
    mandatory = new MyMandatory(target2, Boolean.FALSE);
    // Should not be mandatory
    mandatory.execute();
    Assert.assertFalse("Target (WField) should not be mandatory", textArea.isMandatory());
    // ---------------------
    // Valid Target (WFieldSet) and TRUE Boolean Value
    WFieldSet target3 = new WFieldSet("Test");
    mandatory = new MyMandatory(target3, Boolean.TRUE);
    // Should be mandatory
    mandatory.execute();
    Assert.assertTrue("Target (WFieldSet) should be mandatory", target3.isMandatory());
    // FALSE Boolean Value
    mandatory = new MyMandatory(target3, Boolean.FALSE);
    // Should not be mandatory
    mandatory.execute();
    Assert.assertFalse("Target (WFieldSet) should not be mandatory", target3.isMandatory());
    // ---------------------
    // Invalid Target (Cannot be set Mandatory) and Boolean Value
    MyInvalidTarget target4 = new MyInvalidTarget();
    mandatory = new MyMandatory(target4, Boolean.TRUE);
    // Should do nothing
    mandatory.execute();
}
Also used : WTextArea(com.github.bordertech.wcomponents.WTextArea) SubordinateTarget(com.github.bordertech.wcomponents.SubordinateTarget) Input(com.github.bordertech.wcomponents.Input) WField(com.github.bordertech.wcomponents.WField) WFieldSet(com.github.bordertech.wcomponents.WFieldSet) WFieldLayout(com.github.bordertech.wcomponents.WFieldLayout) Test(org.junit.Test)

Example 3 with Input

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

the class AbstractCompare method getTriggerValue.

/**
 * Get the value for the trigger.
 * <p>
 * If no request is passed in, the current value of the trigger is used.
 * </p>
 * <p>
 * It will return the same "value" the client would have used in its subordinate logic.
 * </p>
 * <p>
 * The trigger value will either be (i) a date formatted String for WDateFields, (ii) a BigDecimal for WNumberFields
 * or (iii) a List of String values for MultiSelect components or (iv) a String value.
 * </p>
 *
 * @param request the request being processed, can be null
 * @return the value to be used for the trigger
 */
protected Object getTriggerValue(final Request request) {
    // Date Compare (Use Date Formatted String - YYYY-MM-DD)
    if (trigger instanceof WDateField) {
        final WDateField input = (WDateField) trigger;
        Date date;
        if (request == null) {
            date = input.getValue();
        } else {
            date = input.getRequestValue(request);
        }
        return date == null ? null : new SimpleDateFormat(INTERNAL_DATE_FORMAT).format(date);
    } else if (trigger instanceof WNumberField) {
        // Number Compare (Use Number Object)
        final WNumberField input = (WNumberField) trigger;
        if (request == null) {
            return input.getValue();
        } else {
            return input.getRequestValue(request);
        }
    } else if (trigger instanceof AbstractWSingleSelectList) {
        // String Compare for Single Select Lists (Use the Option's Code)
        final AbstractWSingleSelectList list = (AbstractWSingleSelectList) trigger;
        final Object selected;
        if (request == null) {
            selected = list.getValue();
        } else {
            selected = list.getRequestValue(request);
        }
        // Convert selected option to its "code" (Should always have a value)
        String code = list.optionToCode(selected);
        return code;
    } else if (trigger instanceof AbstractWMultiSelectList) {
        // String Compare for Multi Select Lists (Use the Option's Code)
        final AbstractWMultiSelectList list = (AbstractWMultiSelectList) trigger;
        final List<?> selected;
        if (request == null) {
            selected = list.getValue();
        } else {
            selected = list.getRequestValue(request);
        }
        // Empty is treated the same as null
        if (selected == null || selected.isEmpty()) {
            return null;
        }
        // Convert selected options to their "code" (Should always have a value)
        List<String> codes = new ArrayList<>(selected.size());
        for (Object select : selected) {
            String code = list.optionToCode(select);
            codes.add(code);
        }
        return codes;
    } else if (trigger instanceof Input) {
        // String Compare - Use the String Value of the Input
        final Input input = (Input) trigger;
        final Object inputValue;
        if (request == null) {
            inputValue = input.getValue();
        } else {
            inputValue = input.getRequestValue(request);
        }
        // Treat empty the same as null
        return (inputValue == null || Util.empty(inputValue.toString())) ? null : inputValue.toString();
    } else {
        throw new SystemException("Trigger is not a valid type.");
    }
}
Also used : Input(com.github.bordertech.wcomponents.Input) WNumberField(com.github.bordertech.wcomponents.WNumberField) SystemException(com.github.bordertech.wcomponents.util.SystemException) WDateField(com.github.bordertech.wcomponents.WDateField) ArrayList(java.util.ArrayList) List(java.util.List) AbstractWSelectList(com.github.bordertech.wcomponents.AbstractWSelectList) AbstractWSingleSelectList(com.github.bordertech.wcomponents.AbstractWSingleSelectList) AbstractWMultiSelectList(com.github.bordertech.wcomponents.AbstractWMultiSelectList) SimpleDateFormat(java.text.SimpleDateFormat) AbstractWMultiSelectList(com.github.bordertech.wcomponents.AbstractWMultiSelectList) Date(java.util.Date) AbstractWSingleSelectList(com.github.bordertech.wcomponents.AbstractWSingleSelectList)

Example 4 with Input

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

the class WLabelRenderer method doRender.

/**
 * Paints the given {@link WLabel}.
 *
 * @param component the WLabel to paint.
 * @param renderContext the RenderContext to paint to.
 */
@Override
public void doRender(final WComponent component, final WebXmlRenderContext renderContext) {
    WLabel label = (WLabel) component;
    XmlStringBuilder xml = renderContext.getWriter();
    xml.appendTagOpen("ui:label");
    xml.appendAttribute("id", component.getId());
    xml.appendOptionalAttribute("class", component.getHtmlClass());
    xml.appendOptionalAttribute("track", component.isTracking(), "true");
    xml.appendOptionalAttribute("for", label.getLabelFor());
    WComponent what = label.getForComponent();
    String whatFor = null;
    if (what instanceof MultiInputComponent) {
        whatFor = "group";
    } else if (what instanceof Labelable) {
        whatFor = "input";
    }
    boolean isReadOnly = ((what instanceof Input) && ((Input) what).isReadOnly()) || (what instanceof WRadioButton && ((WRadioButton) what).isReadOnly());
    boolean isMandatory = (what instanceof Input) && ((Input) what).isMandatory();
    xml.appendOptionalAttribute("what", whatFor);
    xml.appendOptionalAttribute("readonly", isReadOnly, "true");
    xml.appendOptionalAttribute("required", isMandatory, "true");
    xml.appendOptionalAttribute("hiddencomponent", (what != null && what.isHidden()), "true");
    xml.appendOptionalAttribute("hint", label.getHint());
    xml.appendOptionalAttribute("accessKey", Util.upperCase(label.getAccessKeyAsString()));
    xml.appendOptionalAttribute("hidden", label.isHidden(), "true");
    xml.appendOptionalAttribute("toolTip", label.getToolTip());
    xml.appendOptionalAttribute("accessibleText", label.getAccessibleText());
    xml.appendClose();
    xml.append(label.getText(), label.isEncodeText());
    paintChildren(label, renderContext);
    xml.appendEndTag("ui:label");
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) Labelable(com.github.bordertech.wcomponents.Labelable) MultiInputComponent(com.github.bordertech.wcomponents.MultiInputComponent) Input(com.github.bordertech.wcomponents.Input) WRadioButton(com.github.bordertech.wcomponents.WRadioButton) XmlStringBuilder(com.github.bordertech.wcomponents.XmlStringBuilder) WLabel(com.github.bordertech.wcomponents.WLabel)

Example 5 with Input

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

the class DiagnosticImpl method getDescription.

/**
 * {@inheritDoc}
 */
@Override
public String getDescription() {
    // We need to change references to input fields to their label or accessible text.
    Object[] modifiedArgs = new Object[args.length];
    for (int i = 0; i < args.length; i++) {
        if (args[i] instanceof Input) {
            Input input = (Input) args[i];
            String text = null;
            UIContextHolder.pushContext(uic);
            try {
                if (input.getLabel() != null) {
                    text = input.getLabel().getText();
                    // Some apps use colons at the end of labels. We trim these off automatically
                    if (!Util.empty(text) && text.charAt(text.length() - 1) == ':') {
                        text = text.substring(0, text.length() - 1);
                    }
                }
                if (text == null) {
                    text = input.getAccessibleText();
                }
                if (text == null) {
                    text = input.getToolTip();
                }
            } finally {
                UIContextHolder.popContext();
            }
            modifiedArgs[i] = text == null ? "" : text;
        } else {
            modifiedArgs[i] = args[i];
        }
    }
    return I18nUtilities.format(null, message, modifiedArgs);
}
Also used : Input(com.github.bordertech.wcomponents.Input)

Aggregations

Input (com.github.bordertech.wcomponents.Input)6 WComponent (com.github.bordertech.wcomponents.WComponent)2 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)2 SystemException (com.github.bordertech.wcomponents.util.SystemException)2 Test (org.junit.Test)2 AbstractWMultiSelectList (com.github.bordertech.wcomponents.AbstractWMultiSelectList)1 AbstractWSelectList (com.github.bordertech.wcomponents.AbstractWSelectList)1 AbstractWSingleSelectList (com.github.bordertech.wcomponents.AbstractWSingleSelectList)1 Diagnosable (com.github.bordertech.wcomponents.Diagnosable)1 Labelable (com.github.bordertech.wcomponents.Labelable)1 MultiInputComponent (com.github.bordertech.wcomponents.MultiInputComponent)1 SubordinateTarget (com.github.bordertech.wcomponents.SubordinateTarget)1 WDateField (com.github.bordertech.wcomponents.WDateField)1 WField (com.github.bordertech.wcomponents.WField)1 WFieldLayout (com.github.bordertech.wcomponents.WFieldLayout)1 WFieldSet (com.github.bordertech.wcomponents.WFieldSet)1 WLabel (com.github.bordertech.wcomponents.WLabel)1 WNumberField (com.github.bordertech.wcomponents.WNumberField)1 WRadioButton (com.github.bordertech.wcomponents.WRadioButton)1 WTextArea (com.github.bordertech.wcomponents.WTextArea)1