Permalink
Browse files

Aman - Plugin now adds a JBehave story template. New stories ate crea…

…ted based on the template and automatically opened in the editor after creation.
  • Loading branch information...
kumaraman21 committed Nov 22, 2011
1 parent a4d5f47 commit d6a690907c884b0e2e361a408f964b2eb4116a5f
View
@@ -1,10 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PLUGIN_MODULE" version="4">
<component name="DevKit.ModuleBuildProperties" url="file://$MODULE_DIR$/META-INF/plugin.xml" />
- <component name="NewModuleRootManager" inherit-compiler-output="true">
+ <component name="NewModuleRootManager" inherit-compiler-output="false">
+ <output url="file://$MODULE_DIR$/out" />
+ <output-test url="file://$MODULE_DIR$/out/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+ <sourceFolder url="file://$MODULE_DIR$/resources" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="IDEA IC-107.587" jdkType="IDEA JDK" />
<orderEntry type="sourceFolder" forTests="false" />
View
@@ -21,15 +21,16 @@
<idea-version since-build="8000"/>
<application-components>
- <!-- Add your application components here -->
+ <component>
+ <implementation-class>com.github.kumaraman21.intellijbehave.JBehaveTemplateLoaderComponent</implementation-class>
+ </component>
</application-components>
<project-components>
<!-- Add your project components here -->
</project-components>
<actions>
- <!-- Add your actions here -->
<action id="IntelliJBehave.NewJBehaveStory" class="com.github.kumaraman21.intellijbehave.CreateStoryAction" text="JBhehave Story" description="Create New JBehave Story">
<add-to-group group-id="NewGroup1" anchor="after" relative-to-action="NewClass"/>
<keyboard-shortcut keymap="$default" first-keystroke="ctrl alt S"/>
@@ -0,0 +1,11 @@
+Meta:
+
+Narrative:
+As a user
+I want to perform an action
+So that I can achieve a business goal
+
+Scenario: scenario description
+Given a system state
+When I do something
+Then system is in a different state
@@ -17,16 +17,23 @@
import com.intellij.ide.IdeBundle;
import com.intellij.ide.actions.CreateElementActionBase;
+import com.intellij.ide.fileTemplates.FileTemplate;
+import com.intellij.ide.fileTemplates.FileTemplateManager;
import com.intellij.ide.highlighter.HtmlFileType;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
+import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
+import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
+import com.intellij.psi.PsiFile;
+import com.intellij.psi.PsiFileFactory;
+import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
@@ -50,14 +57,31 @@ public CreateStoryAction() {
@Override
protected void checkBeforeCreate(String newName, PsiDirectory directory) throws IncorrectOperationException {
- newName = getName(newName);
+ newName = getFileName(newName);
directory.checkCreateFile(newName);
}
@NotNull
@Override
protected PsiElement[] create(String newName, PsiDirectory directory) throws Exception {
- return new PsiElement[]{directory.createFile(getName(newName))};
+ final FileTemplate template = FileTemplateManager.getInstance().getTemplate(STORY_FILE_TYPE.getName());
+
+ String fileName = getFileName(newName);
+ Project project = directory.getProject();
+
+ directory.checkCreateFile(fileName);
+ PsiFile psiFile = PsiFileFactory.getInstance(project)
+ .createFileFromText(fileName, STORY_FILE_TYPE, template.getText());
+
+ if (template.isReformatCode()) {
+ CodeStyleManager.getInstance(project).reformat(psiFile);
+ }
+ psiFile = (PsiFile)directory.add(psiFile);
+
+ final VirtualFile virtualFile = psiFile.getVirtualFile();
+ FileEditorManager.getInstance(project).openFile(virtualFile, true);
+
+ return new PsiElement[]{psiFile};
}
@Override
@@ -86,7 +110,7 @@ public void update(final AnActionEvent e) {
}
}
- private String getName(String name) {
+ private String getFileName(String name) {
return name + "." + STORY_FILE_TYPE.getDefaultExtension();
}
}
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2000-2011 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.github.kumaraman21.intellijbehave;
+
+import com.intellij.ide.fileTemplates.FileTemplate;
+import com.intellij.ide.fileTemplates.FileTemplateManager;
+import com.intellij.openapi.components.ApplicationComponent;
+import org.jetbrains.annotations.NotNull;
+
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+import static com.github.kumaraman21.intellijbehave.StoryFileType.STORY_FILE_TYPE;
+import static com.intellij.openapi.util.io.FileUtil.loadTextAndClose;
+
+public class JBehaveTemplateLoaderComponent implements ApplicationComponent {
+ @Override
+ public void initComponent() {
+ FileTemplate template = FileTemplateManager.getInstance().getTemplate(STORY_FILE_TYPE.getName());
+ if (template == null) {
+ template = FileTemplateManager.getInstance()
+ .addTemplate(STORY_FILE_TYPE.getName(), STORY_FILE_TYPE.getDefaultExtension());
+ try {
+ template.setText(
+ loadTextAndClose(new InputStreamReader(getClass().getResourceAsStream("/fileTemplates/JBehave Story.story.ft"))));
+ }
+ catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ @Override
+ public void disposeComponent() {
+ // do nothing
+ }
+
+ @NotNull
+ @Override
+ public String getComponentName() {
+ return this.getClass().getName();
+ }
+}

0 comments on commit d6a6909

Please sign in to comment.