5

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.

BSeitkazin
  • 2,889
  • 25
  • 40

1 Answers1

5

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.

Michael Zhavzharov
  • 1,727
  • 13
  • 26
  • 2
    And to turn it around, if you **do** want special markup in your drop down, `Panel` is the way to go. – biziclop Oct 28 '14 at 14:53