Example usage for com.vaadin.client DateTimeService getISOWeekNumber

List of usage examples for com.vaadin.client DateTimeService getISOWeekNumber

Introduction

In this page you can find the example usage for com.vaadin.client DateTimeService getISOWeekNumber.

Prototype

public static int getISOWeekNumber(Date date) 

Source Link

Document

Returns the ISO-8601 week number of the given date.

Usage

From source file:com.example.test.client.VMultiSelectCalendarWidget.java

License:Apache License

/**
 * Builds the day and time selectors of the calendar.
 *///  www.  ja  va 2s  .c om
private void buildCalendarBody() {

    final int weekColumn = 0;
    final int firstWeekdayColumn = 1;
    final int headerRow = 0;

    setWidget(1, 0, daysTable);
    setCellPadding(0);
    setCellSpacing(0);
    getFlexCellFormatter().setColSpan(1, 0, 5);
    getFlexCellFormatter().setStyleName(1, 0, STYLE_PRIMARY + "-calendarpanel-body");

    daysTable.getFlexCellFormatter().setStyleName(headerRow, weekColumn, "v-week");
    daysTable.setHTML(headerRow, weekColumn, "<strong></strong>");
    // Hide the week column if week numbers are not to be displayed.
    daysTable.getFlexCellFormatter().setVisible(headerRow, weekColumn, isShowISOWeekNumbers());

    daysTable.getRowFormatter().setStyleName(headerRow, STYLE_PRIMARY + "-calendarpanel-weekdays");

    if (isShowISOWeekNumbers()) {
        daysTable.getFlexCellFormatter().setStyleName(headerRow, weekColumn, "v-first");
        daysTable.getFlexCellFormatter().setStyleName(headerRow, firstWeekdayColumn, "");
        daysTable.getRowFormatter().addStyleName(headerRow, STYLE_PRIMARY + "-calendarpanel-weeknumbers");
    } else {
        daysTable.getFlexCellFormatter().setStyleName(headerRow, weekColumn, "");
        daysTable.getFlexCellFormatter().setStyleName(headerRow, firstWeekdayColumn, "v-first");
    }

    daysTable.getFlexCellFormatter().setStyleName(headerRow, firstWeekdayColumn + 6, "v-last");

    // Print weekday names
    final int firstDay = getDateTimeService().getFirstDayOfWeek();
    for (int i = 0; i < 7; i++) {
        int day = i + firstDay;
        if (day > 6) {
            day = 0;
        }
        daysTable.setHTML(headerRow, firstWeekdayColumn + i,
                "<strong>" + getDateTimeService().getShortDay(day) + "</strong>");

        Roles.getColumnheaderRole()
                .set(daysTable.getCellFormatter().getElement(headerRow, firstWeekdayColumn + i));
    }

    // Zero out hours, minutes, seconds, and milliseconds to compare dates
    // without time part
    final Date tmp = new Date();
    final Date today = new Date(tmp.getYear(), tmp.getMonth(), tmp.getDate());

    //final Date selectedDate = value == null ? null : new Date(value.getYear(), value.getMonth(), value.getDate());

    final int startWeekDay = getDateTimeService().getStartWeekDay(displayedMonth);
    final Date dateToAdd = (Date) displayedMonth.clone();
    // Start from the first day of the week that at least partially belongs
    // to the current month
    dateToAdd.setDate(1 - startWeekDay);

    // No month has more than 6 weeks so 6 is a safe maximum for rows.
    for (int weekOfMonth = 1; weekOfMonth < 7; weekOfMonth++) {
        for (int dayOfWeek = 0; dayOfWeek < 7; dayOfWeek++) {

            // Actually write the day of month
            Date dayDate = (Date) dateToAdd.clone();
            DayWidget day = new DayWidget(dayDate);

            day.setStyleName(STYLE_PRIMARY + "-calendarpanel-day");

            if (!isDateInsideRange(dayDate, Resolution.DAY)) {
                day.addStyleDependentName(CN_OUTSIDE_RANGE);
            }

            //if (dateToAdd.equals(selectedDate)) {
            if (selectedDates.contains(dateToAdd)) {
                day.addStyleDependentName(CN_SELECTED);
                Roles.getGridcellRole().setAriaSelectedState(day.getElement(), SelectedValue.TRUE);
                selectedDays.add(day);
            }
            if (dateToAdd.equals(today)) {
                day.addStyleDependentName(CN_TODAY);
            }
            if (focusedDay != null && dateToAdd.equals(focusedDay.getDate())) {
                focusedDay = day;
                if (hasFocus) {
                    day.addStyleDependentName(CN_FOCUSED);
                }
            }
            if (dateToAdd.getMonth() != displayedMonth.getMonth()) {
                day.addStyleDependentName(CN_OFFMONTH);
            }

            daysTable.setWidget(weekOfMonth, firstWeekdayColumn + dayOfWeek, day);
            Roles.getGridcellRole()
                    .set(daysTable.getCellFormatter().getElement(weekOfMonth, firstWeekdayColumn + dayOfWeek));

            // ISO week numbers if requested
            daysTable.getCellFormatter().setVisible(weekOfMonth, weekColumn, isShowISOWeekNumbers());

            if (isShowISOWeekNumbers()) {
                final String baseCssClass = STYLE_PRIMARY + "-calendarpanel-weeknumber";
                String weekCssClass = baseCssClass;

                int weekNumber = DateTimeService.getISOWeekNumber(dateToAdd);

                daysTable.setHTML(weekOfMonth, 0,
                        "<span class=\"" + weekCssClass + "\"" + ">" + weekNumber + "</span>");
            }
            dateToAdd.setDate(dateToAdd.getDate() + 1);
        }
    }
}