From 21b6f8b4674178354db32b8e2fa338d070c8809e Mon Sep 17 00:00:00 2001 From: Yuriy Artamonov Date: Tue, 11 Oct 2016 15:30:42 +0400 Subject: [PATCH] PL-7985 Timer doesn't call the action in custom MainWindow --- .../com/haulmont/cuba/web/gui/WebWindow.java | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/modules/web/src/com/haulmont/cuba/web/gui/WebWindow.java b/modules/web/src/com/haulmont/cuba/web/gui/WebWindow.java index 79836d94a3..5a3ff5a34b 100644 --- a/modules/web/src/com/haulmont/cuba/web/gui/WebWindow.java +++ b/modules/web/src/com/haulmont/cuba/web/gui/WebWindow.java @@ -45,13 +45,13 @@ import com.haulmont.cuba.web.toolkit.ui.CubaSingleModeContainer; import com.haulmont.cuba.web.toolkit.ui.CubaTree; import com.haulmont.cuba.web.toolkit.ui.CubaVerticalActionsLayout; import com.vaadin.event.ItemClickEvent; +import com.vaadin.server.ClientConnector; import com.vaadin.server.Page; import com.vaadin.server.Sizeable.Unit; import com.vaadin.shared.ui.MarginInfo; import com.vaadin.ui.*; import com.vaadin.ui.Button; import com.vaadin.ui.TabSheet; -import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.BooleanUtils; import org.apache.commons.lang.StringUtils; import org.dom4j.Element; @@ -671,7 +671,21 @@ public class WebWindow implements Window, Component.Wrapper, @Override public void addTimer(Timer timer) { - AppUI.getCurrent().addTimer(((WebTimer) timer).getTimerImpl()); + if (component.isAttached()) { + attachTimerToUi((WebTimer) timer); + } else { + ClientConnector.AttachListener attachListener = new ClientConnector.AttachListener() { + @Override + public void attach(ClientConnector.AttachEvent event) { + if (timers.contains(timer)) { + attachTimerToUi((WebTimer) timer); + } + // execute attach listener only once + component.removeAttachListener(this); + } + }; + component.addAttachListener(attachListener); + } if (timers == null) { timers = new LinkedList<>(); @@ -679,13 +693,21 @@ public class WebWindow implements Window, Component.Wrapper, timers.add(timer); } + protected void attachTimerToUi(WebTimer timer) { + AppUI appUI = (AppUI) component.getUI(); + appUI.addTimer(timer.getTimerImpl()); + } + @Override - public Timer getTimer(final String id) { + public Timer getTimer(String id) { if (timers == null) { return null; } - return (Timer) CollectionUtils.find(timers, object -> StringUtils.equals(id, ((Timer) object).getId())); + return timers.stream() + .filter(timer -> Objects.equals(timer.getId(), id)) + .findFirst() + .orElse(null); } public void stopTimers() {