/*
 * OpenFileDialog.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.core.client.files.filedialog;

import org.rstudio.core.client.files.FileSystemContext;
import org.rstudio.core.client.files.FileSystemItem;
import org.rstudio.core.client.widget.ProgressOperationWithInput;

public class OpenFileDialog extends FileDialog
{
   public OpenFileDialog(String title,
                         FileSystemContext context,
                         String filter,
                         boolean canChooseDirectories,
                         ProgressOperationWithInput<FileSystemItem> operation)
   {
      super(title, null, "Open", false, false, false, context, filter, 
            operation);
      
      canChooseDirectories_ = canChooseDirectories;
   }
   
   @Override
   public boolean shouldAccept()
   {
      if (canChooseDirectories_)
      {
         FileSystemItem item = browser_.getSelectedItem();
         if (item == null || item.isDirectory())
            return true;
      }
      
      return super.shouldAccept();
   }
   
   @Override
   protected FileSystemItem getSelectedItem()
   {
      FileSystemItem item = browser_.getSelectedItem();
      if (item == null)
         item = browser_.getCurrentDirectory();
      return item;
   }
   
   protected final boolean canChooseDirectories_;
}

@@ -805,16 +805,76 @@
 Input;%0A%0A
+import com.google.gwt.event.logical.shared.SelectionEvent;%0A%0A
 public c
@@ -1512,341 +1512,1463 @@
     
-if (item == null %7C%7C item.isDirectory())%0A            return true;%0A      %7D%0A      %0A      return super.shouldAccept();%0A   %7D%0A   %0A   @Override%0A   protected FileSystemItem getSelectedItem()%0A   %7B%0A      FileSystemItem item = browser_.getSelectedItem();%0A      if (item == null)%0A         item = browser_.getCurrentDirectory();%0A      return item
+String fileInput = browser_.getFilename().trim();%0A         %0A         // if we have no user input nor a selected item,%0A         // interpret this as a request to open the active%0A         // directory as an RStudio project%0A         if (item == null && fileInput.isEmpty())%0A            return true;%0A         %0A         // if the user has selected a directory, and there%0A         // is no user input, interpret that as a request to%0A         // navigate into that directory%0A         if (item != null && item.isDirectory())%0A         %7B%0A            cd(item.getPath());%0A            return false;%0A         %7D%0A      %7D%0A      %0A      return super.shouldAccept();%0A   %7D%0A   %0A   @Override%0A   public void onNavigated()%0A   %7B%0A      super.onNavigated();%0A      browser_.setFilename(%22%22);%0A   %7D%0A   %0A   @Override%0A   protected FileSystemItem getSelectedItem()%0A   %7B%0A      FileSystemItem item = browser_.getSelectedItem();%0A      if (item == null)%0A         item = browser_.getCurrentDirectory();%0A      return item;%0A   %7D%0A   %0A   @Override%0A   public void onSelection(SelectionEvent%3CFileSystemItem%3E event)%0A   %7B%0A      super.onSelection(event);%0A      %0A      // clear the active filename whenever a directory%0A      // is selected -- this allows us to disambiguate%0A      // 'fresh' from 'stale' user input; ie, tell whether%0A      // the user is navigating in the widget or typing in%0A      // the file name textbox%0A      if (event.getSelectedItem().isDirectory())%0A         browser_.setFilename(%22%22)
 ;%0A  

improve 'open project' behaviors re: directory selection