/*
 * ChunkOutputFrame.java
 *
 * Copyright (C) 2009-16 by RStudio, Inc.
 *
 * Unless you have received this program directly from RStudio pursuant
 * to the terms of a commercial license agreement with RStudio, then
 * this program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
package org.rstudio.studio.client.workbench.views.source.editors.text;

import org.rstudio.core.client.widget.DynamicIFrame;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Timer;

public class ChunkOutputFrame extends DynamicIFrame
{
   void loadUrl(String url, Command onCompleted)
   {
      onCompleted_ = onCompleted;
      super.setUrl(url);
   }
   
   /**
    * Loads a URL after a fixed amount of time.
    * 
    * @param url The URL to load.
    * @param delayMs The number of milliseconds to delay before loading the URL.
    * @param onCompleted The command to run after the time has passed *and* the
    *   URL had been loaded.
    */
   void loadUrlDelayed(String url, int delayMs, 
         Command onCompleted)
   {
      // prevent stacking timers
      if (timer_ != null && timer_.isRunning())
         timer_.cancel();
      
      onCompleted_ = onCompleted;
      url_ = url;

      timer_ = new FrameLoadTimer();
      timer_.schedule(delayMs);
   }
   
   public void cancelPendingLoad()
   {
      if (timer_ != null && timer_.isRunning())
         timer_.cancel();
   }

   @Override
   public String getUrl()
   {
      // return the pending URL if we haven't loaded one yet
      if (timer_ != null && timer_.isRunning())
         return url_;
      else
         return super.getUrl();
   }
   
   @Override
   protected void onFrameLoaded()
   {
      if (onCompleted_ != null)
         onCompleted_.execute();
   }
   
   private class FrameLoadTimer extends Timer
   {
      @Override
      public void run()
      {
         loadUrl(url_, onCompleted_);
      }
   }
   
   private FrameLoadTimer timer_;
   private String url_;
   private Command onCompleted_;
}

@@ -845,16 +845,210 @@
 Frame%0A%7B%0A
+   public ChunkOutputFrame()%0A   %7B%0A      timer_ = new Timer() %0A      %7B%0A         @Override%0A         public void run()%0A         %7B%0A            loadUrl(url_, onCompleted_);%0A         %7D%0A      %7D;%0A   %7D%0A%0A
    void 
@@ -1080,32 +1080,32 @@
 nd onCompleted)%0A
-
    %7B%0A      onCom
@@ -1587,34 +1587,16 @@
  (timer_
- != null && timer_
 .isRunni
@@ -1691,45 +1691,8 @@
 l;%0A%0A
-      timer_ = new FrameLoadTimer();%0A
     
@@ -1784,34 +1784,16 @@
  (timer_
- != null && timer_
 .isRunni
@@ -1955,26 +1955,8 @@
 mer_
- != null && timer_
 .isR
@@ -2180,171 +2180,14 @@
 ate 
-class FrameLoadTimer extends Timer%0A   %7B%0A      @Override%0A      public void run()%0A      %7B%0A         loadUrl(url_, onCompleted_);%0A      %7D%0A   %7D%0A   %0A   private FrameLoad
+final 
 Time

use single instance of delay load timer