Create JavaEE Web Page action

This commit is contained in:
Dmitry Avdeev
2010-08-02 14:06:23 +04:00
parent a89603e5c0
commit 0340b76b81
4 changed files with 157 additions and 57 deletions
@@ -0,0 +1,74 @@
/*
* 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.ide.fileTemplates.FileTemplate;
import com.intellij.ide.fileTemplates.FileTemplateManager;
import com.intellij.ide.fileTemplates.FileTemplateUtil;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import javax.swing.*;
/**
* @author Dmitry Avdeev
*/
public abstract class CreateFileFromTemplateAction extends CreateFromTemplateAction<PsiFile> {
public CreateFileFromTemplateAction(String text, String description, Icon icon) {
super(text, description, icon);
}
protected PsiFile createFileFromTemplate(String name, FileTemplate template, PsiDirectory dir) {
PsiElement element;
try {
element = FileTemplateUtil
.createFromTemplate(template, name, FileTemplateManager.getInstance().getDefaultProperties(), dir);
final PsiFile psiFile = element.getContainingFile();
final VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile != null) {
FileEditorManager.getInstance(dir.getProject()).openFile(virtualFile, true);
String property = getDefaultTemplateProperty();
if (property != null) {
PropertiesComponent.getInstance(dir.getProject()).setValue(property, template.getName());
}
return psiFile;
}
}
catch (IncorrectOperationException e) {
throw e;
}
catch (Exception e) {
LOG.error(e);
}
return null;
}
@Override
protected PsiFile createFile(String name, String templateName, PsiDirectory dir) {
final FileTemplate template = FileTemplateManager.getInstance().getInternalTemplate(templateName);
return createFileFromTemplate(name, template, dir);
}
}
@@ -17,19 +17,13 @@ package com.intellij.ide.actions;
import com.intellij.CommonBundle;
import com.intellij.ide.IdeView;
import com.intellij.ide.fileTemplates.FileTemplate;
import com.intellij.ide.fileTemplates.FileTemplateManager;
import com.intellij.ide.fileTemplates.FileTemplateUtil;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -39,37 +33,12 @@ import javax.swing.*;
* @author Eugene.Kudelevsky
*/
public abstract class CreateFromTemplateAction<T extends PsiElement> extends AnAction {
private static final Logger LOG = Logger.getInstance("#com.intellij.ide.actions.CreateFromTemplateAction");
protected static final Logger LOG = Logger.getInstance("#com.intellij.ide.actions.CreateFromTemplateAction");
public CreateFromTemplateAction(String text, String description, Icon icon) {
super(text, description, icon);
}
protected static PsiFile createFileFromTemplate(String name, String templateName, PsiDirectory dir) {
final FileTemplate template = FileTemplateManager.getInstance().getInternalTemplate(templateName);
PsiElement element;
try {
element = FileTemplateUtil
.createFromTemplate(template, name, FileTemplateManager.getInstance().getDefaultProperties(), dir);
final PsiFile psiFile = element.getContainingFile();
final VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile != null) {
FileEditorManager.getInstance(dir.getProject()).openFile(virtualFile, true);
return psiFile;
}
}
catch (IncorrectOperationException e) {
throw e;
}
catch (Exception e) {
LOG.error(e);
}
return null;
}
public final void actionPerformed(final AnActionEvent e) {
final DataContext dataContext = e.getDataContext();
@@ -105,22 +74,30 @@ public abstract class CreateFromTemplateAction<T extends PsiElement> extends AnA
});
if (createdElement != null) {
view.selectElement(createdElement);
postProcesss(createdElement, selectedTemplateName.get());
postProcess(createdElement, selectedTemplateName.get());
}
}
protected void postProcesss(T createdElement, String templateName) {
protected void postProcess(T createdElement, String templateName) {
}
@Nullable
protected abstract T createFile(String name, String templateName, PsiDirectory dir);
protected abstract void checkBeforeCreate(String name, String templateName, PsiDirectory dir);
protected void checkBeforeCreate(String name, String templateName, PsiDirectory dir) {
dir.checkCreateFile(name);
}
protected abstract void buildDialog(Project project, PsiDirectory directory, CreateFileFromTemplateDialog.Builder builder);
@Nullable
protected String getDefaultTemplateName(@NotNull PsiDirectory dir) {
String property = getDefaultTemplateProperty();
return property == null ? null : PropertiesComponent.getInstance(dir.getProject()).getValue(property);
}
@Nullable
protected String getDefaultTemplateProperty() {
return null;
}
@@ -0,0 +1,56 @@
/*
* 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.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.psi.PsiDirectory;
import javax.swing.*;
/**
* @author Dmitry Avdeev
*/
public abstract class TemplateKindProvider {
private final static ExtensionPointName<TemplateKindProvider> EP_NAME =
ExtensionPointName.create("com.intellij.javaee.templateKindProvider");
public static void addAdditionalKinds(AnAction action, PsiDirectory dir, CreateFileFromTemplateDialog.Builder builder) {
String id = ActionManager.getInstance().getId(action);
for (TemplateKindProvider provider : Extensions.getExtensions(EP_NAME)) {
for (Kind kind : provider.getAdditionalKinds(dir)) {
builder.addKind(kind.name, kind.icon, kind.templateName);
}
}
}
public abstract boolean isAvailable(Class<? extends AnAction> actionClass);
public abstract Kind[] getAdditionalKinds(PsiDirectory dir);
public static class Kind {
public final String name;
public final String templateName;
public final Icon icon;
public Kind(String name, String templateName, Icon icon) {
this.name = name;
this.templateName = templateName;
this.icon = icon;
}
}
}
@@ -16,21 +16,16 @@
package com.intellij.ide.actions;
import com.intellij.ide.fileTemplates.FileTemplateManager;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.xml.XmlBundle;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
/**
* @author Eugene.Kudelevsky
*/
public class CreateHtmlFileAction extends CreateFromTemplateAction<PsiFile> {
private static final Logger LOG = Logger.getInstance("#com.intellij.ide.actions.CreateHtmlFileAction");
public class CreateHtmlFileAction extends CreateFileFromTemplateAction {
@NonNls private static final String DEFAULT_HTML_TEMPLATE_PROPERTY = "DefaultHtmlFileTemplate";
@@ -39,15 +34,8 @@ public class CreateHtmlFileAction extends CreateFromTemplateAction<PsiFile> {
}
@Override
protected PsiFile createFile(String name, String templateName, PsiDirectory dir) {
PropertiesComponent.getInstance(dir.getProject()).setValue(DEFAULT_HTML_TEMPLATE_PROPERTY, templateName);
return createFileFromTemplate(name, templateName, dir);
}
@Override
protected void checkBeforeCreate(String name, String templateName, PsiDirectory dir) {
dir.checkCreateFile(name);
protected String getDefaultTemplateProperty() {
return DEFAULT_HTML_TEMPLATE_PROPERTY;
}
@Override
@@ -59,13 +47,18 @@ public class CreateHtmlFileAction extends CreateFromTemplateAction<PsiFile> {
.addKind("XHTML file", StdFileTypes.XHTML.getIcon(), FileTemplateManager.INTERNAL_XHTML_TEMPLATE_NAME);
}
@Override
protected String getDefaultTemplateName(@NotNull PsiDirectory dir) {
return PropertiesComponent.getInstance(dir.getProject()).getValue(DEFAULT_HTML_TEMPLATE_PROPERTY);
}
@Override
protected String getActionName(PsiDirectory directory, String newName, String templateName) {
return XmlBundle.message("new.html.file.action");
}
@Override
public int hashCode() {
return 0;
}
@Override
public boolean equals(Object obj) {
return obj instanceof CreateHtmlFileAction;
}
}