You cannot interact with UI in the PreCommitEvent
- it’s too late in the request lifecycle.
You have to pause the commit process, show a dialog and run the task, then resume the process. There are two options for this.
The first one is to use BeforeCommitChangesEvent
and its preventCommit()
and resume()
methods:
@UiController("Customer.edit")
@UiDescriptor("customer-edit.xml")
@EditedEntityContainer("customerDc")
public class CustomerEdit extends StandardEditor<Customer> {
@Autowired
private Dialogs dialogs;
@Subscribe
public void onBeforeCommitChanges(BeforeCommitChangesEvent event) {
dialogs.createBackgroundWorkDialog(this, new LongTask(event))
.withCaption("Please wait...")
.withMessage("Saving data...")
.show();
event.preventCommit();
}
private class LongTask extends BackgroundTask<Integer, Void> {
private BeforeCommitChangesEvent event;
protected LongTask(BeforeCommitChangesEvent event) {
super(100, CustomerEdit.this);
this.event = event;
}
@Override
public Void run(TaskLifeCycle<Integer> taskLifeCycle) throws Exception {
Thread.sleep(3000);
return null;
}
@Override
public void done(Void result) {
event.resume();
}
}
}
Another option is to override the closeWithCommit()
method of the editor screen controller and return OperationResult.fail()
from it, then commit and close when the background operation is done:
@UiController("Customer.edit")
@UiDescriptor("customer-edit.xml")
@EditedEntityContainer("customerDc")
public class CustomerEdit extends StandardEditor<Customer> {
@Autowired
private Dialogs dialogs;
@Override
public OperationResult closeWithCommit() {
dialogs.createBackgroundWorkDialog(this, new LongTask())
.withCaption("Please wait...")
.withMessage("Saving data...")
.show();
return OperationResult.fail();
}
private class LongTask extends BackgroundTask<Integer, Void> {
protected LongTask() {
super(100, CustomerEdit.this);
}
@Override
public Void run(TaskLifeCycle<Integer> taskLifeCycle) throws Exception {
Thread.sleep(3000);
return null;
}
@Override
public void done(Void result) {
CustomerEdit.this.commitChanges()
.then(() -> CustomerEdit.this.close(StandardOutcome.COMMIT));
}
}
}