ComboBox in FormFieldFactory

Hi guys,

I started using Vaadin for a project recently, and ecountered such an issue with ComboBox in a class implements FormFieldFactory. The situation is like this.

There is a Form made to display Company bean, Company has a CompanyType bean. So when it comes to the edit Company screen I would like to have a ComboBox filled with possible CompanyTypes, and showing the default CompanyType of the selected Company. Simple scenario.

So I used say a class called CompanyFormFactory implemented FormFieldFactory. in the createField method I did this


Query result = HibernateUtil.getSessionWithOpenTransaction().createQuery("from CompanyType");
List<CompanyType> l = result.list();		
ComboBox cb = new ComboBox(TextUtil.translate(TextUtil.COLUMN_COMPANY_TYPE_NATURAL), l);
cb.setNewItemsAllowed(false);
Collection<?> itemIds = cb.getItemIds();
//For the sake of showing some value in the combobox I did below
cb.setValue(itemIds.iterator().next());
return cb;

Now I am very sure the List was loaded into the ComboBox because I can open the drop down menu. But there is NO value shown in the combobox after it is rendered.

Exactly same code as above, if I place it into the Application and add the ComboBox anywhere, it would show the first item by default.

Now later I realized in the Form class, there is this in the setItemDataSource method.

        
// Adds all the properties to this form
        for (final Iterator i = propertyIds.iterator(); i.hasNext();) {
            final Object id = i.next();
            final Property property = itemDatasource.getItemProperty(id);
            if (id != null && property != null) {
                final Field f = fieldFactory.createField(itemDatasource, id,
                        this);
                if (f != null) {
                    f.setPropertyDataSource(property);
                    addField(id, f);
                }
            }
        }

Just a guess from me, is this line “f.setPropertyDataSource(property);” made the default value disappear? If so what would be a logical solution to this situation?

Thanks a lot for your help!!!

Kun

Anyone could please help this?

In the FormFieldFactory you are just prepearing the components (your combobox). Later, form binds data source values into this fields, so its correct behaviour that your combobox.setValue is ignored (it is just being overrided on data binding phase)

I see three options:

  1. Set default CompanyType in Company object before passing it to form (before setting bean as form data source)
  2. Use combobox.setNullSelectionItemId to show defaut value if company bean companytype property is null (never tried this, but it should work)
  3. After setting data source into form you can change value of the field: form.getField(“companytype”).setValue(…)

Hi Vyacheslav,

thanks for your reply. I am OK with the combobox having an empty value when creating a new Company. My main concern is when editing an existing company, how to I set the companyType to the correct value (shown in combobox).

When I edit an existing Company, it does have a default (not null) CompanyType linked to it, before I pass it to the form. But still the value gets overwriten by the setPropertyDataSource method.

And the thrid option, in my opinion, it beats the idea of using Databinding in Forms to bind any bean, and use FormFieldFactory to build the actual customized view. This makes my component which contains the Bean-form not generic.

Is there not a way to set the value for the combobox in the FormFieldFactory? Should ComboBox ever be returned in FormFieldFactory? I mean there much be plenty of people met this situation?

Thanks a lot in advance.

You should implement correct CompanyType.equals() method: form tries to set value into combobox, but combobox contains different instance of the same companytype record (default equals couldn’t find the matching value), so combobox become empty.

I agree, but still it’s an option for some cases

Comboboxes and tables may be returned from field factory. I’m using them in my forms.
And yes, i’ve met this situation (wrong “equals”), just didn’t understand your exact problem first time

Great! Thanks a lot, should have thought about it. Thanks for the tip!

I have set the equals to the IDs, but the ID gets changed later. Now I get it.