2 * Copyright 2000-2009 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.
17 package com
.intellij
.ide
.actions
;
19 import com
.intellij
.ide
.IdeView
;
20 import com
.intellij
.openapi
.actionSystem
.*;
21 import com
.intellij
.openapi
.project
.Project
;
22 import com
.intellij
.openapi
.roots
.ProjectFileIndex
;
23 import com
.intellij
.openapi
.roots
.ProjectRootManager
;
24 import com
.intellij
.psi
.JavaDirectoryService
;
25 import com
.intellij
.psi
.PsiDirectory
;
26 import com
.intellij
.psi
.PsiElement
;
27 import com
.intellij
.util
.IncorrectOperationException
;
28 import org
.jetbrains
.annotations
.NotNull
;
29 import org
.jetbrains
.annotations
.Nullable
;
36 public abstract class CreateTemplateInPackageAction
<T
extends PsiElement
> extends AnAction
{
37 protected CreateTemplateInPackageAction(String text
, String description
, Icon icon
) {
38 super(text
, description
, icon
);
41 public final void actionPerformed(final AnActionEvent e
) {
42 final DataContext dataContext
= e
.getDataContext();
44 final IdeView view
= LangDataKeys
.IDE_VIEW
.getData(dataContext
);
49 final Project project
= PlatformDataKeys
.PROJECT
.getData(dataContext
);
51 final PsiDirectory dir
= view
.getOrChooseDirectory();
52 if (dir
== null) return;
54 final T createdElement
= buildDialog(project
, dir
).show(getErrorTitle(), new CreateFileFromTemplateDialog
.FileCreator
<T
>() {
55 public void checkBeforeCreate(@NotNull String name
, @NotNull String templateName
) {
56 CreateTemplateInPackageAction
.this.checkOrCreate(name
, dir
, templateName
, true);
59 public T
createFile(@NotNull String name
, @NotNull String templateName
) {
60 return CreateTemplateInPackageAction
.this.checkOrCreate(name
, dir
, templateName
, false);
64 public String
getActionName(@NotNull String name
, @NotNull String templateName
) {
65 return CreateTemplateInPackageAction
.this.getActionName(dir
, name
, templateName
);
68 if (createdElement
!= null) {
69 final PsiElement navigationElement
= getNavigationElement(createdElement
);
70 view
.selectElement(navigationElement
== null ? createdElement
: navigationElement
);
75 protected abstract PsiElement
getNavigationElement(@NotNull T createdElement
);
78 protected abstract CreateFileFromTemplateDialog
.Builder
buildDialog(Project project
, PsiDirectory directory
);
80 public void update(final AnActionEvent e
) {
81 final DataContext dataContext
= e
.getDataContext();
82 final Presentation presentation
= e
.getPresentation();
84 final boolean enabled
= isAvailable(dataContext
);
86 presentation
.setVisible(enabled
);
87 presentation
.setEnabled(enabled
);
90 protected boolean isAvailable(final DataContext dataContext
) {
91 final Project project
= PlatformDataKeys
.PROJECT
.getData(dataContext
);
92 final IdeView view
= LangDataKeys
.IDE_VIEW
.getData(dataContext
);
93 if (project
== null || view
== null || view
.getDirectories().length
== 0) {
97 ProjectFileIndex projectFileIndex
= ProjectRootManager
.getInstance(project
).getFileIndex();
98 for (PsiDirectory dir
: view
.getDirectories()) {
99 if (projectFileIndex
.isInSourceContent(dir
.getVirtualFile()) && JavaDirectoryService
.getInstance().getPackage(dir
) != null) {
108 private T
checkOrCreate(String newName
, PsiDirectory directory
, String templateName
, boolean check
) throws IncorrectOperationException
{
109 PsiDirectory dir
= directory
;
110 String className
= newName
;
112 if (newName
.contains(".")) {
113 String
[] names
= newName
.split("\\.");
115 for (int i
= 0; i
< names
.length
- 1; i
++) {
116 String name
= names
[i
];
117 PsiDirectory subDir
= dir
.findSubdirectory(name
);
119 if (subDir
== null) {
121 dir
.checkCreateSubdirectory(name
);
125 subDir
= dir
.createSubdirectory(name
);
131 className
= names
[names
.length
- 1];
135 doCheckCreate(dir
, className
, templateName
);
138 return doCreate(dir
, className
, templateName
);
141 protected void doCheckCreate(PsiDirectory dir
, String className
, String templateName
) throws IncorrectOperationException
{
142 JavaDirectoryService
.getInstance().checkCreateClass(dir
, className
);
145 protected abstract T
doCreate(final PsiDirectory dir
, final String className
, String templateName
) throws IncorrectOperationException
;
147 protected abstract String
getActionName(PsiDirectory directory
, String newName
, String templateName
);
149 protected abstract String
getErrorTitle();