2 * Copyright 2000-2010 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.intellij.ide.actions;
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;
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;
43 public CreateDirectoryOrPackageHandler(Project project, PsiDirectory directory, boolean isDirectory, final String delimiters) {
45 myDirectory = directory;
46 myIsDirectory = isDirectory;
47 myDelimiters = delimiters;
50 public boolean checkInput(String inputString) {
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";
58 if (!myIsDirectory && inputString.length() > 0 && !PsiDirectoryFactory.getInstance(myProject).isValidPackageName(inputString)) {
59 return "Not a valid package name";
64 public boolean canClose(String inputString) {
65 final String subDirName = inputString;
67 if (subDirName.length() == 0) {
68 Messages.showMessageDialog(myProject, IdeBundle.message("error.name.should.be.specified"), CommonBundle.getErrorTitle(),
69 Messages.getErrorIcon());
73 final boolean multiCreation = StringUtil.containsAnyChar(subDirName, myDelimiters);
76 myDirectory.checkCreateSubdirectory(subDirName);
78 catch (IncorrectOperationException ex) {
79 Messages.showMessageDialog(myProject, CreateElementActionBase.filterMessage(ex.getMessage()), CommonBundle.getErrorTitle(),
80 Messages.getErrorIcon());
85 Runnable command = new Runnable() {
87 final Runnable run = new Runnable() {
89 LocalHistoryAction action = LocalHistoryAction.NULL;
92 String dirPath = myDirectory.getVirtualFile().getPresentableUrl();
93 actionName = IdeBundle.message("progress.creating.directory", dirPath, File.separator, subDirName);
94 action = LocalHistory.getInstance().startAction(actionName);
96 myCreatedElement = DirectoryUtil.createSubdirectories(subDirName, myDirectory, myDelimiters);
99 catch (final IncorrectOperationException ex) {
100 ApplicationManager.getApplication().invokeLater(new Runnable() {
102 Messages.showMessageDialog(myProject, CreateElementActionBase.filterMessage(ex.getMessage()),
103 CommonBundle.getErrorTitle(), Messages.getErrorIcon());
112 ApplicationManager.getApplication().runWriteAction(run);
115 CommandProcessor.getInstance().executeCommand(myProject, command, myIsDirectory
116 ? IdeBundle.message("command.create.directory")
117 : IdeBundle.message("command.create.package"), null);
119 return myCreatedElement != null;
122 public PsiDirectory getCreatedElement() {
123 return myCreatedElement;