/*
 * ChooseFile.java
 *
 * Copyright (C) 2009-12 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.choosefile;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.rstudio.core.client.files.FileSystemItem;
import org.rstudio.core.client.widget.ProgressIndicator;
import org.rstudio.core.client.widget.ProgressOperationWithInput;
import org.rstudio.studio.client.application.events.EventBus;
import org.rstudio.studio.client.common.FileDialogs;
import org.rstudio.studio.client.server.VoidServerRequestCallback;
import org.rstudio.studio.client.workbench.WorkbenchContext;
import org.rstudio.studio.client.workbench.model.RemoteFileSystemContext;
import org.rstudio.studio.client.workbench.views.choosefile.events.ChooseFileEvent;
import org.rstudio.studio.client.workbench.views.choosefile.events.ChooseFileHandler;
import org.rstudio.studio.client.workbench.views.choosefile.model.ChooseFileServerOperations;

@Singleton
public class ChooseFile implements ChooseFileHandler
{
   @Inject
   public ChooseFile(EventBus events,
                     ChooseFileServerOperations server,
                     RemoteFileSystemContext fsContext,
                     WorkbenchContext workbenchContext,
                     FileDialogs fileDialogs)
   {
      server_ = server;
      fsContext_ = fsContext;
      workbenchContext_ = workbenchContext;
      fileDialogs_ = fileDialogs;

      events.addHandler(ChooseFileEvent.TYPE, this);
      
   }
   
   public void onChooseFile(ChooseFileEvent event)
   {
      fileDialogs_.openFile(
            "Choose File",
            fsContext_,
            workbenchContext_.getCurrentWorkingDir(),
            new ProgressOperationWithInput<FileSystemItem>()
            {
               public void execute(FileSystemItem input,
                                   ProgressIndicator progress)
               {
                  String message, path;
                  if (input != null)
                  {
                     message = "Saving...";
                     path = input.getPath();
                  }
                  else
                  {
                     message = "Cancelling...";
                     path = null;
                  }
                  
                  progress.onProgress(message);
                  server_.chooseFileCompleted(
                        path,
                        new VoidServerRequestCallback(
                              progress));
               }
            });
   }
   
   private final ChooseFileServerOperations server_;
   private final RemoteFileSystemContext fsContext_;
   private final WorkbenchContext workbenchContext_;
   private final FileDialogs fileDialogs_;
}

@@ -2067,135 +2067,62 @@
     
-fileDialogs_.openFile(%0A            %22Choose File%22,%0A            fsContext_,%0A            workbenchContext_.getCurrentWorkingDir(),
+ProgressOperationWithInput%3CFileSystemItem%3E operation =
 %0A   
@@ -2181,38 +2181,26 @@
 em%3E()%0A      
-  
-    %7B%0A      
+%7B%0A
          pub
@@ -2267,22 +2267,16 @@
         
-      
 Progress
@@ -2296,38 +2296,32 @@
 gress)%0A         
-      
 %7B%0A              
@@ -2318,22 +2318,16 @@
         
- 
-     
 String m
@@ -2340,22 +2340,16 @@
 , path;%0A
-      
         
@@ -2379,37 +2379,26 @@
             
-      %7B%0A     
+%7B%0A
             
@@ -2392,33 +2392,32 @@
 %0A               
- 
 message = %22Savin
@@ -2415,38 +2415,32 @@
  = %22Saving...%22;%0A
-      
                p
@@ -2470,38 +2470,26 @@
             
-     
- 
 %7D%0A
-      
             
@@ -2501,38 +2501,26 @@
             
-    
-  %7B%0A      
+%7B%0A
             
@@ -2560,30 +2560,24 @@
             
-      
 path = null;
@@ -2593,41 +2593,11 @@
     
-      %7D%0A                  %0A      
+%7D%0A%0A
     
@@ -2646,22 +2646,16 @@
         
-  
-    
 server_.
@@ -2697,26 +2697,14 @@
     
-      path,%0A      
+path,%0A
     
@@ -2768,30 +2768,24 @@
             
-      
 progress));%0A
@@ -2783,16 +2783,384 @@
 gress));
+%0A         %7D%0A      %7D;%0A      %0A      if (event.getNewFile())%0A      %7B%0A         fileDialogs_.saveFile(%0A               %22Choose File%22,%0A               fsContext_,%0A               workbenchContext_.getCurrentWorkingDir(),%0A               %22%22,%0A               false,%0A               operation);%0A      %7D%0A      else%0A      %7B%0A         fileDialogs_.openFile(%0A               %22Choose File%22,
 %0A       
@@ -3159,33 +3159,43 @@
 %0A               
-%7D
+fsContext_,
 %0A            %7D);
@@ -3195,11 +3195,87 @@
     
-%7D);
+   workbenchContext_.getCurrentWorkingDir(),%0A               operation);%0A      %7D
 %0A   

support file.choose(new = TRUE)