Search in sources :

Example 26 with WDateField

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

the class DateFieldPivotValidator method isValid.

/**
 * {@inheritDoc}
 */
@Override
protected boolean isValid() {
    // Get the date we are validating.
    WDateField dateField = (WDateField) this.getInputField();
    Date date = dateField.getDate();
    if (date == null) {
        // No date, so nothing to validate.
        return true;
    }
    // Determine the pivot date
    Date pivot = null;
    if (variablePivot != null) {
        pivot = variablePivot.getDate();
        if (pivot == null) {
            // No pivot value, so default to true.
            return true;
        }
    } else if (fixedPivot != null) {
        pivot = fixedPivot;
    }
    // We take a null pivot date as meaning "today"
    if (pivot == null) {
        pivot = new Date();
    }
    // Round the dates to nearest day.
    pivot = DateUtilities.roundToDay(pivot);
    date = DateUtilities.roundToDay(date);
    // Perform the comparison with the pivot
    switch(operator) {
        case BEFORE:
            return date.before(pivot);
        case BEFORE_OR_EQUAL:
            return !pivot.before(date);
        case EQUAL:
            return date.equals(pivot);
        case AFTER_OR_EQUAL:
            return !pivot.after(date);
        case AFTER:
            return date.after(pivot);
        default:
            throw new SystemException("Unknown operator. [" + operator + "]");
    }
}
Also used : SystemException(com.github.bordertech.wcomponents.util.SystemException) WDateField(com.github.bordertech.wcomponents.WDateField) Date(java.util.Date)

Example 27 with WDateField

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

the class DateFieldPivotValidator_Test method runTest.

/**
 * @param operator the date operator
 * @return the test results
 */
private String[][] runTest(final int operator) {
    String[][] result = new String[DATES.length][DATES[0].length];
    for (int run = 0; run < DATES.length; run++) {
        DateFieldPivotValidator validator = new DateFieldPivotValidator(operator);
        validator.setFixedPivot(DATES[run][2]);
        WDateField dateField = new WDateField();
        dateField.addValidator(validator);
        List<Diagnostic> diags = new ArrayList<>();
        for (int i = 0; i < DATES[run].length; i++) {
            diags.clear();
            dateField.setDate(DATES[run][i]);
            dateField.validate(diags);
            Assert.assertTrue("Should only have a maximum of 1 error", diags.size() < 2);
            if (!diags.isEmpty()) {
                result[run][i] = (diags.get(0)).getDescription();
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) WDateField(com.github.bordertech.wcomponents.WDateField) Diagnostic(com.github.bordertech.wcomponents.validation.Diagnostic)

Aggregations

WDateField (com.github.bordertech.wcomponents.WDateField)27 Test (org.junit.Test)11 WTextField (com.github.bordertech.wcomponents.WTextField)8 WTableColumn (com.github.bordertech.wcomponents.WTableColumn)7 Date (java.util.Date)7 SimpleDateFormat (java.text.SimpleDateFormat)5 WDataTable (com.github.bordertech.wcomponents.WDataTable)4 WNumberField (com.github.bordertech.wcomponents.WNumberField)4 WFieldLayout (com.github.bordertech.wcomponents.WFieldLayout)3 WHeading (com.github.bordertech.wcomponents.WHeading)3 WLabel (com.github.bordertech.wcomponents.WLabel)3 WText (com.github.bordertech.wcomponents.WText)3 SystemException (com.github.bordertech.wcomponents.util.SystemException)3 ArrayList (java.util.ArrayList)3 AbstractWSelectList (com.github.bordertech.wcomponents.AbstractWSelectList)2 RadioButtonGroup (com.github.bordertech.wcomponents.RadioButtonGroup)2 TableTreeNode (com.github.bordertech.wcomponents.TableTreeNode)2 WCheckBox (com.github.bordertech.wcomponents.WCheckBox)2 WCheckBoxSelect (com.github.bordertech.wcomponents.WCheckBoxSelect)2 WFieldSet (com.github.bordertech.wcomponents.WFieldSet)2