/*
 * HTMLPreviewPresenter.java
 *
 * Copyright (C) 2009-11 by RStudio, Inc.
 *
 * 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.htmlpreview;

import org.rstudio.core.client.command.CommandBinder;
import org.rstudio.studio.client.application.events.EventBus;
import org.rstudio.studio.client.common.GlobalDisplay;
import org.rstudio.studio.client.htmlpreview.events.HTMLPreviewCompletedEvent;
import org.rstudio.studio.client.htmlpreview.events.HTMLPreviewOutputEvent;
import org.rstudio.studio.client.htmlpreview.events.HTMLPreviewStartedEvent;
import org.rstudio.studio.client.htmlpreview.model.HTMLPreviewParams;
import org.rstudio.studio.client.htmlpreview.model.HTMLPreviewResult;
import org.rstudio.studio.client.htmlpreview.model.HTMLPreviewServerOperations;
import org.rstudio.studio.client.workbench.commands.Commands;
import org.rstudio.studio.client.server.VoidServerRequestCallback;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class HTMLPreviewPresenter implements IsWidget
{
   public interface Binder extends CommandBinder<Commands, HTMLPreviewPresenter>
   {}

   
   public interface Display extends IsWidget
   {
      void showProgress(String caption);
      void setProgressCaption(String caption);
      void showProgressOutput(String output);
      void stopProgress();
      void closeProgress();
      HandlerRegistration addProgressClickHandler(ClickHandler handler);
      
      void showPreview(String url, boolean enableScripts);
   }
   
   @Inject
   public HTMLPreviewPresenter(Display view,
                               Binder binder,
                               Commands commands,
                               GlobalDisplay globalDisplay,
                               EventBus eventBus,
                               HTMLPreviewServerOperations server)
   {
      view_ = view;
      globalDisplay_ = globalDisplay;
      server_ = server;
      
      binder.bind(commands, this);
      
      eventBus.addHandler(HTMLPreviewStartedEvent.TYPE, 
                          new HTMLPreviewStartedEvent.Handler()
      {
         @Override
         public void onHTMLPreviewStarted(HTMLPreviewStartedEvent event)
         {
            previewRunning_ = true;
            view_.showProgress("Generating preview...");
            view_.addProgressClickHandler(new ClickHandler() {
               @Override
               public void onClick(ClickEvent event)
               {
                  if (previewRunning_)
                  {
                     server_.terminatePreviewHTML(
                                             new VoidServerRequestCallback());
                  }
                  else
                  {
                     view_.closeProgress();
                  }
               }
               
            });
         }
      });
      
      eventBus.addHandler(HTMLPreviewOutputEvent.TYPE, 
                          new HTMLPreviewOutputEvent.Handler()
      {
         @Override
         public void onHTMLPreviewOutput(HTMLPreviewOutputEvent event)
         {
            view_.showProgressOutput(event.getOutput());
         }
      });
      
      eventBus.addHandler(HTMLPreviewCompletedEvent.TYPE, 
                          new HTMLPreviewCompletedEvent.Handler()
      { 
         @Override
         public void onHTMLPreviewCompleted(HTMLPreviewCompletedEvent event)
         {
            previewRunning_ = false;
            
            HTMLPreviewResult result = event.getResult();
            if (result.getSucceeded())
            {
               view_.closeProgress();
               view_.showPreview(
                  server_.getApplicationURL(result.getPreviewURL()),
                  result.getEnableScripts());
            }
            else
            {
               view_.setProgressCaption("Preview failed");
               view_.stopProgress();
            }
         }
      });
   }
   
   
   public void onActivated(HTMLPreviewParams params)
   {
   }
  
   
   @Override
   public Widget asWidget()
   {
      return view_.asWidget();
   }

   private final Display view_;
   private boolean previewRunning_ = false;
   
   @SuppressWarnings("unused")
   private final GlobalDisplay globalDisplay_;
   private final HTMLPreviewServerOperations server_;
}

@@ -662,16 +662,77 @@
 isplay;%0A
+import org.rstudio.studio.client.common.satellite.Satellite;%0A
 import o
@@ -794,32 +794,32 @@
 CompletedEvent;%0A
-
 import org.rstud
@@ -1405,24 +1405,136 @@
 ickHandler;%0A
+import com.google.gwt.event.logical.shared.CloseEvent;%0Aimport com.google.gwt.event.logical.shared.CloseHandler;%0A
 import com.g
@@ -2500,16 +2500,68 @@
 entBus,%0A
+                               Satellite satellite,%0A
         
@@ -2747,32 +2747,287 @@
 , this);%0A      %0A
+      satellite.addCloseHandler(new CloseHandler%3CSatellite%3E() %7B%0A         @Override%0A         public void onClose(CloseEvent%3CSatellite%3E event)%0A         %7B%0A            if (previewRunning_)%0A               terminateRunningPreview();%0A         %7D%0A      %7D);%0A      %0A
       eventBus.a
@@ -3554,159 +3554,37 @@
     
-%7B%0A                     server_.terminatePreviewHTML(%0A                                             new VoidServerRequestCallback());%0A                  %7D
+   terminateRunningPreview();
 %0A   
@@ -3607,28 +3607,8 @@
 lse%0A
-                  %7B%0A
     
@@ -3651,28 +3651,8 @@
 ();%0A
-                  %7D%0A
     
@@ -4968,16 +4968,141 @@
 ;%0A   %7D%0A%0A
+   private void terminateRunningPreview()%0A   %7B%0A      server_.terminatePreviewHTML(new VoidServerRequestCallback());%0A   %7D%0A   %0A
    priva

terminate preview when preview window closed