/*
 * Copyright (c) 2008-2013 Haulmont. All rights reserved.
 * Use is subject to license terms, see http://www.cuba-platform.com/license for details.
 */

package com.haulmont.cuba.web.toolkit.ui.client.timer;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.Widget;
import com.haulmont.cuba.web.toolkit.ui.CubaTimer;
import com.vaadin.client.communication.RpcProxy;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractComponentConnector;
import com.vaadin.shared.ui.Connect;

/**
 * @author artamonov
 * @version $Id$
 */
@Connect(CubaTimer.class)
public class CubaTimerConnector extends AbstractComponentConnector {

    protected static int DEFFERED_DELAY_MS = 1000;

    protected CubaTimerServerRpc rpc = RpcProxy.create(CubaTimerServerRpc.class, this);

    protected boolean running = false;
    protected boolean scheduled = false;

    protected Timer jsTimer = new CubaTimerSource();

    public CubaTimerConnector() {
        registerRpc(CubaTimerClientRpc.class, new CubaTimerClientRpc() {
            @Override
            public void setRunning(boolean running) {
                CubaTimerConnector.this.setRunning(running);
            }

            @Override
            public void requestCompleted() {
                CubaTimerConnector.this.requestCompleted();
            }
        });
    }

    public void setRunning(boolean running) {
        jsTimer.cancel();

        if (running) {
            jsTimer.schedule(getState().delay);
            scheduled = true;
        } else {
            scheduled = false;
        }

        this.running = running;
    }

    public void onTimer() {
        if (getConnection().isApplicationRunning()) {
            if (running && getState().listeners) {
                if (!getConnection().hasActiveRequest()) {
                    rpc.onTimer();
                } else {
                    jsTimer.schedule(DEFFERED_DELAY_MS);
                }
            } else {
                scheduled = false;
            }
        }
    }

    public void requestCompleted() {
        if (running && getState().repeating) {
            jsTimer.schedule(getState().delay);
            scheduled = true;
        } else {
            scheduled = false;
        }
    }

    @Override
    public void onStateChanged(StateChangeEvent stateChangeEvent) {
        super.onStateChanged(stateChangeEvent);

        // in case of page refresh we need to schedule timer here
        if (!running && getState().running && getState().repeating) {
            setRunning(true);
        }
    }

    @Override
    public CubaTimerState getState() {
        return (CubaTimerState) super.getState();
    }

    @Override
    protected Widget createWidget() {
        return GWT.create(Hidden.class);
    }

    protected class CubaTimerSource extends Timer {
        @Override
        public void run() {
            onTimer();
        }
    }
}
@@ -951,49 +951,8 @@
 lse;
-%0A    protected boolean scheduled = false;
 %0A%0A  
@@ -1570,86 +1570,8 @@
 y);%0A
-            scheduled = true;%0A        %7D else %7B%0A            scheduled = false;%0A
     
@@ -1660,46 +1660,15 @@
 if (
-getConnection().isApplicationR
+r
 unning
-()
 ) %7B%0A
@@ -1679,35 +1679,24 @@
         if (
-running && 
 getState().l
@@ -1790,22 +1790,197 @@
     
-rpc.onTimer();
+// if application stopped we will not schedule new timer event%0A                    if (getConnection().isApplicationRunning()) %7B%0A                        rpc.onTimer();%0A                    %7D
 %0A   
@@ -2113,33 +2113,34 @@
         
-scheduled = false
+requestCompleted()
 ;%0A      
@@ -2300,86 +2300,8 @@
 y);%0A
-            scheduled = true;%0A        %7D else %7B%0A            scheduled = false;%0A
     

Fix web Timers behaviour #PL-5345