Hi,

    First, let's see if we are talking about the same code, here is the
actual code I use:


import org.apache.wicket.Page;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.util.time.Duration;

/**
 * @author toffedan
 */
public class RestartebleAjaxSelfUpdatingTimerBehavior extends
AbstractDefaultAjaxBehavior {

    private static final long serialVersionUID = 1L;

        /** The update interval */
        private Duration updateInterval;

        private boolean stopped = false;

        private boolean headRendered = false;

        /**
         * Construct.
         *
         * @param updateInterval
         *            Duration between AJAX callbacks
         */
        public RestartebleAjaxSelfUpdatingTimerBehavior(final Duration
updateInterval)
        {
                if (updateInterval == null || updateInterval.getMilliseconds() 
<= 0)
                {
                        throw new IllegalArgumentException("Invalid update 
interval");
                }
                this.updateInterval = updateInterval;
        }

        /**
         * Stops the timer
         */
        public final void stop()
        {
                stopped = true;
        }

        /**
         * Starts the timer
         */
        public final void start(final AjaxRequestTarget target)
        {
                stopped = false;
    
target.getHeaderResponse().renderOnLoadJavascript(getJsTimeoutCall(updateInterval));

        }

        /**
         * Sets the update interval duration. This method should only be called
within the
         * {...@link #onTimer(AjaxRequestTarget)} method.
         *
         * @param updateInterval
         */
        protected final void setUpdateInterval(Duration updateInterval)
        {
                if (updateInterval == null || updateInterval.getMilliseconds() 
<= 0)
                {
                        throw new IllegalArgumentException("Invalid update 
interval");
                }
                this.updateInterval = updateInterval;
        }

        /**
         * Returns the update interval
         *
         * @return The update interval
         */
        public final Duration getUpdateInterval()
        {
                return updateInterval;
        }

        /**
         * @see
org.apache.wicket.behavior.AbstractAjaxBehavior#renderHead(org.apache.wicket.markup.html.IHeaderResponse)
         */
    @Override
        public void renderHead(IHeaderResponse response)
        {
                super.renderHead(response);

                WebRequest request = 
(WebRequest)RequestCycle.get().getRequest();

                if (!stopped && (!headRendered || !request.isAjax()))
                {
                        headRendered = true;
                        
response.renderOnLoadJavascript(getJsTimeoutCall(updateInterval));
                }
        }

        /**
         * @param updateInterval
         *            Duration between AJAX callbacks
         * @return JS script
         */
        protected final String getJsTimeoutCall(final Duration updateInterval)
        {
                // this might look strange, but it is necessary for IE not to 
leak :(
                return "setTimeout(\"" + getCallbackScript() + "\", " +
updateInterval.getMilliseconds() +
                        ");";
        }

    @Override
        protected CharSequence getCallbackScript()
        {
                return generateCallbackScript("wicketAjaxGet('" +
getCallbackUrl(onlyTargetActivePage()) +
                        "'");
        }

        /**
         * @see
org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#getPreconditionScript()
         */
    @Override
        protected CharSequence getPreconditionScript()
        {
                String precondition = null;
                if (!(getComponent() instanceof Page))
                {
                        String componentId = getComponent().getMarkupId();
                        precondition = "var c = Wicket.$('" + componentId +
                                "'); return typeof(c) != 'undefined' && c != 
null";
                }
                return precondition;
        }

        protected boolean onlyTargetActivePage()
        {
                return true;
        }

        /**
         *
         * @see
org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#respond(org.apache.wicket.ajax.AjaxRequestTarget)
         */
        protected final void respond(final AjaxRequestTarget target)
        {
                if (!stopped) {

                onTimer(target);

                
target.getHeaderResponse().renderOnLoadJavascript(getJsTimeoutCall(updateInterval));
                }

        }

        /**
         * @see
org.apache.wicket.ajax.AbstractAjaxTimerBehavior#onTimer(org.apache.wicket.ajax.AjaxRequestTarget)
         */
        protected final void onTimer(final AjaxRequestTarget target)
        {
                target.addComponent(getComponent());
                onPostProcessTarget(target);
        }

        /**
         * Give the subclass a chance to add something to the target, like a
javascript effect call.
         * Called after the hosting component has been added to the target.
         *
         * @param target
         *            The AJAX target
         */
        protected void onPostProcessTarget(final AjaxRequestTarget target)
        {
        }

}


    If I recall correctly, I had to make a small change in other method, I
believe it's in renderHead() or in respond(), please check that, I'm in a
hurry right now.

hth,

Daniel



CrocodileShoes wrote:
> 
> Thanks Daniel,
> 
> I've just tried this and it's not worked.  Obviously it's a slightly
> different use case, that is, changing tabs and popping up a modal window
> but I'd hoped it would work.
> 
> It turns out that the stopped flag is not actually set to true.  That's
> why your method doesn't work.  When I change tabs the timer is still
> running and when it fires I get the following message:
> 
> INFO: Ajax GET stopped because of precondition check,
> url:?wicket:interface=:0:tabs:panel:logOutput::IActivePageBehaviorListener:0:-1&wicket:ignoreIfNotActive=true
> 
> 
> 
> dtoffe wrote:
>> 
>> Hi,
>> 
>>     Also take a look at this thread:
>> 
>>  
>> http://www.nabble.com/AjaxSelfUpdatingTimerBehavior-and-ModalWindow-to22202102.html
>> 
>>     I don't mean it's a correct or better solution, but so far it works
>> for me.
>> 
>> Hth,
>> 
>> Daniel
>> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Is-it-possible-to-restart-an-AjaxSelfUpdatingBehaviour-after-it-has-been-stopped--tp24626909p24644786.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to