1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224 |
- /*
- * Copyright 2000-2018 Vaadin Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
- package com.vaadin.client.ui;
-
- import static com.vaadin.client.DateTimeService.asTwoDigits;
-
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.function.Predicate;
- import java.util.logging.Logger;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
-
- import com.google.gwt.aria.client.Id;
- import com.google.gwt.aria.client.Roles;
- import com.google.gwt.aria.client.SelectedValue;
- import com.google.gwt.dom.client.Element;
- import com.google.gwt.dom.client.NativeEvent;
- import com.google.gwt.event.dom.client.BlurEvent;
- import com.google.gwt.event.dom.client.BlurHandler;
- import com.google.gwt.event.dom.client.ClickHandler;
- import com.google.gwt.event.dom.client.DomEvent;
- import com.google.gwt.event.dom.client.FocusEvent;
- import com.google.gwt.event.dom.client.FocusHandler;
- import com.google.gwt.event.dom.client.KeyCodes;
- import com.google.gwt.event.dom.client.KeyDownEvent;
- import com.google.gwt.event.dom.client.KeyDownHandler;
- import com.google.gwt.event.dom.client.KeyPressEvent;
- import com.google.gwt.event.dom.client.KeyPressHandler;
- import com.google.gwt.event.dom.client.MouseDownEvent;
- import com.google.gwt.event.dom.client.MouseDownHandler;
- import com.google.gwt.event.dom.client.MouseOutEvent;
- import com.google.gwt.event.dom.client.MouseOutHandler;
- import com.google.gwt.event.dom.client.MouseUpEvent;
- import com.google.gwt.event.dom.client.MouseUpHandler;
- import com.google.gwt.i18n.client.DateTimeFormat;
- import com.google.gwt.user.client.DOM;
- import com.google.gwt.user.client.Event;
- import com.google.gwt.user.client.Timer;
- import com.google.gwt.user.client.ui.Button;
- import com.google.gwt.user.client.ui.FlexTable;
- import com.google.gwt.user.client.ui.InlineHTML;
- import com.google.gwt.user.client.ui.Widget;
- import com.vaadin.client.DateTimeService;
- import com.vaadin.client.WidgetUtil;
- import com.vaadin.client.ui.aria.AriaHelper;
- import com.vaadin.shared.util.SharedUtil;
-
- /**
- * Abstract calendar panel to show and select a date using a resolution. The
- * class is parameterized by the date resolution enumeration type.
- *
- * @author Vaadin Ltd
- *
- * @param <R>
- * the resolution type which this field is based on (day, month, ...)
- * @since 8.0
- */
- @SuppressWarnings("deprecation")
- public abstract class VAbstractCalendarPanel<R extends Enum<R>>
- extends FocusableFlexTable implements KeyDownHandler, KeyPressHandler,
- MouseOutHandler, MouseDownHandler, MouseUpHandler, BlurHandler,
- FocusHandler, SubPartAware {
-
- public interface SubmitListener {
-
- /**
- * Called when calendar user triggers a submitting operation in calendar
- * panel. E.g. clicking on day or hitting enter.
- */
- void onSubmit();
-
- /**
- * On e.g. ESC key.
- */
- void onCancel();
- }
-
- /**
- * Blur listener that listens to blur event from the panel.
- */
- public interface FocusOutListener {
- /**
- * @return true if the calendar panel is not used after focus moves out
- */
- boolean onFocusOut(DomEvent<?> event);
- }
-
- /**
- * FocusChangeListener is notified when the panel changes its _focused_
- * value.
- */
- public interface FocusChangeListener {
- void focusChanged(Date focusedDate);
- }
-
- /**
- * Represents a Date button in the calendar
- */
- private class VEventButton extends Button {
- public VEventButton() {
- addMouseDownHandler(VAbstractCalendarPanel.this);
- addMouseOutHandler(VAbstractCalendarPanel.this);
- addMouseUpHandler(VAbstractCalendarPanel.this);
- }
- }
-
- private static final String CN_FOCUSED = "focused";
-
- private static final String CN_TODAY = "today";
-
- private static final String CN_SELECTED = "selected";
-
- private static final String CN_OFFMONTH = "offmonth";
-
- private static final String CN_OUTSIDE_RANGE = "outside-range";
-
- private VEventButton prevYear;
-
- private VEventButton nextYear;
-
- private VEventButton prevMonth;
-
- private VEventButton nextMonth;
-
- private FlexTable days = new FlexTable();
-
- private R resolution;
-
- private Timer mouseTimer;
-
- private Date value;
-
- private DateTimeService dateTimeService;
-
- private boolean showISOWeekNumbers;
-
- private FocusedDate displayedMonth;
-
- private FocusedDate focusedDate;
-
- private Day selectedDay;
-
- private Day focusedDay;
-
- private FocusOutListener focusOutListener;
-
- private SubmitListener submitListener;
-
- private FocusChangeListener focusChangeListener;
-
- private boolean hasFocus = false;
-
- private VDateField<R> parent;
-
- private boolean initialRenderDone = false;
-
- private String prevMonthAssistiveLabel;
-
- private String nextMonthAssistiveLabel;
-
- private String prevYearAssistiveLabel;
-
- private String nextYearAssistiveLabel;
-
- /**
- * Represents a click handler for when a user selects a value by using the
- * mouse
- */
- private ClickHandler dayClickHandler = event -> {
- if (!isEnabled() || isReadonly()) {
- return;
- }
-
- Date newDate = ((Day) event.getSource()).getDate();
- if (!isDateInsideRange(newDate,
- getResolution(VAbstractCalendarPanel.this::isDay))) {
- return;
- }
- if (newDate.getMonth() != displayedMonth.getMonth()
- || newDate.getYear() != displayedMonth.getYear()) {
- // If an off-month date was clicked, we must change the
- // displayed month and re-render the calendar (#8931)
- displayedMonth.setMonth(newDate.getMonth());
- displayedMonth.setYear(newDate.getYear());
- renderCalendar();
- }
- focusDay(newDate);
- selectFocused();
- onSubmit();
- };
-
- private Map<String, String> dateStyles = new HashMap<String, String>();
- private DateTimeFormat df = DateTimeFormat.getFormat("yyyy-MM-dd");
-
- public VAbstractCalendarPanel() {
- getElement().setId(DOM.createUniqueId());
- setStyleName(VDateField.CLASSNAME + "-calendarpanel");
- Roles.getGridRole().set(getElement());
-
- addKeyDownHandler(this);
- addFocusHandler(this);
- addBlurHandler(this);
- }
-
- public void setParentField(VDateField<R> parent) {
- this.parent = parent;
- }
-
- /**
- * Sets the focus to given date in the current view. Used when moving in the
- * calendar with the keyboard.
- *
- * @param date
- * A Date representing the day of month to be focused. Must be
- * one of the days currently visible.
- */
- private void focusDay(Date date) {
- // Only used when calendar body is present
- if (acceptDayFocus()) {
- if (focusedDay != null) {
- focusedDay.removeStyleDependentName(CN_FOCUSED);
- }
-
- if (date != null && focusedDate != null) {
- focusedDate.setTime(date.getTime());
- int rowCount = days.getRowCount();
- for (int i = 0; i < rowCount; i++) {
- int cellCount = days.getCellCount(i);
- for (int j = 0; j < cellCount; j++) {
- Widget widget = days.getWidget(i, j);
- if (widget instanceof VAbstractCalendarPanel.Day) {
- Day curday = (Day) widget;
- if (curday.getDate().equals(date)) {
- curday.addStyleDependentName(CN_FOCUSED);
- focusedDay = curday;
-
- // Reference focused day from calendar panel
- Roles.getGridRole()
- .setAriaActivedescendantProperty(
- getElement(),
- Id.of(curday.getElement()));
-
- return;
- }
- }
- }
- }
- }
- }
- }
-
- /**
- * Returns {@code true} if current resolution assumes handling focus event
- * for day UI component.
- *
- * @return {@code true} if day focus events should be handled, {@code false}
- * otherwise
- */
- protected abstract boolean acceptDayFocus();
-
- /**
- * Returns {@code true} if the provided {@code resolution} represents a day.
- *
- * @param resolution
- * the given resolution
- * @return {@code true} if the {@code resolution} represents a day
- */
- protected abstract boolean isDay(R resolution);
-
- /**
- * Returns {@code true} if the provided {@code resolution} represents a
- * month.
- *
- * @param resolution
- * the given resolution
- * @return {@code true} if the {@code resolution} represents a month
- */
- protected abstract boolean isMonth(R resolution);
-
- /**
- * Returns {@code true} if the provided {@code resolution} represents an
- * year.
- *
- * @param resolution
- * the given resolution
- * @return {@code true} if the {@code resolution} represents a year
- */
- protected boolean isYear(R resolution) {
- return parent.isYear(resolution);
- }
-
- /**
- * Returns {@code true} if the {@code resolution} representation is strictly
- * below month (day, hour, etc..).
- *
- * @param resolution
- * the given resolution
- * @return whether the {@code resolution} is below the month resolution
- */
- protected abstract boolean isBelowMonth(R resolution);
-
- /**
- * Returns all available resolutions for the widget.
- *
- * @return all available resolutions
- */
- protected Stream<R> getResolutions() {
- return parent.getResolutions();
- }
-
- /**
- * Finds the resolution by the {@code filter}.
- *
- * @param filter
- * predicate to filter resolutions
- * @return the resolution accepted by the {@code filter}
- */
- protected R getResolution(Predicate<R> filter) {
- List<R> resolutions = getResolutions().filter(filter)
- .collect(Collectors.toList());
- assert resolutions
- .size() == 1 : "The result of filtering by the predicate "
- + "contains unexpected number of resolution items :"
- + resolutions.size();
- return resolutions.get(0);
- }
-
- /**
- * Sets the selection highlight to a given day in the current view
- *
- * @param date
- * A Date representing the day of month to be selected. Must be
- * one of the days currently visible.
- *
- */
- private void selectDate(Date date) {
- if (selectedDay != null) {
- selectedDay.removeStyleDependentName(CN_SELECTED);
- Roles.getGridcellRole()
- .removeAriaSelectedState(selectedDay.getElement());
- }
-
- int rowCount = days.getRowCount();
- for (int i = 0; i < rowCount; i++) {
- int cellCount = days.getCellCount(i);
- for (int j = 0; j < cellCount; j++) {
- Widget widget = days.getWidget(i, j);
- if (widget instanceof VAbstractCalendarPanel.Day) {
- Day curday = (Day) widget;
- if (curday.getDate().equals(date)) {
- curday.addStyleDependentName(CN_SELECTED);
- selectedDay = curday;
- Roles.getGridcellRole().setAriaSelectedState(
- selectedDay.getElement(), SelectedValue.TRUE);
- return;
- }
- }
- }
- }
- }
-
- /**
- * Updates year, month, day from focusedDate to value
- */
- private void selectFocused() {
- if (focusedDate != null
- && isDateInsideRange(focusedDate, getResolution())) {
- if (value == null) {
- // No previously selected value (set to null on server side).
- // Create a new date using current date and time
- value = new Date();
- }
- /*
- * #5594 set Date (day) to 1 in order to prevent any kind of
- * wrapping of months when later setting the month. (e.g. 31 ->
- * month with 30 days -> wraps to the 1st of the following month,
- * e.g. 31st of May -> 31st of April = 1st of May)
- */
- value.setDate(1);
- if (value.getYear() != focusedDate.getYear()) {
- value.setYear(focusedDate.getYear());
- }
- if (value.getMonth() != focusedDate.getMonth()) {
- value.setMonth(focusedDate.getMonth());
- }
- if (value.getDate() != focusedDate.getDate()) {
- }
- // We always need to set the date, even if it hasn't changed, since
- // it was forced to 1 above.
- value.setDate(focusedDate.getDate());
-
- selectDate(focusedDate);
- } else {
- getLogger()
- .info("Trying to select the focused date which is NULL!");
- }
- }
-
- protected boolean onValueChange() {
- return false;
- }
-
- public R getResolution() {
- return resolution;
- }
-
- public void setResolution(R resolution) {
- this.resolution = resolution;
- }
-
- /**
- * Checks whether the widget is not editable (read-only).
- *
- * @return {@code true} if the widget is read-only
- */
- protected boolean isReadonly() {
- return parent.isReadonly();
- }
-
- /**
- * Checks whether the widget is enabled.
- *
- * @return {@code true} is the widget is enabled
- */
- protected boolean isEnabled() {
- return parent.isEnabled();
- }
-
- @Override
- public void setStyleName(String style) {
- super.setStyleName(style);
- if (initialRenderDone) {
- // Dynamic updates to the stylename needs to render the calendar to
- // update the inner element stylenames
- renderCalendar();
- }
- }
-
- @Override
- public void setStylePrimaryName(String style) {
- super.setStylePrimaryName(style);
- if (initialRenderDone) {
- // Dynamic updates to the stylename needs to render the calendar to
- // update the inner element stylenames
- renderCalendar();
- }
- }
-
- /**
- * Sets the style names for dates.
- *
- * @param dateStyles
- * the map of date string to style name
- *
- * @since 8.3
- */
- public void setDateStyles(Map<String, String> dateStyles) {
- this.dateStyles.clear();
- if (dateStyles != null) {
- this.dateStyles.putAll(dateStyles);
- }
- }
-
- private void clearCalendarBody(boolean remove) {
- if (!remove) {
- // Leave the cells in place but clear their contents
-
- // This has the side effect of ensuring that the calendar always
- // contain 7 rows.
- for (int row = 1; row < 7; row++) {
- for (int col = 0; col < 8; col++) {
- days.setHTML(row, col, " ");
- }
- }
- } else if (getRowCount() > 1) {
- removeRow(1);
- days.clear();
- }
- }
-
- /**
- * Builds the top buttons and current month and year header.
- *
- * @param needsMonth
- * Should the month buttons be visible?
- * @param needsBody
- * indicates whether the calendar body is drawn
- */
- private void buildCalendarHeader(boolean needsMonth, boolean needsBody) {
-
- getRowFormatter().addStyleName(0,
- parent.getStylePrimaryName() + "-calendarpanel-header");
-
- if (prevMonth == null && needsMonth) {
- prevMonth = new VEventButton();
- prevMonth.setHTML("‹");
- prevMonth.setStyleName("v-button-prevmonth");
-
- nextMonth = new VEventButton();
- nextMonth.setHTML("›");
- nextMonth.setStyleName("v-button-nextmonth");
-
- setWidget(0, 3, nextMonth);
- setWidget(0, 1, prevMonth);
-
- Roles.getButtonRole().set(prevMonth.getElement());
- Roles.getButtonRole()
- .setTabindexExtraAttribute(prevMonth.getElement(), -1);
-
- Roles.getButtonRole().set(nextMonth.getElement());
- Roles.getButtonRole()
- .setTabindexExtraAttribute(nextMonth.getElement(), -1);
- } else if (prevMonth != null && !needsMonth) {
- // Remove month traverse buttons
- remove(prevMonth);
- remove(nextMonth);
- prevMonth = null;
- nextMonth = null;
- }
-
- if (prevYear == null) {
-
- prevYear = new VEventButton();
- prevYear.setHTML("«");
- prevYear.setStyleName("v-button-prevyear");
-
- nextYear = new VEventButton();
- nextYear.setHTML("»");
- nextYear.setStyleName("v-button-nextyear");
-
- setWidget(0, 0, prevYear);
- setWidget(0, 4, nextYear);
-
- Roles.getButtonRole().set(prevYear.getElement());
- Roles.getButtonRole()
- .setTabindexExtraAttribute(prevYear.getElement(), -1);
-
- Roles.getButtonRole().set(nextYear.getElement());
- Roles.getButtonRole()
- .setTabindexExtraAttribute(nextYear.getElement(), -1);
- }
-
- updateControlButtonRangeStyles(needsMonth);
-
- updateAssistiveLabels();
-
- final String monthName = needsMonth
- ? getDateTimeService().getMonth(displayedMonth.getMonth())
- : "";
- final int year = displayedMonth.getYear() + 1900;
-
- getFlexCellFormatter().setStyleName(0, 2,
- parent.getStylePrimaryName() + "-calendarpanel-month");
- getFlexCellFormatter().setStyleName(0, 0,
- parent.getStylePrimaryName() + "-calendarpanel-prevyear");
- getFlexCellFormatter().setStyleName(0, 4,
- parent.getStylePrimaryName() + "-calendarpanel-nextyear");
- getFlexCellFormatter().setStyleName(0, 3,
- parent.getStylePrimaryName() + "-calendarpanel-nextmonth");
- getFlexCellFormatter().setStyleName(0, 1,
- parent.getStylePrimaryName() + "-calendarpanel-prevmonth");
-
- // Set ID to be referenced from focused date or calendar panel
- Element monthYearElement = getFlexCellFormatter().getElement(0, 2);
- AriaHelper.ensureHasId(monthYearElement);
- Event.sinkEvents(monthYearElement, Event.ONCLICK);
- Event.setEventListener(monthYearElement, event -> {
- // Don't handle header clicks if resolution in below month
- if (!isEnabled() || isReadonly() || isBelowMonth(getResolution())) {
- return;
- }
- selectFocused();
- onSubmit();
- });
- if (!needsBody) {
- Roles.getGridRole().setAriaLabelledbyProperty(getElement(),
- Id.of(monthYearElement));
- } else {
- Roles.getGridRole().removeAriaLabelledbyProperty(getElement());
- }
-
- setHTML(0, 2,
- "<span class=\"" + parent.getStylePrimaryName()
- + "-calendarpanel-month\">" + monthName + " " + year
- + "</span>");
- if (!isBelowMonth(getResolution())) {
- monthYearElement.addClassName("header-month-year");
- }
- }
-
- private void updateControlButtonRangeStyles(boolean needsMonth) {
-
- if (focusedDate == null) {
- return;
- }
-
- if (needsMonth) {
- Date prevMonthDate = (Date) focusedDate.clone();
- removeOneMonth(prevMonthDate);
-
- R month = getResolution(VAbstractCalendarPanel.this::isMonth);
- if (!isDateInsideRange(prevMonthDate, month)) {
- prevMonth.addStyleName(CN_OUTSIDE_RANGE);
- } else {
- prevMonth.removeStyleName(CN_OUTSIDE_RANGE);
- }
- Date nextMonthDate = (Date) focusedDate.clone();
- addOneMonth(nextMonthDate);
- if (!isDateInsideRange(nextMonthDate, month)) {
- nextMonth.addStyleName(CN_OUTSIDE_RANGE);
- } else {
- nextMonth.removeStyleName(CN_OUTSIDE_RANGE);
- }
- }
-
- Date prevYearDate = (Date) focusedDate.clone();
- prevYearDate.setYear(prevYearDate.getYear() - 1);
- R year = getResolution(VAbstractCalendarPanel.this::isYear);
- if (!isDateInsideRange(prevYearDate, year)) {
- prevYear.addStyleName(CN_OUTSIDE_RANGE);
- } else {
- prevYear.removeStyleName(CN_OUTSIDE_RANGE);
- }
-
- Date nextYearDate = (Date) focusedDate.clone();
- nextYearDate.setYear(nextYearDate.getYear() + 1);
- if (!isDateInsideRange(nextYearDate, year)) {
- nextYear.addStyleName(CN_OUTSIDE_RANGE);
- } else {
- nextYear.removeStyleName(CN_OUTSIDE_RANGE);
- }
-
- }
-
- /**
- * Returns date time service for the widget.
- *
- * @see #setDateTimeService(DateTimeService)
- *
- * @return date time service
- */
- protected DateTimeService getDateTimeService() {
- return dateTimeService;
- }
-
- /**
- * Returns the date field which this panel is attached to.
- *
- * @return the "parent" date field
- */
- protected VDateField<R> getDateField() {
- return parent;
- }
-
- public void setDateTimeService(DateTimeService dateTimeService) {
- this.dateTimeService = dateTimeService;
- }
-
- /**
- * Returns whether ISO 8601 week numbers should be shown in the value
- * selector or not. ISO 8601 defines that a week always starts with a Monday
- * so the week numbers are only shown if this is the case.
- *
- * @return true if week number should be shown, false otherwise
- */
- public boolean isShowISOWeekNumbers() {
- return showISOWeekNumbers;
- }
-
- public void setShowISOWeekNumbers(boolean showISOWeekNumbers) {
- this.showISOWeekNumbers = showISOWeekNumbers;
- if (initialRenderDone && isBelowMonth(resolution)) {
- clearCalendarBody(false);
- buildCalendarBody();
- }
- }
-
- /**
- * Checks inclusively whether a date is inside a range of dates or not.
- *
- * @param date
- * @return
- */
- private boolean isDateInsideRange(Date date, R minResolution) {
- assert (date != null);
-
- return isAcceptedByRangeEnd(date, minResolution)
- && isAcceptedByRangeStart(date, minResolution);
- }
-
- /**
- * Accepts dates greater than or equal to rangeStart, depending on the
- * resolution. If the resolution is set to DAY, the range will compare on a
- * day-basis. If the resolution is set to YEAR, only years are compared. So
- * even if the range is set to one millisecond in next year, also next year
- * will be included.
- *
- * @param date
- * @param minResolution
- * @return
- */
- private boolean isAcceptedByRangeStart(Date date, R minResolution) {
- assert (date != null);
-
- // rangeStart == null means that we accept all values below rangeEnd
- if (rangeStart == null) {
- return true;
- }
-
- String dateStrResolution = dateStrResolution(date, minResolution);
- return rangeStart.substring(0, dateStrResolution.length())
- .compareTo(dateStrResolution) <= 0;
- }
-
- private String dateStrResolution(Date date, R minResolution) {
- String dateStrResolution = (1900 + date.getYear()) + "";
- while (dateStrResolution.length() < 4) {
- dateStrResolution = "0" + dateStrResolution;
- }
- if (!isYear(minResolution)) {
- dateStrResolution += "-" + asTwoDigits(1 + date.getMonth());
- if (!isMonth(minResolution)) {
- dateStrResolution += "-" + asTwoDigits(date.getDate());
- }
- }
- return dateStrResolution;
- }
-
- /**
- * Accepts dates earlier than or equal to rangeStart, depending on the
- * resolution. If the resolution is set to DAY, the range will compare on a
- * day-basis. If the resolution is set to YEAR, only years are compared. So
- * even if the range is set to one millisecond in next year, also next year
- * will be included.
- *
- * @param date
- * @param minResolution
- * @return
- */
- private boolean isAcceptedByRangeEnd(Date date, R minResolution) {
- assert (date != null);
-
- // rangeEnd == null means that we accept all values above rangeStart
- if (rangeEnd == null) {
- return true;
- }
-
- // If dateStrResolution has more year digits than rangeEnd, we need
- // to pad it in order to be lexicographically compatible
- String dateStrResolution = dateStrResolution(date, minResolution);
- String paddedEnd = rangeEnd.substring(0);
- int yearDigits = dateStrResolution.indexOf("-");
- if (yearDigits == -1) {
- yearDigits = dateStrResolution.length();
- }
- while (paddedEnd.indexOf("-") < yearDigits) {
- paddedEnd = "0" + paddedEnd;
- }
- return paddedEnd.substring(0, dateStrResolution.length())
- .compareTo(dateStrResolution) >= 0;
- }
-
- private static Date clearDateBelowMonth(Date date) {
- date.setDate(1);
- return clearDateBelowDay(date);
- }
-
- private static Date clearDateBelowDay(Date date) {
- date.setHours(0);
- date.setMinutes(0);
- date.setSeconds(0);
- // Clearing milliseconds
- long time = date.getTime() / 1000;
- date = new Date(time * 1000);
- return date;
- }
-
- /**
- * Builds the day and time selectors of the calendar.
- */
- private void buildCalendarBody() {
-
- final int weekColumn = 0;
- final int firstWeekdayColumn = 1;
- final int headerRow = 0;
-
- setWidget(1, 0, days);
- setCellPadding(0);
- setCellSpacing(0);
- getFlexCellFormatter().setColSpan(1, 0, 5);
- getFlexCellFormatter().setStyleName(1, 0,
- getDateField().getStylePrimaryName() + "-calendarpanel-body");
-
- days.getFlexCellFormatter().setStyleName(headerRow, weekColumn,
- "v-week");
- days.setHTML(headerRow, weekColumn, "<strong></strong>");
- // Hide the week column if week numbers are not to be displayed.
- days.getFlexCellFormatter().setVisible(headerRow, weekColumn,
- isShowISOWeekNumbers());
-
- days.getRowFormatter().setStyleName(headerRow,
- getDateField().getStylePrimaryName()
- + "-calendarpanel-weekdays");
-
- if (isShowISOWeekNumbers()) {
- days.getFlexCellFormatter().setStyleName(headerRow, weekColumn,
- "v-first");
- days.getFlexCellFormatter().setStyleName(headerRow,
- firstWeekdayColumn, "");
- days.getRowFormatter().addStyleName(headerRow,
- getDateField().getStylePrimaryName()
- + "-calendarpanel-weeknumbers");
- } else {
- days.getFlexCellFormatter().setStyleName(headerRow, weekColumn, "");
- days.getFlexCellFormatter().setStyleName(headerRow,
- firstWeekdayColumn, "v-first");
- }
-
- days.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) % 7;
- if (isBelowMonth(getResolution())) {
- days.setHTML(headerRow, firstWeekdayColumn + i, "<strong>"
- + getDateTimeService().getShortDay(day) + "</strong>");
- } else {
- days.setHTML(headerRow, firstWeekdayColumn + i, "");
- }
-
- Roles.getColumnheaderRole().set(days.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 curr = (Date) displayedMonth.clone();
- // Start from the first day of the week that at least partially belongs
- // to the current month
- curr.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) curr.clone();
- Day day = new Day(dayDate);
-
- // Set ID with prefix of the calendar panel's ID
- day.getElement().setId(getElement().getId() + "-" + weekOfMonth
- + "-" + dayOfWeek);
-
- // Set assistive label to read focused date and month/year
- Roles.getButtonRole().set(day.getElement());
- Roles.getButtonRole().setAriaLabelledbyProperty(
- day.getElement(), Id.of(day.getElement()),
- Id.of(getFlexCellFormatter().getElement(0, 2)));
-
- day.setStyleName(getDateField().getStylePrimaryName()
- + "-calendarpanel-day");
-
- if (!isDateInsideRange(dayDate, getResolution(this::isDay))) {
- day.addStyleDependentName(CN_OUTSIDE_RANGE);
- }
-
- if (curr.equals(selectedDate)) {
- day.addStyleDependentName(CN_SELECTED);
- Roles.getGridcellRole().setAriaSelectedState(
- day.getElement(), SelectedValue.TRUE);
- selectedDay = day;
- }
- if (curr.equals(today)) {
- day.addStyleDependentName(CN_TODAY);
- }
- if (curr.equals(focusedDate)) {
- focusedDay = day;
- if (hasFocus) {
- day.addStyleDependentName(CN_FOCUSED);
-
- // Reference focused day from calendar panel
- Roles.getGridRole().setAriaActivedescendantProperty(
- getElement(), Id.of(day.getElement()));
- }
- }
- if (curr.getMonth() != displayedMonth.getMonth()) {
- day.addStyleDependentName(CN_OFFMONTH);
- }
- String dayDateString = df.format(dayDate);
- if (dateStyles.containsKey(dayDateString)) {
- day.addStyleName(dateStyles.get(dayDateString));
- }
-
- days.setWidget(weekOfMonth, firstWeekdayColumn + dayOfWeek,
- day);
- Roles.getGridcellRole().set(days.getCellFormatter().getElement(
- weekOfMonth, firstWeekdayColumn + dayOfWeek));
-
- // ISO week numbers if requested
- days.getCellFormatter().setVisible(weekOfMonth, weekColumn,
- isShowISOWeekNumbers());
-
- if (isShowISOWeekNumbers()) {
- final String baseCssClass = getDateField()
- .getStylePrimaryName()
- + "-calendarpanel-weeknumber";
- String weekCssClass = baseCssClass;
-
- int weekNumber = DateTimeService.getISOWeekNumber(curr);
-
- days.setHTML(weekOfMonth, 0, "<span class=\"" + weekCssClass
- + "\"" + ">" + weekNumber + "</span>");
- }
- curr.setDate(curr.getDate() + 1);
- }
- }
- }
-
- /**
- * Updates the calendar and text field with the selected dates.
- */
- public void renderCalendar() {
- renderCalendar(true);
- }
-
- /**
- * Returns the value of initialRenderDone
- *
- * @since 8.7
- */
- public boolean isInitialRenderDone() {
- return initialRenderDone;
- }
-
- /**
- * For internal use only. May be removed or replaced in the future.
- *
- * Updates the calendar and text field with the selected dates.
- *
- * @param updateDate
- * The value false prevents setting the selected date of the
- * calendar based on focusedDate. That can be used when only the
- * resolution of the calendar is changed and no date has been
- * selected.
- */
- @SuppressWarnings("rawtypes")
- public void renderCalendar(boolean updateDate) {
- if (parent instanceof VAbstractPopupCalendar
- && !((VAbstractPopupCalendar) parent).popup.isShowing()) {
- // a popup that isn't open cannot possibly need a focus change event
- updateDate = false;
- }
- doRenderCalendar(updateDate);
-
- initialRenderDone = true;
- }
-
- /**
- * Performs the rendering required by the {@link #renderCalendar(boolean)}.
- * Subclasses may override this method to provide a custom implementation
- * avoiding {@link #renderCalendar(boolean)} override. The latter method
- * contains a common logic which should not be overridden.
- *
- * @param updateDate
- * The value false prevents setting the selected date of the
- * calendar based on focusedDate. That can be used when only the
- * resolution of the calendar is changed and no date has been
- * selected.
- */
- protected void doRenderCalendar(boolean updateDate) {
- super.setStylePrimaryName(
- getDateField().getStylePrimaryName() + "-calendarpanel");
-
- if (focusedDate == null) {
- Date date = getDate();
- if (date == null) {
- date = new Date();
- }
- // focusedDate must have zero hours, mins, secs, millisecs
- focusedDate = new FocusedDate(date.getYear(), date.getMonth(),
- date.getDate());
- displayedMonth = new FocusedDate(date.getYear(), date.getMonth(),
- 1);
- }
-
- if (updateDate && !isDay(getResolution())
- && focusChangeListener != null) {
- focusChangeListener.focusChanged(new Date(focusedDate.getTime()));
- }
-
- final boolean needsMonth = !isYear(getResolution());
- boolean needsBody = isBelowMonth(resolution);
- buildCalendarHeader(needsMonth, needsBody);
- clearCalendarBody(!needsBody);
- if (needsBody) {
- buildCalendarBody();
- }
- }
-
- /**
- * Moves the focus forward the given number of days.
- */
- private void focusNextDay(int days) {
- if (focusedDate == null) {
- return;
- }
-
- Date focusCopy = ((Date) focusedDate.clone());
- focusCopy.setDate(focusedDate.getDate() + days);
- if (!isDateInsideRange(focusCopy, getResolution())) {
- // If not inside allowed range, then do not move anything
- return;
- }
-
- int oldMonth = focusedDate.getMonth();
- int oldYear = focusedDate.getYear();
- focusedDate.setDate(focusedDate.getDate() + days);
-
- if (focusedDate.getMonth() == oldMonth
- && focusedDate.getYear() == oldYear) {
- // Month did not change, only move the selection
- focusDay(focusedDate);
- } else {
-
- // If the month changed we need to re-render the calendar
- displayedMonth.setMonth(focusedDate.getMonth());
- displayedMonth.setYear(focusedDate.getYear());
- renderCalendar();
- }
- }
-
- /**
- * Moves the focus backward the given number of days.
- */
- private void focusPreviousDay(int days) {
- focusNextDay(-days);
- }
-
- /**
- * Selects the next month
- */
- private void focusNextMonth() {
-
- if (focusedDate == null) {
- return;
- }
- // Trying to request next month
- Date requestedNextMonthDate = (Date) focusedDate.clone();
- addOneMonth(requestedNextMonthDate);
-
- if (!isDateInsideRange(requestedNextMonthDate,
- getResolution(this::isMonth))) {
- return;
- }
-
- // Now also checking whether the day is inside the range or not. If not
- // inside,
- // correct it
- if (!isDateInsideRange(requestedNextMonthDate,
- getResolution(this::isDay))) {
- requestedNextMonthDate = adjustDateToFitInsideRange(
- requestedNextMonthDate);
- }
-
- focusedDate.setTime(requestedNextMonthDate.getTime());
- displayedMonth.setMonth(displayedMonth.getMonth() + 1);
-
- renderCalendar();
- }
-
- private static void addOneMonth(Date date) {
- int currentMonth = date.getMonth();
- int requestedMonth = (currentMonth + 1) % 12;
-
- date.setMonth(date.getMonth() + 1);
-
- /*
- * If the selected value was e.g. 31.3 the new value would be 31.4 but
- * this value is invalid so the new value will be 1.5. This is taken
- * care of by decreasing the value until we have the correct month.
- */
- while (date.getMonth() != requestedMonth) {
- date.setDate(date.getDate() - 1);
- }
- }
-
- private static void removeOneMonth(Date date) {
- int currentMonth = date.getMonth();
-
- date.setMonth(date.getMonth() - 1);
-
- /*
- * If the selected value was e.g. 31.12 the new value would be 31.11 but
- * this value is invalid so the new value will be 1.12. This is taken
- * care of by decreasing the value until we have the correct month.
- */
- while (date.getMonth() == currentMonth) {
- date.setDate(date.getDate() - 1);
- }
- }
-
- /**
- * Selects the previous month
- */
- private void focusPreviousMonth() {
-
- if (focusedDate == null) {
- return;
- }
- Date requestedPreviousMonthDate = (Date) focusedDate.clone();
- removeOneMonth(requestedPreviousMonthDate);
-
- if (!isDateInsideRange(requestedPreviousMonthDate,
- getResolution(this::isMonth))) {
- return;
- }
-
- if (!isDateInsideRange(requestedPreviousMonthDate,
- getResolution(this::isDay))) {
- requestedPreviousMonthDate = adjustDateToFitInsideRange(
- requestedPreviousMonthDate);
- }
- focusedDate.setTime(requestedPreviousMonthDate.getTime());
- displayedMonth.setMonth(displayedMonth.getMonth() - 1);
-
- renderCalendar();
- }
-
- /**
- * Selects the previous year
- */
- private void focusPreviousYear(int years) {
-
- if (focusedDate == null) {
- return;
- }
- Date previousYearDate = (Date) focusedDate.clone();
- previousYearDate.setYear(previousYearDate.getYear() - years);
- // Do not focus if not inside range
- if (!isDateInsideRange(previousYearDate, getResolution(this::isYear))) {
- return;
- }
- // If we remove one year, but have to roll back a bit, fit it
- // into the calendar. Also the months have to be changed
- if (!isDateInsideRange(previousYearDate, getResolution(this::isDay))) {
- previousYearDate = adjustDateToFitInsideRange(previousYearDate);
-
- focusedDate.setYear(previousYearDate.getYear());
- focusedDate.setMonth(previousYearDate.getMonth());
- focusedDate.setDate(previousYearDate.getDate());
- displayedMonth.setYear(previousYearDate.getYear());
- displayedMonth.setMonth(previousYearDate.getMonth());
- } else {
-
- int currentMonth = focusedDate.getMonth();
- focusedDate.setYear(focusedDate.getYear() - years);
- displayedMonth.setYear(displayedMonth.getYear() - years);
- /*
- * If the focused date was a leap day (Feb 29), the new date becomes
- * Mar 1 if the new year is not also a leap year. Set it to Feb 28
- * instead.
- */
- if (focusedDate.getMonth() != currentMonth) {
- focusedDate.setDate(0);
- }
- }
-
- renderCalendar();
- }
-
- /**
- * Selects the next year
- */
- private void focusNextYear(int years) {
-
- if (focusedDate == null) {
- return;
- }
- Date nextYearDate = (Date) focusedDate.clone();
- nextYearDate.setYear(nextYearDate.getYear() + years);
- // Do not focus if not inside range
- if (!isDateInsideRange(nextYearDate, getResolution(this::isYear))) {
- return;
- }
- // If we add one year, but have to roll back a bit, fit it
- // into the calendar. Also the months have to be changed
- if (!isDateInsideRange(nextYearDate, getResolution(this::isDay))) {
- nextYearDate = adjustDateToFitInsideRange(nextYearDate);
-
- focusedDate.setYear(nextYearDate.getYear());
- focusedDate.setMonth(nextYearDate.getMonth());
- focusedDate.setDate(nextYearDate.getDate());
- displayedMonth.setYear(nextYearDate.getYear());
- displayedMonth.setMonth(nextYearDate.getMonth());
- } else {
-
- int currentMonth = focusedDate.getMonth();
- focusedDate.setYear(focusedDate.getYear() + years);
- displayedMonth.setYear(displayedMonth.getYear() + years);
- /*
- * If the focused date was a leap day (Feb 29), the new date becomes
- * Mar 1 if the new year is not also a leap year. Set it to Feb 28
- * instead.
- */
- if (focusedDate.getMonth() != currentMonth) {
- focusedDate.setDate(0);
- }
- }
-
- renderCalendar();
- }
-
- /**
- * Handles a user click on the component
- *
- * @param sender
- * The component that was clicked
- */
- private void processClickEvent(Widget sender) {
- if (!isEnabled() || isReadonly()) {
- return;
- }
- if (sender == prevYear) {
- focusPreviousYear(1);
- } else if (sender == nextYear) {
- focusNextYear(1);
- } else if (sender == prevMonth) {
- focusPreviousMonth();
- } else if (sender == nextMonth) {
- focusNextMonth();
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.google.gwt.event.dom.client.KeyDownHandler#onKeyDown(com.google.gwt
- * .event.dom.client.KeyDownEvent)
- */
- @Override
- public void onKeyDown(KeyDownEvent event) {
- handleKeyPress(event);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.google.gwt.event.dom.client.KeyPressHandler#onKeyPress(com.google
- * .gwt.event.dom.client.KeyPressEvent)
- */
- @Override
- public void onKeyPress(KeyPressEvent event) {
- handleKeyPress(event);
- }
-
- /**
- * Handles the keypress from both the onKeyPress event and the onKeyDown
- * event
- *
- * @param event
- * The keydown/keypress event
- */
- private void handleKeyPress(DomEvent<?> event) {
- // Check tabs
- int keycode = event.getNativeEvent().getKeyCode();
- if (keycode == KeyCodes.KEY_TAB
- && event.getNativeEvent().getShiftKey()) {
- if (onTabOut(event)) {
- return;
- }
- }
-
- // Handle the navigation
- if (handleNavigation(keycode,
- event.getNativeEvent().getCtrlKey()
- || event.getNativeEvent().getMetaKey(),
- event.getNativeEvent().getShiftKey())) {
- event.preventDefault();
- }
- }
-
- /**
- * Notifies submit-listeners of a submit event
- */
- private void onSubmit() {
- if (getSubmitListener() != null) {
- getSubmitListener().onSubmit();
- }
- }
-
- /**
- * Notifies submit-listeners of a cancel event
- */
- private void onCancel() {
- if (getSubmitListener() != null) {
- getSubmitListener().onCancel();
- }
- }
-
- /**
- * Handles the keyboard navigation when the resolution is set to years.
- *
- * @param keycode
- * The keycode to process
- * @param ctrl
- * Is ctrl pressed?
- * @param shift
- * is shift pressed
- * @return Returns true if the keycode was processed, else false
- */
- protected boolean handleNavigationYearMode(int keycode, boolean ctrl,
- boolean shift) {
-
- // Ctrl and Shift selection not supported
- if (ctrl || shift) {
- return false;
- } else if (keycode == getPreviousKey()) {
- focusNextYear(10); // Add 10 years
- return true;
- } else if (keycode == getForwardKey()) {
- focusNextYear(1); // Add 1 year
- return true;
- } else if (keycode == getNextKey()) {
- focusPreviousYear(10); // Subtract 10 years
- return true;
- } else if (keycode == getBackwardKey()) {
- focusPreviousYear(1); // Subtract 1 year
- return true;
-
- } else if (keycode == getSelectKey()) {
- value = (Date) focusedDate.clone();
- onSubmit();
- return true;
-
- } else if (keycode == getResetKey()) {
- // Restore showing value the selected value
- focusedDate.setTime(value.getTime());
- renderCalendar();
- return true;
-
- } else if (keycode == getCloseKey()) {
- // TODO fire listener, on users responsibility??
-
- onCancel();
- return true;
- }
- return false;
- }
-
- /**
- * Handle the keyboard navigation when the resolution is set to MONTH.
- *
- * @param keycode
- * The keycode to handle
- * @param ctrl
- * Was the ctrl key pressed?
- * @param shift
- * Was the shift key pressed?
- * @return
- */
- protected boolean handleNavigationMonthMode(int keycode, boolean ctrl,
- boolean shift) {
-
- // Ctrl selection not supported
- if (ctrl) {
- return false;
-
- } else if (keycode == getPreviousKey()) {
- focusNextYear(1); // Add 1 year
- return true;
-
- } else if (keycode == getForwardKey()) {
- focusNextMonth(); // Add 1 month
- return true;
-
- } else if (keycode == getNextKey()) {
- focusPreviousYear(1); // Subtract 1 year
- return true;
-
- } else if (keycode == getBackwardKey()) {
- focusPreviousMonth(); // Subtract 1 month
- return true;
-
- } else if (keycode == getSelectKey()) {
- value = (Date) focusedDate.clone();
- onSubmit();
- return true;
-
- } else if (keycode == getResetKey()) {
- // Restore showing value the selected value
- focusedDate.setTime(value.getTime());
- renderCalendar();
- return true;
-
- } else if (keycode == getCloseKey()) {
- onCancel();
-
- // TODO fire close event
-
- return true;
- }
-
- return false;
- }
-
- /**
- * Handle keyboard navigation what the resolution is set to DAY.
- *
- * @param keycode
- * The keycode to handle
- * @param ctrl
- * Was the ctrl key pressed?
- * @param shift
- * Was the shift key pressed?
- * @return Return true if the key press was handled by the method, else
- * return false.
- */
- protected boolean handleNavigationDayMode(int keycode, boolean ctrl,
- boolean shift) {
-
- // Ctrl key is not in use
- if (ctrl) {
- return false;
- }
-
- /*
- * Jumps to the next day.
- */
- if (keycode == getForwardKey() && !shift) {
- focusNextDay(1);
- return true;
-
- /*
- * Jumps to the previous day
- */
- } else if (keycode == getBackwardKey() && !shift) {
- focusPreviousDay(1);
- return true;
-
- /*
- * Jumps one week forward in the calendar
- */
- } else if (keycode == getNextKey() && !shift) {
- focusNextDay(7);
- return true;
-
- /*
- * Jumps one week back in the calendar
- */
- } else if (keycode == getPreviousKey() && !shift) {
- focusPreviousDay(7);
- return true;
-
- /*
- * Selects the value that is chosen
- */
- } else if (keycode == getSelectKey() && !shift) {
- selectFocused();
- onSubmit(); // submit
- return true;
-
- } else if (keycode == getCloseKey()) {
- onCancel();
- // TODO close event
-
- return true;
-
- /*
- * Jumps to the next month
- */
- } else if (shift && keycode == getForwardKey()) {
- focusNextMonth();
- return true;
-
- /*
- * Jumps to the previous month
- */
- } else if (shift && keycode == getBackwardKey()) {
- focusPreviousMonth();
- return true;
-
- /*
- * Jumps to the next year
- */
- } else if (shift && keycode == getPreviousKey()) {
- focusNextYear(1);
- return true;
-
- /*
- * Jumps to the previous year
- */
- } else if (shift && keycode == getNextKey()) {
- focusPreviousYear(1);
- return true;
-
- /*
- * Resets the selection
- */
- } else if (keycode == getResetKey() && !shift) {
- // Restore showing value the selected value
- focusedDate = new FocusedDate(value.getYear(), value.getMonth(),
- value.getDate());
- displayedMonth = new FocusedDate(value.getYear(), value.getMonth(),
- 1);
- renderCalendar();
- return true;
- }
-
- return false;
- }
-
- /**
- * Handles the keyboard navigation.
- *
- * @param keycode
- * The key code that was pressed
- * @param ctrl
- * Was the ctrl key pressed
- * @param shift
- * Was the shift key pressed
- * @return Return true if key press was handled by the component, else
- * return false
- */
- protected boolean handleNavigation(int keycode, boolean ctrl,
- boolean shift) {
- if (!isEnabled() || isReadonly()) {
- return false;
- } else if (isYear(getResolution())) {
- return handleNavigationYearMode(keycode, ctrl, shift);
- } else if (isMonth(getResolution())) {
- return handleNavigationMonthMode(keycode, ctrl, shift);
- } else if (isDay(getResolution())) {
- return handleNavigationDayMode(keycode, ctrl, shift);
- } else {
- return handleNavigationDayMode(keycode, ctrl, shift);
- }
-
- }
-
- /**
- * Returns the reset key which will reset the calendar to the previous
- * selection. By default this is backspace but it can be overridden to
- * change the key to whatever you want.
- *
- * @return
- */
- protected int getResetKey() {
- return KeyCodes.KEY_BACKSPACE;
- }
-
- /**
- * Returns the select key which selects the value. By default this is the
- * enter key but it can be changed to whatever you like by overriding this
- * method.
- *
- * @return
- */
- protected int getSelectKey() {
- return KeyCodes.KEY_ENTER;
- }
-
- /**
- * Returns the key that closes the popup window if this is a VPopopCalendar.
- * Else this does nothing. By default this is the Escape key but you can
- * change the key to whatever you want by overriding this method.
- *
- * @return
- */
- protected int getCloseKey() {
- return KeyCodes.KEY_ESCAPE;
- }
-
- /**
- * The key that selects the next day in the calendar. By default this is the
- * right arrow key but by overriding this method it can be changed to
- * whatever you like.
- *
- * @return
- */
- protected int getForwardKey() {
- return KeyCodes.KEY_RIGHT;
- }
-
- /**
- * The key that selects the previous day in the calendar. By default this is
- * the left arrow key but by overriding this method it can be changed to
- * whatever you like.
- *
- * @return
- */
- protected int getBackwardKey() {
- return KeyCodes.KEY_LEFT;
- }
-
- /**
- * The key that selects the next week in the calendar. By default this is
- * the down arrow key but by overriding this method it can be changed to
- * whatever you like.
- *
- * @return
- */
- protected int getNextKey() {
- return KeyCodes.KEY_DOWN;
- }
-
- /**
- * The key that selects the previous week in the calendar. By default this
- * is the up arrow key but by overriding this method it can be changed to
- * whatever you like.
- *
- * @return
- */
- protected int getPreviousKey() {
- return KeyCodes.KEY_UP;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.google.gwt.event.dom.client.MouseOutHandler#onMouseOut(com.google
- * .gwt.event.dom.client.MouseOutEvent)
- */
- @Override
- public void onMouseOut(MouseOutEvent event) {
- if (mouseTimer != null) {
- mouseTimer.cancel();
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.google.gwt.event.dom.client.MouseDownHandler#onMouseDown(com.google
- * .gwt.event.dom.client.MouseDownEvent)
- */
- @Override
- public void onMouseDown(MouseDownEvent event) {
- // Click-n-hold the left mouse button for fast-forward or fast-rewind.
- // Timer is first used for a 500ms delay after mousedown. After that has
- // elapsed, another timer is triggered to go off every 150ms. Both
- // timers are cancelled on mouseup or mouseout.
- if (event.getNativeButton() == NativeEvent.BUTTON_LEFT && event
- .getSource() instanceof VAbstractCalendarPanel.VEventButton) {
- final VEventButton sender = (VEventButton) event.getSource();
- processClickEvent(sender);
- mouseTimer = new Timer() {
- @Override
- public void run() {
- mouseTimer = new Timer() {
- @Override
- public void run() {
- processClickEvent(sender);
- }
- };
- mouseTimer.scheduleRepeating(150);
- }
- };
- mouseTimer.schedule(500);
- }
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.google.gwt.event.dom.client.MouseUpHandler#onMouseUp(com.google.gwt
- * .event.dom.client.MouseUpEvent)
- */
- @Override
- public void onMouseUp(MouseUpEvent event) {
- if (mouseTimer != null) {
- mouseTimer.cancel();
- }
- }
-
- /**
- * Adjusts a date to fit inside the range, only if outside
- *
- * @param date
- */
- private Date adjustDateToFitInsideRange(Date date) {
- if (!isAcceptedByRangeStart(date, resolution)) {
- date = parseRangeString(rangeStart);
- } else if (!isAcceptedByRangeEnd(date, resolution)) {
- date = parseRangeString(rangeEnd);
- }
- return date;
- }
-
- private Date parseRangeString(String dateStr) {
- if (dateStr == null || "".equals(dateStr))
- return null;
- int year = Integer.parseInt(dateStr.substring(0, 4)) - 1900;
- int month = parsePart(dateStr, 5, 2, 1) - 1;
- int day = parsePart(dateStr, 8, 2, 1);
- int hrs = parsePart(dateStr, 11, 2, 0);
- int min = parsePart(dateStr, 14, 2, 0);
- int sec = parsePart(dateStr, 17, 2, 0);
-
- return new Date(year, month, day, hrs, min, sec);
- }
-
- private int parsePart(String dateStr, int beginIndex, int length,
- int defValue) {
- if (dateStr.length() < beginIndex + length)
- return defValue;
- return Integer
- .parseInt(dateStr.substring(beginIndex, beginIndex + length));
- }
-
- /**
- * Sets the data of the Panel.
- *
- * @param currentDate
- * The date to set
- */
- public void setDate(Date currentDate) {
- doSetDate(currentDate, false, () -> {
- });
- }
-
- /**
- * The actual implementation of the logic which sets the data of the Panel.
- * The method {@link #setDate(Date)} just delegate a call to this method
- * providing additional config parameters.
- *
- * @param currentDate
- * currentDate The date to set
- * @param needRerender
- * if {@code true} then calendar will be rerendered regardless of
- * internal logic, otherwise the decision will be made on the
- * internal state inside the method
- * @param focusAction
- * an additional action which will be executed in case
- * rerendering is not required
- */
- protected void doSetDate(Date currentDate, boolean needRerender,
- Runnable focusAction) {
- // Check that we are not re-rendering an already active date
- if (currentDate == value && currentDate != null) {
- return;
- }
- boolean currentDateWasAdjusted = false;
- // Check that selected date is inside the allowed range
- if (currentDate != null
- && !isDateInsideRange(currentDate, getResolution())) {
- currentDate = adjustDateToFitInsideRange(currentDate);
- currentDateWasAdjusted = true;
- }
-
- Date oldDisplayedMonth = displayedMonth;
- value = currentDate;
-
- // If current date was adjusted, we will not select any date,
- // since that will look like a date is selected. Instead we
- // only focus on the adjusted value
- if (value == null || currentDateWasAdjusted) {
- // If ranges enabled, we may need to focus on a different view to
- // potentially not get stuck
- if (rangeStart != null || rangeEnd != null) {
- Date dateThatFitsInsideRange = adjustDateToFitInsideRange(
- new Date());
- focusedDate = new FocusedDate(dateThatFitsInsideRange.getYear(),
- dateThatFitsInsideRange.getMonth(),
- dateThatFitsInsideRange.getDate());
- displayedMonth = new FocusedDate(
- dateThatFitsInsideRange.getYear(),
- dateThatFitsInsideRange.getMonth(), 1);
- // value was adjusted. Set selected to null to not cause
- // confusion, but this is only needed (and allowed) when we have
- // a day
- // resolution
- if (isDay(getResolution())) {
- value = null;
- }
- } else {
- focusedDate = displayedMonth = null;
- }
- } else {
- focusedDate = new FocusedDate(value.getYear(), value.getMonth(),
- value.getDate());
- displayedMonth = new FocusedDate(value.getYear(), value.getMonth(),
- 1);
- }
-
- // Re-render calendar if the displayed month is changed.
- if (needRerender || oldDisplayedMonth == null || value == null
- || oldDisplayedMonth.getYear() != value.getYear()
- || oldDisplayedMonth.getMonth() != value.getMonth()) {
- renderCalendar();
- } else {
- focusDay(focusedDate);
- selectFocused();
- focusAction.run();
- }
-
- if (!hasFocus) {
- focusDay(null);
- }
- }
-
- /**
- * A widget representing a single day in the calendar panel.
- */
- private class Day extends InlineHTML {
- private final Date date;
-
- Day(Date date) {
- super("" + date.getDate());
- this.date = date;
- addClickHandler(dayClickHandler);
- }
-
- public Date getDate() {
- return date;
- }
- }
-
- public Date getDate() {
- return value;
- }
-
- /**
- * If true should be returned if the panel will not be used after this
- * event.
- *
- * @param event
- * @return
- */
- protected boolean onTabOut(DomEvent<?> event) {
- if (focusOutListener != null) {
- return focusOutListener.onFocusOut(event);
- }
- return false;
- }
-
- /**
- * A focus out listener is triggered when the panel loosed focus. This can
- * happen either after a user clicks outside the panel or tabs out.
- *
- * @param listener
- * The listener to trigger
- */
- public void setFocusOutListener(FocusOutListener listener) {
- focusOutListener = listener;
- }
-
- /**
- * The submit listener is called when the user selects a value from the
- * calendar either by clicking the day or selects it by keyboard.
- *
- * @param submitListener
- * The listener to trigger
- */
- public void setSubmitListener(SubmitListener submitListener) {
- this.submitListener = submitListener;
- }
-
- /**
- * The given FocusChangeListener is notified when the focused date changes
- * by user either clicking on a new date or by using the keyboard.
- *
- * @param listener
- * The FocusChangeListener to be notified
- */
- public void setFocusChangeListener(FocusChangeListener listener) {
- focusChangeListener = listener;
- }
-
- /**
- * Returns the submit listener that listens to selection made from the
- * panel.
- *
- * @return The listener or NULL if no listener has been set
- */
- public SubmitListener getSubmitListener() {
- return submitListener;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.google.gwt.event.dom.client.BlurHandler#onBlur(com.google.gwt.event
- * .dom.client.BlurEvent)
- */
- @Override
- public void onBlur(final BlurEvent event) {
- if (event.getSource() instanceof VAbstractCalendarPanel) {
- hasFocus = false;
- focusDay(null);
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.google.gwt.event.dom.client.FocusHandler#onFocus(com.google.gwt.event
- * .dom.client.FocusEvent)
- */
- @Override
- public void onFocus(FocusEvent event) {
- if (event.getSource() instanceof VAbstractCalendarPanel) {
- hasFocus = true;
-
- // Focuses the current day if the calendar shows the days
- if (focusedDay != null) {
- focusDay(focusedDate);
- }
- }
- }
-
- private static final String SUBPART_NEXT_MONTH = "nextmon";
- private static final String SUBPART_PREV_MONTH = "prevmon";
-
- private static final String SUBPART_NEXT_YEAR = "nexty";
- private static final String SUBPART_PREV_YEAR = "prevy";
- private static final String SUBPART_HOUR_SELECT = "h";
- private static final String SUBPART_MINUTE_SELECT = "m";
- private static final String SUBPART_SECS_SELECT = "s";
- private static final String SUBPART_AMPM_SELECT = "ampm";
- private static final String SUBPART_DAY = "day";
- private static final String SUBPART_MONTH_YEAR_HEADER = "header";
-
- private String rangeStart;
-
- private String rangeEnd;
-
- @Override
- public String getSubPartName(
- com.google.gwt.user.client.Element subElement) {
- if (contains(nextMonth, subElement)) {
- return SUBPART_NEXT_MONTH;
- } else if (contains(prevMonth, subElement)) {
- return SUBPART_PREV_MONTH;
- } else if (contains(nextYear, subElement)) {
- return SUBPART_NEXT_YEAR;
- } else if (contains(prevYear, subElement)) {
- return SUBPART_PREV_YEAR;
- } else if (contains(days, subElement)) {
- // Day, find out which dayOfMonth and use that as the identifier
- Day day = WidgetUtil.findWidget(subElement, Day.class);
- if (day != null) {
- Date date = day.getDate();
- int id = date.getDate();
- // Zero or negative ids map to days of the preceding month,
- // past-the-end-of-month ids to days of the following month
- if (date.getMonth() < displayedMonth.getMonth()) {
- id -= DateTimeService.getNumberOfDaysInMonth(date);
- } else if (date.getMonth() > displayedMonth.getMonth()) {
- id += DateTimeService
- .getNumberOfDaysInMonth(displayedMonth);
- }
- return SUBPART_DAY + id;
- }
- } else if (getCellFormatter().getElement(0, 2)
- .isOrHasChild(subElement)) {
- return SUBPART_MONTH_YEAR_HEADER;
- }
-
- return null;
- }
-
- /**
- * Checks if subElement is inside the widget DOM hierarchy.
- *
- * @param w
- * @param subElement
- * @return true if {@code w} is a parent of subElement, false otherwise.
- */
- protected boolean contains(Widget w, Element subElement) {
- if (w == null || w.getElement() == null) {
- return false;
- }
-
- return w.getElement().isOrHasChild(subElement);
- }
-
- @Override
- public com.google.gwt.user.client.Element getSubPartElement(
- String subPart) {
- if (SUBPART_NEXT_MONTH.equals(subPart)) {
- return nextMonth.getElement();
- }
- if (SUBPART_PREV_MONTH.equals(subPart)) {
- return prevMonth.getElement();
- }
- if (SUBPART_NEXT_YEAR.equals(subPart)) {
- return nextYear.getElement();
- }
- if (SUBPART_PREV_YEAR.equals(subPart)) {
- return prevYear.getElement();
- }
- if (subPart.startsWith(SUBPART_DAY)) {
- // Zero or negative ids map to days in the preceding month,
- // past-the-end-of-month ids to days in the following month
- int dayOfMonth = Integer
- .parseInt(subPart.substring(SUBPART_DAY.length()));
- Date date = new Date(displayedMonth.getYear(),
- displayedMonth.getMonth(), dayOfMonth);
- for (Widget w : days) {
- if (w instanceof VAbstractCalendarPanel.Day) {
- Day day = (Day) w;
- if (day.getDate().equals(date)) {
- return day.getElement();
- }
- }
- }
- }
-
- if (SUBPART_MONTH_YEAR_HEADER.equals(subPart)) {
- return DOM.asOld(
- (Element) getCellFormatter().getElement(0, 2).getChild(0));
- }
- return null;
- }
-
- @Override
- protected void onDetach() {
- super.onDetach();
- if (mouseTimer != null) {
- mouseTimer.cancel();
- }
- }
-
- /**
- * Helper class to inform the screen reader that the user changed the
- * selected date. It sets the value of a field that is outside the view, and
- * is defined as a live area. That way the screen reader recognizes the
- * change and reads it to the user.
- */
- public class FocusedDate extends Date {
-
- public FocusedDate(int year, int month, int date) {
- super(year, month, date);
- }
-
- @Override
- public void setTime(long time) {
- super.setTime(time);
- setLabel();
- }
-
- @Override
- @Deprecated
- public void setDate(int date) {
- super.setDate(date);
- setLabel();
- }
-
- @Override
- @Deprecated
- public void setMonth(int month) {
- super.setMonth(month);
- setLabel();
- }
-
- @Override
- @Deprecated
- public void setYear(int year) {
- super.setYear(year);
- setLabel();
- }
-
- private void setLabel() {
- if (getDateField() instanceof VAbstractPopupCalendar) {
- ((VAbstractPopupCalendar<?, ?>) getDateField())
- .setFocusedDate(this);
- }
- }
- }
-
- /**
- * Sets the start range for this component. The start range is inclusive,
- * and it depends on the current resolution, what is considered inside the
- * range.
- *
- * @param newRangeStart
- * - the allowed range's start date
- */
- public void setRangeStart(String newRangeStart) {
- if (!SharedUtil.equals(rangeStart, newRangeStart)) {
- rangeStart = newRangeStart;
- if (initialRenderDone) {
- // Dynamic updates to the range needs to render the calendar to
- // update the element stylenames
- renderCalendar();
- }
- }
- }
-
- /**
- * Sets the end range for this component. The end range is inclusive, and it
- * depends on the current resolution, what is considered inside the range.
- *
- * @param newRangeEnd
- * - the allowed range's end date
- */
- public void setRangeEnd(String newRangeEnd) {
- if (!SharedUtil.equals(rangeEnd, newRangeEnd)) {
- // Dates with year 10000 or more has + prefix, which is not compatible
- // with format returned by dateStrResolution method
- if (newRangeEnd.startsWith("+")) {
- rangeEnd = newRangeEnd.substring(1);
- } else {
- rangeEnd = newRangeEnd;
- }
- if (initialRenderDone) {
- // Dynamic updates to the range needs to render the calendar to
- // update the element stylenames
- renderCalendar();
- }
- }
- }
-
- /**
- * Set assistive label for the previous year element.
- *
- * @param label
- * the label to set
- * @since 8.4
- */
- public void setAssistiveLabelPreviousYear(String label) {
- prevYearAssistiveLabel = label;
- }
-
- /**
- * Set assistive label for the next year element.
- *
- * @param label
- * the label to set
- * @since 8.4
- */
- public void setAssistiveLabelNextYear(String label) {
- nextYearAssistiveLabel = label;
- }
-
- /**
- * Set assistive label for the previous month element.
- *
- * @param label
- * the label to set
- * @since 8.4
- */
- public void setAssistiveLabelPreviousMonth(String label) {
- prevMonthAssistiveLabel = label;
- }
-
- /**
- * Set assistive label for the next month element.
- *
- * @param label
- * the label to set
- * @since 8.4
- */
- public void setAssistiveLabelNextMonth(String label) {
- nextMonthAssistiveLabel = label;
- }
-
- /**
- * Updates assistive labels of the navigation elements.
- *
- * @since 8.4
- */
- public void updateAssistiveLabels() {
- if (prevMonth != null) {
- Roles.getButtonRole().setAriaLabelProperty(prevMonth.getElement(),
- prevMonthAssistiveLabel);
- }
-
- if (nextMonth != null) {
- Roles.getButtonRole().setAriaLabelProperty(nextMonth.getElement(),
- nextMonthAssistiveLabel);
- }
-
- if (prevYear != null) {
- Roles.getButtonRole().setAriaLabelProperty(prevYear.getElement(),
- prevYearAssistiveLabel);
- }
-
- if (nextYear != null) {
- Roles.getButtonRole().setAriaLabelProperty(nextYear.getElement(),
- nextYearAssistiveLabel);
- }
- }
-
- private static Logger getLogger() {
- return Logger.getLogger(VAbstractCalendarPanel.class.getName());
- }
- }
|