Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusable DropDownChoice in Wicket Form

In my project I have more than 50 forms, and they are mostly simillar to each other and use the same DropDownChoice component. May I create separate Panel, where I define my DropDownChoice, and after that I will use that Panel in my another forms? Otherwise, how I can implement that situation?

For example

form1 has the next fields:
name(TextField)
surname(TextField)
city(DropDownChoice)

form2 has the next fields:
Code(TextField)
Amount(TextField)
city(again the same DropDownChoice)

I want to make beautiful solution of that approach.

like image 322
BSeitkazin Avatar asked Oct 28 '14 10:10

BSeitkazin


1 Answers

It is better to extend DropDownChoice with your predefined parameters, and not a Panel with real DropDownChoice.

There are at least two advantages of this approach:

  1. You don't need to create separate markup file, as it comes with Panel-approach.
  2. You could use DropDownChoice methods directly. Otherwise, you should forward such methods as Panel's methods, or implement getter method for DDC.

So, it would be better to something like this:

public class CityDropDownChoice extends DropDownChoice<City> // use your own generic
{

    /* define only one constructor, but you can implement another */
    public CityDropDownChoice ( final String id )
    {
        super ( id );

        init();
    }

    /* private method to construct ddc according to your will */
    private void init ()
    {        
        setChoices ( /* Your cities model or list of cities, fetched from somewhere */ );
        setChoiceRenderer ( /*  You can define default choice renderer */ );

        setOutputMarkupId ( true );

        /* maybe add some Ajax behaviors */
        add(new AjaxEventBehavior ("change")
        {
            @Override
            protected void onEvent ( final AjaxRequestTarget target )
            {
                onChange ( target );
            }
        });
    }

    /*in some forms you can override this method to do something
      when choice is changed*/
    protected void onChange ( final AjaxRequestTarget target )
    {
        // override to do something.
    }
}

And in your forms simply use:

Form form = ...;
form.add ( new CityDropDownChoice ( "cities" ) );

Think that this approach will suit your needs.

like image 169
Michael Zhavzharov Avatar answered Oct 18 '22 11:10

Michael Zhavzharov