Author: akushunin
Date: 2007-10-01 09:12:23 -0400 (Mon, 01 Oct 2007)
New Revision: 3173
Modified:
trunk/ui/calendar/src/main/config/component/calendar.xml
trunk/ui/calendar/src/main/java/org/richfaces/component/UICalendar.java
Log:
RF-1028
Modified: trunk/ui/calendar/src/main/config/component/calendar.xml
===================================================================
--- trunk/ui/calendar/src/main/config/component/calendar.xml 2007-10-01 13:01:32 UTC (rev
3172)
+++ trunk/ui/calendar/src/main/config/component/calendar.xml 2007-10-01 13:12:23 UTC (rev
3173)
@@ -111,10 +111,7 @@
<property elonly="true">
<name>currentDate</name>
<classname>java.util.Date</classname>
- <description>Defines current date</description>
- <defaultvalue>
- java.util.Calendar.getInstance(getTimeZone()).getTime()
- </defaultvalue>
+ <description>Defines current date</description>
</property>
<property>
<name>currentDateChangeListener</name>
Modified: trunk/ui/calendar/src/main/java/org/richfaces/component/UICalendar.java
===================================================================
--- trunk/ui/calendar/src/main/java/org/richfaces/component/UICalendar.java 2007-10-01
13:01:32 UTC (rev 3172)
+++ trunk/ui/calendar/src/main/java/org/richfaces/component/UICalendar.java 2007-10-01
13:12:23 UTC (rev 3173)
@@ -29,6 +29,7 @@
import java.util.Locale;
import java.util.TimeZone;
+import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIInput;
import javax.faces.component.UIViewRoot;
@@ -38,15 +39,12 @@
import javax.faces.el.MethodBinding;
import javax.faces.el.ValueBinding;
import javax.faces.event.AbortProcessingException;
-import javax.faces.event.ActionEvent;
import javax.faces.event.FacesEvent;
-import javax.faces.event.PhaseId;
import javax.faces.event.ValueChangeEvent;
import org.ajax4jsf.component.AjaxComponent;
import org.ajax4jsf.context.AjaxContext;
import org.ajax4jsf.event.AjaxEvent;
-import org.ajax4jsf.event.AjaxSingleEvent;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.richfaces.event.CurrentDateChangeEvent;
@@ -66,13 +64,13 @@
public static final String COMPONENT_TYPE = "org.richfaces.Calendar";
private static final String COMPONENT_FAMILY = "org.richfaces.Calendar";
-
+
public static final String AJAX_MODE = "ajax";
-
- public static final String CLIENT_MODE = "client";
-
+
+ public static final String CLIENT_MODE = "client";
+
private final static Log log = LogFactory.getLog(UICalendar.class);
-
+
public abstract Locale getLocale();
public abstract void setLocale(Locale locale);
@@ -128,7 +126,7 @@
public abstract boolean isPopup();
public abstract void setPopup(boolean popup);
-
+
public abstract boolean isDisabled();
public abstract void setDisabled(boolean disabled);
@@ -140,30 +138,31 @@
public abstract String getToolTipMode();
public abstract void setToolTipMode(String toolTipMode);
-
+
public abstract String getBoundaryDatesMode();
public abstract void setBoundaryDatesMode(String boundaryDatesMode);
-
+
public abstract MethodBinding getCurrentDateChangeListener();
- public abstract void setCurrentDateChangeListener(MethodBinding scrollerListener);
-
+ public abstract void setCurrentDateChangeListener(
+ MethodBinding scrollerListener);
+
public abstract String getMode();
public abstract void setMode(String mode);
-
+
public abstract int getVerticalOffset();
public abstract void setVerticalOffset(int verticalOffset);
-
+
public abstract int getHorizontalOffset();
public abstract void setHorizontalOffset(int horizontalOffset);
-
-
- //TODO onclick add users onclick
+
+ // TODO onclick add users onclick
+
// currentDate processing -------------------------------------------------
public Calendar getCalendar() {
@@ -205,12 +204,13 @@
} else {
setCurrentDate(currentDate);
}
-
+
} catch (Exception e) {
setValid(false);
- //XXX nick - kaa - add log.debug(...)
+ // XXX nick - kaa - add log.debug(...)
if (log.isDebugEnabled()) {
- log.debug(" updateCurrentDate method throws exception: "+e.toString(),e);
+ log.debug(" updateCurrentDate method throws exception: "
+ + e.toString(), e);
}
e.printStackTrace();
String messageString = e.toString();
@@ -221,16 +221,51 @@
}
public Date getCurrentDateOrDefault() {
+
Date date = getCurrentDate();
+
if (date != null) {
return date;
} else {
- Calendar calendar = getCalendar();
- calendar.set(Calendar.DATE, 1);
- return calendar.getTime();
+ Date value = getAsDate(this.getValue());
+ if (value != null) {
+ return value;
+ } else {
+ return java.util.Calendar.getInstance(getTimeZone()).getTime();
+ }
+
}
}
+ public Date getAsDate(Object date) {
+
+ if (date == null) {
+ return null;
+ } else {
+ if (date instanceof Date) {
+ return (Date) date;
+ } else {
+ if (date instanceof String) {
+ DateTimeConverter converter = new DateTimeConverter();
+ converter.setPattern(this.getDatePattern());
+ converter.setLocale(this.getLocale());
+ converter.setTimeZone(this.getTimeZone());
+ FacesContext context = FacesContext.getCurrentInstance();
+ return (Date) converter.getAsObject(context, this,
+ (String) date);
+ } else {
+ if (date instanceof Calendar) {
+ return ((Calendar) date).getTime();
+ } else {
+
+ throw new FacesException("Wrong attibute type");
+ }
+ }
+ }
+ }
+
+ }
+
public Object getTooltip(Date date) {
CalendarDataModel calendarDM = (CalendarDataModel) getDataModel();
@@ -289,14 +324,16 @@
calendar.clear();
calendar.set(Calendar.DATE, 1);
int idx = currentDateString.indexOf('/');
- if (idx != -1){
- calendar.set(Calendar.MONTH, Integer.parseInt(currentDateString
- .substring(0, idx)) - 1);
- calendar.set(Calendar.YEAR, Integer.parseInt(currentDateString
- .substring(idx + 1)));
+ if (idx != -1) {
+ calendar.set(Calendar.MONTH, Integer.parseInt(currentDateString
+ .substring(0, idx)) - 1);
+ calendar.set(Calendar.YEAR, Integer.parseInt(currentDateString
+ .substring(idx + 1)));
- return calendar.getTime();
- }else {return null;}
+ return calendar.getTime();
+ } else {
+ return null;
+ }
}
@@ -307,7 +344,9 @@
AjaxContext ajaxContext = AjaxContext
.getCurrentInstance(facesContext);
ajaxContext.addRegionsFromComponent(this);
- if(getPreload()!=null){ajaxContext.setResponseData(getPreload());}
+ if (getPreload() != null) {
+ ajaxContext.setResponseData(getPreload());
+ }
} else {
if (event instanceof CurrentDateChangeEvent) {
FacesContext facesContext = FacesContext.getCurrentInstance();
@@ -327,36 +366,40 @@
// method
// for more
- //XX nick - kaa - throw exception and review resulting message :)
-
+ // XX nick - kaa - throw exception and review resulting
+ // message :)
+
Date currentDate = convertCurrentDate(currentDateString);
CurrentDateChangeEvent newDateChangeEvent = new CurrentDateChangeEvent(
this, currentDate);
newDateChangeEvent.queue();
MethodBinding binding = getCurrentDateChangeListener();
-
- if(binding!=null){
-
- binding.invoke(facesContext, new Object[]{event});
+
+ if (binding != null) {
+
+ binding
+ .invoke(facesContext,
+ new Object[] { event });
}
-
-
-
- } catch (Exception e) {
+
+ } catch (Exception e) {
// XXX nick - kaa - add log.debug(...)
// XXX nick - kaa - we should stop processing on exc.
// setValid(false) and then call
// FacesContext.renderResponse(...)
// update model phase shouldn't start
if (log.isDebugEnabled()) {
- log.debug(" currentDate convertion fails with following exception:
"+e.toString(),e);
+ log.debug(
+ " currentDate convertion fails with following exception: "
+ + e.toString(), e);
}
setValid(false);
String messageString = e.toString();
e.printStackTrace();
- FacesMessage message = new FacesMessage(messageString);
+ FacesMessage message = new FacesMessage(messageString);
message.setSeverity(FacesMessage.SEVERITY_ERROR);
- facesContext.addMessage(getClientId(facesContext),message);
+ facesContext.addMessage(getClientId(facesContext),
+ message);
facesContext.renderResponse();
}
@@ -391,8 +434,7 @@
HashMap args = new HashMap();
args.put("startDate", CalendarRendererBase
.formatDate(preloadDateRange[0]));
- args
- .put("days", calendarDataModelItems);
+ args.put("days", calendarDataModelItems);
return args;
}
}
@@ -403,37 +445,37 @@
public Date[] getPreloadDateRange() {
Date dateRangeBegin = this.getPreloadDateRangeBegin();
Date dateRangeEnd = this.getPreloadDateRangeEnd();
-
- if(dateRangeBegin==null&&dateRangeEnd==null){
+
+ if (dateRangeBegin == null && dateRangeEnd == null) {
return null;
- }
- else{
- if (dateRangeBegin.after(dateRangeEnd)) {
- // XXX add message
- FacesMessage message = new FacesMessage("preloadDateRangeBegin is greater than
preloadDateRangeEnd");
- message.setSeverity(FacesMessage.SEVERITY_ERROR);
- FacesContext context = FacesContext.getCurrentInstance();
- context.addMessage(getClientId(context), message);
- throw new IllegalArgumentException();
- }
+ } else {
+ if (dateRangeBegin.after(dateRangeEnd)) {
+ // XXX add message
+ FacesMessage message = new FacesMessage(
+ "preloadDateRangeBegin is greater than preloadDateRangeEnd");
+ message.setSeverity(FacesMessage.SEVERITY_ERROR);
+ FacesContext context = FacesContext.getCurrentInstance();
+ context.addMessage(getClientId(context), message);
+ throw new IllegalArgumentException();
+ }
- List dates = new ArrayList();
+ List dates = new ArrayList();
- Calendar calendar = Calendar.getInstance(this.getTimeZone(), this
- .getLocale());
- Calendar calendar2 = (Calendar) calendar.clone();
- calendar.setTime(dateRangeBegin);
- calendar2.setTime(dateRangeEnd);
+ Calendar calendar = Calendar.getInstance(this.getTimeZone(), this
+ .getLocale());
+ Calendar calendar2 = (Calendar) calendar.clone();
+ calendar.setTime(dateRangeBegin);
+ calendar2.setTime(dateRangeEnd);
- do {
- dates.add(calendar.getTime());
- calendar.add(Calendar.DATE, 1);
- } while (!calendar.after(calendar2));
+ do {
+ dates.add(calendar.getTime());
+ calendar.add(Calendar.DATE, 1);
+ } while (!calendar.after(calendar2));
- return (Date[]) dates.toArray(new Date[dates.size()]);
+ return (Date[]) dates.toArray(new Date[dates.size()]);
+ }
}
- }
-
+
public void addCurrentDateChangeListener(CurrentDateChangeListener listener) {
addFacesListener(listener);
}
@@ -442,8 +484,9 @@
return (CurrentDateChangeListener[])
getFacesListeners(CurrentDateChangeListener.class);
}
- public void removeCurrentDateChangeListener(CurrentDateChangeListener listener) {
+ public void removeCurrentDateChangeListener(
+ CurrentDateChangeListener listener) {
removeFacesListener(listener);
- }
-
+ }
+
}