org.primefaces.component.calendar.Calendar Maven / Gradle / Ivy
/*
* The MIT License
*
* Copyright (c) 2009-2023 PrimeTek Informatics
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.primefaces.component.calendar;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.faces.application.ResourceDependency;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.FacesEvent;
import javax.faces.event.PhaseId;
import org.primefaces.event.DateViewChangeEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.util.CalendarUtils;
import org.primefaces.util.ComponentUtils;
import org.primefaces.util.Constants;
@ResourceDependency(library = "primefaces", name = "components.css")
@ResourceDependency(library = "primefaces", name = "jquery/jquery.js")
@ResourceDependency(library = "primefaces", name = "core.js")
@ResourceDependency(library = "primefaces", name = "inputmask/inputmask.js")
@ResourceDependency(library = "primefaces", name = "calendar/calendar.css")
@ResourceDependency(library = "primefaces", name = "calendar/calendar.js")
public class Calendar extends CalendarBase {
public static final String COMPONENT_TYPE = "org.primefaces.component.Calendar";
private Map customEvents = new HashMap<>(1);
public boolean isPopup() {
return getMode().equalsIgnoreCase("popup");
}
@Override
public Collection getEventNames() {
return CALENDAR_EVENT_NAMES;
}
@Override
public Collection getUnobstrusiveEventNames() {
return UNOBSTRUSIVE_EVENT_NAMES;
}
@Override
public void queueEvent(FacesEvent event) {
FacesContext context = getFacesContext();
if (ComponentUtils.isRequestSource(this, context) && (event instanceof AjaxBehaviorEvent)) {
Map params = context.getExternalContext().getRequestParameterMap();
String eventName = params.get(Constants.RequestParams.PARTIAL_BEHAVIOR_EVENT_PARAM);
String clientId = getClientId(context);
AjaxBehaviorEvent behaviorEvent = (AjaxBehaviorEvent) event;
if (eventName != null) {
if ("dateSelect".equals(eventName) || "close".equals(eventName)) {
customEvents.put(eventName, (AjaxBehaviorEvent) event);
}
else if ("viewChange".equals(eventName)) {
int month = Integer.parseInt(params.get(clientId + "_month"));
int year = Integer.parseInt(params.get(clientId + "_year"));
DateViewChangeEvent dateViewChangeEvent = new DateViewChangeEvent(this, behaviorEvent.getBehavior(), month, year);
dateViewChangeEvent.setPhaseId(behaviorEvent.getPhaseId());
super.queueEvent(dateViewChangeEvent);
}
else {
super.queueEvent(event); //regular events like change, click, blur
}
}
}
else {
super.queueEvent(event); //valueChange
}
}
@Override
public void validate(FacesContext context) {
super.validate(context);
if (isValid() && ComponentUtils.isRequestSource(this, context) && customEvents != null) {
for (Map.Entry event : customEvents.entrySet()) {
SelectEvent selectEvent = new SelectEvent(this, event.getValue().getBehavior(), getValue());
if (event.getValue().getPhaseId().equals(PhaseId.APPLY_REQUEST_VALUES)) {
selectEvent.setPhaseId(PhaseId.PROCESS_VALIDATIONS);
}
else {
selectEvent.setPhaseId(PhaseId.INVOKE_APPLICATION);
}
super.queueEvent(selectEvent);
}
}
}
@Override
protected void validateValue(FacesContext context, Object value) {
super.validateValue(context, value);
ValidationResult validationResult = ValidationResult.OK;
if (isValid() && !isEmpty(value) && (value instanceof LocalDate || value instanceof LocalDateTime || value instanceof Date)) {
LocalDate date = null;
if (value instanceof LocalDate) {
date = (LocalDate) value;
}
else if (value instanceof LocalDateTime) {
date = ((LocalDateTime) value).toLocalDate();
}
else if (value instanceof LocalTime) {
//no check necessary
}
else if (value instanceof Date) {
date = CalendarUtils.convertDate2LocalDate((Date) value, CalendarUtils.calculateZoneId(getTimeZone()));
}
if (date != null) {
LocalDate minDate = CalendarUtils.getObjectAsLocalDate(context, this, getMindate());
LocalDate maxDate = CalendarUtils.getObjectAsLocalDate(context, this, getMaxdate());
if (minDate != null && date.isBefore(minDate)) {
setValid(false);
if (maxDate != null) {
validationResult = ValidationResult.INVALID_OUT_OF_RANGE;
}
else {
validationResult = ValidationResult.INVALID_MIN_DATE;
}
}
if (isValid()) {
if (maxDate != null && date.isAfter(maxDate)) {
setValid(false);
if (minDate != null) {
validationResult = ValidationResult.INVALID_OUT_OF_RANGE;
}
else {
validationResult = ValidationResult.INVALID_MAX_DATE;
}
}
}
}
if (!isValid()) {
createFacesMessageFromValidationResult(context, validationResult);
}
}
}
@Override
public Object saveState(FacesContext context) {
// reset component for MyFaces view pooling
if (customEvents != null) {
customEvents.clear();
}
return super.saveState(context);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy