/** * Determine if this component is on the Request. * * @param request the request being responded to. * @return true if this component is on the Request, otherwise return false. */ protected boolean isPresent(final Request request) { return request.getParameter(getId()) != null; }
/** * {@inheritDoc} */ @Override public String getValueAsString() { Object data = getValue(); return data == null ? null : data.toString(); }
/** * @return a String representation of this component, for debugging purposes. */ @Override public String toString() { String text = getValueAsString(); text = text == null ? "null" : '"' + text + '"'; return toString(text); }
/** * Set the changed flag to indicate if the component changed in the last request. * * @param changed true if the value changed in the request */ protected void setChangedInLastRequest(final boolean changed) { if (isChangedInLastRequest() != changed) { InputModel model = getOrCreateComponentModel(); model.changedInLastRequest = changed; } }
/** * Override WComponent's validatorComponent in order to use the validators which have been added to this input * field. Subclasses may still override this method, but it is suggested to call super.validateComponent to ensure * that the validators are still used. * * @param diags the list into which any validation diagnostics are added. */ @Override protected void validateComponent(final List<Diagnostic> diags) { // Mandatory validation if (isMandatory() && isEmpty()) { diags.add(createMandatoryDiagnostic()); } // Other validations List<FieldValidator> validators = getComponentModel().validators; if (validators != null) { for (FieldValidator validator : validators) { validator.validate(diags); } } }
if (!beforeHandleRequest(request)) { return; setChangedInLastRequest(false); if (isDisabled() || isReadOnly()) { if (isPresent(request)) { boolean changed = doHandleRequest(request); setChangedInLastRequest(true); doHandleChanged();
AbstractInput input = new MyInput(); TestAction action = new TestAction(); input.setActionOnChange(action); input.serviceRequest(request); Assert.assertNull("Input value after empty request should be null", input.getValue()); Assert.assertFalse("Change Action should not have triggered for an empty request", action. wasTriggered()); Assert.assertFalse("Changed in last request should be false for an empty request", input.isChangedInLastRequest()); request.setParameter(input.getId(), value); action.reset(); input.serviceRequest(request); Assert.assertEquals("Input value is incorrect after request with a value", value, input. getValue()); Assert.assertTrue("Change Action should have triggered for a request with a value", action. wasTriggered()); Assert.assertTrue("Changed in last request should be true for a request with a value", input.isChangedInLastRequest()); request.setParameter(input.getId(), value); action.reset(); input.serviceRequest(request); Assert.assertEquals("Input value is incorrect after request with same value", value, input. getValue()); Assert.assertFalse("Change Action should not have triggered for a request with same value", action.wasTriggered()); Assert.assertFalse("Changed in last request should be false for a request with same value",
/** * Perform change logic for this component. * <p>Reset focus ONLY if the current Request is an Ajax request. See https://github.com/BorderTech/wcomponents/issues/501.</p> */ protected void doHandleChanged() { // If there is an associated action, execute it if (getActionOnChange() != null) { final ActionEvent event = new ActionEvent(this, getActionCommand(), getActionObject()); final boolean isCAT = isCurrentAjaxTrigger(); Runnable later = new Runnable() { @Override public void run() { getActionOnChange().execute(event); if (isCAT && UIContextHolder.getCurrent().getFocussed() == null) { setFocussed(); } } }; invokeLater(later); } else if (AjaxHelper.isCurrentAjaxTrigger(this) && UIContextHolder.getCurrent().getFocussed() == null) { setFocussed(); } }
input.validate(diags); Assert.assertTrue("Input with no validation should return an empty daignostics list", diags. isEmpty()); input.setMandatory(true); input.validate(diags); Assert.assertEquals("Mandatory input with no value should return a diagnostic message", 1, diags.size()); input.setReadOnly(true); diags.clear(); input.validate(diags); Assert.assertTrue( "Mandatory input that is ReadOnly with no value should return an empty diagnostic message", input.setMandatory(true); input.setData("value"); input.validate(diags); Assert.assertTrue("Mandatory input with a value should return an empty daignostics list", diags.isEmpty()); input = new MyInput(); diags.clear(); input.addValidator(validator); input.validate(diags); Assert.assertEquals("Input with custom validator should return a diagnostic message", 1, diags.size());
@Test public void testIsEmpty() { AbstractInput input = new MyInput(); Assert.assertTrue("isEmpty for an input with no value should be true", input.isEmpty()); // Empty String should be "empty" input.setData(""); Assert.assertTrue("isEmpty for an input with an empty string as its value should be true", input.isEmpty()); // Not Empty value Boolean value = Boolean.TRUE; input.setData(value); Assert.assertFalse("isEmpty for an input with a non-empty value should be false", input. isEmpty()); }
@Test public void testGetValueAsString() { AbstractInput input = new MyInput(); Assert.assertNull("Default value as string should be null", input.getValueAsString()); Boolean value = Boolean.TRUE; input.setData(value); Assert.assertEquals("Incorrect value as string returned", value.toString(), input. getValueAsString()); }
@Test public void testChangedInLastRequestAccessors() { AbstractInput input = new MyInput(); Assert.assertFalse("changedInLastRequest flag should default to false", input. isChangedInLastRequest()); input.setChangedInLastRequest(true); Assert. assertTrue("changedInLastRequest flag should be true", input. isChangedInLastRequest()); }
@Test public void testSubmitOnChangeAccessors() { AbstractInput input = new MyInput(); Assert.assertFalse("submitOnChange flag should default to false", input.isSubmitOnChange()); input.setSubmitOnChange(true); Assert.assertTrue("submitOnChange flag should be true", input.isSubmitOnChange()); }
@Test public void testActionCommand() { String value = "test value"; AbstractInput input = new MyInput(); input.setData(value); // Action command default to the string value Assert.assertEquals("Action command should defualt to the string value", value, input. getActionCommand()); }
@Test public void testIsPresent() { AbstractInput input = new MyInput(); String testValue = "ABC"; // Empty Request setActiveContext(createUIContext()); MockRequest request = new MockRequest(); Assert.assertFalse("IsPresent should return false", input.isPresent(request)); // Input on the request setActiveContext(createUIContext()); request = new MockRequest(); request.setParameter(input.getId(), testValue); Assert.assertTrue("IsPresent should return true", input.isPresent(request)); }
/** * {@inheritDoc} */ @Override public void setData(final Object data) { // This override is necessary to maintain other internal state NumberFieldModel model = getOrCreateComponentModel(); try { super.setData(convertValue(data)); model.text = null; model.validNumber = true; } catch (SystemException e) { super.setData(data); model.text = data.toString(); model.validNumber = false; } }
/** * Indicates whether the form should be submitted when the checkbox is checked/unchecked. * * @return true if the form should be submitted on change. * @deprecated 1.4.0 as it results in a level A accessibility problem See * https://www.w3.org/TR/UNDERSTANDING-WCAG20/consistent-behavior-unpredictable-change.html. */ @Deprecated @Override public boolean isSubmitOnChange() { return super.isSubmitOnChange(); }
/** * {@inheritDoc} */ @Override public String getActionCommand() { return getValueAsString(); }
/** * <p> * Indicates whether this list was present in the request. * </p> * <p> * Lists that allow no option to be selected have a hidden input field, whose name is name-h to indicate that it is * in the request. * </p> * * @param request the request being responded to. * @return true if this list was present in the request, false if not. */ @Override protected boolean isPresent(final Request request) { if (isAllowNoSelection()) { String id = getId(); return request.getParameter(id + "-h") != null; } else { return super.isPresent(request); } }