I am trying to create a “Waiting dialog”, a dialog with a ProgressBar that show up at front and blocks the app until the “task” is finished. I could achieve this in V7 but with this new UI thing (from V10+ I guess) I can’t make it work
The task (a runnable instance) is execute by a ExecutionService when the Dialog is attached, this is my “trigger point”
//in the constructor
addAttachListener(evt -> {
System.out.println("Attachinggg... initial=" + evt.isInitialAttach());
new Thread(() -> runTask()).start(); //this must be run in another thread for the dialog be displayed when #open() is called
});
public void setTask(Runnable task)..
private void runTask() {
es = Executors.newSingleThreadExecutor();
System.out.println("after start1");
try {
es.submit(task);
// es.awaitTermination(5, TimeUnit.SECONDS);
// } catch (InterruptedException exx) {
} finally {
es.shutdown();
System.out.println("executor: " + es.isTerminated());
System.out.println("closing");
push(() -> close());
}
}
the Dialog also has a Label called statusLabel which can be updated from the external task
public void updateStatus(String message) {
System.out.println("WaitingDialog hasLock: " + ui.getSession().hasLock()); // always FALSE!
ui.access(() -> statusLabel.setValue(message));
}
In the LoginView, when the user is trying to login I create a instance of this Dialog, the task and Dialog#open
private void login(LoginForm.LoginEvent event) {
WaitingDialog ww = new WaitingDialog(UI.getCurrent(), false, true, null);
ww.getTitleLabel().setText("Verificando..");
Runnable r2 = () -> {
try {
ww.updateStatus("1");
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(LoginView.class.getName()).log(Level.SEVERE, null, ex);
}
PrestadorWebJpaController dao = new PrestadorWebJpaController();
dao.setForceRefresh(true);
PrestadorWeb u = dao.findBy(Long.valueOf(event.getUsername()));
try {
ww.updateStatus("2");
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(LoginView.class.getName()).log(Level.SEVERE, null, ex);
}
try {
//DAO stuff
} catch (MessageException ex) {
ww.push(() -> {
message.setText(ex.getMessage());
loginForm.setError(true);
loginForm.setEnabled(true);
});
}
try {
ww.updateStatus("byeeee");
Thread.sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(LoginView.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("chau login task");
};
ww.setTask(r2);
ww.addDetachListener(evt -> {
if (ApplicationServiceInitListener.isLogged()) {
ApplicationServiceInitListener.navigateToHome();
} else {
loginForm.setEnabled(true);
}
});
ww.open();
}
the dialog is displayed, the task runs and finish its business but the label is never updated