Helptext from database

Dear Support

In my application I want to populate the helperText from a database.

	@Subscribe
	protected void onInit(InitEvent event) {
		initNotificationField();
	}

	protected void initNotificationField()
	{
		String helpText = dataManager.loadValue("select e.nlsHelp from TNL e where e.nlsGuid = :field", String.class)
				.parameter("field","Contact.salutation.de").one();
		if ( !helpText.isEmpty() ) {
			JmixButton helperButton = createHelperButton();
			helperButton.addClickListener(event ->
					notifications.create(helpText)
							.withCloseable(true)
							.show());
			conSalutationField.setSuffixComponent(helperButton);
		}
	}

In a classic java programm I would extend the textField class with the helperfunctionality.

How to proceed in JMix ?

  • does it make sense to overwrite the textField constructor ?
  • or should i go through the list of all textField’s in the init phase and check if there is help available in the database ?

Thank you for your advise !

Felix

Hi,

Considering that TextField (or any other field) can be bound to different entity attributes, it would be easier to go through the list of all textField’s in the init phase and check if there is help available in the database.

Alternatively, you can extend TypedTextField component, but instead of constructor set helperButton in the setValueSource method, because it lets you to get information about entity attribute, e.g.:

public class TypedTextFieldExt<V> extends TypedTextField<V> {

    @Override
    public void setValueSource(ValueSource<V> valueSource) {
        super.setValueSource(valueSource);
        
        if (valueSource instanceof ContainerValueSource<?,?> containerValueSource) {
            DataManager dataManager = applicationContext.getBean(DataManager.class);
            MetaClass entityMetaClass = containerValueSource.getEntityMetaClass();
            MetaPropertyPath metaPropertyPath = containerValueSource.getMetaPropertyPath();
            
            // ...
        }
    }
}

Register your component as a replacement for the built-in in the main application class:

@Bean
public ComponentRegistration textField() {
    return ComponentRegistrationBuilder.create(TypedTextFieldExt.class)
            .replaceComponent(TypedTextField.class)
            .build();
}

Gleb