use of au.com.vaadinutils.dao.RunnableUI in project VaadinUtils by rlsutton1.
the class BaseCrudView method initButtons.
private void initButtons() {
actionNewButton.addClickListener(new ClickEventLogged.ClickListener() {
private static final long serialVersionUID = 1L;
@Override
public void clicked(ClickEvent event) {
if (isDirty()) {
Notification.show("You must Save or Discard your changes first", Type.ERROR_MESSAGE);
return;
}
newClicked();
}
});
actionApplyButton.addClickListener(new ClickEventLogged.ClickListener() {
private static final long serialVersionUID = 1L;
@Override
public void clicked(ClickEvent event) {
final Object entityId = entityTable.getValue();
if (entityId != null) {
@SuppressWarnings("unchecked") CrudAction<E> action = (CrudAction<E>) actionCombo.getValue();
if (action != null) {
if (action.showPreparingDialog()) {
performActionWithWaitDialog(entityId, action);
} else {
performAction(entityId, action);
}
} else {
Notification.show("Please select an Action first.");
}
} else {
Notification.show("Please select record first.");
}
}
private void performAction(final Object entityId, final CrudAction<E> action) {
EntityItem<E> entity = container.getItem(entityId);
if (interceptAction(action, entity)) {
action.exec(BaseCrudView.this, entity);
}
container.commit();
container.refreshItem(entity.getItemId());
// actionCombo.select(actionCombo.getNullSelectionItemId());
}
private void performActionWithWaitDialog(final Object entityId, final CrudAction<E> action) {
final ConfirmDialog pleaseWaitMessage = createPleaseWaitDialog();
if (action != null) {
// we have to delay, because if we try to close the window
// before it's created - that won't work.
final ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
final EntityManagerRunnable runner = invokeAction(entityId, pleaseWaitMessage, action);
exec.schedule(runner, 1, TimeUnit.SECONDS);
exec.shutdown();
UI.getCurrent().setPollInterval(500);
}
}
private EntityManagerRunnable invokeAction(final Object entityId, final ConfirmDialog pleaseWaitMessage, final CrudAction<E> action) {
final EntityManagerRunnable runner = new EntityManagerRunnable(new RunnableUI(UI.getCurrent()) {
@Override
protected void run(final UI ui) {
ui.access(new Runnable() {
@Override
public void run() {
try {
EntityItem<E> entity = container.getItem(entityId);
if (interceptAction(action, entity)) {
action.exec(BaseCrudView.this, entity);
}
container.commit();
container.refreshItem(entity.getItemId());
// actionCombo.select(actionCombo.getNullSelectionItemId());
} finally {
pleaseWaitMessage.close();
ui.setPollInterval(-1);
}
}
});
}
});
return runner;
}
private ConfirmDialog createPleaseWaitDialog() {
final ConfirmDialog pleaseWaitMessage = ConfirmDialog.show(UI.getCurrent(), "Please wait...", new ConfirmDialog.Listener() {
private static final long serialVersionUID = 1L;
@Override
public void onClose(ConfirmDialog dialog) {
}
});
pleaseWaitMessage.setClosable(false);
pleaseWaitMessage.getCancelButton().setVisible(false);
pleaseWaitMessage.getOkButton().setVisible(false);
pleaseWaitMessage.setModal(true);
pleaseWaitMessage.setCaption("Preparing Action");
return pleaseWaitMessage;
}
});
}
Aggregations