How to set default (custom) ui component for entity attribute?

I have integer entity attributes with values 0 and 1. I need handle them like boolean.
I create custom checkbox component as described here
How to set this component as default for entity attribute for browser and editor screens ?

I found that “column” has “generator” attribute and I need create (in browser screen controller) one method for every entity attribute which I want display as CustomCheckBox for Integer 0/1 value or use
addGeneratedColumn().
Also I need change TextField to CustomCheckBox for every such attribute in edit screen.
I must don’t forget do this every time I create every screen :frowning:
Moreover, Filter component know nothing about my wish to handle Integer entity attribute as Boolean…

I noticed that changing Integer 0/1 entity attributes to Boolean solve all my problem: checkbox in browser screen, checkbox in editor screen, Yes/No in filter. Entity creates and updates as expected.

Is this good decision to change type of entity attribute or may be some drawbacks ?

P.S. Entity generated from additional data store with schema update disabled. So I can’t change database column type.

Hello,

Take a look at Pluggable Component Factories. Component Factory works when component should be generated for Table, DataGrid, Form. You can check the Entity and property the component is generated for and replace it with your custom. Unfortunately, for Filter it does not work.

For instance:

@Order(50)
@org.springframework.stereotype.Component(MyComponentGenerationStrategy.NAME)
public class MyComponentGenerationStrategy implements ComponentGenerationStrategy {

    public static final String NAME = "app_MyComponentGenerationStrategy";

    @Inject
    private Metadata metadata;
    @Inject
    private UiComponents uiComponents;

    @Nullable
    @Override
    public Component createComponent(ComponentGenerationContext context) {
        String property = context.getProperty();
        MetaClass customerMetaClass = metadata.getClassNN(Customer.class);

        // Check the specific field of the Customer entity
        // and that the component is created for the Form or Table/DataGrid components
        if(customerMetaClass.equals(context.getMetaClass())
                && "isVip".equals(property)
                && context.getComponentClass() != null
                && (Form.class.isAssignableFrom(context.getComponentClass())
                    || ListComponent.class.isAssignableFrom(context.getComponentClass()))) {

            CustomCheckBox customCheckBox = uiComponents.create(CustomCheckBox.NAME);

            ValueSource valueSource = context.getValueSource();
            if (valueSource != null) {
                //noinspection unchecked
                customCheckBox.setValueSource(valueSource);
            }

            return customCheckBox;
        }

        return null;
    }
}

Factory will create a component for Form if it contains generic <field> element:

<form id="form" dataContainer="customerDc">
    <column width="350px">
        <field id="isVipField" property="isVip"/>
    </column>
</form>

For showing correct values in the Table/DataGrid you can create custom Datatype.

See demo project for more details: demo.zip (93.1 KB)