You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Stephane Boisson <sb...@gmail.com> on 2007/07/27 12:49:48 UTC
Troubles combining submodels in a FormComponentPanel
Hi,
I am trying to make a ComboDatePicker component composed of three
DropDownChoices (day, month and year).
I overloaded convertInput to combine the year, month and day dropdown
choices into a Date, but I am unable to get proper values..
When I ask for ModelObject, I got null and when I ask for converted input I
got the option index instead of the real date..
I am missing something?
Thank you!
Stéphane
public class ComboDatePicker extends FormComponentPanel {
private Logger log = LoggerFactory.getLogger( ComboDatePicker.class );
private DropDownChoice dayPicker;
private DropDownChoice monthPicker;
private DropDownChoice yearPicker;
private IModel day = new Model();
private IModel month = new Model();
private IModel year = new Model();
public ComboDatePicker(String id) {
super( id );
init();
}
public ComboDatePicker(String id, IModel model) {
super( id, model );
init();
}
private void init() {
setType( Date.class );
add( dayPicker = newDropDownChoice( "day", day, getListDate() ) );
add( monthPicker = newDropDownChoice( "month", month, getListMonth()
) );
add( yearPicker = newDropDownChoice( "year", year, getListYears() )
);
}
private DropDownChoice newDropDownChoice( String id, IModel model,
List<Integer> values ) {
DropDownChoice menu = new DropDownChoice( id, model, values );
menu.setType( Integer.class );
menu.setRequired( true );
return menu;
}
@Override
public boolean checkRequired() {
return true;
}
@Override
protected void onAttach() {
log.debug( "Attaching" );
super.onAttach();
Date date = (Date)getModelObject();
if( date != null ) {
DateTime current = new DateTime( date );
year.setObject ( current.getYear() );
month.setObject( current.getMonthOfYear() );
day.setObject( current.getDayOfMonth() );
}
}
@Override
protected void convertInput() {
log.debug( "Getting converted value" );
// I got null with this
//Integer dayHs = (Integer) dayPicker.getModelObject();
//Integer monthHs = (Integer) monthPicker.getModelObject ();
//Integer yearHs = (Integer) yearPicker.getModelObject();
// I got indexes and not values
Integer dayHs = (Integer) dayPicker.getConvertedInput();
Integer monthHs = (Integer) monthPicker.getConvertedInput();
Integer yearHs = (Integer) yearPicker.getConvertedInput ();
log.debug( "Selected {}-{}-{}", new Integer[]{ yearHs, monthHs,
dayHs } );
try {
DateTime date = new DateTime( yearHs, monthHs, dayHs, 12, 0, 0,
0 );
log.debug( "Selected date={}", date );
setConvertedInput( date.toDate() );
}
catch( Exception e ) {
setConvertedInput( null );
log.warn( "Unable to convert date", e );
// error( getString( " error.badDateFormat" ) );
}
}
// TODO Optimize!
public List<Integer> getListDate() {
List<Integer> days = new ArrayList<Integer>(31);
for( int i = 1; i <= 31; i++ ) {
days.add( Integer.valueOf( i ) );
}
return days;
}
// TODO Optimize!
public List<Integer> getListMonth() {
List<Integer> months = new ArrayList<Integer>(12);
for( int i = 1; i <= 12; i++ ) {
months.add( Integer.valueOf( i ) );
}
return months;
}
// TODO Optimize!
public List<Integer> getListYears() {
List<Integer> years = new ArrayList<Integer>(120);
int currentYear = new DateTime().getYear();
for( int i = 12; i <= 120; i++ ) {
years.add( Integer.valueOf( currentYear - i ) );
}
return years;
}
}
Re: Troubles combining submodels in a FormComponentPanel
Posted by James Law <jl...@umich.edu>.
I think you want to change this:
private IModel day = new Model();
private IModel month = new Model();
private IModel year = new Model();
You probably want to use a PropertyModel, or a CompoundPropertyModel to
bind the widget values to a bean that holds the picked values....
I have never used the generic Model class, but from the javadoc it
appears an empty constructor means you are not setting the model to any
object at all...hence you get null when you ask for the model...
--James
Stephane Boisson wrote:
> Hi,
>
> I am trying to make a ComboDatePicker component composed of three
> DropDownChoices (day, month and year).
>
> I overloaded convertInput to combine the year, month and day dropdown
> choices into a Date, but I am unable to get proper values..
>
> When I ask for ModelObject, I got null and when I ask for converted input I
> got the option index instead of the real date..
>
> I am missing something?
>
> Thank you!
> Stéphane
>
>
> public class ComboDatePicker extends FormComponentPanel {
> private Logger log = LoggerFactory.getLogger( ComboDatePicker.class );
> private DropDownChoice dayPicker;
> private DropDownChoice monthPicker;
> private DropDownChoice yearPicker;
>
> private IModel day = new Model();
> private IModel month = new Model();
> private IModel year = new Model();
>
> public ComboDatePicker(String id) {
> super( id );
> init();
> }
>
> public ComboDatePicker(String id, IModel model) {
> super( id, model );
> init();
> }
>
> private void init() {
> setType( Date.class );
>
> add( dayPicker = newDropDownChoice( "day", day, getListDate() ) );
> add( monthPicker = newDropDownChoice( "month", month, getListMonth()
> ) );
> add( yearPicker = newDropDownChoice( "year", year, getListYears() )
> );
> }
>
> private DropDownChoice newDropDownChoice( String id, IModel model,
> List<Integer> values ) {
> DropDownChoice menu = new DropDownChoice( id, model, values );
> menu.setType( Integer.class );
> menu.setRequired( true );
> return menu;
> }
>
> @Override
> public boolean checkRequired() {
> return true;
> }
>
> @Override
> protected void onAttach() {
> log.debug( "Attaching" );
> super.onAttach();
>
> Date date = (Date)getModelObject();
> if( date != null ) {
> DateTime current = new DateTime( date );
> year.setObject ( current.getYear() );
> month.setObject( current.getMonthOfYear() );
> day.setObject( current.getDayOfMonth() );
> }
> }
>
> @Override
> protected void convertInput() {
> log.debug( "Getting converted value" );
>
> // I got null with this
> //Integer dayHs = (Integer) dayPicker.getModelObject();
> //Integer monthHs = (Integer) monthPicker.getModelObject ();
> //Integer yearHs = (Integer) yearPicker.getModelObject();
>
> // I got indexes and not values
> Integer dayHs = (Integer) dayPicker.getConvertedInput();
> Integer monthHs = (Integer) monthPicker.getConvertedInput();
> Integer yearHs = (Integer) yearPicker.getConvertedInput ();
>
> log.debug( "Selected {}-{}-{}", new Integer[]{ yearHs, monthHs,
> dayHs } );
>
> try {
> DateTime date = new DateTime( yearHs, monthHs, dayHs, 12, 0, 0,
> 0 );
> log.debug( "Selected date={}", date );
> setConvertedInput( date.toDate() );
> }
> catch( Exception e ) {
> setConvertedInput( null );
> log.warn( "Unable to convert date", e );
> // error( getString( " error.badDateFormat" ) );
> }
> }
>
> // TODO Optimize!
> public List<Integer> getListDate() {
> List<Integer> days = new ArrayList<Integer>(31);
>
> for( int i = 1; i <= 31; i++ ) {
> days.add( Integer.valueOf( i ) );
> }
>
> return days;
> }
>
> // TODO Optimize!
> public List<Integer> getListMonth() {
> List<Integer> months = new ArrayList<Integer>(12);
>
> for( int i = 1; i <= 12; i++ ) {
> months.add( Integer.valueOf( i ) );
> }
>
> return months;
> }
>
> // TODO Optimize!
> public List<Integer> getListYears() {
> List<Integer> years = new ArrayList<Integer>(120);
>
> int currentYear = new DateTime().getYear();
>
> for( int i = 12; i <= 120; i++ ) {
> years.add( Integer.valueOf( currentYear - i ) );
> }
>
> return years;
> }
>
> }
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org