Author: amarkhel
Date: 2010-10-28 15:05:38 -0400 (Thu, 28 Oct 2010)
New Revision: 19745
Added:
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/events/
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/events/CurrentDateChangeEvent.java
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/events/CurrentDateChangeListener.java
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/taglib/
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/taglib/CalendarHandler.java
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/taglib/MethodExpressionCurrentDateChangeListener.java
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/utils/
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/utils/CalendarHelper.java
Modified:
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/component/AbstractCalendar.java
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java
sandbox/trunk/ui/calendar/ui/src/main/templates/calendar.template.xml
Log:
continue migration; implement base functionality (without CalendarDataModel), move utility
methods to the CalendarHelper class, add currentDateChangeEvent support
Modified:
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/component/AbstractCalendar.java
===================================================================
---
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/component/AbstractCalendar.java 2010-10-28
19:01:39 UTC (rev 19744)
+++
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/component/AbstractCalendar.java 2010-10-28
19:05:38 UTC (rev 19745)
@@ -20,151 +20,150 @@
* 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
*/
-
package org.richfaces.component;
-import java.text.DateFormat;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import javax.faces.FacesException;
-import javax.faces.application.Application;
+import javax.el.ELContext;
+import javax.el.ValueExpression;
+import javax.faces.application.FacesMessage;
import javax.faces.component.UIInput;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
-import javax.faces.convert.Converter;
import javax.faces.convert.DateTimeConverter;
+import javax.faces.event.AbortProcessingException;
+import javax.faces.event.FacesEvent;
+import javax.faces.event.PhaseId;
import org.richfaces.cdk.annotations.Attribute;
import org.richfaces.cdk.annotations.EventName;
import org.richfaces.cdk.annotations.JsfComponent;
import org.richfaces.cdk.annotations.JsfRenderer;
import org.richfaces.cdk.annotations.Tag;
+import org.richfaces.events.CurrentDateChangeEvent;
+import org.richfaces.events.CurrentDateChangeListener;
+import org.richfaces.log.Logger;
+import org.richfaces.log.RichfacesLogger;
+import org.richfaces.utils.CalendarHelper;
/**
* @author amarkhel
- *
+ *
*/
-@JsfComponent(
- type = AbstractCalendar.COMPONENT_TYPE,
- family = AbstractCalendar.COMPONENT_FAMILY,
- generate = "org.richfaces.component.UICalendar",
- renderer = @JsfRenderer(type = "org.richfaces.CalendarRenderer"),
- tag = @Tag(name="calendar")
-)
-public abstract class AbstractCalendar extends UIInput{
-
+@JsfComponent(type = AbstractCalendar.COMPONENT_TYPE, family =
AbstractCalendar.COMPONENT_FAMILY, generate =
"org.richfaces.component.UICalendar", renderer = @JsfRenderer(type =
"org.richfaces.CalendarRenderer"), tag = @Tag(name = "calendar"))
+public abstract class AbstractCalendar extends UIInput {
+
public static final String COMPONENT_TYPE = "org.richfaces.Calendar";
public static final String COMPONENT_FAMILY = "org.richfaces.Calendar";
-
+
public static final String SUB_TIME_PATTERN =
"\\s*[hHkKma]+[\\W&&\\S]+[hHkKma]+\\s*";
-
+
public static final String TIME_PATTERN = "HH:mm";
-
+
public static final String DEFAULT_DATE_PATTERN = "MMM d, yyyy";
-
- @Attribute(defaultValue="MMM d, yyyy")
- public abstract String getDatePattern();
+ Logger log = RichfacesLogger.COMPONENTS.getLogger();
- @Attribute(defaultValue="getDefaultLocale()")
- public abstract Object getLocale();
+ protected enum PropertyKeys {
+ locale
+ };
+
- @Attribute(defaultValue="TimeZone.getDefault()")
+ @Attribute(defaultValue = "MMM d, yyyy")
+ public abstract String getDatePattern();
+
+ @Attribute(defaultValue = "TimeZone.getDefault()")
public abstract TimeZone getTimeZone();
-
- @Attribute(defaultValue="Integer.MIN_VALUE")
+
+ @Attribute(defaultValue = "Integer.MIN_VALUE")
public abstract int getFirstWeekDay();
-
- @Attribute(defaultValue="Integer.MIN_VALUE")
+
+ @Attribute(defaultValue = "Integer.MIN_VALUE")
public abstract int getMinDaysInFirstWeek();
-
- @Attribute(defaultValue="select")
+
+ @Attribute(defaultValue = "select")
public abstract String getTodayControlMode();
-
- @Attribute(defaultValue="true")
+
+ @Attribute(defaultValue = "true")
public abstract boolean isShowWeekDaysBar();
-
- @Attribute(defaultValue="true")
+
+ @Attribute(defaultValue = "true")
public abstract boolean isShowWeeksBar();
-
- @Attribute(defaultValue="true")
+
+ @Attribute(defaultValue = "true")
public abstract boolean isShowFooter();
-
- @Attribute(defaultValue="true")
+
+ @Attribute(defaultValue = "true")
public abstract boolean isShowHeader();
-
- @Attribute(defaultValue="true")
+
+ @Attribute(defaultValue = "true")
public abstract boolean isShowInput();
-
- @Attribute(defaultValue="true")
+
+ @Attribute(defaultValue = "true")
public abstract boolean isPopup();
-
- @Attribute(defaultValue="true")
+
+ @Attribute(defaultValue = "true")
public abstract String getHidePopupOnScroll();
-
- @Attribute(defaultValue="false")
+
+ @Attribute(defaultValue = "false")
public abstract boolean isDisable();
-
- @Attribute(defaultValue="false")
+
+ @Attribute(defaultValue = "false")
public abstract boolean isEnableManualInput();
-
- @Attribute(defaultValue="false")
+
+ @Attribute(defaultValue = "false")
public abstract boolean isDayEnabled();
-
- @Attribute(defaultValue="false")
+
+ @Attribute(defaultValue = "false")
public abstract boolean isShowApplyButton();
-
- @Attribute(defaultValue="false")
+
+ @Attribute(defaultValue = "false")
public abstract boolean isResetTimeOnDateSelect();
-
- @Attribute(defaultValue="AA")
+
+ @Attribute(defaultValue = "AA")
public abstract String getJointPoint();
-
- @Attribute(defaultValue="AA")
+
+ @Attribute(defaultValue = "AA")
public abstract String getDirection();
-
- @Attribute(defaultValue="inactive")
+
+ @Attribute(defaultValue = "inactive")
public abstract String getBoundaryDatesMode();
-
- @Attribute(defaultValue="0")
+
+ @Attribute(defaultValue = "0")
public abstract String getHorizontalOffset();
-
- @Attribute(defaultValue="0")
+
+ @Attribute(defaultValue = "0")
public abstract String getVerticalOffsetOffset();
-
- @Attribute(defaultValue="3")
+
+ @Attribute(defaultValue = "3")
public abstract int getZindex();
-
+
@Attribute
public abstract String getStyle();
-
+
@Attribute
public abstract Object getMonthLabels();
-
+
@Attribute
public abstract Object getMonthLabelsShort();
-
+
@Attribute
public abstract Object getWeekDayLabelsShort();
-
+
@Attribute
public abstract Object getWeekDayLabels();
@Attribute
public abstract String getDayStyleClass();
-
+
@Attribute
public abstract String getTabindex();
-
+
@Attribute
public abstract String getInputStyle();
@@ -173,235 +172,178 @@
@Attribute
public abstract String getInputClass();
-
+
@Attribute
public abstract String getButtonLabel();
-
+
@Attribute
public abstract String getInputSize();
-
+
@Attribute
public abstract Object getCurrentDate();
-
+
+ public abstract void setCurrentDate(Object date);
+
@Attribute
public abstract String getButtonIcon();
-
+
@Attribute
public abstract String getButtonIconDisabled();
-
+
@Attribute
public abstract Object getDefaultTime();
-
- @Attribute(events=@EventName("inputclick"))
+
+ @Attribute(events = @EventName("inputclick"))
public abstract String getOninputclick();
- @Attribute(events=@EventName("inputdblclick"))
+ @Attribute(events = @EventName("inputdblclick"))
public abstract String getOninputdblclick();
- @Attribute(events=@EventName("inputchange"))
+ @Attribute(events = @EventName("inputchange"))
public abstract String getOninputchange();
-
- @Attribute(events=@EventName("inputselect"))
+
+ @Attribute(events = @EventName("inputselect"))
public abstract String getOninputselect();
-
- @Attribute(events=@EventName("inputmousedown"))
+
+ @Attribute(events = @EventName("inputmousedown"))
public abstract String getOninputmousedown();
-
- @Attribute(events=@EventName("inputmousemove"))
+
+ @Attribute(events = @EventName("inputmousemove"))
public abstract String getOninputmousemove();
-
- @Attribute(events=@EventName("inputmouseout"))
+
+ @Attribute(events = @EventName("inputmouseout"))
public abstract String getOninputmouseout();
-
- @Attribute(events=@EventName("inputmouseover"))
+
+ @Attribute(events = @EventName("inputmouseover"))
public abstract String getOninputmouseover();
-
- @Attribute(events=@EventName("inputmouseup"))
+
+ @Attribute(events = @EventName("inputmouseup"))
public abstract String getOninputmouseup();
-
- @Attribute(events=@EventName("inputkeydown"))
+
+ @Attribute(events = @EventName("inputkeydown"))
public abstract String getOninputkeydown();
-
- @Attribute(events=@EventName("inputkeypress"))
+
+ @Attribute(events = @EventName("inputkeypress"))
public abstract String getOninputkeypress();
-
- @Attribute(events=@EventName("inputkeyup"))
+
+ @Attribute(events = @EventName("inputkeyup"))
public abstract String getOninputkeypup();
-
- @Attribute(events=@EventName("inputfocus"))
+
+ @Attribute(events = @EventName("inputfocus"))
public abstract String getOninputfocus();
-
- @Attribute(events=@EventName("inputblur"))
+
+ @Attribute(events = @EventName("inputblur"))
public abstract String getOninputblur();
-
- public Calendar getCalendar() {
- return Calendar.getInstance(getTimeZone(), getAsLocale());
- }
-
- public Date getCurrentDateOrDefault() {
- Date date = getAsDate(getCurrentDate());
- if (date == null) {
- date = getAsDate(this.getValue());
-
- if (date == null) {
- TimeZone timeZone = getTimeZone();
- Calendar calendar = timeZone != null ? Calendar.getInstance(timeZone) :
Calendar.getInstance();
- date = calendar.getTime();
+
+ @Attribute
+ public Object getLocale() {
+ Object locale = getStateHelper().eval(PropertyKeys.locale);
+ if (locale == null) {
+ FacesContext facesContext = getFacesContext();
+ UIViewRoot viewRoot = facesContext.getViewRoot();
+ if (viewRoot != null) {
+ locale = viewRoot.getLocale();
}
-
}
- return date;
+ return locale != null ? locale : Locale.US;
}
- public Date getAsDate(Object date) {
- if(date == null) {
- return null;
- }
+ public void setLocale(Object locale) {
+ getStateHelper().put(PropertyKeys.locale, locale);
+ }
- Date value = null;
- FacesContext facesContext = getFacesContext();
- if(date instanceof Date) {
- value = (Date)date;
-
- } else if(date instanceof String) {
- DateTimeConverter converter = new DateTimeConverter();
- converter.setPattern(this.getDatePattern());
- converter.setLocale(getAsLocale());
- converter.setTimeZone(this.getTimeZone());
- value = (Date)converter.getAsObject(facesContext, this,(String) date);
-
- } else if(date instanceof Calendar) {
- value = ((Calendar) date).getTime();
-
- } else {
- Converter converter = getConverter();
- if(converter != null) {
- return getAsDate(converter.getAsString(facesContext, this, date));
- }
-
- Application application = facesContext.getApplication();
- converter = application.createConverter(date.getClass());
- if (null != converter) {
- value = getAsDate(converter.getAsString(facesContext, this, date));
+ public void updateCurrentDate(FacesContext facesContext, Object currentDate) {
+ if (facesContext == null) {
+ throw new NullPointerException();
+ }
+ // RF-1073
+ try {
+ ValueExpression ve = getValueExpression("currentDate");
+ if (ve != null) {
+ ELContext elContext = facesContext.getELContext();
+ if (ve.getType(elContext).equals(String.class)) {
+ DateTimeConverter convert = new DateTimeConverter();
+ convert.setLocale(CalendarHelper.getAsLocale(facesContext, this,
getLocale()));
+ convert.setPattern(getDatePattern());
+ ve.setValue(facesContext.getELContext(),
convert.getAsString(facesContext, this, currentDate));
+ return;
+ } else if (ve.getType(elContext).equals(Calendar.class)) {
+ Calendar c = Calendar.getInstance();
+ c.setTime((Date) currentDate);
+ ve.setValue(elContext, c);
+ return;
+ } else {
+ ve.setValue(elContext, currentDate);
+ return;
+ }
} else {
- throw new FacesException("Wrong attibute type or there is no
converter for custom attibute type");
+ setCurrentDate(currentDate);
}
-
+
+ } catch (Exception e) {
+ setValid(false);
+ if (log.isDebugEnabled()) {
+ log.debug(" updateCurrentDate method throws exception: " +
e.toString(), e);
+ }
+
+ String messageString = e.toString();
+ FacesMessage message = new FacesMessage(messageString);
+ message.setSeverity(FacesMessage.SEVERITY_ERROR);
+ facesContext.addMessage(getClientId(facesContext), message);
}
-
- return value;
}
-
- public Locale getAsLocale() {
- Object locale = getLocale();
- return getAsLocale(locale);
+
+ public void addCurrentDateChangeListener(CurrentDateChangeListener listener) {
+ addFacesListener(listener);
}
-
- protected Locale getDefaultLocale() {
- FacesContext facesContext = FacesContext.getCurrentInstance();
- if (facesContext != null) {
- UIViewRoot viewRoot = facesContext.getViewRoot();
- if (viewRoot != null) {
- Locale locale = viewRoot.getLocale();
- if (locale != null) {
- return locale;
- }
- }
- }
- return Locale.US;
+
+ public void removeToggleListener(CurrentDateChangeListener listener) {
+ removeFacesListener(listener);
}
- public Locale getAsLocale(Object locale) {
-
- if(locale == null) {
- return null;
- }
-
- Locale localeValue = null;
- if (locale instanceof Locale) {
- localeValue = (Locale)locale;
+ public CurrentDateChangeListener[] getCurrentDateChangeListeners() {
+ return (CurrentDateChangeListener[])
getFacesListeners(CurrentDateChangeListener.class);
+ }
- } else if (locale instanceof String) {
- localeValue = parseLocale((String) locale);
+ public void broadcast(FacesEvent event) throws AbortProcessingException {
+ if (event instanceof CurrentDateChangeEvent) {
+ FacesContext facesContext = getFacesContext();
+ CurrentDateChangeEvent currentDateChangeEvent = (CurrentDateChangeEvent)
event;
+ String currentDateString = currentDateChangeEvent.getCurrentDateString();
- } else {
- FacesContext context = FacesContext.getCurrentInstance();
- Application application = context.getApplication();
- Converter converter = application .createConverter(locale.getClass());
-
- if (null != converter) {
- localeValue = parseLocale(converter.getAsString(context, this, locale));
- } else {
- throw new FacesException("Wrong locale attibute type or there is no
converter for custom attibute type");
+ try {
+ // we should use datePattern attribute-based converter only for
+ // selectedDate
+ // current date string always has predefined format: m/y
+ Date currentDate = CalendarHelper.getAsDate(facesContext, this,
getCurrentDate());
+ Date submittedCurrentDate =
CalendarHelper.convertCurrentDate(currentDateString);
+ currentDateChangeEvent.setCurrentDate(submittedCurrentDate);
+
+ if (!submittedCurrentDate.equals(currentDate)) {
+ updateCurrentDate(facesContext, submittedCurrentDate);
+ }
+
+ } catch (Exception e) {
+ if (log.isDebugEnabled()) {
+ log.debug(" currentDate convertion fails with following
exception: " + e.toString(), e);
+ }
+ setValid(false);
+ String messageString = e.toString();
+ FacesMessage message = new FacesMessage(messageString);
+ message.setSeverity(FacesMessage.SEVERITY_ERROR);
+ facesContext.addMessage(getClientId(facesContext), message);
+ facesContext.renderResponse();
}
+ } else {
+ super.broadcast(event);
}
- return localeValue;
}
-
- public Locale parseLocale(String localeStr){
- if(null==localeStr || localeStr.trim().length() < 2) {
- return Locale.getDefault();
- }
-
- //Lookup index of first '_' in string locale representation.
- int index1 = localeStr.indexOf("_");
- //Get first charters (if exist) from string
- String language = null;
- if(index1!=-1){
- language = localeStr.substring(0, index1);
- }else{
- return new Locale(localeStr);
- }
-
- //Lookup index of second '_' in string locale representation.
- int index2 = localeStr.indexOf("_", index1+1);
- String country = null;
- if(index2!=-1){
- country = localeStr.substring(index1+1, index2);
- String variant = localeStr.substring(index2+1);
- return new Locale(language, country, variant);
- }else{
- country = localeStr.substring(index1+1);
- return new Locale(language, country);
- }
- }
-
- public Date getFormattedDefaultTime() {
- Object defaultTime = getDefaultTime();
- if(defaultTime == null) {
- return null;
+ @Override
+ public void queueEvent(FacesEvent event) {
+ super.queueEvent(event);
+ if (event instanceof CurrentDateChangeEvent) {
+ event.setPhaseId(PhaseId.PROCESS_VALIDATIONS);
}
-
- Date result = null;
- if (defaultTime instanceof Calendar) {
- result = ((Calendar) defaultTime).getTime();
-
- } else if (defaultTime instanceof Date) {
- result = (Date) defaultTime;
-
- } else {
- String defaultTimeString = defaultTime.toString();
- String datePattern = getDatePattern();
-
- String timePattern = TIME_PATTERN;
- Pattern pattern = Pattern.compile(timePattern);
- Matcher matcher = pattern.matcher(datePattern);
-
- String subTimePattern = SUB_TIME_PATTERN;
- if(matcher.find()) {
- subTimePattern = matcher.group().trim();
- }
-
- DateFormat format = new SimpleDateFormat(subTimePattern);
- try {
- result = format.parse(defaultTimeString);
- } catch (ParseException parseException) {
- // log??
- result = null;
- }
- }
-
- return result;
}
}
Added:
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/events/CurrentDateChangeEvent.java
===================================================================
---
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/events/CurrentDateChangeEvent.java
(rev 0)
+++
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/events/CurrentDateChangeEvent.java 2010-10-28
19:05:38 UTC (rev 19745)
@@ -0,0 +1,61 @@
+/**
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.richfaces.events;
+
+import java.util.Date;
+
+import javax.faces.component.UIComponent;
+import javax.faces.event.FacesEvent;
+import javax.faces.event.FacesListener;
+
+public class CurrentDateChangeEvent extends FacesEvent {
+
+ private static final long serialVersionUID = -8169207286087810907L;
+ private Date currentDate = null;
+ private String currentDateString = null;
+
+ public CurrentDateChangeEvent(UIComponent component, String curentDateString) {
+ super(component);
+ this.currentDateString = curentDateString;
+ }
+
+ public boolean isAppropriateListener(FacesListener listener) {
+ return false;
+ }
+
+ public void processListener(FacesListener listener) {
+ // TODO Auto-generated method stub
+ throw new UnsupportedOperationException();
+ }
+
+ public Date getCurrentDate() {
+ return currentDate;
+ }
+
+ public void setCurrentDate(Date currentDate) {
+ this.currentDate = currentDate;
+ }
+
+ public String getCurrentDateString() {
+ return currentDateString;
+ }
+
+}
Added:
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/events/CurrentDateChangeListener.java
===================================================================
---
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/events/CurrentDateChangeListener.java
(rev 0)
+++
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/events/CurrentDateChangeListener.java 2010-10-28
19:05:38 UTC (rev 19745)
@@ -0,0 +1,36 @@
+/**
+ * License Agreement.
+ *
+ * JBoss RichFaces - Ajax4jsf Component Library
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.events;
+
+import javax.faces.event.FacesListener;
+
+/**
+ * @author Alexej Kushunin - akushunin(a)exadel.com
+ * created 28.08.2007
+ *
+ * Base calendar events interface
+ */
+public interface CurrentDateChangeListener extends FacesListener {
+
+ public void processCurrentDateChange(CurrentDateChangeEvent event);
+
+}
\ No newline at end of file
Modified:
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java
===================================================================
---
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java 2010-10-28
19:01:39 UTC (rev 19744)
+++
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/renderkit/CalendarRendererBase.java 2010-10-28
19:05:38 UTC (rev 19745)
@@ -52,6 +52,8 @@
import org.richfaces.component.util.MessageUtil;
import org.richfaces.component.util.SelectUtils;
import org.richfaces.component.util.ViewUtil;
+import org.richfaces.events.CurrentDateChangeEvent;
+import org.richfaces.utils.CalendarHelper;
/**
* @author amarkhel
@@ -115,8 +117,6 @@
public static final String OPTION_SELECTED_DATE = "selectedDate";
- public static final String OPTION_SUBMIT_FUNCTION = "submitFunction";
-
public static final String OPTION_DAY_CELL_CLASS = "dayCellClass";
public static final String OPTION_DAY_STYLE_CLASS = "dayStyleClass";
@@ -145,7 +145,10 @@
public static final String CALENDAR_DISABLE_ICON_RESOURCE_NAME =
"disabledCalendarIcon.png";
+ public static final String CURRENT_DATE_INPUT = "InputCurrentDate";
+
+
protected static final Map<String, ComponentAttribute>
CALENDAR_INPUT_HANDLER_ATTRIBUTES =
Collections.unmodifiableMap(ComponentAttribute.createMap(
@@ -196,36 +199,90 @@
private static final String MINUTES_VALUE = "minutes";
+ protected void doDecode(FacesContext context, UIComponent component) {
+ if(!(component instanceof AbstractCalendar)) {
+ return;
+ }
+
+ AbstractCalendar calendar = (AbstractCalendar)component;
+ if (calendar.isDisable()){
+ return;
+ }
+
+ Map<String, String> requestParameterMap =
context.getExternalContext().getRequestParameterMap();
+
+ String clientId = calendar.getClientId(context);
+ String currentDateString = (String) requestParameterMap.get(clientId +
CURRENT_DATE_INPUT);
+ if (currentDateString != null) {
+ calendar.queueEvent(new CurrentDateChangeEvent(calendar,
currentDateString));
+ }
+
+ String selectedDateString = (String) requestParameterMap.get(clientId +
"InputDate");
+ if (selectedDateString != null) {
+ calendar.setSubmittedValue(selectedDateString);
+ }
+ }
public void renderInputHandlers(FacesContext facesContext, UIComponent component)
throws IOException {
RenderKitUtils.renderPassThroughAttributesOptimized(facesContext, component,
CALENDAR_INPUT_HANDLER_ATTRIBUTES);
}
@Override
- public Object getConvertedValue(FacesContext context, UIComponent component, Object
submittedValue) throws ConverterException {
- if ((context == null) || (component == null)) {
+ public Object getConvertedValue(FacesContext facesContext, UIComponent component,
Object submittedValue) throws ConverterException {
+ if ((facesContext == null) || (component == null)) {
throw new NullPointerException();
}
// skip conversion of already converted date
if (submittedValue instanceof Date) {
- return (Date) submittedValue;
+ return (Date)submittedValue;
}
// Store submitted value in the local variable as a string
String newValue = (String) submittedValue;
// if we have no local value, try to get the valueExpression.
AbstractCalendar calendar = (AbstractCalendar) component;
- Converter converter = SelectUtils.getConverterForProperty(context, calendar,
"value");
+ Converter converter = SelectUtils.getConverterForProperty(facesContext, calendar,
"value");
// in case the converter hasn't been set, try to use default
// DateTimeConverter
if (converter == null) {
converter = createDefaultConverter();
}
- setupDefaultConverter(converter, calendar);
+ setupConverter(facesContext, converter, calendar);
+ return converter.getAsObject(facesContext, component, newValue);
+ }
+
+ @Override
+ public String getInputValue(FacesContext facesContext, UIComponent component) {
+ if(!(component instanceof AbstractCalendar)) {
+ return null;
+ }
+
+ AbstractCalendar calendar = (AbstractCalendar) component;
+ String value = (String) calendar.getSubmittedValue();
+ if (value == null) {
+ Object curVal = calendar.getValue();
+ Converter converter = SelectUtils.getConverterForProperty(facesContext,
calendar, "value");
+
+ if(converter == null) {
+ converter = createDefaultConverter();
+ setupConverter(facesContext, converter, calendar);
+ }
+
+ if (converter != null) {
+ value = converter.getAsString(facesContext, calendar, curVal);
+ } else {
+ value = curVal !=null ? curVal.toString() : "";
+ }
+ }
- return converter.getAsObject(context, component, newValue);
+ if (value == null) {
+ value = "";
+ }
+
+ return value;
+
}
public String getButtonIcon(FacesContext facesContext, UIComponent component) {
@@ -251,7 +308,7 @@
if(calendar.isValid()) {
Date date;
Object value = calendar.getValue();
- date = calendar.getAsDate(value);
+ date = CalendarHelper.getAsDate(facesContext, calendar, value);
if(date != null) {
returnValue = formatSelectedDate(calendar.getTimeZone(), date);
}
@@ -276,15 +333,15 @@
}
public Object getCurrentDate(FacesContext facesContext, AbstractCalendar calendar)
throws IOException {
- Date date = calendar.getCurrentDateOrDefault();
+ Date date = CalendarHelper.getCurrentDateOrDefault(facesContext, calendar);
return formatDate(date);
}
- public String getCurrentDateAsString(FacesContext context, UIComponent component)
throws IOException {
+ public String getCurrentDateAsString(FacesContext facesContext, UIComponent
component) throws IOException {
AbstractCalendar calendar = (AbstractCalendar)component;
Format formatter = new SimpleDateFormat("MM/yyyy");
- Date currentDate = calendar.getCurrentDateOrDefault();
+ Date currentDate = CalendarHelper.getCurrentDateOrDefault(facesContext,
calendar);
return formatter.format(currentDate);
}
@@ -322,28 +379,29 @@
String dayStyleClass = calendar.getDayStyleClass();
return ((dayStyleClass != null && dayStyleClass.trim().length() != 0)) ?
new JSReference(dayStyleClass) : null;
}
-
+
+
public Map<String, Object> getLabels(FacesContext facesContext,
AbstractCalendar calendar) {
ResourceBundle bundle1 = null;
ResourceBundle bundle2 = null;
- Object locale = calendar.getAsLocale();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String messageBundle = facesContext.getApplication().getMessageBundle();
+ Locale locale = CalendarHelper.getAsLocale(facesContext, calendar);
if (null != messageBundle) {
- bundle1 = ResourceBundle.getBundle(messageBundle,
calendar.getAsLocale(locale), loader);
+ bundle1 = ResourceBundle.getBundle(messageBundle,locale , loader);
}
try {
- bundle2 = ResourceBundle.getBundle(CALENDAR_BUNDLE,
calendar.getAsLocale(locale), loader);
+ bundle2 = ResourceBundle.getBundle(CALENDAR_BUNDLE, locale, loader);
} catch (MissingResourceException e) {
//No external bundle was found, ignore this exception.
}
-
+
ResourceBundle [] bundles = {bundle1, bundle2};
- String [] test = new String []{};
String[] names = {"apply", "today", "clean",
"cancel", "ok", "close"};
+
return getCollectedLabels(bundles, names);
}
@@ -370,49 +428,12 @@
}
return labels;
}
-
- public Object getSubmitFunction(FacesContext context, AbstractCalendar calendar)
throws IOException {
- /*
- if
(!UICalendar.AJAX_MODE.equals(calendar.getAttributes().get("mode")))
- return null;
-
- JSFunction ajaxFunction = AjaxRendererUtils.buildAjaxFunction(calendar, context,
- AjaxRendererUtils.AJAX_FUNCTION_NAME);
- ajaxFunction.addParameter(JSReference.NULL);
-
- HashMap<String, Object> params = new HashMap<String, Object>();
- params.put(calendar.getClientId(context) + CURRENT_DATE_PRELOAD, Boolean.TRUE);
-
- Map<String, Object> options = AjaxRendererUtils.buildEventOptions(context,
calendar, params, true);
- options.put("calendar", JSReference.THIS);
-
- String oncomplete = AjaxRendererUtils.getAjaxOncomplete(calendar);
- JSFunctionDefinition oncompleteDefinition = new JSFunctionDefinition();
- oncompleteDefinition.addParameter("request");
- oncompleteDefinition.addParameter("event");
- oncompleteDefinition.addParameter("data");
- oncompleteDefinition.addToBody("this.calendar.load(data, true);");
- if (oncomplete != null) {
- oncompleteDefinition.addToBody(oncomplete);
- }
-
- options.put("oncomplete", oncompleteDefinition);
- JSReference requestValue = new JSReference("requestValue");
- ajaxFunction.addParameter(options);
- JSFunctionDefinition definition = new JSFunctionDefinition();
- definition.addParameter(requestValue);
- definition.addToBody(ajaxFunction);
- return definition;
- */
-
- return null;
- }
- public Map<String, Object> getPreparedDefaultTime(AbstractCalendar
abstractCalendar) {
- Date date = abstractCalendar.getFormattedDefaultTime();
+ public Map<String, Object> getPreparedDefaultTime(FacesContext facesContext,
AbstractCalendar abstractCalendar) {
+ Date date = CalendarHelper.getFormattedDefaultTime(abstractCalendar);
Map<String, Object> result = new HashMap<String, Object>();
if (date != null) {
- Calendar calendar = abstractCalendar.getCalendar();
+ Calendar calendar = CalendarHelper.getCalendar(facesContext,
abstractCalendar);
calendar.setTime(date);
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
@@ -440,10 +461,10 @@
protected Map<String, Object> getLocaleOptions(FacesContext facesContext,
AbstractCalendar calendarComponent) {
Map<String, Object> map = new HashMap<String, Object>();
- Locale locale = calendarComponent.getAsLocale();
+ Locale locale = CalendarHelper.getAsLocale(facesContext, calendarComponent);
DateFormatSymbols dateFormat = new DateFormatSymbols(locale);
- Calendar calendar = calendarComponent.getCalendar();
+ Calendar calendar = CalendarHelper.getCalendar(facesContext, calendarComponent);
int maximum = calendar.getActualMaximum(Calendar.DAY_OF_WEEK);
int minimum = calendar.getActualMinimum(Calendar.DAY_OF_WEEK);
@@ -505,9 +526,7 @@
public ScriptOptions createCalendarScriptOption(FacesContext facesContext,
UIComponent component) throws IOException {
AbstractCalendar calendar = (AbstractCalendar)component;
-
ScriptOptions scriptOptions = new ScriptOptions(component);
-
scriptOptions.addOption(OPTION_ENABLE_MANUAL_INPUT);
scriptOptions.addOption(OPTION_DISABLED);
scriptOptions.addOption(OPTION_READONLY);
@@ -527,7 +546,6 @@
scriptOptions.addOption(OPTION_VERTICAL_OFFSET);
scriptOptions.addOption(OPTION_CURRENT_DATE, getCurrentDate(facesContext,
calendar));
scriptOptions.addOption(OPTION_SELECTED_DATE, getSelectedDate(facesContext,
calendar));
- scriptOptions.addOption(OPTION_SUBMIT_FUNCTION, getSubmitFunction(facesContext,
calendar));
scriptOptions.addOption(OPTION_DAY_CELL_CLASS, getDayCellClass(facesContext,
calendar));
scriptOptions.addOption(OPTION_DAY_STYLE_CLASS, getDayStyleClass(facesContext,
calendar));
/*
@@ -535,7 +553,7 @@
*<cdk:scriptOption attributes="ondateselected, ondateselect,
ontimeselect, ontimeselected, onchanged, ondatemouseover, ondatemouseout, onexpand,
oncollapse, oncurrentdateselect, oncurrentdateselected"
wrapper="eventHandler" />
* */
scriptOptions.addOption(OPTION_LABELS, getLabels(facesContext, calendar));
- scriptOptions.addOption(OPTION_DEFAULT_TIME, getPreparedDefaultTime(calendar));
+ scriptOptions.addOption(OPTION_DEFAULT_TIME, getPreparedDefaultTime(facesContext,
calendar));
scriptOptions.addOption(OPTION_HIDE_POPUP_ON_SCROLL);
scriptOptions.addOption("showWeekDaysBar");
@@ -545,21 +563,24 @@
return scriptOptions;
}
- public void buildLocaleScript(ResponseWriter writer, FacesContext facesContext,
UIComponent component) throws IOException {
- AbstractCalendar calendar = (AbstractCalendar)component;
- JSFunction function = new JSFunction("RichFaces.ui.Calendar.addLocale",
calendar.getAsLocale(), getLocaleOptions(facesContext, calendar));
- writer.write(function.toScript());
- writer.write(";");
+ public void buildAddLocaleScript(ResponseWriter writer, FacesContext facesContext,
UIComponent component) throws IOException {
+ if(component instanceof AbstractCalendar) {
+ AbstractCalendar calendar = (AbstractCalendar)component;
+ JSFunction function = new
JSFunction("RichFaces.ui.Calendar.addLocale",
CalendarHelper.getAsLocale(facesContext, calendar), getLocaleOptions(facesContext,
calendar));
+ writer.write(function.toScript());
+ writer.write(";");
+ }
}
- public void buildScript(ResponseWriter writer, FacesContext facesContext, UIComponent
component) throws IOException {
- AbstractCalendar calendar = (AbstractCalendar)component;
-
- ScriptOptions scriptOptions = createCalendarScriptOption(facesContext,
calendar);
- JSFunction function = new JSFunction("new RichFaces.ui.Calendar",
calendar.getClientId(facesContext), calendar.getAsLocale(), scriptOptions, "");
- StringBuffer scriptBuffer = new StringBuffer();
- scriptBuffer.append(function.toScript()).append(".load();");
- writer.write(scriptBuffer.toString());
+ public void buildCalendarScript(ResponseWriter writer, FacesContext facesContext,
UIComponent component) throws IOException {
+ if(component instanceof AbstractCalendar) {
+ AbstractCalendar calendar = (AbstractCalendar)component;
+ ScriptOptions scriptOptions = createCalendarScriptOption(facesContext,
calendar);
+ JSFunction function = new JSFunction("new RichFaces.ui.Calendar",
calendar.getClientId(facesContext), CalendarHelper.getAsLocale(facesContext, calendar),
scriptOptions, "");
+ StringBuffer scriptBuffer = new StringBuffer();
+ scriptBuffer.append(function.toScript()).append(".load();");
+ writer.write(scriptBuffer.toString());
+ }
}
public boolean isUseIcons(FacesContext facesContext, UIComponent component) {
@@ -567,24 +588,22 @@
return (label == null || ((String)label).trim().length() == 0);
}
- protected static Converter createDefaultConverter() {
+ protected Converter createDefaultConverter() {
return new DateTimeConverter();
}
- protected static Converter setupDefaultConverter(Converter converter,
AbstractCalendar calendar) {
- // skip id converter is null
- if(converter == null) {
+ protected Converter setupConverter(FacesContext facesContext, Converter converter,
AbstractCalendar calendar) {
+ if(converter == null || calendar == null) {
return null;
}
if(converter instanceof DateTimeConverter) {
DateTimeConverter defaultConverter = (DateTimeConverter) converter;
defaultConverter.setPattern(calendar.getDatePattern());
- defaultConverter.setLocale(calendar.getAsLocale());
+ defaultConverter.setLocale( CalendarHelper.getAsLocale(facesContext,
calendar));
defaultConverter.setTimeZone(calendar.getTimeZone());
}
-
return converter;
}
-
+
}
\ No newline at end of file
Added:
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/taglib/CalendarHandler.java
===================================================================
--- sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/taglib/CalendarHandler.java
(rev 0)
+++
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/taglib/CalendarHandler.java 2010-10-28
19:05:38 UTC (rev 19745)
@@ -0,0 +1,54 @@
+package org.richfaces.taglib;
+
+import javax.faces.view.facelets.ComponentConfig;
+import javax.faces.view.facelets.ComponentHandler;
+import javax.faces.view.facelets.FaceletContext;
+import javax.faces.view.facelets.MetaRule;
+import javax.faces.view.facelets.MetaRuleset;
+import javax.faces.view.facelets.Metadata;
+import javax.faces.view.facelets.MetadataTarget;
+import javax.faces.view.facelets.TagAttribute;
+
+import org.richfaces.component.AbstractCalendar;
+
+public class CalendarHandler extends ComponentHandler{
+
+ private static final CalendarHandlerMetaRule METARULE = new
CalendarHandlerMetaRule();
+
+
+ public CalendarHandler(ComponentConfig config) {
+ super(config);
+ }
+
+ protected MetaRuleset createMetaRuleset(Class type) {
+ MetaRuleset m = super.createMetaRuleset(type);
+ m.addRule(METARULE);
+ return m;
+ }
+
+ static class CalendarHandlerMetaRule extends MetaRule {
+
+ public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget
meta) {
+ if (meta.isTargetInstanceOf(AbstractCalendar.class) &&
"currentDataChangeListener".equals(name)) {
+ return new CalendarMapper(attribute);
+ }
+ return null;
+ }
+
+ }
+
+ static class CalendarMapper extends Metadata {
+
+ private static final Class[] SIGNATURE = new Class[] {
org.richfaces.events.CurrentDateChangeListener.class };
+
+ private final TagAttribute attribute;
+
+ public CalendarMapper(TagAttribute attribute) {
+ this.attribute = attribute;
+ }
+
+ public void applyMetadata(FaceletContext ctx, Object instance) {
+ ((AbstractCalendar) instance).addCurrentDateChangeListener((new
MethodExpressionCurrentDateChangeListener(this.attribute.getMethodExpression(ctx, null,
SIGNATURE))));
+ }
+ }
+}
Added:
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/taglib/MethodExpressionCurrentDateChangeListener.java
===================================================================
---
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/taglib/MethodExpressionCurrentDateChangeListener.java
(rev 0)
+++
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/taglib/MethodExpressionCurrentDateChangeListener.java 2010-10-28
19:05:38 UTC (rev 19745)
@@ -0,0 +1,48 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright ${year}, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.richfaces.taglib;
+
+import javax.el.MethodExpression;
+import javax.faces.context.FacesContext;
+
+import org.richfaces.events.CurrentDateChangeEvent;
+import org.richfaces.events.CurrentDateChangeListener;
+
+public class MethodExpressionCurrentDateChangeListener implements
CurrentDateChangeListener {
+
+ private MethodExpression methodExpression;
+
+ public MethodExpressionCurrentDateChangeListener() {
+ super();
+ }
+
+ MethodExpressionCurrentDateChangeListener(MethodExpression methodExpression) {
+ super();
+ this.methodExpression = methodExpression;
+ }
+
+ public void processCurrentDateChange(CurrentDateChangeEvent event) {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ methodExpression.invoke(facesContext.getELContext(), new Object[] { event });
+ }
+}
Added: sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/utils/CalendarHelper.java
===================================================================
--- sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/utils/CalendarHelper.java
(rev 0)
+++
sandbox/trunk/ui/calendar/ui/src/main/java/org/richfaces/utils/CalendarHelper.java 2010-10-28
19:05:38 UTC (rev 19745)
@@ -0,0 +1,235 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.richfaces.utils;
+
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import javax.faces.FacesException;
+import javax.faces.application.Application;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.DateTimeConverter;
+
+import org.richfaces.component.AbstractCalendar;
+
+/**
+ * @author amarkhel
+ *
+ */
+public final class CalendarHelper {
+
+ protected CalendarHelper(){
+ }
+
+ public static Date getAsDate(FacesContext facesContext, AbstractCalendar calendar,
Object date) {
+ if (date == null) {
+ return null;
+ }
+
+ Date value = null;
+
+ if (date instanceof Date) {
+ value = (Date) date;
+ } else if (date instanceof String) {
+ value = convertStringToDate(facesContext, calendar, (String) date);
+ } else if (date instanceof Calendar) {
+ value = ((Calendar) date).getTime();
+ } else {
+ Converter converter = calendar.getConverter();
+ if (converter == null) {
+ Application application = facesContext.getApplication();
+ converter = application.createConverter(date.getClass());
+ if (converter == null) {
+ throw new FacesException("Wrong attibute type or there is no
converter for custom attibute type");
+ }
+ }
+ value = convertStringToDate(facesContext, calendar,
converter.getAsString(facesContext, calendar, date));
+ }
+
+ return value;
+ }
+
+ public static Object getDefaultValueOfDefaultTime(FacesContext facesContext,
AbstractCalendar calendarComponent) {
+ if (calendarComponent == null) {
+ return null;
+ }
+
+ Calendar calendar = getCalendar(facesContext, calendarComponent);
+ calendar.set(Calendar.HOUR_OF_DAY, 12);
+ calendar.set(Calendar.MINUTE, 0);
+ return calendar.getTime();
+ }
+
+ public static Date getFormattedDefaultTime(AbstractCalendar calendar) {
+ if (calendar == null || calendar.getDefaultTime() == null) {
+ return null;
+
+ }
+ Object defaultTime = calendar.getDefaultTime();
+ Date result = null;
+
+ if (defaultTime instanceof Calendar) {
+ result = ((Calendar) defaultTime).getTime();
+
+ } else if (defaultTime instanceof Date) {
+ result = (Date) defaultTime;
+
+ } else {
+ String defaultTimeString = defaultTime.toString();
+ String datePattern = calendar.getDatePattern();
+
+ String timePattern = AbstractCalendar.TIME_PATTERN;
+ Pattern pattern = Pattern.compile(timePattern);
+ Matcher matcher = pattern.matcher(datePattern);
+
+ String subTimePattern = AbstractCalendar.SUB_TIME_PATTERN;
+ if (matcher.find()) {
+ subTimePattern = matcher.group().trim();
+ }
+
+ DateFormat format = new SimpleDateFormat(subTimePattern);
+ try {
+ result = format.parse(defaultTimeString);
+ } catch (ParseException parseException) {
+ // log??
+ result = null;
+ }
+ }
+
+ return result;
+ }
+
+ public static Locale getAsLocale(FacesContext facesContext, AbstractCalendar
calendar) {
+ if (calendar == null || facesContext == null) {
+ return null;
+ }
+
+ Object locale = calendar.getLocale();
+ return getAsLocale(facesContext, calendar, locale);
+ }
+
+ public static Locale getAsLocale(FacesContext facesContext, AbstractCalendar
calendar, Object locale) {
+ if (calendar == null || facesContext == null) {
+ return null;
+ }
+
+ Locale localeValue = null;
+ if (locale instanceof Locale) {
+ localeValue = (Locale) locale;
+
+ } else if (locale instanceof String) {
+ localeValue = parseLocale((String) locale);
+
+ } else if (locale != null) {
+ Application application = facesContext.getApplication();
+ Converter converter = application.createConverter(locale.getClass());
+ if (null != converter) {
+ localeValue = parseLocale(converter.getAsString(facesContext, calendar,
locale));
+ } else {
+ throw new FacesException("Wrong locale attibute type or there is no
converter for custom attibute type");
+ }
+ }
+ return localeValue;
+ }
+
+ public static Locale parseLocale(String localeStr) {
+ if (null == localeStr || localeStr.trim().length() < 2) {
+ return Locale.getDefault();
+ }
+
+ // Lookup index of first '_' in string locale representation.
+ int index1 = localeStr.indexOf("_");
+ // Get first charters (if exist) from string
+ String language = null;
+ if (index1 != -1) {
+ language = localeStr.substring(0, index1);
+ } else {
+ return new Locale(localeStr);
+ }
+
+ // Lookup index of second '_' in string locale representation.
+ int index2 = localeStr.indexOf("_", index1 + 1);
+ String country = null;
+ if (index2 != -1) {
+ country = localeStr.substring(index1 + 1, index2);
+ String variant = localeStr.substring(index2 + 1);
+ return new Locale(language, country, variant);
+ } else {
+ country = localeStr.substring(index1 + 1);
+ return new Locale(language, country);
+ }
+ }
+
+ public static Date convertCurrentDate(String currentDateString) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.clear();
+ calendar.set(Calendar.DATE, 1);
+ int idx = currentDateString.indexOf('/');
+
+ Date date = null;
+ if (idx != -1) {
+ calendar.set(Calendar.MONTH, Integer.parseInt(currentDateString.substring(0,
idx)) - 1);
+ calendar.set(Calendar.YEAR, Integer.parseInt(currentDateString.substring(idx
+ 1)));
+ date = calendar.getTime();
+ }
+ return date;
+ }
+
+ public static Calendar getCalendar(FacesContext facesContext, AbstractCalendar
calendar) {
+ if (calendar == null || facesContext == null) {
+ return Calendar.getInstance();
+ }
+ return Calendar.getInstance(calendar.getTimeZone(), getAsLocale(facesContext,
calendar));
+ }
+
+ public static Date convertStringToDate(FacesContext facesContext, AbstractCalendar
calendar, String date) {
+ DateTimeConverter converter = new DateTimeConverter();
+ converter.setPattern(calendar.getDatePattern());
+ converter.setLocale(getAsLocale(facesContext, calendar));
+ converter.setTimeZone(calendar.getTimeZone());
+ return (Date) converter.getAsObject(facesContext, calendar, date);
+ }
+
+ public static Date getCurrentDateOrDefault(FacesContext facesContext,
AbstractCalendar calendar) {
+ if (calendar == null || facesContext == null) {
+ return null;
+ }
+
+ Date date = getAsDate(facesContext, calendar, calendar.getCurrentDate());
+ if (date == null) {
+ date = getAsDate(facesContext, calendar, calendar.getValue());
+ }
+
+ if (date == null) {
+ date = getCalendar(facesContext, calendar).getTime();
+ }
+ return date;
+ }
+}
Modified: sandbox/trunk/ui/calendar/ui/src/main/templates/calendar.template.xml
===================================================================
--- sandbox/trunk/ui/calendar/ui/src/main/templates/calendar.template.xml 2010-10-28
19:01:39 UTC (rev 19744)
+++ sandbox/trunk/ui/calendar/ui/src/main/templates/calendar.template.xml 2010-10-28
19:05:38 UTC (rev 19745)
@@ -59,15 +59,17 @@
</c:choose>
</c:otherwise>
</c:choose>
- <input type="hidden" id="#{clientId}InputCurrentDate"
style="display:none" autocomplete="off"
name="#{clientId}InputCurrentDate"
value="#{getCurrentDateAsString(facesContext, component)}" />
+ <input type="hidden" id="#{clientId}InputCurrentDate"
style="display:none"
+ autocomplete="off" name="#{clientId}InputCurrentDate"
+ value="#{getCurrentDateAsString(facesContext, component)}" />
</span>
-
<div style="display: none;" id="#{clientId}"></div>
<div style="display: none;" id="#{clientId}Script">
<script type="text/javascript">
<cdk:call
- expression="buildLocaleScript(responseWriter, facesContext, component);"
/>
- <cdk:call expression="buildScript(responseWriter, facesContext,
component);" />
+ expression="buildAddLocaleScript(responseWriter, facesContext,
component);" />
+ <cdk:call
+ expression="buildCalendarScript(responseWriter, facesContext, component);"
/>
</script>
</div>
</cc:implementation>