update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / ide / actions / CreateTemplateInPackageAction.java
blob423118294fc3151b74a42352b5b30e937d94ed4f
1 /*
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;
31 import javax.swing.*;
33 /**
34 * @author peter
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);
45 if (view == null) {
46 return;
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);
63 @NotNull
64 public String getActionName(@NotNull String name, @NotNull String templateName) {
65 return CreateTemplateInPackageAction.this.getActionName(dir, name, templateName);
67 });
68 if (createdElement != null) {
69 final PsiElement navigationElement = getNavigationElement(createdElement);
70 view.selectElement(navigationElement == null ? createdElement : navigationElement);
74 @Nullable
75 protected abstract PsiElement getNavigationElement(@NotNull T createdElement);
77 @NotNull
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) {
94 return false;
97 ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
98 for (PsiDirectory dir : view.getDirectories()) {
99 if (projectFileIndex.isInSourceContent(dir.getVirtualFile()) && JavaDirectoryService.getInstance().getPackage(dir) != null) {
100 return true;
104 return false;
107 @Nullable
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) {
120 if (check) {
121 dir.checkCreateSubdirectory(name);
122 return null;
125 subDir = dir.createSubdirectory(name);
128 dir = subDir;
131 className = names[names.length - 1];
134 if (check) {
135 doCheckCreate(dir, className, templateName);
136 return null;
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();