Author: vbaranov
Date: 2008-04-21 11:09:24 -0400 (Mon, 21 Apr 2008)
New Revision: 8018
Modified:
trunk/ui/calendar/src/main/config/component/calendar.xml
trunk/ui/calendar/src/main/java/org/richfaces/component/UICalendar.java
Log:
http://jira.jboss.com/jira/browse/RF-3106
Modified: trunk/ui/calendar/src/main/config/component/calendar.xml
===================================================================
--- trunk/ui/calendar/src/main/config/component/calendar.xml 2008-04-21 15:05:43 UTC (rev
8017)
+++ trunk/ui/calendar/src/main/config/component/calendar.xml 2008-04-21 15:09:24 UTC (rev
8018)
@@ -116,8 +116,24 @@
<description>Defines date pattern. Default value is "MMM d,
yyyy".</description>
<defaultvalue>"MMM d, yyyy"</defaultvalue>
</property>
+
+ <property>
+ <name>defaultTime</name>
+ <classname>java.lang.Object</classname>
+ <description>Defines time that will be used:
+ 1) to set time when the value is empty
+ 2) to set time when date changes and flag "resetTimeOnDateChange" is
true </description>
+ <defaultvalue>getDefaultValueOfDefaultTime()</defaultvalue>
+ </property>
<property>
+ <name>resetTimeOnDateChange</name>
+ <classname>boolean</classname>
+ <description>If value is true then calendar should change time to defaultTime
for newly-selected dates.</description>
+ <defaultvalue>false</defaultvalue>
+ </property>
+
+ <property>
<name>popup</name>
<classname>boolean</classname>
<description>
Modified: trunk/ui/calendar/src/main/java/org/richfaces/component/UICalendar.java
===================================================================
--- trunk/ui/calendar/src/main/java/org/richfaces/component/UICalendar.java 2008-04-21
15:05:43 UTC (rev 8017)
+++ trunk/ui/calendar/src/main/java/org/richfaces/component/UICalendar.java 2008-04-21
15:09:24 UTC (rev 8018)
@@ -28,9 +28,9 @@
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
-import javax.el.ELException;
-import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
@@ -58,9 +58,9 @@
import org.richfaces.renderkit.CalendarRendererBase;
import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
-// import org.richfaces.renderkit.html.BaseGradient.Data;
-
/**
* JSF component class
*
@@ -104,7 +104,15 @@
public abstract String getDatePattern();
public abstract void setDatePattern(String pattern);
+
+ public abstract Object getDefaultTime();
+ public abstract void setDefaultTime(Object defaultTime);
+
+ public abstract boolean isResetTimeOnDateChange();
+
+ public abstract void setResetTimeOnDateChange(boolean resetTimeOnDateChange);
+
public abstract Object getMonthLabels();
public abstract void setMonthLabels(Object labels);
@@ -231,6 +239,91 @@
return newCurrentDate;
}
+ /**
+ * Returns default value of "defaultTime" attribute
+ * @return default value of "defaultTime" attribute
+ */
+ protected Object getDefaultValueOfDefaultTime() {
+ Calendar calendar = getCalendar();
+ calendar.set(Calendar.HOUR_OF_DAY, 12);
+ calendar.set(Calendar.MINUTE, 0);
+
+ return calendar.getTime();
+ }
+
+ /**
+ * Returns default time as a <code>Date</code> value.
+ * Hours and minutes values should be taken from the returned date.
+ *
+ * @return default time as a <code>Date</code> value
+ */
+ public Date getFormattedDefaultTime() {
+ Date result = null;
+ Object defaultTime = getDefaultTime();
+
+ 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 = "\\s*[hHkKma]+[\\W&&\\S]+[hHkKma]+\\s*";
+ Pattern pattern = Pattern.compile(timePattern);
+ Matcher matcher = pattern.matcher(datePattern);
+
+ String subTimePattern = null;
+ if(matcher.find()) {
+ subTimePattern = matcher.group().trim();
+ }
+
+ DateFormat format = new SimpleDateFormat(subTimePattern);
+
+ try {
+ result = format.parse(defaultTimeString);
+ } catch (ParseException parseException) {
+ // parse exception
+ result = null;
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Returns hours from "defaultTime" attribute
+ * @return hours from "defaultTime" attribute
+ */
+ public int getDefaultTimeHours() {
+ Date date = getFormattedDefaultTime();
+
+ if (date == null) {
+ return 0;
+ }
+
+ Calendar calendar = getCalendar();
+ calendar.setTime(date);
+
+ return calendar.get(Calendar.HOUR_OF_DAY);
+ }
+
+ /**
+ * Returns minutes from "defaultTime" attribute
+ * @return minutes from "defaultTime" attribute
+ */
+ public int getDefaultTimeMinutes() {
+ Date date = getFormattedDefaultTime();
+
+ if (date == null) {
+ return 0;
+ }
+
+ Calendar calendar = getCalendar();
+ calendar.setTime(date);
+
+ return calendar.get(Calendar.MINUTE);
+ }
+
protected void validateValue(FacesContext context, Object newValue) {
// TODO nick - nick - do we need this?
// store converted value in submitted value to ease client-side code's