Author: akushunin
Date: 2007-07-10 10:49:51 -0400 (Tue, 10 Jul 2007)
New Revision: 1579
Modified:
branches/3.0.2/sandbox/calendar/src/main/java/org/richfaces/component/UICalendar.java
Log:
Modified:
branches/3.0.2/sandbox/calendar/src/main/java/org/richfaces/component/UICalendar.java
===================================================================
---
branches/3.0.2/sandbox/calendar/src/main/java/org/richfaces/component/UICalendar.java 2007-07-10
12:35:55 UTC (rev 1578)
+++
branches/3.0.2/sandbox/calendar/src/main/java/org/richfaces/component/UICalendar.java 2007-07-10
14:49:51 UTC (rev 1579)
@@ -30,12 +30,18 @@
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
+import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
+import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UIViewRoot;
+import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.ConverterException;
+import javax.faces.convert.DateTimeConverter;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.FacesEvent;
import javax.faces.render.Renderer;
@@ -46,66 +52,119 @@
import org.richfaces.renderkit.CalendarDataModelItemAdaptor;
import org.richfaces.renderkit.CalendarRendererBase;
+// import org.richfaces.renderkit.html.BaseGradient.Data;
+
/**
* JSF component class
- *
+ *
*/
public abstract class UICalendar extends UIInput {
-
- public static final String COMPONENT_TYPE = "org.richfaces.Calendar";
-
+
+ public static final String COMPONENT_TYPE = "org.richfaces.Calendar";
+
private static final String COMPONENT_FAMILY = "org.richfaces.Calendar";
-
+
public abstract Locale getLocale();
+
public abstract void setLocale(Locale locale);
-
+
public abstract TimeZone getTimeZone();
+
public abstract void setTimeZone(TimeZone timeZone);
public abstract Date getPreloadDateRangeBegin();
+
public abstract void setPreloadDateRangeBegin(Date date);
-
+
public abstract Date getPreloadDateRangeEnd();
+
public abstract void setPreloadDateRangeEnd(Date date);
- public abstract Date getCurrentDate();
- public abstract void setCurrentDate(Date date);
+ // public abstract Date getCurrentDate();
+ // public abstract void setCurrentDate(Date date);
+
public abstract CalendarDataModel getData();
+
public abstract void setData(CalendarDataModel dataModel);
+ // currentDate processing -------------------------------------------------
+
+ private Date currentDate = null;
+
+ public Date getCurrentDate() {
+ return (this.currentDate);
+ }
+
+ public void setCurrentDate(Date currentDate) {
+ this.currentDate = currentDate;
+ }
+
+ public Converter getConverter(FacesContext context, UICalendar calendar) {
+
+ Converter converter = calendar.getConverter();
+ if (converter == null) {
+ DateTimeConverter datetime = new DateTimeConverter();
+ if (context.getViewRoot().getLocale() == null) {
+ datetime.setLocale(getDefaultLocale());
+ } else {
+ datetime.setLocale(context.getViewRoot().getLocale());
+ }
+ datetime.setTimeZone(TimeZone.getDefault());
+ converter = datetime;
+ }
+ return converter;
+ }
+
+ public Object getConvertedValue(FacesContext context,
+ UIComponent component, Object currentDate)
+ throws ConverterException {
+ UICalendar calendar = (UICalendar) component;
+ Converter converter = getConverter(context, calendar);
+ String valueString = (String) currentDate;
+ return converter.getAsObject(context, component, valueString);
+
+ }
+
+ public String getValueAsString(FacesContext context, UIComponent component)
+ throws IOException {
+ UICalendar calendar = (UICalendar) component;
+ Date valueString = calendar.getCurrentDate();
+ return valueString.toString();
+ }
+
+ // --------------------------------------------------------
+
private int getLastDayOfWeek(Calendar calendar) {
int i = calendar.getFirstDayOfWeek();
if (i == calendar.getActualMinimum(Calendar.DAY_OF_WEEK)) {
i = calendar.getActualMaximum(Calendar.DAY_OF_WEEK);
} else {
- i --;
+ i--;
}
-
+
return i;
}
-
+
protected Date getDefaultPreloadBegin(Date date) {
Calendar calendar = Calendar.getInstance(getTimeZone(), getLocale());
calendar.setTime(date);
calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
/*
- //force recalculation
- calendar.getTimeInMillis();
- calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
- */
+ * //force recalculation calendar.getTimeInMillis();
+ * calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
+ */
return calendar.getTime();
}
-
+
protected Date getDefaultPreloadEnd(Date date) {
Calendar calendar = Calendar.getInstance(getTimeZone(), getLocale());
calendar.setTime(date);
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
/*
- //force recalculation
- calendar.getTimeInMillis();
- calendar.set(Calendar.DAY_OF_WEEK, getLastDayOfWeek(calendar));
- */
+ * //force recalculation calendar.getTimeInMillis();
+ * calendar.set(Calendar.DAY_OF_WEEK, getLastDayOfWeek(calendar));
+ */
return calendar.getTime();
}
@@ -120,10 +179,10 @@
}
}
}
-
+
return Locale.US;
}
-
+
protected TimeZone getDefaultTimeZone() {
return TimeZone.getDefault();
}
@@ -133,67 +192,73 @@
super.broadcast(event);
if (event instanceof AjaxEvent) {
FacesContext facesContext = FacesContext.getCurrentInstance();
- AjaxContext ajaxContext = AjaxContext.getCurrentInstance(facesContext);
+ AjaxContext ajaxContext = AjaxContext
+ .getCurrentInstance(facesContext);
ajaxContext.setResponseData(getPreload());
}
}
-
+
public Object getPreload() {
Date[] preloadDateRange = getPreloadDateRange();
if (preloadDateRange != null && preloadDateRange.length != 0) {
CalendarDataModel calendarDataModel = (CalendarDataModel) getData();
if (calendarDataModel != null) {
- CalendarDataModelItem[] calendarDataModelItems =
calendarDataModel.getData(preloadDateRange);
-
+ CalendarDataModelItem[] calendarDataModelItems = calendarDataModel
+ .getData(preloadDateRange);
+
HashMap args = new HashMap();
- args.put("startDate",
CalendarRendererBase.formatDate(preloadDateRange[0]));
- args.put("days", new AdaptingCollection(calendarDataModelItems));
+ args.put("startDate", CalendarRendererBase
+ .formatDate(preloadDateRange[0]));
+ args
+ .put("days", new AdaptingCollection(
+ calendarDataModelItems));
return args;
}
}
return null;
}
-
+
public Date[] getPreloadDateRange() {
Date dateRangeBegin = this.getPreloadDateRangeBegin();
Date dateRangeEnd = this.getPreloadDateRangeEnd();
-
+
if (dateRangeBegin.after(dateRangeEnd)) {
- //TODO add message
+ // TODO add message
throw new IllegalArgumentException();
}
List dates = new ArrayList();
-
- Calendar calendar = Calendar.getInstance(this.getTimeZone(), this.getLocale());
+
+ 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));
-
+
return (Date[]) dates.toArray(new Date[dates.size()]);
}
-
public void encodeAjaxChild(FacesContext context, String path, Set ids,
Set renderedAreas) throws IOException {
Renderer renderer = this.getRenderer(context);
if (renderer instanceof AjaxChildrenEncoder) {
- ((AjaxChildrenEncoder) renderer).encodeAjaxChild(context, path, ids, renderedAreas);
+ ((AjaxChildrenEncoder) renderer).encodeAjaxChild(context, path,
+ ids, renderedAreas);
}
}
}
class AdaptingCollection extends AbstractCollection {
private CalendarDataModelItem[] items;
-
+
public AdaptingCollection(CalendarDataModelItem[] items) {
super();
this.items = items;
@@ -202,8 +267,9 @@
public Iterator iterator() {
return new Iterator() {
private int idx = 0;
+
private CalendarDataModelItemAdaptor adaptor = new CalendarDataModelItemAdaptor();
-
+
public boolean hasNext() {
return idx < items.length;
}
@@ -216,7 +282,7 @@
public void remove() {
throw new UnsupportedOperationException();
}
-
+
};
}
Show replies by date