Editable for a field build with a generator

Hi,

can someone tell me the correct way to put setEditable to true or false for a CustomSearchPickerField built with a generator?

Thank you.

Maurizio.

Hi,

Could you share a code example and show how you create that CustomSearchPickerField ?

1 Like

Here the CustomSearchPickerField class:


public class CustomSearchPickerField
{
    public static Component generate(Datasource datasource, String property, 
                                     CollectionDatasource<?, String> optionsDatasource, ComponentsFactory componentsFactory)
    {
        SearchPickerField searchPickerField = componentsFactory.createComponent(SearchPickerField.class);
        searchPickerField.setDatasource(datasource, property);
        searchPickerField.setOptionsDatasource(optionsDatasource);

        searchPickerField.addLookupAction();
        searchPickerField.addClearAction();
        searchPickerField.addOpenAction();

        return searchPickerField;
    }
}

Here the code used to generate the Component on the AbstracEditor:


public Component generateVen2010Field(Datasource datasource, String property)
 {
        return CustomSearchPickerField.generate(datasource, property, ana10Ds, componentsFactory);
 }

In the file xml define the generator:


<field id="ven2010" generator="generateVen2010Field"></field>

In the AbstracEditor i try to define the field:


@Named("fieldGroup.ven2010")
private CustomSearchPickerField ven2010Field;

And disable it on postInit if the record is stored:


if (PersistenceHelper.isNew(getItem()))
{
    okBtn.setCaption("Save");
    ven21TableCreate.setEnabled(false);

    ven2001Field.setEditable(true);
    ven2002Field.setEditable(true);
    ven2003Field.setEditable(true);
    ven2040Field.setEditable(true);

    if ( ven2010Field != null )
        ((SearchPickerField)ven2010Field).setEditable(true);
}
else
{
    okBtn.setCaption("Save and close");
    ven21TableCreate.setEnabled(true);

    ven2001Field.setEditable(false);
    ven2002Field.setEditable(false);
    ven2003Field.setEditable(false);
    ven2040Field.setEditable(false);

    if ( ven2010Field != null )
        ((SearchPickerField)ven2010Field).setEditable(false);
}

The other fields works well, ven2010Field is always enabled.

Thank you Yuriy

I’ve checked your example and found one small problem - you inject field created programmatically using generator. In your case you have to get that component using API of Window instead of @Inject.


SearchPickerField searchPickerField = (SearchPickerField) getComponentNN("fieldGroup.fieldGroup");

That issue occurs because of initialization order of a Window, first we inject all required fields and then we call your code to generate FieldGroup fields programmatically. That allows you to use already injected Components and infrastructure beans in your code.

Perfect Yuriy, works like a charm!

Maurizio.