Re: D2W Timestamp attribute component
Re: D2W Timestamp attribute component
- Subject: Re: D2W Timestamp attribute component
- From: Mark Wardle <email@hidden>
- Date: Wed, 19 Jan 2011 21:31:20 +0000
This is probably hopelessly broken but it seems to work for me:
Add the rule "shouldEditTime" for the property.
****************
RSD2WDatePicker.html
<webobject name="ChooseDate"/><webobject
name="ShouldEditTime"><webobject name="Hours"/><webobject
name="Minutes"/></webobject>
****************
RSD2WDatePicker.wod
ChooseDate: AjaxDatePicker {
value = objectPropertyValue;
format = format;
}
ShouldEditTime: WOConditional {
condition = shouldEditTime;
}
Hours: WOPopUpButton {
list = hoursList;
selection = hours;
}
Minutes: WOPopUpButton {
list = minutesList;
selection = minutes;
}
****************
RSD2WDatePicker.java
package com.eldrix.rsd2w;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import com.webobjects.appserver.WOContext;
import com.webobjects.appserver.WORequest;
import com.webobjects.foundation.NSArray;
import com.webobjects.foundation.NSMutableArray;
import com.webobjects.foundation.NSTimestamp;
import com.webobjects.foundation.NSValidation;
import er.directtoweb.components.ERDCustomEditComponent;
import er.extensions.formatters.ERXTimestampFormatter;
/**
* D2W component to edit a date.
* Uses Chuck Hill's AjaxDatePicker
*
* @binding formatter : a text string (format) for the date format
* @binding shouldEditTime : whether to edit the time as well?
*
* FIXME: This doesn't use localisation...
*
* @author mark
* @see er.ajax.AjaxDatePicker
*/
public class RSD2WDatePicker extends ERDCustomEditComponent {
protected String _format;
protected GregorianCalendar _calendar;
protected static final NSArray<String> _hoursList = new
NSMutableArray<String>() {{
for(int i=0; i < 24; i++) {
add(String.format("d", i));
}
}};
protected static final NSArray<String> _minutesList = new
NSMutableArray<String>() {{
for (int i=0; i < 60; i++) {
add(String.format("d", i));
}
}};
public interface Keys extends ERDCustomEditComponent.Keys {
public static final String formatter = "formatter";
public static final String shouldEditTime = "shouldEditTime";
}
public RSD2WDatePicker(WOContext context) {
super(context);
}
public NSArray<String> hoursList() {
return _hoursList;
}
public NSArray<String> minutesList() {
return _minutesList;
}
/**
* Initialises calendar from the current property value.
* @return
*/
public GregorianCalendar calendar() {
if (_calendar == null) {
_calendar = new GregorianCalendar();
_calendar.setLenient(true);
if (date() != null) {
_calendar.setTime(date());
}
}
return _calendar;
}
public String hours() {
return String.format("d", calendar().get(Calendar.HOUR_OF_DAY));
}
public String minutes() {
return String.format("d", calendar().get(Calendar.MINUTE));
}
public void setHours(String hours) {
calendar().set(Calendar.HOUR_OF_DAY, Integer.parseInt(hours));
}
public void setMinutes(String minutes) {
calendar().set(Calendar.MINUTE, Integer.parseInt(minutes));
}
/**
* We try to keep our calendar object up to date with property changes.
*/
@Override public void setObjectPropertyValue(Object value) {
if (value == null || value instanceof Date) {
if (value != null) {
calendar().setTime((Date)value);
}
super.setObjectPropertyValue(value);
}
else throw new IllegalArgumentException("RSD2WDatePicker must be
used with an NSTimestamp");
}
public NSTimestamp date() {
Object o = objectPropertyValue();
if (o == null || o instanceof NSTimestamp) {
return (NSTimestamp) o;
}
throw new IllegalArgumentException("RSD2WDatePicker must be used
with an NSTimestamp");
}
@Override public boolean synchronizesVariablesWithBindings() {
return false;
}
public String format() {
if(_format == null) {
_format = (String)valueForBinding("formatter");
}
if(_format == null || _format.length() == 0) {
_format = ERXTimestampFormatter.DEFAULT_PATTERN;
}
return _format;
}
public void setFormatter(String format) {
_format = format;
}
public boolean shouldEditTime() {
return booleanValueForBinding(Keys.shouldEditTime);
}
public void takeValuesFromRequest (WORequest request, WOContext context) {
super.takeValuesFromRequest (request,context);
if (_calendar != null && objectPropertyValue() != null) {
NSTimestamp date = new NSTimestamp(calendar().getTime());
try {
object().validateTakeValueForKeyPath(date, key());
} catch (NSValidation.ValidationException v) {
parent().validationFailedWithException(v,date,key());
} catch(Exception e) {
parent().validationFailedWithException(e,date,key());
}
}
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden