refactor "create directory or package" action for better reusability
authorDmitry Jemerov <yole@jetbrains.com>
Mon, 13 Sep 2010 11:20:31 +0000 (15:20 +0400)
committerDmitry Jemerov <yole@jetbrains.com>
Mon, 13 Sep 2010 18:00:09 +0000 (22:00 +0400)
platform/lang-impl/src/com/intellij/ide/actions/CreateDirectoryOrPackageAction.java
platform/lang-impl/src/com/intellij/ide/actions/CreateDirectoryOrPackageHandler.java [new file with mode: 0644]
platform/lang-impl/src/com/intellij/ide/util/DirectoryChooserUtil.java

index 733c610cded82aa7607b9a2abf6767fe559a0ec2..a66f6de4a6c1ba699ba957d6be8e14b13fc4dc01 100644 (file)
 
 package com.intellij.ide.actions;
 
-import com.intellij.CommonBundle;
-import com.intellij.history.LocalHistory;
-import com.intellij.history.LocalHistoryAction;
 import com.intellij.ide.IdeBundle;
 import com.intellij.ide.IdeView;
 import com.intellij.ide.util.DirectoryChooserUtil;
-import com.intellij.ide.util.DirectoryUtil;
 import com.intellij.openapi.actionSystem.*;
-import com.intellij.openapi.application.ApplicationManager;
-import com.intellij.openapi.command.CommandProcessor;
-import com.intellij.openapi.fileTypes.FileTypeManager;
 import com.intellij.openapi.project.DumbAware;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.ui.InputValidatorEx;
 import com.intellij.openapi.ui.Messages;
 import com.intellij.psi.PsiDirectory;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.impl.file.PsiDirectoryFactory;
 import com.intellij.util.Icons;
-import com.intellij.util.IncorrectOperationException;
-
-import java.io.File;
 
 public class CreateDirectoryOrPackageAction extends AnAction implements DumbAware {
   public CreateDirectoryOrPackageAction() {
@@ -45,40 +34,39 @@ public class CreateDirectoryOrPackageAction extends AnAction implements DumbAwar
   }
 
   public void actionPerformed(AnActionEvent e) {
-    DataContext dataContext = e.getDataContext();
-
-    IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
-    Project project = PlatformDataKeys.PROJECT.getData(dataContext);
+    IdeView view = e.getData(LangDataKeys.IDE_VIEW);
+    Project project = e.getData(PlatformDataKeys.PROJECT);
 
     PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
 
     if (directory == null) return;
     boolean isDirectory = !PsiDirectoryFactory.getInstance(project).isPackage(directory);
 
-    MyInputValidator validator = new MyInputValidator(project, directory, isDirectory);
+    CreateDirectoryOrPackageHandler validator = new CreateDirectoryOrPackageHandler(project, directory, isDirectory,
+                                                                                   isDirectory ? "\\/" : ".");
     Messages.showInputDialog(project, isDirectory
                                       ? IdeBundle.message("prompt.enter.new.directory.name")
                                       : IdeBundle.message("prompt.enter.new.package.name"),
                                       isDirectory ? IdeBundle.message("title.new.directory") : IdeBundle.message("title.new.package"),
                                       Messages.getQuestionIcon(), "", validator);
 
-    if (validator.myCreatedElement == null) return;
-
-    view.selectElement(validator.myCreatedElement);
+    final PsiElement result = validator.getCreatedElement();
+    if (result != null && view != null) {
+      view.selectElement(result);
+    }
   }
 
   public void update(AnActionEvent event) {
     Presentation presentation = event.getPresentation();
-    DataContext dataContext = event.getDataContext();
 
-    Project project = PlatformDataKeys.PROJECT.getData(dataContext);
+    Project project = event.getData(PlatformDataKeys.PROJECT);
     if (project == null) {
       presentation.setVisible(false);
       presentation.setEnabled(false);
       return;
     }
 
-    IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
+    IdeView view = event.getData(LangDataKeys.IDE_VIEW);
     if (view == null) {
       presentation.setVisible(false);
       presentation.setEnabled(false);
@@ -114,100 +102,4 @@ public class CreateDirectoryOrPackageAction extends AnAction implements DumbAwar
     }
   }
 
-  protected class MyInputValidator implements InputValidatorEx {
-    private final Project myProject;
-    private final PsiDirectory myDirectory;
-    private final boolean myIsDirectory;
-    private PsiElement myCreatedElement = null;
-
-    public MyInputValidator(Project project, PsiDirectory directory, boolean isDirectory) {
-      myProject = project;
-      myDirectory = directory;
-      myIsDirectory = isDirectory;
-    }
-
-    public boolean checkInput(String inputString) {
-      return true;
-    }
-
-    public String getErrorText(String inputString) {
-      if (FileTypeManager.getInstance().isFileIgnored(inputString)) {
-        return "Trying to create a " + (myIsDirectory ? "directory" : "package") + " with ignored name, result will not be visible";
-      }
-      if (!myIsDirectory && inputString.length() > 0 && !PsiDirectoryFactory.getInstance(myProject).isValidPackageName(inputString)) {
-        return "Not a valid package name";
-      }
-      return null;
-    }
-
-    public boolean canClose(String inputString) {
-      final String subDirName = inputString;
-
-      if (subDirName.length() == 0) {
-        Messages.showMessageDialog(myProject, IdeBundle.message("error.name.should.be.specified"), CommonBundle.getErrorTitle(),
-                                   Messages.getErrorIcon());
-        return false;
-      }
-
-      final boolean multiCreation = myIsDirectory
-                                    ? subDirName.indexOf('/') != -1 || subDirName.indexOf('\\') != -1
-                                    : subDirName.indexOf('.') != -1;
-
-      if (!multiCreation) {
-        try {
-          myDirectory.checkCreateSubdirectory(subDirName);
-        }
-        catch (IncorrectOperationException ex) {
-          Messages.showMessageDialog(myProject, CreateElementActionBase.filterMessage(ex.getMessage()), CommonBundle.getErrorTitle(),
-                                     Messages.getErrorIcon());
-          return false;
-        }
-      }
-
-      Runnable command = new Runnable() {
-        public void run() {
-          final Runnable run = new Runnable() {
-            public void run() {
-              LocalHistoryAction action = LocalHistoryAction.NULL;
-              try {
-                String actionName;
-                String dirPath = myDirectory.getVirtualFile().getPresentableUrl();
-                actionName = IdeBundle.message("progress.creating.directory", dirPath, File.separator, subDirName);
-                action = LocalHistory.getInstance().startAction(actionName);
-
-                final PsiDirectory createdDir;
-                if (myIsDirectory) {
-                  createdDir = DirectoryUtil.createSubdirectories(subDirName, myDirectory, "\\/");
-                }
-                else {
-                  createdDir = DirectoryUtil.createSubdirectories(subDirName, myDirectory, ".");
-                }
-
-
-                myCreatedElement = createdDir;
-
-              }
-              catch (final IncorrectOperationException ex) {
-                ApplicationManager.getApplication().invokeLater(new Runnable() {
-                  public void run() {
-                    Messages.showMessageDialog(myProject, CreateElementActionBase.filterMessage(ex.getMessage()),
-                                               CommonBundle.getErrorTitle(), Messages.getErrorIcon());
-                  }
-                });
-              }
-              finally {
-                action.finish();
-              }
-            }
-          };
-          ApplicationManager.getApplication().runWriteAction(run);
-        }
-      };
-      CommandProcessor.getInstance().executeCommand(myProject, command, myIsDirectory
-                                                                        ? IdeBundle.message("command.create.directory")
-                                                                        : IdeBundle.message("command.create.package"), null);
-
-      return myCreatedElement != null;
-    }
-  }
 }
diff --git a/platform/lang-impl/src/com/intellij/ide/actions/CreateDirectoryOrPackageHandler.java b/platform/lang-impl/src/com/intellij/ide/actions/CreateDirectoryOrPackageHandler.java
new file mode 100644 (file)
index 0000000..f820d62
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ * Copyright 2000-2010 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.ide.actions;
+
+import com.intellij.CommonBundle;
+import com.intellij.history.LocalHistory;
+import com.intellij.history.LocalHistoryAction;
+import com.intellij.ide.IdeBundle;
+import com.intellij.ide.util.DirectoryUtil;
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.command.CommandProcessor;
+import com.intellij.openapi.fileTypes.FileTypeManager;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.ui.InputValidatorEx;
+import com.intellij.openapi.ui.Messages;
+import com.intellij.openapi.util.text.StringUtil;
+import com.intellij.psi.PsiDirectory;
+import com.intellij.psi.impl.file.PsiDirectoryFactory;
+import com.intellij.util.IncorrectOperationException;
+
+import java.io.File;
+
+public class CreateDirectoryOrPackageHandler implements InputValidatorEx {
+  private final Project myProject;
+  private final PsiDirectory myDirectory;
+  private final boolean myIsDirectory;
+  private PsiDirectory myCreatedElement = null;
+  private String myDelimiters;
+
+  public CreateDirectoryOrPackageHandler(Project project, PsiDirectory directory, boolean isDirectory, final String delimiters) {
+    myProject = project;
+    myDirectory = directory;
+    myIsDirectory = isDirectory;
+    myDelimiters = delimiters;
+  }
+
+  public boolean checkInput(String inputString) {
+    return true;
+  }
+
+  public String getErrorText(String inputString) {
+    if (FileTypeManager.getInstance().isFileIgnored(inputString)) {
+      return "Trying to create a " + (myIsDirectory ? "directory" : "package") + " with ignored name, result will not be visible";
+    }
+    if (!myIsDirectory && inputString.length() > 0 && !PsiDirectoryFactory.getInstance(myProject).isValidPackageName(inputString)) {
+      return "Not a valid package name";
+    }
+    return null;
+  }
+
+  public boolean canClose(String inputString) {
+    final String subDirName = inputString;
+
+    if (subDirName.length() == 0) {
+      Messages.showMessageDialog(myProject, IdeBundle.message("error.name.should.be.specified"), CommonBundle.getErrorTitle(),
+                                 Messages.getErrorIcon());
+      return false;
+    }
+
+    final boolean multiCreation = StringUtil.containsAnyChar(subDirName, myDelimiters);
+    if (!multiCreation) {
+      try {
+        myDirectory.checkCreateSubdirectory(subDirName);
+      }
+      catch (IncorrectOperationException ex) {
+        Messages.showMessageDialog(myProject, CreateElementActionBase.filterMessage(ex.getMessage()), CommonBundle.getErrorTitle(),
+                                   Messages.getErrorIcon());
+        return false;
+      }
+    }
+
+    Runnable command = new Runnable() {
+      public void run() {
+        final Runnable run = new Runnable() {
+          public void run() {
+            LocalHistoryAction action = LocalHistoryAction.NULL;
+            try {
+              String actionName;
+              String dirPath = myDirectory.getVirtualFile().getPresentableUrl();
+              actionName = IdeBundle.message("progress.creating.directory", dirPath, File.separator, subDirName);
+              action = LocalHistory.getInstance().startAction(actionName);
+
+              myCreatedElement = DirectoryUtil.createSubdirectories(subDirName, myDirectory, myDelimiters);
+
+            }
+            catch (final IncorrectOperationException ex) {
+              ApplicationManager.getApplication().invokeLater(new Runnable() {
+                public void run() {
+                  Messages.showMessageDialog(myProject, CreateElementActionBase.filterMessage(ex.getMessage()),
+                                             CommonBundle.getErrorTitle(), Messages.getErrorIcon());
+                }
+              });
+            }
+            finally {
+              action.finish();
+            }
+          }
+        };
+        ApplicationManager.getApplication().runWriteAction(run);
+      }
+    };
+    CommandProcessor.getInstance().executeCommand(myProject, command, myIsDirectory
+                                                                      ? IdeBundle.message("command.create.directory")
+                                                                      : IdeBundle.message("command.create.package"), null);
+
+    return myCreatedElement != null;
+  }
+
+  public PsiDirectory getCreatedElement() {
+    return myCreatedElement;
+  }
+}
index 296679097d83a2e3bf2c340cc9c0b1b310c8aab1..cf8d9c23e6817efd0712a42ec0015bd9f61c7a49 100644 (file)
@@ -33,6 +33,7 @@ public class DirectoryChooserUtil {
   private DirectoryChooserUtil() {
   }
 
+  @Nullable
   public static PsiDirectory getOrChooseDirectory(IdeView view) {
     PsiDirectory[] dirs = view.getDirectories();
     if (dirs.length == 0) return null;
@@ -45,6 +46,7 @@ public class DirectoryChooserUtil {
     }
   }
 
+  @Nullable
   public static PsiDirectory selectDirectory(Project project,
                                              PsiDirectory[] packageDirectories,
                                              PsiDirectory defaultDirectory,