Merge branch 'master' of git.labs.intellij.net:idea/community
[idea/community.git] / platform / lang-impl / src / com / intellij / ide / actions / CreateDirectoryOrPackageHandler.java
1 /*
2  * Copyright 2000-2010 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.ide.actions;
17
18 import com.intellij.CommonBundle;
19 import com.intellij.history.LocalHistory;
20 import com.intellij.history.LocalHistoryAction;
21 import com.intellij.ide.IdeBundle;
22 import com.intellij.ide.util.DirectoryUtil;
23 import com.intellij.openapi.application.ApplicationManager;
24 import com.intellij.openapi.command.CommandProcessor;
25 import com.intellij.openapi.fileTypes.FileTypeManager;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.openapi.ui.InputValidatorEx;
28 import com.intellij.openapi.ui.Messages;
29 import com.intellij.openapi.util.text.StringUtil;
30 import com.intellij.psi.PsiDirectory;
31 import com.intellij.psi.impl.file.PsiDirectoryFactory;
32 import com.intellij.util.IncorrectOperationException;
33
34 import java.io.File;
35
36 public class CreateDirectoryOrPackageHandler implements InputValidatorEx {
37   private final Project myProject;
38   private final PsiDirectory myDirectory;
39   private final boolean myIsDirectory;
40   private PsiDirectory myCreatedElement = null;
41   private String myDelimiters;
42
43   public CreateDirectoryOrPackageHandler(Project project, PsiDirectory directory, boolean isDirectory, final String delimiters) {
44     myProject = project;
45     myDirectory = directory;
46     myIsDirectory = isDirectory;
47     myDelimiters = delimiters;
48   }
49
50   public boolean checkInput(String inputString) {
51     return true;
52   }
53
54   public String getErrorText(String inputString) {
55     if (FileTypeManager.getInstance().isFileIgnored(inputString)) {
56       return "Trying to create a " + (myIsDirectory ? "directory" : "package") + " with ignored name, result will not be visible";
57     }
58     if (!myIsDirectory && inputString.length() > 0 && !PsiDirectoryFactory.getInstance(myProject).isValidPackageName(inputString)) {
59       return "Not a valid package name";
60     }
61     return null;
62   }
63
64   public boolean canClose(String inputString) {
65     final String subDirName = inputString;
66
67     if (subDirName.length() == 0) {
68       Messages.showMessageDialog(myProject, IdeBundle.message("error.name.should.be.specified"), CommonBundle.getErrorTitle(),
69                                  Messages.getErrorIcon());
70       return false;
71     }
72
73     final boolean multiCreation = StringUtil.containsAnyChar(subDirName, myDelimiters);
74     if (!multiCreation) {
75       try {
76         myDirectory.checkCreateSubdirectory(subDirName);
77       }
78       catch (IncorrectOperationException ex) {
79         Messages.showMessageDialog(myProject, CreateElementActionBase.filterMessage(ex.getMessage()), CommonBundle.getErrorTitle(),
80                                    Messages.getErrorIcon());
81         return false;
82       }
83     }
84
85     Runnable command = new Runnable() {
86       public void run() {
87         final Runnable run = new Runnable() {
88           public void run() {
89             LocalHistoryAction action = LocalHistoryAction.NULL;
90             try {
91               String actionName;
92               String dirPath = myDirectory.getVirtualFile().getPresentableUrl();
93               actionName = IdeBundle.message("progress.creating.directory", dirPath, File.separator, subDirName);
94               action = LocalHistory.getInstance().startAction(actionName);
95
96               myCreatedElement = DirectoryUtil.createSubdirectories(subDirName, myDirectory, myDelimiters);
97
98             }
99             catch (final IncorrectOperationException ex) {
100               ApplicationManager.getApplication().invokeLater(new Runnable() {
101                 public void run() {
102                   Messages.showMessageDialog(myProject, CreateElementActionBase.filterMessage(ex.getMessage()),
103                                              CommonBundle.getErrorTitle(), Messages.getErrorIcon());
104                 }
105               });
106             }
107             finally {
108               action.finish();
109             }
110           }
111         };
112         ApplicationManager.getApplication().runWriteAction(run);
113       }
114     };
115     CommandProcessor.getInstance().executeCommand(myProject, command, myIsDirectory
116                                                                       ? IdeBundle.message("command.create.directory")
117                                                                       : IdeBundle.message("command.create.package"), null);
118
119     return myCreatedElement != null;
120   }
121
122   public PsiDirectory getCreatedElement() {
123     return myCreatedElement;
124   }
125 }