/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.fileChooser.actions;

import com.intellij.CommonBundle;
import com.intellij.ide.DeleteProvider;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationBundle;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.UIBundle;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public final class VirtualFileDeleteProvider implements DeleteProvider {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.fileChooser.actions.VirtualFileDeleteProvider");

  public boolean canDeleteElement(@NotNull DataContext dataContext) {
    final VirtualFile[] files = PlatformDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
    return files != null && files.length > 0;
  }

  public void deleteElement(@NotNull DataContext dataContext) {
    final VirtualFile[] files = PlatformDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
    if (files == null || files.length == 0) return;

    String message = createConfirmationMessage(files);
    int returnValue = Messages.showOkCancelDialog(message, UIBundle.message("delete.dialog.title"), ApplicationBundle.message("button.delete"),
      CommonBundle.getCancelButtonText(), Messages.getQuestionIcon());
    if (returnValue != 0) return;

    Arrays.sort(files, FileComparator.getInstance());
    
    ApplicationManager.getApplication().runWriteAction(new Runnable() {
      public void run() {
        List<String> problems = ContainerUtil.newLinkedList();
        for (final VirtualFile file : files) {
          try {
            file.delete(this);
          }
          catch (IOException e) {
            LOG.info(e);
            problems.add(file.getName());
          }
        }
        if (!problems.isEmpty()) {
          boolean more = false;
          if (problems.size() > 10) {
            problems = problems.subList(0, 10);
            more = true;
          }
          Messages.showMessageDialog("Could not erase files or folders:\n  " + StringUtil.join(problems, ",\n  ") + (more ? "\n  ..." : ""),
                                     UIBundle.message("error.dialog.title"), Messages.getErrorIcon());
        }
      }
    }
    );
  }

  private static final class FileComparator implements Comparator<VirtualFile> {
    private static final FileComparator ourInstance = new FileComparator();

    public static FileComparator getInstance() {
      return ourInstance;
    }

    public int compare(final VirtualFile o1, final VirtualFile o2) {
      // files first
      return o2.getPath().compareTo(o1.getPath());
    }
  }

  private static String createConfirmationMessage(VirtualFile[] filesToDelete) {
    if (filesToDelete.length == 1) {
      if (filesToDelete[0].isDirectory()) {
        return UIBundle.message("are.you.sure.you.want.to.delete.selected.folder.confirmation.message");
      }
      else {
        return UIBundle.message("are.you.sure.you.want.to.delete.selected.file.confirmation.message", filesToDelete[0].getName());
      }
    }
    else {
      boolean hasFiles = false;
      boolean hasFolders = false;
      for (VirtualFile file : filesToDelete) {
        boolean isDirectory = file.isDirectory();
        hasFiles |= !isDirectory;
        hasFolders |= isDirectory;
      }
      LOG.assertTrue(hasFiles || hasFolders);
      if (hasFiles && hasFolders) {
        return UIBundle.message("are.you.sure.you.want.to.delete.selected.files.and.directories.confirmation.message");
      }
      else if (hasFolders) {
        return UIBundle.message("are.you.sure.you.want.to.delete.selected.folders.confirmation.message");
      }
      else {
        return UIBundle.message("are.you.sure.you.want.to.delete.selected.files.and.files.confirmation.message");
      }
    }
  }
}
@@ -923,38 +923,34 @@
 api.
-application.ApplicationManager
+command.WriteCommandAction
 ;%0Aim
@@ -2345,173 +2345,219 @@
 ));%0A
-    
 %0A    
-ApplicationManager.getApplication().runWriteAction(new Runnable() %7B%0A      public void run() %7B%0A        List%3CString%3E problems = ContainerUtil.newLinkedList();
+final List%3CString%3E problems = ContainerUtil.newLinkedList();%0A    new WriteCommandAction.Simple(PlatformDataKeys.PROJECT.getData(dataContext)) %7B%0A%0A      @Override%0A      protected void run() throws Throwable %7B
 %0A   
@@ -2778,28 +2778,51 @@
 %7D%0A        %7D%0A
+%0A
     
+  %7D%0A    %7D.execute();%0A%0A
     if (!pro
@@ -2842,24 +2842,137 @@
 ()) %7B%0A      
+reportDeletionProblem(problems);%0A    %7D%0A  %7D%0A%0A  private static void reportDeletionProblem(List%3CString%3E problems) %7B%0A
     boolean 
@@ -2985,22 +2985,16 @@
  false;%0A
-      
     if (
@@ -3013,30 +3013,24 @@
 e() %3E 10) %7B%0A
-      
       proble
@@ -3065,22 +3065,16 @@
 ;%0A      
-     
- 
 more = t
@@ -3078,38 +3078,26 @@
 = true;%0A    
-      %7D%0A      
+%7D%0A
     Messages
@@ -3250,22 +3250,16 @@
         
-      
 UIBundle
@@ -3320,39 +3320,8 @@
 ));%0A
-        %7D%0A      %7D%0A    %7D%0A    );%0A
   %7D%0A

delete files in command