Search code examples
javavaadinimmutabilityvaadin8

How do can I edit List of Immutable objects in Vaadin?


MyObj is immutable so it is impossible to edit them, however I should be able to swap them in the list. How should I achieve this?

Here is my Vaadin grid:

List<MyOBj> data = new ArrayList<>();
data.add(new MyObj(..));
data.add(new MyObj(..));
Grid<MyOBj> grid = new Grid<>(MyOBj.class);
grid.setItems(data);
grid.getEditor().setEnabled(true);
grid.addColumn(MyObj::someField).setCaption("someField");

Would MyObj mutable I would just bind the editor:

Binder<MyOBj> binder = grid.getEditor().getBinder();
Binding<MyOBj, Boolean> binding = binder.bind(..binding setters/getter..);
column.setEditorBinding(binding);

Now I am looking for something like this:

Binder<MyOBj> binder = grid.getEditor().getBinder();
Binding<MyOBj, Boolean> binding = binder.bind(..creating new Object and put on the given index of the list..);
column.setEditorBinding(binding);

Is that possible somehow?


Solution

  • Two general solutions form the top of my mind:

    1. Define a mutable DTO that you use while the object is being edited. Once the user saves their edit, you create a new immutable MyObj instance based on the current values in the DTO.
    2. Change your Binder to edit a reference to a MyObj instead of directly editing a MyOjb. A practical way of doing this, albeit with a small semantic mismatch, is to do Binder<AtomcReference<MyObj>>.