How to determine if a component in UI has been Updated in Vaadin 8

807 Views Asked by At

In an application based on Vaadin 8 I want to implement a Command that remove the value of all fields. I have done this Using Binder. When user is activating the command a new Bean will be created.

    @Override public void menuSelected(MenuBar.MenuItem selectedItem) {

    controller.createNewBean();
    Page.getCurrent().reload();

}

I want to show a message box to the user which warns that by proceeding all values for all components will be lost. I am using a TabView and there are relatively many components.
I want to show this message only if user has changed a value for at least one component. I wonder if Vaadin has something like UI.getCurrent().isDirty() or another mechanism to determine if user has entered a new value to a component?

Thanks for help

2

There are 2 best solutions below

3
Axel Meier On

You may want to use binder.hasChanges(). This basically checks whether your fields and the underlying bean bound to the fields differ because of user interactions.

From Vaadin's Docs:

Check whether any of the bound fields' values have changed since last explicit call to setBean(Object), readBean(Object), removeBean(), writeBean(Object) or writeBeanIfValid(Object).

See Vaadin's API and Vaadin Doc's chapter Binding Data to Forms for more information.

With Page.getCurrent().reload() you get a new UI and all your components are reset as if you opened a new tab. For resetting your components I would recommend to use binder.readBean(theNewBeanWithFreshValues). This sets the values of the bound fields back to the values of the bean.

1
Youness On

The most effective way that I found was to register a new listener to the binder to get notified of any changes in the component value.

In this listener, I use an atomic integer that gets incremented for every change. Later on, using this atomic integer, I decide if any of the fields have been touched by the user.

Not that, this does not detect changes but it detects the modifications (e.g. the user might change a field value multiple times but set the value to the original value finally).

AtomicInteger counter = new AtomicInteger();
binder.addValueChangeListener((HasValue.ValueChangeListener<Object>) event -> counter.incrementAndGet());

Make sure to reset the counter when required (e.g. at the save time).