avoid dlg from write action
authorAnna.Kozlova <anna.kozlova@jetbrains.com>
Wed, 16 Nov 2016 15:58:23 +0000 (16:58 +0100)
committerAnna.Kozlova <anna.kozlova@jetbrains.com>
Wed, 16 Nov 2016 18:04:30 +0000 (19:04 +0100)
java/java-impl/src/com/intellij/refactoring/copy/CopyClassesHandler.java
platform/lang-impl/src/com/intellij/refactoring/copy/CopyFilesOrDirectoriesHandler.java

index b2d8a64b1ef2414cb6a4290f8a22f93d8da9a7c6..6e196eb5d97114cfa47fcaa80946a3d269d84b38 100644 (file)
@@ -365,7 +365,7 @@ public class CopyClassesHandler extends CopyHandlerDelegateBase {
         if (relativePath != null && !relativePath.isEmpty()) {
           finalTarget = WriteAction.compute(() -> buildRelativeDir(targetDirectory, relativePath).findOrCreateTargetDirectory());
         }
-        final PsiFile fileCopy = CopyFilesOrDirectoriesHandler.copyToDirectory(file, getNewFileName(file, copyClassName), finalTarget, choice);
+        final PsiFile fileCopy = CopyFilesOrDirectoriesHandler.copyToDirectory(file, getNewFileName(file, copyClassName), finalTarget, choice, null);
         if (fileCopy != null) {
           createdFiles.add(fileCopy);
         }
index 089df084fb100f5ef843c9a76ce2f93df8094b37..b791850c0434c7b9b89c81bad663c512f518eca8 100644 (file)
@@ -222,37 +222,30 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase {
     }
 
     String title = RefactoringBundle.message(doClone ? "copy,handler.clone.files.directories" : "copy.handler.copy.files.directories");
-    new WriteCommandAction(project, title) {
-      @Override
-      protected void run(@NotNull Result result) {
-        try {
-          PsiFile firstFile = null;
-          final int[] choice = elements.length > 1 || elements[0] instanceof PsiDirectory ? new int[]{-1} : null;
-          for (PsiElement element : elements) {
-            PsiFile f = copyToDirectory((PsiFileSystemItem)element, newName, targetDirectory, choice);
-            if (firstFile == null) {
-              firstFile = f;
-            }
-          }
-
-          if (firstFile != null && openInEditor) {
-            CopyHandler.updateSelectionInActiveProjectView(firstFile, project, doClone);
-            if (!(firstFile instanceof PsiBinaryFile)) {
-              EditorHelper.openInEditor(firstFile);
-              ApplicationManager.getApplication().invokeLater(() -> ToolWindowManager.getInstance(project).activateEditorComponent(), project.getDisposed());
-            }
-          }
-        }
-        catch (final IncorrectOperationException ex) {
-          ApplicationManager.getApplication().invokeLater(
-            () -> Messages.showErrorDialog(project, ex.getMessage(), RefactoringBundle.message("error.title")));
+    try {
+      PsiFile firstFile = null;
+      final int[] choice = elements.length > 1 || elements[0] instanceof PsiDirectory ? new int[]{-1} : null;
+      for (PsiElement element : elements) {
+        PsiFile f = copyToDirectory((PsiFileSystemItem)element, newName, targetDirectory, choice, title);
+        if (firstFile == null) {
+          firstFile = f;
         }
-        catch (final IOException ex) {
-          ApplicationManager.getApplication().invokeLater(
-            () -> Messages.showErrorDialog(project, ex.getMessage(), RefactoringBundle.message("error.title")));
+      }
+
+      if (firstFile != null && openInEditor) {
+        CopyHandler.updateSelectionInActiveProjectView(firstFile, project, doClone);
+        if (!(firstFile instanceof PsiBinaryFile)) {
+          EditorHelper.openInEditor(firstFile);
+          ToolWindowManager.getInstance(project).activateEditorComponent();
         }
       }
-    }.execute();
+    }
+    catch (final IncorrectOperationException ex) {
+      Messages.showErrorDialog(project, ex.getMessage(), RefactoringBundle.message("error.title"));
+    }
+    catch (final IOException ex) {
+      Messages.showErrorDialog(project, ex.getMessage(), RefactoringBundle.message("error.title"));
+    }
   }
 
   /**
@@ -264,7 +257,7 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase {
   public static PsiFile copyToDirectory(@NotNull PsiFileSystemItem elementToCopy,
                                         @Nullable String newName,
                                         @NotNull PsiDirectory targetDirectory) throws IncorrectOperationException, IOException {
-    return copyToDirectory(elementToCopy, newName, targetDirectory, null);
+    return copyToDirectory(elementToCopy, newName, targetDirectory, null, null);
   }
 
   /**
@@ -277,12 +270,18 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase {
   public static PsiFile copyToDirectory(@NotNull PsiFileSystemItem elementToCopy,
                                         @Nullable String newName,
                                         @NotNull PsiDirectory targetDirectory,
-                                        @Nullable int[] choice) throws IncorrectOperationException, IOException {
+                                        @Nullable int[] choice,
+                                        @Nullable String title) throws IncorrectOperationException, IOException {
     if (elementToCopy instanceof PsiFile) {
       PsiFile file = (PsiFile)elementToCopy;
       String name = newName == null ? file.getName() : newName;
       if (checkFileExist(targetDirectory, choice, file, name, "Copy")) return null;
-      return targetDirectory.copyFileFrom(name, file);
+      return new WriteCommandAction<PsiFile>(targetDirectory.getProject(), title) {
+        @Override
+        protected void run(@NotNull Result<PsiFile> result) throws Throwable {
+          result.setResult(targetDirectory.copyFileFrom(name, file));
+        }
+      }.execute().getResultObject();
     }
     else if (elementToCopy instanceof PsiDirectory) {
       PsiDirectory directory = (PsiDirectory)elementToCopy;
@@ -299,7 +298,7 @@ public class CopyFilesOrDirectoriesHandler extends CopyHandlerDelegateBase {
       PsiElement[] children = directory.getChildren();
       for (PsiElement child : children) {
         PsiFileSystemItem item = (PsiFileSystemItem)child;
-        PsiFile f = copyToDirectory(item, item.getName(), subdirectory, choice);
+        PsiFile f = copyToDirectory(item, item.getName(), subdirectory, choice, title);
         if (firstFile == null) {
           firstFile = f;
         }