Sunday, May 18, 2014

jquery calendar in java 8

package com.algocrafts.calendar;

import com.algocrafts.conditions.IsEquals;
import com.algocrafts.converters.Filter;
import com.algocrafts.converters.FirstItem;
import com.algocrafts.decorators.AbstractPage;
import com.algocrafts.decorators.Element;
import com.algocrafts.locators.ElementLocator;
import com.algocrafts.locators.ElementTryLocator;
import com.algocrafts.locators.ElementsLocator;
import com.algocrafts.locators.Locator;

import static com.algocrafts.conditions.ElementFunction.CLICK_IF_NOT_NULL;
import static com.algocrafts.conditions.PageCondition.CALENDAR_NOT_DISPLAYED;
import static com.algocrafts.converters.GetText.TEXT;
import static com.algocrafts.converters.Ordinal.ORDINAL;
import static com.algocrafts.converters.StringToInt.PARSE_INT;
import static com.algocrafts.converters.ToMonth.TO_MONTH;
import static com.algocrafts.searchmethods.ByClassName.*;
import static com.algocrafts.searchmethods.ById.UI_DATEPICKER_DIV;
import static com.algocrafts.searchmethods.ByTagName.TD;

/**
 * This is the reference implementation of the Calendar interface which can be
 * operated by a DatePicker.
 * The location of the date picker is here,
 * http://jqueryui.com/datepicker/
 *
 * @author Yujun Liang
 * @since 0.1
 */
public class JQueryCalendar implements Calendar {

    private final AbstractPage page;
    private final Locator<AbstractPage, Element> trigger;

    /**
     * Constructor of the JQueryCalendar, an active page and a search
     * criteria of the trigger element.
     *
     * @param page
     * @param trigger
     */
    public JQueryCalendar(AbstractPage page, Locator<AbstractPage, Element> trigger) {
        this.page = page;
        this.trigger = trigger;
    }

    @Override
    public void show() {
        trigger.and(CLICK_IF_NOT_NULL).apply(page);
    }

    @Override
    public int currentYear() {
        return new ElementLocator<AbstractPage>(UI_DATEPICKER_DIV)
            .and(new ElementLocator<>(UI_DATEPICKER_HEADER))
            .and(new ElementLocator<>(UI_DATEPICKER_YEAR))
            .and(TEXT)
            .and(PARSE_INT)
            .apply(page);
    }

    @Override
    public int currentMonth() {
        return new ElementLocator<AbstractPage>(UI_DATEPICKER_DIV)
            .and(new ElementLocator<>(UI_DATEPICKER_MONTH))
            .and(TEXT)
            .and(TO_MONTH)
            .and(ORDINAL)
            .apply(page);
    }

    @Override
    public void previousMonth() {
        new ElementLocator<AbstractPage>(UI_DATEPICKER_DIV)
            .and(new ElementLocator<>(UI_DATEPICKER_PREV))
            .and(CLICK_IF_NOT_NULL)
            .apply(page);
    }

    @Override
    public void nextMonth() {
        new ElementLocator<AbstractPage>(UI_DATEPICKER_DIV)
            .and(new ElementLocator<>(UI_DATEPICKER_NEXT))
            .and(CLICK_IF_NOT_NULL)
            .apply(page);
    }

    @Override
    public void pickDay(int day) {
        new ElementLocator<AbstractPage>(UI_DATEPICKER_DIV)
            .and(new ElementLocator<>(UI_DATEPICKER_CALENDAR))
            .and(new ElementsLocator<>(TD))
            .and(new Filter<>(new IsEquals(TEXT, day)))
            .and(new FirstItem<>())
            .and(CLICK_IF_NOT_NULL)
            .apply(page);
        new ElementLocator<AbstractPage>(UI_DATEPICKER_DIV)
            .and(new ElementTryLocator<>(UI_DATEPICKER_CLOSE))
            .and(CLICK_IF_NOT_NULL)
            .apply(page);
        page.until(CALENDAR_NOT_DISPLAYED);
    }

}

No comments:

Post a Comment