You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by nino martinez wael <ni...@gmail.com> on 2011/03/17 19:53:18 UTC

[1.5] Two formcomponents editing the same model?

Hi

Essentially I've made my own datepicker and timepicker using some jquery
components. I am having my two components editing the same model (one edits
time and the other date). Both components extending datetextfield

Problem are if I change time then month/day are reset. Im thinking that it
has something todo with hourdatetextfield are the last component added. I've
tried various things also editing the raw model object. Nothing works..



public class HourDateTextField extends DateTextField {

public HourDateTextField(String id, IModel<Date> model,
DateConverter converter) {
super(id, model, converter);

add(new TimePickerBehavior());
}

@Override
protected void convertInput() {
MutableDateTime dateOld = new MutableDateTime(getModelObject());
super.convertInput();
Date dateNew = this.getConvertedInput();
dateOld.setHourOfDay(dateNew.getHours());
dateOld.setMinuteOfHour(dateNew.getMinutes());
setModelObject(dateOld.toDate());

}

}

public class MonthDayDateTextField extends DateTextField {

public MonthDayDateTextField(String id, IModel<Date> model,
DateConverter converter) {
super(id, model, converter);

add(new DatePickerBehavior());
}

@Override
protected void convertInput() {

MutableDateTime dateOld = new MutableDateTime(getModelObject());
super.convertInput();
Date dateNew = this.getConvertedInput();
dateOld.setDayOfMonth(dateNew.getDay());
dateOld.setMonthOfYear(dateNew.getMonth());
setModelObject(dateOld.toDate());

}

}

Re: [1.5] Two formcomponents editing the same model?

Posted by Igor Vaynberg <ig...@gmail.com>.
exactly the same in 1.4

-igor

On Fri, Mar 18, 2011 at 10:12 AM, xFlasH <rc...@gmail.com> wrote:
> Thanks for this one.
>
> This is a great preview oft 1.5, I haven't yet tried.
>
> What about the same issue in 1.4 ?
>
>
> Thanks again for this one.
>
> -R-
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/1-5-Two-formcomponents-editing-the-same-model-tp3385350p3387832.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: [1.5] Two formcomponents editing the same model?

Posted by xFlasH <rc...@gmail.com>.
Thanks for this one.

This is a great preview oft 1.5, I haven't yet tried.

What about the same issue in 1.4 ?


Thanks again for this one.

-R-

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/1-5-Two-formcomponents-editing-the-same-model-tp3385350p3387832.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: [1.5] Two formcomponents editing the same model?

Posted by nino martinez wael <ni...@gmail.com>.
Thanks, exactly on the spot, as always :)

2011/3/17 Igor Vaynberg <ig...@gmail.com>

> filter your models to preserve their respective parts, for example
> below is a model you would assign to the textfield that edits the date
> part. you would need to build the opposite for your textfield that
> edits the time part.
>
> -igor
>
> /**
>  * Only modifies the date portion of the adapted {@link Date} model
>  *
>  * @author igor
>  *
>  */
> public class DateFilterModel extends AdapterModel<Date, Date>
> {
>        public DateFilterModel(IModel<Date> delegate)
>        {
>                super(delegate);
>        }
>
>        @Override
>        protected Date getObject(IModel<Date> delegate)
>        {
>                Date source = delegate.getObject();
>                return (source != null) ? new Date(source.getTime()) : null;
>        }
>
>        @Override
>        protected void setObject(Date object, IModel<Date> delegate)
>        {
>                if (object == null)
>                {
>                        delegate.setObject(null);
>                }
>                else if (delegate.getObject() == null)
>                {
>                        delegate.setObject(new Date(object.getTime()));
>                }
>                else
>                {
>                        LocalDate src = new LocalDate(object.getTime());
>                        LocalDateTime dest = new
> LocalDateTime(delegate.getObject().getTime()).withYear(src.getYear())
>                                .withMonthOfYear(src.getMonthOfYear())
>                                .withDayOfMonth(src.getDayOfMonth());
>                        delegate.setObject(dest.toDateTime().toDate());
>                }
>
>        }
> }
>
>
> On Thu, Mar 17, 2011 at 11:53 AM, nino martinez wael
> <ni...@gmail.com> wrote:
> > Hi
> >
> > Essentially I've made my own datepicker and timepicker using some jquery
> > components. I am having my two components editing the same model (one
> edits
> > time and the other date). Both components extending datetextfield
> >
> > Problem are if I change time then month/day are reset. Im thinking that
> it
> > has something todo with hourdatetextfield are the last component added.
> I've
> > tried various things also editing the raw model object. Nothing works..
> >
> >
> >
> > public class HourDateTextField extends DateTextField {
> >
> > public HourDateTextField(String id, IModel<Date> model,
> > DateConverter converter) {
> > super(id, model, converter);
> >
> > add(new TimePickerBehavior());
> > }
> >
> > @Override
> > protected void convertInput() {
> > MutableDateTime dateOld = new MutableDateTime(getModelObject());
> > super.convertInput();
> > Date dateNew = this.getConvertedInput();
> > dateOld.setHourOfDay(dateNew.getHours());
> > dateOld.setMinuteOfHour(dateNew.getMinutes());
> > setModelObject(dateOld.toDate());
> >
> > }
> >
> > }
> >
> > public class MonthDayDateTextField extends DateTextField {
> >
> > public MonthDayDateTextField(String id, IModel<Date> model,
> > DateConverter converter) {
> > super(id, model, converter);
> >
> > add(new DatePickerBehavior());
> > }
> >
> > @Override
> > protected void convertInput() {
> >
> > MutableDateTime dateOld = new MutableDateTime(getModelObject());
> > super.convertInput();
> > Date dateNew = this.getConvertedInput();
> > dateOld.setDayOfMonth(dateNew.getDay());
> > dateOld.setMonthOfYear(dateNew.getMonth());
> > setModelObject(dateOld.toDate());
> >
> > }
> >
> > }
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: [1.5] Two formcomponents editing the same model?

Posted by Igor Vaynberg <ig...@gmail.com>.
filter your models to preserve their respective parts, for example
below is a model you would assign to the textfield that edits the date
part. you would need to build the opposite for your textfield that
edits the time part.

-igor

/**
 * Only modifies the date portion of the adapted {@link Date} model
 *
 * @author igor
 *
 */
public class DateFilterModel extends AdapterModel<Date, Date>
{
	public DateFilterModel(IModel<Date> delegate)
	{
		super(delegate);
	}

	@Override
	protected Date getObject(IModel<Date> delegate)
	{
		Date source = delegate.getObject();
		return (source != null) ? new Date(source.getTime()) : null;
	}

	@Override
	protected void setObject(Date object, IModel<Date> delegate)
	{
		if (object == null)
		{
			delegate.setObject(null);
		}
		else if (delegate.getObject() == null)
		{
			delegate.setObject(new Date(object.getTime()));
		}
		else
		{
			LocalDate src = new LocalDate(object.getTime());
			LocalDateTime dest = new
LocalDateTime(delegate.getObject().getTime()).withYear(src.getYear())
				.withMonthOfYear(src.getMonthOfYear())
				.withDayOfMonth(src.getDayOfMonth());
			delegate.setObject(dest.toDateTime().toDate());
		}

	}
}


On Thu, Mar 17, 2011 at 11:53 AM, nino martinez wael
<ni...@gmail.com> wrote:
> Hi
>
> Essentially I've made my own datepicker and timepicker using some jquery
> components. I am having my two components editing the same model (one edits
> time and the other date). Both components extending datetextfield
>
> Problem are if I change time then month/day are reset. Im thinking that it
> has something todo with hourdatetextfield are the last component added. I've
> tried various things also editing the raw model object. Nothing works..
>
>
>
> public class HourDateTextField extends DateTextField {
>
> public HourDateTextField(String id, IModel<Date> model,
> DateConverter converter) {
> super(id, model, converter);
>
> add(new TimePickerBehavior());
> }
>
> @Override
> protected void convertInput() {
> MutableDateTime dateOld = new MutableDateTime(getModelObject());
> super.convertInput();
> Date dateNew = this.getConvertedInput();
> dateOld.setHourOfDay(dateNew.getHours());
> dateOld.setMinuteOfHour(dateNew.getMinutes());
> setModelObject(dateOld.toDate());
>
> }
>
> }
>
> public class MonthDayDateTextField extends DateTextField {
>
> public MonthDayDateTextField(String id, IModel<Date> model,
> DateConverter converter) {
> super(id, model, converter);
>
> add(new DatePickerBehavior());
> }
>
> @Override
> protected void convertInput() {
>
> MutableDateTime dateOld = new MutableDateTime(getModelObject());
> super.convertInput();
> Date dateNew = this.getConvertedInput();
> dateOld.setDayOfMonth(dateNew.getDay());
> dateOld.setMonthOfYear(dateNew.getMonth());
> setModelObject(dateOld.toDate());
>
> }
>
> }
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: [1.5] Two formcomponents editing the same model?

Posted by Jim Pinkham <pi...@gmail.com>.
Hmm... If I were designing a component like this (and for some reason didn't
like the ones out there like datepicker that already do this)  I'd make a
new component that extends panel with 2 separate child controls, each with
their own date models.  Then use a top level model  getter / setter that
just pushes/pulls the 2 child dates independently using the
java.util.Calendar's fields independently.

Hope that helps,
-- Jim.

On Thu, Mar 17, 2011 at 2:53 PM, nino martinez wael <
nino.martinez.wael@gmail.com> wrote:

> Hi
>
> Essentially I've made my own datepicker and timepicker using some jquery
> components. I am having my two components editing the same model (one edits
> time and the other date). Both components extending datetextfield
>
> Problem are if I change time then month/day are reset. Im thinking that it
> has something todo with hourdatetextfield are the last component added.
> I've
> tried various things also editing the raw model object. Nothing works..
>
>
>
> public class HourDateTextField extends DateTextField {
>
> public HourDateTextField(String id, IModel<Date> model,
> DateConverter converter) {
> super(id, model, converter);
>
> add(new TimePickerBehavior());
> }
>
> @Override
> protected void convertInput() {
> MutableDateTime dateOld = new MutableDateTime(getModelObject());
> super.convertInput();
> Date dateNew = this.getConvertedInput();
> dateOld.setHourOfDay(dateNew.getHours());
> dateOld.setMinuteOfHour(dateNew.getMinutes());
> setModelObject(dateOld.toDate());
>
> }
>
> }
>
> public class MonthDayDateTextField extends DateTextField {
>
> public MonthDayDateTextField(String id, IModel<Date> model,
> DateConverter converter) {
> super(id, model, converter);
>
> add(new DatePickerBehavior());
> }
>
> @Override
> protected void convertInput() {
>
> MutableDateTime dateOld = new MutableDateTime(getModelObject());
> super.convertInput();
> Date dateNew = this.getConvertedInput();
> dateOld.setDayOfMonth(dateNew.getDay());
> dateOld.setMonthOfYear(dateNew.getMonth());
> setModelObject(dateOld.toDate());
>
> }
>
> }
>