/*
* Copyright 2009 Prime Technology.
*
* 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 org.primefaces.component.calendar;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.el.ValueExpression;
import javax.el.MethodExpression;
import javax.faces.render.Renderer;
import java.io.IOException;
import org.primefaces.renderkit.PartialRenderer;
import org.primefaces.component.resource.Resource;
import javax.faces.component.UIComponent;
import javax.faces.event.ListenerFor;
import javax.faces.event.PostAddToViewEvent;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.event.AbortProcessingException;
import java.util.List;
import java.util.ArrayList;
import org.primefaces.component.calendar.Calendar;
import org.primefaces.event.DateSelectEvent;
@ListenerFor(systemEventClass = PostAddToViewEvent.class)
public class Calendar extends UIInput {
public static final String COMPONENT_TYPE = "org.primefaces.component.Calendar";
public static final String COMPONENT_FAMILY = "org.primefaces.component";
private static final String DEFAULT_RENDERER = "org.primefaces.component.CalendarRenderer";
private static final String OPTIMIZED_PACKAGE = "org.primefaces.component.";
protected enum PropertyKeys {
widgetVar
,mindate
,maxdate
,pages
,disabled
,mode
,pattern
,locale
,popupIcon
,popupIconOnly
,navigator
,timeZone
,readOnlyInputText
,onSelectUpdate
,selectListener
,style
,styleClass
,inputStyle
,inputStyleClass
,showButtonPanel
,effect
,effectDuration
,showOn
,showWeek
,showOtherMonths
,selectOtherMonths;
String toString;
PropertyKeys(String toString) {
this.toString = toString;
}
PropertyKeys() {}
public String toString() {
return ((this.toString != null) ? this.toString : super.toString());
}
}
public Calendar() {
setRendererType(DEFAULT_RENDERER);
}
public String getFamily() {
return COMPONENT_FAMILY;
}
public java.lang.String getWidgetVar() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.widgetVar, null);
}
public void setWidgetVar(java.lang.String _widgetVar) {
getStateHelper().put(PropertyKeys.widgetVar, _widgetVar);
handleAttribute("widgetVar", _widgetVar);
}
public java.lang.Object getMindate() {
return (java.lang.Object) getStateHelper().eval(PropertyKeys.mindate, null);
}
public void setMindate(java.lang.Object _mindate) {
getStateHelper().put(PropertyKeys.mindate, _mindate);
handleAttribute("mindate", _mindate);
}
public java.lang.Object getMaxdate() {
return (java.lang.Object) getStateHelper().eval(PropertyKeys.maxdate, null);
}
public void setMaxdate(java.lang.Object _maxdate) {
getStateHelper().put(PropertyKeys.maxdate, _maxdate);
handleAttribute("maxdate", _maxdate);
}
public int getPages() {
return (java.lang.Integer) getStateHelper().eval(PropertyKeys.pages, 1);
}
public void setPages(int _pages) {
getStateHelper().put(PropertyKeys.pages, _pages);
handleAttribute("pages", _pages);
}
public boolean isDisabled() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.disabled, false);
}
public void setDisabled(boolean _disabled) {
getStateHelper().put(PropertyKeys.disabled, _disabled);
handleAttribute("disabled", _disabled);
}
public java.lang.String getMode() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.mode, "popup");
}
public void setMode(java.lang.String _mode) {
getStateHelper().put(PropertyKeys.mode, _mode);
handleAttribute("mode", _mode);
}
public java.lang.String getPattern() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.pattern, "MM/dd/yyyy");
}
public void setPattern(java.lang.String _pattern) {
getStateHelper().put(PropertyKeys.pattern, _pattern);
handleAttribute("pattern", _pattern);
}
public java.lang.Object getLocale() {
return (java.lang.Object) getStateHelper().eval(PropertyKeys.locale, null);
}
public void setLocale(java.lang.Object _locale) {
getStateHelper().put(PropertyKeys.locale, _locale);
handleAttribute("locale", _locale);
}
public java.lang.String getPopupIcon() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.popupIcon, null);
}
public void setPopupIcon(java.lang.String _popupIcon) {
getStateHelper().put(PropertyKeys.popupIcon, _popupIcon);
handleAttribute("popupIcon", _popupIcon);
}
public boolean isPopupIconOnly() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.popupIconOnly, false);
}
public void setPopupIconOnly(boolean _popupIconOnly) {
getStateHelper().put(PropertyKeys.popupIconOnly, _popupIconOnly);
handleAttribute("popupIconOnly", _popupIconOnly);
}
public boolean isNavigator() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.navigator, false);
}
public void setNavigator(boolean _navigator) {
getStateHelper().put(PropertyKeys.navigator, _navigator);
handleAttribute("navigator", _navigator);
}
public java.lang.Object getTimeZone() {
return (java.lang.Object) getStateHelper().eval(PropertyKeys.timeZone, null);
}
public void setTimeZone(java.lang.Object _timeZone) {
getStateHelper().put(PropertyKeys.timeZone, _timeZone);
handleAttribute("timeZone", _timeZone);
}
public boolean isReadOnlyInputText() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.readOnlyInputText, false);
}
public void setReadOnlyInputText(boolean _readOnlyInputText) {
getStateHelper().put(PropertyKeys.readOnlyInputText, _readOnlyInputText);
handleAttribute("readOnlyInputText", _readOnlyInputText);
}
public java.lang.String getOnSelectUpdate() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.onSelectUpdate, null);
}
public void setOnSelectUpdate(java.lang.String _onSelectUpdate) {
getStateHelper().put(PropertyKeys.onSelectUpdate, _onSelectUpdate);
handleAttribute("onSelectUpdate", _onSelectUpdate);
}
public javax.el.MethodExpression getSelectListener() {
return (javax.el.MethodExpression) getStateHelper().eval(PropertyKeys.selectListener, null);
}
public void setSelectListener(javax.el.MethodExpression _selectListener) {
getStateHelper().put(PropertyKeys.selectListener, _selectListener);
handleAttribute("selectListener", _selectListener);
}
public java.lang.String getStyle() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.style, null);
}
public void setStyle(java.lang.String _style) {
getStateHelper().put(PropertyKeys.style, _style);
handleAttribute("style", _style);
}
public java.lang.String getStyleClass() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.styleClass, null);
}
public void setStyleClass(java.lang.String _styleClass) {
getStateHelper().put(PropertyKeys.styleClass, _styleClass);
handleAttribute("styleClass", _styleClass);
}
public java.lang.String getInputStyle() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.inputStyle, null);
}
public void setInputStyle(java.lang.String _inputStyle) {
getStateHelper().put(PropertyKeys.inputStyle, _inputStyle);
handleAttribute("inputStyle", _inputStyle);
}
public java.lang.String getInputStyleClass() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.inputStyleClass, null);
}
public void setInputStyleClass(java.lang.String _inputStyleClass) {
getStateHelper().put(PropertyKeys.inputStyleClass, _inputStyleClass);
handleAttribute("inputStyleClass", _inputStyleClass);
}
public boolean isShowButtonPanel() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.showButtonPanel, false);
}
public void setShowButtonPanel(boolean _showButtonPanel) {
getStateHelper().put(PropertyKeys.showButtonPanel, _showButtonPanel);
handleAttribute("showButtonPanel", _showButtonPanel);
}
public java.lang.String getEffect() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.effect, null);
}
public void setEffect(java.lang.String _effect) {
getStateHelper().put(PropertyKeys.effect, _effect);
handleAttribute("effect", _effect);
}
public java.lang.String getEffectDuration() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.effectDuration, "normal");
}
public void setEffectDuration(java.lang.String _effectDuration) {
getStateHelper().put(PropertyKeys.effectDuration, _effectDuration);
handleAttribute("effectDuration", _effectDuration);
}
public java.lang.String getShowOn() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.showOn, null);
}
public void setShowOn(java.lang.String _showOn) {
getStateHelper().put(PropertyKeys.showOn, _showOn);
handleAttribute("showOn", _showOn);
}
public boolean isShowWeek() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.showWeek, false);
}
public void setShowWeek(boolean _showWeek) {
getStateHelper().put(PropertyKeys.showWeek, _showWeek);
handleAttribute("showWeek", _showWeek);
}
public boolean isShowOtherMonths() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.showOtherMonths, false);
}
public void setShowOtherMonths(boolean _showOtherMonths) {
getStateHelper().put(PropertyKeys.showOtherMonths, _showOtherMonths);
handleAttribute("showOtherMonths", _showOtherMonths);
}
public boolean isSelectOtherMonths() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.selectOtherMonths, false);
}
public void setSelectOtherMonths(boolean _selectOtherMonths) {
getStateHelper().put(PropertyKeys.selectOtherMonths, _selectOtherMonths);
handleAttribute("selectOtherMonths", _selectOtherMonths);
}
public static String POPUP_ICON = "/primefaces/calendar/calendar_icon.png";
private java.util.Locale appropriateLocale;
private java.util.TimeZone appropriateTimeZone;
public java.util.Locale calculateLocale(FacesContext facesContext) {
if(appropriateLocale == null) {
Object userLocale = getLocale();
if(userLocale != null) {
if(userLocale instanceof String) {
String[] tokens = ((String) userLocale).split("_");
if(tokens.length == 1)
appropriateLocale = new java.util.Locale(tokens[0], "");
else
appropriateLocale = new java.util.Locale(tokens[0], tokens[1]);
}
else if(userLocale instanceof java.util.Locale)
appropriateLocale = (java.util.Locale) userLocale;
else
throw new IllegalArgumentException("Type:" + userLocale.getClass() + " is not a valid locale type for calendar:" + this.getClientId(facesContext));
} else {
appropriateLocale = facesContext.getViewRoot().getLocale();
}
}
return appropriateLocale;
}
public java.util.TimeZone calculateTimeZone() {
if(appropriateTimeZone == null) {
Object usertimeZone = getTimeZone();
if(usertimeZone != null) {
if(usertimeZone instanceof String)
appropriateTimeZone = java.util.TimeZone.getTimeZone((String) usertimeZone);
else if(usertimeZone instanceof java.util.TimeZone)
appropriateTimeZone = (java.util.TimeZone) usertimeZone;
else
throw new IllegalArgumentException("TimeZone could be either String or java.util.TimeZone");
} else {
appropriateTimeZone = java.util.TimeZone.getDefault();
}
}
return appropriateTimeZone;
}
public boolean isPopup() {
return getMode().equalsIgnoreCase("popup");
}
public void broadcast(javax.faces.event.FacesEvent event) throws javax.faces.event.AbortProcessingException {
super.broadcast(event);
FacesContext facesContext = FacesContext.getCurrentInstance();
MethodExpression me = getSelectListener();
if (me != null && event instanceof org.primefaces.event.DateSelectEvent) {
me.invoke(facesContext.getELContext(), new Object[] {event});
}
}
protected FacesContext getFacesContext() {
return FacesContext.getCurrentInstance();
}
public boolean resourceExists(FacesContext facesContext, String name) {
java.util.List<UIComponent> resources = facesContext.getViewRoot().getComponentResources(facesContext, "head");
for(UIComponent component : resources) {
String value = component.toString();
if(value != null && value.equals(name))
return true;
}
return false;
}
@Override
public void processEvent(ComponentSystemEvent event) throws AbortProcessingException {
FacesContext facesContext = getFacesContext();
javax.faces.component.UIViewRoot viewroot = facesContext.getViewRoot();
Resource resource = null;
if(!resourceExists(facesContext, "/jquery/plugins/ui/jquery.ui.datepicker.css")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/jquery/plugins/ui/jquery.ui.datepicker.css");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/jquery/jquery.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/jquery/jquery.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/jquery/plugins/ui/jquery-ui.custom.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/jquery/plugins/ui/jquery-ui.custom.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/primefaces/core/core.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/primefaces/core/core.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/primefaces/calendar/calendar.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/primefaces/calendar/calendar.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/primefaces/calendar/calendar_i18n.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/primefaces/calendar/calendar_i18n.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
}
public void handleAttribute(String name, Object value) {
List<String> setAttributes = (List<String>) this.getAttributes().get("javax.faces.component.UIComponentBase.attributesThatAreSet");
if(setAttributes == null) {
String cname = this.getClass().getName();
if(cname != null && cname.startsWith(OPTIMIZED_PACKAGE)) {
setAttributes = new ArrayList<String>(6);
this.getAttributes().put("javax.faces.component.UIComponentBase.attributesThatAreSet", setAttributes);
}
}
if(setAttributes != null) {
if(value == null) {
ValueExpression ve = getValueExpression(name);
if(ve == null) {
setAttributes.remove(name);
} else if(!setAttributes.contains(name)) {
setAttributes.add(name);
}
}
}
}
}