From 9a0cd86ff2347f2d0f89e050244e59464c1b1a28 Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Wed, 21 Dec 2016 14:32:51 +0300 Subject: [PATCH] Javafx: New FXML file template (IDEA-106502) --- .../ide/actions/CreateModuleInfoAction.java | 3 +- .../actions/CreateFromTemplateActionBase.java | 4 +- .../fileTemplates/internal/FxmlFile.fxml.ft | 14 +++ .../fileTemplates/internal/FxmlFile.fxml.html | 5 + .../src/META-INF/common-javaFX-plugin.xml | 3 + .../javaFX/actions/CreateFxmlFileAction.java | 108 ++++++++++++++++++ 6 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 plugins/javaFX/resources/fileTemplates/internal/FxmlFile.fxml.ft create mode 100644 plugins/javaFX/resources/fileTemplates/internal/FxmlFile.fxml.html create mode 100644 plugins/javaFX/src/org/jetbrains/plugins/javaFX/actions/CreateFxmlFileAction.java diff --git a/java/java-impl/src/com/intellij/ide/actions/CreateModuleInfoAction.java b/java/java-impl/src/com/intellij/ide/actions/CreateModuleInfoAction.java index d41e9dbf5317..296b40e2ecc0 100644 --- a/java/java-impl/src/com/intellij/ide/actions/CreateModuleInfoAction.java +++ b/java/java-impl/src/com/intellij/ide/actions/CreateModuleInfoAction.java @@ -33,6 +33,7 @@ import com.intellij.openapi.roots.ProjectRootManager; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; import com.intellij.psi.search.FilenameIndex; import com.intellij.psi.util.PsiUtil; import org.jetbrains.annotations.NotNull; @@ -93,7 +94,7 @@ public class CreateModuleInfoAction extends CreateFromTemplateActionBase { } @Override - protected Map getLiveTemplateDefaults(@NotNull DataContext ctx) { + protected Map getLiveTemplateDefaults(@NotNull DataContext ctx, @NotNull PsiFile file) { Module module = LangDataKeys.MODULE.getData(ctx); return Collections.singletonMap("MODULE_NAME", module != null ? StringUtil.sanitizeJavaIdentifier(module.getName()) : "module_name"); } diff --git a/platform/lang-impl/src/com/intellij/ide/fileTemplates/actions/CreateFromTemplateActionBase.java b/platform/lang-impl/src/com/intellij/ide/fileTemplates/actions/CreateFromTemplateActionBase.java index 2f8c592aac8b..fe50d4775684 100644 --- a/platform/lang-impl/src/com/intellij/ide/fileTemplates/actions/CreateFromTemplateActionBase.java +++ b/platform/lang-impl/src/com/intellij/ide/fileTemplates/actions/CreateFromTemplateActionBase.java @@ -71,7 +71,7 @@ public abstract class CreateFromTemplateActionBase extends AnAction { elementCreated(dialog, createdElement); view.selectElement(createdElement); if (selectedTemplate.isLiveTemplateEnabled() && createdElement instanceof PsiFile) { - Map defaultValues = getLiveTemplateDefaults(dataContext); + Map defaultValues = getLiveTemplateDefaults(dataContext, ((PsiFile)createdElement)); startLiveTemplate((PsiFile)createdElement, notNull(defaultValues, Collections.emptyMap())); } } @@ -129,7 +129,7 @@ public abstract class CreateFromTemplateActionBase extends AnAction { protected void elementCreated(CreateFromTemplateDialog dialog, PsiElement createdElement) { } @Nullable - protected Map getLiveTemplateDefaults(DataContext dataContext) { + protected Map getLiveTemplateDefaults(DataContext dataContext, @NotNull PsiFile file) { return null; } } \ No newline at end of file diff --git a/plugins/javaFX/resources/fileTemplates/internal/FxmlFile.fxml.ft b/plugins/javaFX/resources/fileTemplates/internal/FxmlFile.fxml.ft new file mode 100644 index 000000000000..84df1e1512b2 --- /dev/null +++ b/plugins/javaFX/resources/fileTemplates/internal/FxmlFile.fxml.ft @@ -0,0 +1,14 @@ + + + + + + + + + + + diff --git a/plugins/javaFX/resources/fileTemplates/internal/FxmlFile.fxml.html b/plugins/javaFX/resources/fileTemplates/internal/FxmlFile.fxml.html new file mode 100644 index 000000000000..099a5a37dca4 --- /dev/null +++ b/plugins/javaFX/resources/fileTemplates/internal/FxmlFile.fxml.html @@ -0,0 +1,5 @@ + + +This is a built-in template for FXML file. + + \ No newline at end of file diff --git a/plugins/javaFX/src/META-INF/common-javaFX-plugin.xml b/plugins/javaFX/src/META-INF/common-javaFX-plugin.xml index 65515493b6db..53dd134129ee 100644 --- a/plugins/javaFX/src/META-INF/common-javaFX-plugin.xml +++ b/plugins/javaFX/src/META-INF/common-javaFX-plugin.xml @@ -79,5 +79,8 @@ + + + diff --git a/plugins/javaFX/src/org/jetbrains/plugins/javaFX/actions/CreateFxmlFileAction.java b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/actions/CreateFxmlFileAction.java new file mode 100644 index 000000000000..d3648dfc12d2 --- /dev/null +++ b/plugins/javaFX/src/org/jetbrains/plugins/javaFX/actions/CreateFxmlFileAction.java @@ -0,0 +1,108 @@ +/* + * Copyright 2000-2016 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 org.jetbrains.plugins.javaFX.actions; + +import com.intellij.icons.AllIcons; +import com.intellij.ide.fileTemplates.FileTemplate; +import com.intellij.ide.fileTemplates.FileTemplateManager; +import com.intellij.ide.fileTemplates.actions.CreateFromTemplateActionBase; +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.ProjectFileIndex; +import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.util.Computable; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; +import com.intellij.util.PathUtil; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.plugins.javaFX.fxml.JavaFxFileTypeFactory; + +import java.util.Collections; +import java.util.Map; + +/** + * @author pdolgov + */ +public class CreateFxmlFileAction extends CreateFromTemplateActionBase { + private static final String INTERNAL_TEMPLATE_NAME = "FxmlFile.fxml"; + + public CreateFxmlFileAction() { + super("FXML File", "Create New FXML File", AllIcons.FileTypes.Xml); + } + + @Override + protected FileTemplate getTemplate(Project project, PsiDirectory dir) { + return FileTemplateManager.getInstance(project).getInternalTemplate(INTERNAL_TEMPLATE_NAME); + } + + @Nullable + @Override + protected Map getLiveTemplateDefaults(DataContext dataContext, @NotNull PsiFile file) { + String packageName = ApplicationManager.getApplication().runReadAction((Computable)() -> { + PsiDirectory psiDirectory = file.getContainingDirectory(); + if (psiDirectory != null) { + VirtualFile vDirectory = psiDirectory.getVirtualFile(); + ProjectFileIndex index = ProjectRootManager.getInstance(file.getProject()).getFileIndex(); + if (index.isInSourceContent(vDirectory)) { + return index.getPackageNameByDirectory(vDirectory); + } + } + return null; + }); + @NonNls String name = file.getName(); + name = PathUtil.getFileName(name); + if (JavaFxFileTypeFactory.FXML_EXTENSION.equals(PathUtil.getFileExtension(name))) { + name = name.substring(0, name.length() - JavaFxFileTypeFactory.FXML_EXTENSION.length() - 1); + } + + name = toClassName(name); + name = !StringUtil.isEmpty(packageName) ? packageName + "." + name : name; + return Collections.singletonMap("CONTROLLER_NAME", name); + } + + private static String toClassName(String name) { + int start; + for (start = 0; start < name.length(); start++) { + char c = name.charAt(start); + if (Character.isJavaIdentifierStart(c) && c != '_' && c != '$') { + break; + } + } + StringBuilder className = new StringBuilder(); + boolean skip = true; + + for (int i = start; i < name.length(); i++) { + char c = name.charAt(i); + if (!Character.isJavaIdentifierPart(c) || c == '_' || c == '$') { + skip = true; + continue; + } + if (skip) { + skip = false; + className.append(Character.toUpperCase(c)); + } + else { + className.append(c); + } + } + return className.toString(); + } +}