├── .editorconfig ├── .gitignore ├── META-INF └── plugin.xml ├── README.md ├── gist-templates-plugin.iml ├── images ├── generate.png ├── generateProject-2.png ├── generateProject.png ├── github-settings.png ├── plugin-settings.png └── templates.png ├── intellij-11-adapter ├── intellij-11-adapter.iml └── src │ └── com │ └── gisttemplates │ ├── action │ └── EditorActionFactory11.java │ ├── github │ └── Github11.java │ ├── gui │ └── GUIFactory11.java │ └── icons │ └── Icons11.java ├── intellij-12-adapter ├── intellij-12-adapter.iml └── src │ └── com │ └── gisttemplates │ ├── action │ └── EditorActionFactory12.java │ ├── github │ └── Github12.java │ ├── gui │ └── GUIFactory12.java │ └── icons │ └── Icons12.java ├── intellij-13-adapter ├── intellij-13-adapter.iml └── src │ └── com │ └── gisttemplates │ ├── action │ └── EditorActionFactory13.java │ ├── github │ └── Github13.java │ ├── gui │ └── GUIFactory13.java │ └── icons │ └── Icons13.java ├── intellij-adapter-api ├── intellij-adapter-api.iml └── src │ └── com │ └── gisttemplates │ ├── adapter │ ├── EditorActionFactory.java │ ├── GUIFactory.java │ ├── GithubAdapter.java │ └── Icons.java │ └── api │ ├── GistService.java │ ├── GistTemplate.java │ └── LazyGistTemplate.java ├── lib ├── gson-2.2.2.jar └── org.eclipse.egit.github.core-2.1.5.jar └── src ├── main └── java │ └── com │ └── gisttemplates │ ├── GistTemplatesApplication.java │ ├── action │ └── GistTemplateEditorAction.java │ ├── configuration │ ├── GistTemplatesConfigurable.form │ ├── GistTemplatesConfigurable.java │ └── GistTemplatesSettings.java │ ├── gist │ ├── GistAccountFetcher.java │ ├── GistServiceImpl.java │ └── LoadGistListTask.java │ ├── gistfiles │ ├── GistFileCreator.java │ ├── GistFilesDialog.form │ ├── GistFilesDialog.java │ └── NewFileFromGistAction.java │ └── utils │ └── MyDataKeys.java └── test └── java └── com └── gisttemplates └── gistfiles └── GistFilesDialogTest.java /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | indent_style = space 7 | indent_size = 4 8 | 9 | [*.java] 10 | insert_final_newline = true 11 | trim_trailing_whitespace = true 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Intellij 2 | .idea/ 3 | *.iws 4 | *.ipr 5 | 6 | # Mac 7 | .DS_Store 8 | 9 | # Maven 10 | log/ 11 | out/ 12 | -------------------------------------------------------------------------------- /META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | com.gisttemplates 3 | Gist Templates 4 | 0.3.6 5 | Geoffroy Warin 6 | 7 | 9 | 10 | If you like the plugin don't forget to rank and comment the plugin on JetBrains site.
11 | 12 | If you don't, please drop by our github repository and submit an 13 | issue or propose an improvement !
14 | ]]>
15 | 16 | 18 |
  • v0.3.6 : Fixed compatibility issues with API 131 (AppCode, Intellij 13.0...)
  • 19 |
  • v0.3.5 : More information in case of error while fetching gists
  • 20 |
  • v0.3.4 : Fix compatibility with intellij 13.1
  • 21 |
  • v0.3.3 : Bug fix
  • 22 |
  • v0.3.2 : Fix compatibility issue in intellij 11 and minor GUI improvements
  • 23 |
  • v0.3.1 : Fix bug in intellij 12
  • 24 |
  • v0.3 : New feature >> ability to add one or several gist files directly from the "New" actions in project view !
  • 25 |
  • v0.2 : Templates are now lazy loaded. The menu appears more quickly and gists are always up to date.
  • 26 |
  • v0.12 : Fixes a crash in intellij 13
  • 27 |
  • v0.11 : Support for intellij 11 to 13
  • 28 |
  • v0.1 : basic support for user's github account and his favorites
  • 29 | 30 | ]]> 31 |
    32 | 33 | 34 | 35 | 36 | com.intellij.modules.lang 37 | org.jetbrains.plugins.github 38 | 39 | 40 | 41 | com.gisttemplates.GistTemplatesApplication 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 |
    -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gist Templates IntelliJ Plugin 2 | 3 | A plugin to use your gists as live templates ! 4 | [http://geowarin.github.io/gist-templates-plugin/](http://geowarin.github.io/gist-templates-plugin/) 5 | 6 | ## Installation 7 | 8 | The plugin is available in `browse repositories` in intellij plugins 9 | [http://plugins.jetbrains.com/plugin/7400](http://plugins.jetbrains.com/plugin/7400) 10 | 11 | We spend a lot of time making it compatible with as much versions of JetBrains IDEs as possible. 12 | Supported and tested versions are : intellij-11, intellij-12 and intellij-13. 13 | 14 | It should also be compatible with other IDEs like WebStorm, Appcode, etc. if it is not the case, please [report the problem](https://github.com/geowarin/gist-templates-plugin/issues) 15 | 16 | ## Usage 17 | 18 | 1. Configure your github account in intellij 19 | 20 | ![image](/images/github-settings.png) 21 | 22 | 2. Allow the plugin to use your github identification 23 | 24 | ![image](/images/plugin-settings.png) 25 | 26 | 3. The gist templates appear in the code > generate menu 27 | 28 | ![image](/images/generate.png) 29 | 30 | ![image](/images/templates.png) 31 | 32 | ## Generate files from "New" dialog in project view 33 | 34 | In version 0.3 you can 35 | 36 | 1. Access the new menu and select "Files from gist..." 37 | 38 | ![image](/images/generateProject.png) 39 | 40 | 2. Then select a gist and the files you are interested in 41 | 42 | ![image](/images/generateProject-2.png) 43 | 44 | ## Change log 45 | 46 | * v0.3.6 : Fixed compatibility issues with API 131 (AppCode, Intellij 13.0...) 47 | * v0.3.5 : More information in case of error while fetching gists 48 | * v0.3.4 : Fixed compatibility with intellij 13.1 49 | * v0.3.3 : Bug fix 50 | * v0.3.2 : Fix compatibility issue in intellij 11 and minor GUI improvements 51 | * v0.3.1 : Fix compatibility issue in intellij 12 52 | * v0.3 : New feature >> ability to add one or several gist files directly from the "New" actions in project view ! 53 | * v0.2 : Templates are now lazy loaded. The menu appears more quickly and gists are always up to date. 54 | * v0.12 : Fixes a crash in intellij 13 55 | * v0.11 : Support for intellij 11 to 13 56 | * v0.1 : basic support for user's github account and his favorites 57 | 58 | 59 | ## Road Map 60 | 61 | * Support file type for smarter propositions 62 | * Support variables in templates 63 | * Support multiple files gists 64 | * Assist user when creating a template from the IDE -------------------------------------------------------------------------------- /gist-templates-plugin.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /images/generate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geowarin/gist-templates-plugin/f4596984f25aba5e1d8f4a5c8692b55be42c8452/images/generate.png -------------------------------------------------------------------------------- /images/generateProject-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geowarin/gist-templates-plugin/f4596984f25aba5e1d8f4a5c8692b55be42c8452/images/generateProject-2.png -------------------------------------------------------------------------------- /images/generateProject.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geowarin/gist-templates-plugin/f4596984f25aba5e1d8f4a5c8692b55be42c8452/images/generateProject.png -------------------------------------------------------------------------------- /images/github-settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geowarin/gist-templates-plugin/f4596984f25aba5e1d8f4a5c8692b55be42c8452/images/github-settings.png -------------------------------------------------------------------------------- /images/plugin-settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geowarin/gist-templates-plugin/f4596984f25aba5e1d8f4a5c8692b55be42c8452/images/plugin-settings.png -------------------------------------------------------------------------------- /images/templates.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geowarin/gist-templates-plugin/f4596984f25aba5e1d8f4a5c8692b55be42c8452/images/templates.png -------------------------------------------------------------------------------- /intellij-11-adapter/intellij-11-adapter.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /intellij-11-adapter/src/com/gisttemplates/action/EditorActionFactory11.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.action; 2 | 3 | import com.gisttemplates.adapter.EditorActionFactory; 4 | import com.gisttemplates.api.GistService; 5 | import com.gisttemplates.api.GistTemplate; 6 | import com.gisttemplates.api.LazyGistTemplate; 7 | import com.intellij.codeInsight.template.impl.ListTemplatesHandler; 8 | import com.intellij.codeInsight.template.impl.TemplateImpl; 9 | import com.intellij.openapi.actionSystem.DataContext; 10 | import com.intellij.openapi.editor.Editor; 11 | import com.intellij.openapi.editor.actionSystem.EditorActionHandler; 12 | 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | 16 | /** 17 | * Date: 27/03/2014 18 | * Time: 07:07 19 | * 20 | * @author Geoffroy Warin (http://geowarin.github.io) 21 | */ 22 | public class EditorActionFactory11 extends EditorActionFactory { 23 | 24 | @Override public EditorActionHandler createActionHandler() { 25 | return new GistTemplateActionHandler(); 26 | } 27 | 28 | private class GistTemplateActionHandler extends EditorActionHandler { 29 | @Override 30 | public void execute(Editor editor, DataContext dataContext) { 31 | ListTemplatesHandler.showTemplatesLookup(editor.getProject(), editor, "", createGistTemplatesList()); 32 | } 33 | 34 | private List createGistTemplatesList() { 35 | List templates = new ArrayList(); 36 | for (GistTemplate gist : GistService.getInstance().fetchGistList()) { 37 | templates.add(new LazyGistTemplate(gist)); 38 | } 39 | return templates; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /intellij-11-adapter/src/com/gisttemplates/github/Github11.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.github; 2 | 3 | import com.gisttemplates.adapter.GithubAdapter; 4 | import com.jgoodies.common.base.Strings; 5 | import org.jetbrains.plugins.github.GithubSettings; 6 | 7 | /** 8 | * Date: 25/02/2014 9 | * Time: 21:17 10 | * 11 | * @author Geoffroy Warin (http://geowarin.github.io) 12 | */ 13 | @SuppressWarnings("UnusedDeclaration") 14 | public class Github11 extends GithubAdapter { 15 | 16 | @Override 17 | public boolean isCredentialsDefined() { 18 | return Strings.isNotBlank(getLogin()); 19 | } 20 | 21 | @Override 22 | public String getLogin() { 23 | return GithubSettings.getInstance().getLogin(); 24 | } 25 | 26 | @Override 27 | public String getPassword() { 28 | return GithubSettings.getInstance().getPassword(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /intellij-11-adapter/src/com/gisttemplates/gui/GUIFactory11.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.gui; 2 | 3 | import com.gisttemplates.adapter.GUIFactory; 4 | import com.gisttemplates.adapter.Icons; 5 | import com.gisttemplates.api.GistTemplate; 6 | import com.intellij.ui.ColoredListCellRenderer; 7 | import com.intellij.ui.SimpleColoredText; 8 | import com.intellij.ui.SimpleTextAttributes; 9 | import org.jetbrains.annotations.NotNull; 10 | 11 | import javax.swing.*; 12 | import java.awt.*; 13 | 14 | /** 15 | * Date: 22/03/2014 16 | * Time: 12:35 17 | * 18 | * @author Geoffroy Warin (http://geowarin.github.io) 19 | */ 20 | @SuppressWarnings("UnusedDeclaration") 21 | public class GUIFactory11 extends GUIFactory { 22 | 23 | @Override public ListCellRenderer createGistTemplateColoredListCellRenderer() { 24 | return new GistTemplateColoredListCellRendererWrapper(); 25 | } 26 | 27 | private class GistTemplateColoredListCellRendererWrapper extends ColoredListCellRendererWrapper { 28 | @Override 29 | protected void doCustomize(JList list, GistTemplate template, int index, boolean selected, boolean hasFocus) { 30 | append(new SimpleColoredText(template.getFilename(), new SimpleTextAttributes(SimpleTextAttributes.STYLE_PLAIN, Color.BLACK))); 31 | setToolTipText(template.getDescription()); 32 | if (template.isStarred()) 33 | setIcon(Icons.getInstance().favorite()); 34 | else 35 | setIcon(Icons.getInstance().github()); 36 | } 37 | } 38 | 39 | 40 | public abstract class ColoredListCellRendererWrapper extends ColoredListCellRenderer { 41 | @Override 42 | protected final void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus) { 43 | @SuppressWarnings("unchecked") final T t = (T)value; 44 | doCustomize(list, t, index, selected, hasFocus); 45 | } 46 | 47 | protected abstract void doCustomize(JList list, T value, int index, boolean selected, boolean hasFocus); 48 | 49 | public void append(@NotNull SimpleColoredText text) { 50 | int length = text.getTexts().size(); 51 | for (int i = 0; i < length; i++) { 52 | String fragment = text.getTexts().get(i); 53 | SimpleTextAttributes attributes = text.getAttributes().get(i); 54 | append(fragment, attributes); 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /intellij-11-adapter/src/com/gisttemplates/icons/Icons11.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.icons; 2 | 3 | import com.gisttemplates.adapter.Icons; 4 | import com.intellij.openapi.util.IconLoader; 5 | import org.jetbrains.plugins.github.GithubUtil; 6 | 7 | import javax.swing.*; 8 | 9 | /** 10 | * Date: 22/03/2014 11 | * Time: 12:24 12 | * 13 | * @author Geoffroy Warin (http://geowarin.github.io) 14 | */ 15 | @SuppressWarnings("UnusedDeclaration") 16 | public class Icons11 extends Icons { 17 | private static final Icon Warning = IconLoader.getIcon("/general/balloonWarning.png"); 18 | public static final Icon Favorite = IconLoader.getIcon("/general/toolWindowFavorites.png"); 19 | public static final Icon Text = IconLoader.getIcon("/fileTypes/text.png"); 20 | 21 | 22 | @Override public Icon warning() { 23 | return Warning; 24 | } 25 | 26 | @Override public Icon github() { 27 | return GithubUtil.GITHUB_ICON; 28 | } 29 | 30 | @Override public Icon favorite() { 31 | return Favorite; 32 | } 33 | 34 | @Override public Icon textFile() { 35 | return Text; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /intellij-12-adapter/intellij-12-adapter.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /intellij-12-adapter/src/com/gisttemplates/action/EditorActionFactory12.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.action; 2 | 3 | import com.gisttemplates.adapter.EditorActionFactory; 4 | import com.gisttemplates.api.GistService; 5 | import com.gisttemplates.api.GistTemplate; 6 | import com.gisttemplates.api.LazyGistTemplate; 7 | import com.intellij.codeInsight.template.impl.ListTemplatesHandler; 8 | import com.intellij.codeInsight.template.impl.TemplateImpl; 9 | import com.intellij.openapi.actionSystem.DataContext; 10 | import com.intellij.openapi.editor.Editor; 11 | import com.intellij.openapi.editor.actionSystem.EditorActionHandler; 12 | 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | 16 | /** 17 | * Date: 27/03/2014 18 | * Time: 07:07 19 | * 20 | * @author Geoffroy Warin (http://geowarin.github.io) 21 | */ 22 | public class EditorActionFactory12 extends EditorActionFactory { 23 | 24 | @Override public EditorActionHandler createActionHandler() { 25 | return new GistTemplateActionHandler(); 26 | } 27 | 28 | private class GistTemplateActionHandler extends EditorActionHandler { 29 | @Override 30 | public void execute(Editor editor, DataContext dataContext) { 31 | ListTemplatesHandler.showTemplatesLookup(editor.getProject(), editor, "", createGistTemplatesList()); 32 | } 33 | 34 | private List createGistTemplatesList() { 35 | List templates = new ArrayList(); 36 | for (GistTemplate gist : GistService.getInstance().fetchGistList()) { 37 | templates.add(new LazyGistTemplate(gist)); 38 | } 39 | return templates; 40 | } 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /intellij-12-adapter/src/com/gisttemplates/github/Github12.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.github; 2 | 3 | import com.gisttemplates.adapter.GithubAdapter; 4 | import com.jgoodies.common.base.Strings; 5 | import org.jetbrains.plugins.github.GithubSettings; 6 | 7 | /** 8 | * Date: 25/02/2014 9 | * Time: 21:17 10 | * 11 | * @author Geoffroy Warin (http://geowarin.github.io) 12 | */ 13 | @SuppressWarnings("UnusedDeclaration") 14 | public class Github12 extends GithubAdapter { 15 | 16 | @Override 17 | public boolean isCredentialsDefined() { 18 | return Strings.isNotBlank(getLogin()); 19 | } 20 | 21 | @Override 22 | public String getLogin() { 23 | return GithubSettings.getInstance().getLogin(); 24 | } 25 | 26 | @Override 27 | public String getPassword() { 28 | return GithubSettings.getInstance().getPassword(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /intellij-12-adapter/src/com/gisttemplates/gui/GUIFactory12.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.gui; 2 | 3 | import com.gisttemplates.adapter.GUIFactory; 4 | import com.gisttemplates.api.GistTemplate; 5 | import com.gisttemplates.adapter.Icons; 6 | import com.intellij.ui.ColoredListCellRendererWrapper; 7 | import com.intellij.ui.JBColor; 8 | import com.intellij.ui.SimpleColoredText; 9 | import com.intellij.ui.SimpleTextAttributes; 10 | 11 | import javax.swing.*; 12 | 13 | /** 14 | * Date: 22/03/2014 15 | * Time: 12:36 16 | * 17 | * @author Geoffroy Warin (http://geowarin.github.io) 18 | */ 19 | @SuppressWarnings("UnusedDeclaration") 20 | public class GUIFactory12 extends GUIFactory { 21 | 22 | @Override public ListCellRenderer createGistTemplateColoredListCellRenderer() { 23 | return new GistTemplateColoredListCellRendererWrapper(); 24 | } 25 | 26 | private static class GistTemplateColoredListCellRendererWrapper extends ColoredListCellRendererWrapper { 27 | @Override 28 | protected void doCustomize(JList list, GistTemplate template, int index, boolean selected, boolean hasFocus) { 29 | append(new SimpleColoredText(template.getFilename(), new SimpleTextAttributes(SimpleTextAttributes.STYLE_PLAIN, JBColor.BLACK))); 30 | setToolTipText(template.getDescription()); 31 | if (template.isStarred()) 32 | setIcon(Icons.getInstance().favorite()); 33 | else 34 | setIcon(Icons.getInstance().github()); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /intellij-12-adapter/src/com/gisttemplates/icons/Icons12.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.icons; 2 | 3 | import com.gisttemplates.adapter.Icons; 4 | import com.intellij.icons.AllIcons; 5 | import icons.GithubIcons; 6 | 7 | import javax.swing.*; 8 | 9 | /** 10 | * Date: 22/03/2014 11 | * Time: 12:25 12 | * 13 | * @author Geoffroy Warin (http://geowarin.github.io) 14 | */ 15 | @SuppressWarnings("UnusedDeclaration") 16 | public class Icons12 extends Icons { 17 | @Override public Icon warning() { 18 | return AllIcons.General.BalloonWarning; 19 | } 20 | 21 | @Override public Icon github() { 22 | return GithubIcons.Github_icon; 23 | } 24 | 25 | @Override public Icon favorite() { 26 | return AllIcons.Toolwindows.ToolWindowFavorites; 27 | } 28 | 29 | @Override public Icon textFile() { 30 | return AllIcons.FileTypes.Text; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /intellij-13-adapter/intellij-13-adapter.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /intellij-13-adapter/src/com/gisttemplates/action/EditorActionFactory13.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.action; 2 | 3 | import com.gisttemplates.adapter.EditorActionFactory; 4 | import com.gisttemplates.api.GistService; 5 | import com.gisttemplates.api.GistTemplate; 6 | import com.gisttemplates.api.LazyGistTemplate; 7 | import com.intellij.codeInsight.completion.PlainPrefixMatcher; 8 | import com.intellij.codeInsight.lookup.*; 9 | import com.intellij.codeInsight.lookup.impl.LookupImpl; 10 | import com.intellij.codeInsight.template.TemplateManager; 11 | import com.intellij.codeInsight.template.impl.*; 12 | import com.intellij.featureStatistics.FeatureUsageTracker; 13 | import com.intellij.openapi.actionSystem.DataContext; 14 | import com.intellij.openapi.application.Result; 15 | import com.intellij.openapi.command.WriteCommandAction; 16 | import com.intellij.openapi.editor.Caret; 17 | import com.intellij.openapi.editor.Editor; 18 | import com.intellij.openapi.editor.actionSystem.EditorActionHandler; 19 | import com.intellij.openapi.project.Project; 20 | import com.intellij.openapi.util.Pair; 21 | import com.intellij.psi.PsiFile; 22 | import com.intellij.util.containers.ContainerUtil; 23 | import org.jetbrains.annotations.NotNull; 24 | import org.jetbrains.annotations.Nullable; 25 | 26 | import java.util.*; 27 | 28 | /** 29 | * Date: 27/03/2014 30 | * Time: 07:07 31 | * 32 | * @author Geoffroy Warin (http://geowarin.github.io) 33 | */ 34 | public class EditorActionFactory13 extends EditorActionFactory { 35 | 36 | @Override 37 | public EditorActionHandler createActionHandler() { 38 | return new GistTemplateActionHandler(); 39 | } 40 | 41 | private class GistTemplateActionHandler extends EditorActionHandler { 42 | @Override 43 | protected void doExecute(Editor editor, @Nullable Caret caret, DataContext dataContext) { 44 | showTemplatesLookup(editor.getProject(), editor, createGistTemplatesList()); 45 | } 46 | 47 | private List createGistTemplatesList() { 48 | List templates = new ArrayList(); 49 | for (GistTemplate gist : GistService.getInstance().fetchGistList()) { 50 | templates.add(new LazyGistTemplate(gist)); 51 | } 52 | return templates; 53 | } 54 | } 55 | 56 | public static void showTemplatesLookup(final Project project, final Editor editor, List gistTemplatesList) { 57 | final LookupImpl lookup = (LookupImpl) LookupManager.getInstance(project).createLookup(editor, LookupElement.EMPTY_ARRAY, "", new TemplatesArranger()); 58 | for (TemplateImpl template : gistTemplatesList) { 59 | lookup.addItem(createTemplateElement(template), new PlainPrefixMatcher("")); 60 | } 61 | showLookup(lookup); 62 | } 63 | 64 | private static void showLookup(LookupImpl lookup) { 65 | Editor editor = lookup.getEditor(); 66 | Project project = editor.getProject(); 67 | lookup.addLookupListener(new MyLookupAdapter(project, editor)); 68 | lookup.refreshUi(false, true); 69 | lookup.showLookup(); 70 | } 71 | 72 | private static LiveTemplateLookupElement createTemplateElement(final TemplateImpl template) { 73 | return new LiveTemplateLookupElementImpl(template, false) { 74 | @Override 75 | public Set getAllLookupStrings() { 76 | String description = template.getDescription(); 77 | if (description == null) { 78 | return super.getAllLookupStrings(); 79 | } 80 | return ContainerUtil.newHashSet(getLookupString(), description); 81 | } 82 | }; 83 | } 84 | 85 | private static class TemplatesArranger extends LookupArranger { 86 | @Override 87 | public Pair, Integer> arrangeItems(@NotNull Lookup lookup, boolean onExplicitAction) { 88 | LinkedHashSet result = new LinkedHashSet(); 89 | List items = getMatchingItems(); 90 | for (LookupElement item : items) { 91 | if (item.getLookupString().startsWith(lookup.itemPattern(item))) { 92 | result.add(item); 93 | } 94 | } 95 | result.addAll(items); 96 | ArrayList list = new ArrayList(result); 97 | int selected = lookup.isSelectionTouched() ? list.indexOf(lookup.getCurrentItem()) : 0; 98 | return new Pair, Integer>(list, selected >= 0 ? selected : 0); 99 | } 100 | 101 | @Override 102 | public LookupArranger createEmptyCopy() { 103 | return new TemplatesArranger(); 104 | } 105 | } 106 | 107 | private static class MyLookupAdapter extends LookupAdapter { 108 | private final Project myProject; 109 | private final Editor myEditor; 110 | private final PsiFile myFile; 111 | 112 | public MyLookupAdapter(Project project, Editor editor) { 113 | myProject = project; 114 | myEditor = editor; 115 | myFile = null; 116 | } 117 | 118 | @Override 119 | public void itemSelected(final LookupEvent event) { 120 | FeatureUsageTracker.getInstance().triggerFeatureUsed("codeassists.liveTemplates"); 121 | final LookupElement item = event.getItem(); 122 | if (item instanceof LiveTemplateLookupElementImpl) { 123 | final TemplateImpl template = ((LiveTemplateLookupElementImpl) item).getTemplate(); 124 | new WriteCommandAction(myProject) { 125 | @Override 126 | protected void run(@NotNull Result result) throws Throwable { 127 | ((TemplateManagerImpl) TemplateManager.getInstance(myProject)).startTemplateWithPrefix(myEditor, template, null, null); 128 | } 129 | }.execute(); 130 | } 131 | } 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /intellij-13-adapter/src/com/gisttemplates/github/Github13.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.github; 2 | 3 | import com.gisttemplates.adapter.GithubAdapter; 4 | import org.jetbrains.plugins.github.util.GithubAuthData; 5 | import org.jetbrains.plugins.github.util.GithubSettings; 6 | 7 | /** 8 | * Date: 25/02/2014 9 | * Time: 21:17 10 | * 11 | * @author Geoffroy Warin (http://geowarin.github.io) 12 | */ 13 | public class Github13 extends GithubAdapter { 14 | 15 | @Override 16 | public boolean isCredentialsDefined() { 17 | return GithubSettings.getInstance().isAuthConfigured(); 18 | } 19 | 20 | @Override 21 | public String getLogin() { 22 | return GithubSettings.getInstance().getLogin(); 23 | } 24 | 25 | @Override 26 | public String getPassword() { 27 | GithubAuthData.BasicAuth basicAuth = getBasicAuth(); 28 | assert basicAuth != null; 29 | return basicAuth.getPassword(); 30 | } 31 | 32 | private GithubAuthData.BasicAuth getBasicAuth() { 33 | return GithubSettings.getInstance().getAuthData().getBasicAuth(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /intellij-13-adapter/src/com/gisttemplates/gui/GUIFactory13.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.gui; 2 | 3 | import com.gisttemplates.adapter.GUIFactory; 4 | import com.gisttemplates.adapter.Icons; 5 | import com.gisttemplates.api.GistTemplate; 6 | import com.intellij.ui.ColoredListCellRendererWrapper; 7 | import com.intellij.ui.JBColor; 8 | import com.intellij.ui.SimpleColoredText; 9 | import com.intellij.ui.SimpleTextAttributes; 10 | 11 | import javax.swing.*; 12 | 13 | /** 14 | * Date: 22/03/2014 15 | * Time: 12:36 16 | * 17 | * @author Geoffroy Warin (http://geowarin.github.io) 18 | */ 19 | @SuppressWarnings("UnusedDeclaration") 20 | public class GUIFactory13 extends GUIFactory { 21 | 22 | @Override public ListCellRenderer createGistTemplateColoredListCellRenderer() { 23 | return new GistTemplateColoredListCellRendererWrapper(); 24 | } 25 | 26 | private static class GistTemplateColoredListCellRendererWrapper extends ColoredListCellRendererWrapper { 27 | @Override 28 | protected void doCustomize(JList list, GistTemplate template, int index, boolean selected, boolean hasFocus) { 29 | append(new SimpleColoredText(template.getFilename(), new SimpleTextAttributes(SimpleTextAttributes.STYLE_PLAIN, JBColor.BLACK))); 30 | setToolTipText(template.getDescription()); 31 | if (template.isStarred()) 32 | setIcon(Icons.getInstance().favorite()); 33 | else 34 | setIcon(Icons.getInstance().github()); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /intellij-13-adapter/src/com/gisttemplates/icons/Icons13.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.icons; 2 | 3 | import com.gisttemplates.adapter.Icons; 4 | import com.intellij.icons.AllIcons; 5 | import icons.GithubIcons; 6 | 7 | import javax.swing.*; 8 | 9 | /** 10 | * Date: 22/03/2014 11 | * Time: 12:25 12 | * 13 | * @author Geoffroy Warin (http://geowarin.github.io) 14 | */ 15 | @SuppressWarnings("UnusedDeclaration") 16 | public class Icons13 extends Icons { 17 | @Override public Icon warning() { 18 | return AllIcons.General.BalloonWarning; 19 | } 20 | 21 | @Override public Icon github() { 22 | return GithubIcons.Github_icon; 23 | } 24 | 25 | @Override public Icon favorite() { 26 | return AllIcons.Toolwindows.ToolWindowFavorites; 27 | } 28 | 29 | @Override public Icon textFile() { 30 | return AllIcons.FileTypes.Text; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /intellij-adapter-api/intellij-adapter-api.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /intellij-adapter-api/src/com/gisttemplates/adapter/EditorActionFactory.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.adapter; 2 | 3 | 4 | import com.intellij.openapi.components.ServiceManager; 5 | import com.intellij.openapi.editor.actionSystem.EditorActionHandler; 6 | 7 | /** 8 | * Date: 27/03/2014 9 | * Time: 07:01 10 | * 11 | * @author Geoffroy Warin (http://geowarin.github.io) 12 | */ 13 | public abstract class EditorActionFactory { 14 | 15 | public static EditorActionFactory getInstance() { 16 | return ServiceManager.getService(EditorActionFactory.class); 17 | } 18 | 19 | public abstract EditorActionHandler createActionHandler(); 20 | } 21 | -------------------------------------------------------------------------------- /intellij-adapter-api/src/com/gisttemplates/adapter/GUIFactory.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.adapter; 2 | 3 | import com.intellij.openapi.components.ServiceManager; 4 | 5 | import javax.swing.*; 6 | 7 | /** 8 | * Date: 22/03/2014 9 | * Time: 12:34 10 | * 11 | * @author Geoffroy Warin (http://geowarin.github.io) 12 | */ 13 | public abstract class GUIFactory { 14 | 15 | public static GUIFactory getInstance() { 16 | return ServiceManager.getService(GUIFactory.class); 17 | } 18 | 19 | public abstract ListCellRenderer createGistTemplateColoredListCellRenderer(); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /intellij-adapter-api/src/com/gisttemplates/adapter/GithubAdapter.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.adapter; 2 | 3 | import com.intellij.openapi.components.ServiceManager; 4 | 5 | /** 6 | * Date: 25/02/2014 7 | * Time: 19:52 8 | * 9 | * @author Geoffroy Warin (http://geowarin.github.io) 10 | */ 11 | public abstract class GithubAdapter { 12 | 13 | public static GithubAdapter getInstance() { 14 | return ServiceManager.getService(GithubAdapter.class); 15 | } 16 | 17 | public abstract boolean isCredentialsDefined(); 18 | 19 | public abstract String getLogin(); 20 | 21 | public abstract String getPassword(); 22 | } 23 | -------------------------------------------------------------------------------- /intellij-adapter-api/src/com/gisttemplates/adapter/Icons.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.adapter; 2 | 3 | import com.intellij.openapi.components.ServiceManager; 4 | 5 | import javax.swing.*; 6 | 7 | /** 8 | * Date: 22/03/2014 9 | * Time: 12:17 10 | * 11 | * @author Geoffroy Warin (http://geowarin.github.io) 12 | */ 13 | public abstract class Icons { 14 | 15 | public static Icons getInstance() { 16 | return ServiceManager.getService(Icons.class); 17 | } 18 | 19 | public abstract Icon warning(); 20 | 21 | public abstract Icon github(); 22 | 23 | public abstract Icon favorite(); 24 | 25 | public abstract Icon textFile(); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /intellij-adapter-api/src/com/gisttemplates/api/GistService.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.api; 2 | 3 | import com.intellij.openapi.components.ServiceManager; 4 | 5 | import java.util.List; 6 | 7 | public abstract class GistService { 8 | public static GistService getInstance() { 9 | return ServiceManager.getService(GistService.class); 10 | } 11 | 12 | public abstract void loadGist(GistTemplate gistTemplate); 13 | 14 | public abstract List fetchGistList(); 15 | } 16 | -------------------------------------------------------------------------------- /intellij-adapter-api/src/com/gisttemplates/api/GistTemplate.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.api; 2 | 3 | import org.eclipse.egit.github.core.Gist; 4 | import org.eclipse.egit.github.core.GistFile; 5 | 6 | import java.util.Collection; 7 | 8 | /** 9 | * Date: 16/03/2014 10 | * Time: 16:01 11 | * 12 | * @author Geoffroy Warin (http://geowarin.github.io) 13 | */ 14 | public class GistTemplate { 15 | private Gist gist; 16 | private final boolean isStarred; 17 | 18 | public GistTemplate(Gist gist, boolean starred) { 19 | this.gist = gist; 20 | isStarred = starred; 21 | } 22 | 23 | public boolean isStarred() { 24 | return isStarred; 25 | } 26 | 27 | public GistFile getFirstFile() { 28 | return gist.getFiles().values().iterator().next(); 29 | } 30 | 31 | public Collection getFiles() { 32 | return gist.getFiles().values(); 33 | } 34 | 35 | public String getDescription() { 36 | return gist.getDescription(); 37 | } 38 | 39 | public String getId() { 40 | return gist.getId(); 41 | } 42 | 43 | public String getFilename() { 44 | return getFirstFile().getFilename(); 45 | } 46 | 47 | public void setGist(Gist gist) { 48 | this.gist = gist; 49 | } 50 | 51 | public String getFirstFileContent() { 52 | return getFirstFile().getContent(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /intellij-adapter-api/src/com/gisttemplates/api/LazyGistTemplate.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.api; 2 | 3 | import com.gisttemplates.api.GistTemplate; 4 | import com.gisttemplates.api.GistService; 5 | import com.intellij.codeInsight.template.impl.TemplateImpl; 6 | 7 | /** 8 | * Date: 16/03/2014 9 | * Time: 16:40 10 | * 11 | * @author Geoffroy Warin (http://geowarin.github.io) 12 | */ 13 | public class LazyGistTemplate extends TemplateImpl { 14 | private GistTemplate gist; 15 | 16 | public LazyGistTemplate(GistTemplate gist) { 17 | super((gist.isStarred() ? "★ " : "") + gist.getFilename(), "gists"); 18 | this.gist = gist; 19 | setDescription(gist.getDescription()); 20 | } 21 | 22 | @Override 23 | public String getTemplateText() { 24 | GistService.getInstance().loadGist(gist); 25 | return gist.getFirstFileContent(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /lib/gson-2.2.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geowarin/gist-templates-plugin/f4596984f25aba5e1d8f4a5c8692b55be42c8452/lib/gson-2.2.2.jar -------------------------------------------------------------------------------- /lib/org.eclipse.egit.github.core-2.1.5.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geowarin/gist-templates-plugin/f4596984f25aba5e1d8f4a5c8692b55be42c8452/lib/org.eclipse.egit.github.core-2.1.5.jar -------------------------------------------------------------------------------- /src/main/java/com/gisttemplates/GistTemplatesApplication.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates; 2 | 3 | import com.gisttemplates.adapter.EditorActionFactory; 4 | import com.gisttemplates.adapter.GUIFactory; 5 | import com.gisttemplates.adapter.GithubAdapter; 6 | import com.gisttemplates.adapter.Icons; 7 | import com.intellij.openapi.application.ApplicationInfo; 8 | import com.intellij.openapi.components.ApplicationComponent; 9 | import com.intellij.openapi.components.impl.ComponentManagerImpl; 10 | import com.intellij.openapi.diagnostic.Logger; 11 | import org.jetbrains.annotations.NotNull; 12 | import org.picocontainer.MutablePicoContainer; 13 | 14 | /** 15 | * Date: 09/02/2014 16 | * Time: 20:56 17 | * 18 | * @author Geoffroy Warin (http://geowarin.github.io) 19 | */ 20 | public class GistTemplatesApplication implements ApplicationComponent { 21 | private ComponentManagerImpl componentManager; 22 | 23 | private static final Logger LOG = Logger.getInstance(GistTemplatesApplication.class.getName()); 24 | 25 | public GistTemplatesApplication(@NotNull ComponentManagerImpl componentManager) { 26 | this.componentManager = componentManager; 27 | } 28 | 29 | public void initComponent() { 30 | MutablePicoContainer picoContainer = componentManager.getPicoContainer(); 31 | try { 32 | initGithubAdapter(picoContainer); 33 | initIconsAdapter(picoContainer); 34 | initGuiFactory(picoContainer); 35 | initEditorActionFactory(picoContainer); 36 | } catch (Exception e) { 37 | LOG.error("Error while registering adapters", e); 38 | } 39 | } 40 | 41 | private void initEditorActionFactory(MutablePicoContainer picoContainer) throws Exception { 42 | switch (getVersion()) { 43 | case V11: 44 | picoContainer.registerComponentInstance(EditorActionFactory.class.getName(), Class.forName("com.gisttemplates.action.EditorActionFactory11").newInstance()); 45 | break; 46 | case V12: 47 | // For AppCode and ides still built against 131 API 48 | case V13: 49 | picoContainer.registerComponentInstance(EditorActionFactory.class.getName(), Class.forName("com.gisttemplates.action.EditorActionFactory12").newInstance()); 50 | break; 51 | case V135: 52 | picoContainer.registerComponentInstance(EditorActionFactory.class.getName(), Class.forName("com.gisttemplates.action.EditorActionFactory13").newInstance()); 53 | break; 54 | } 55 | } 56 | 57 | private void initGuiFactory(MutablePicoContainer picoContainer) throws Exception { 58 | switch (getVersion()) { 59 | case V11: 60 | picoContainer.registerComponentInstance(GUIFactory.class.getName(), Class.forName("com.gisttemplates.gui.GUIFactory11").newInstance()); 61 | break; 62 | case V12: 63 | picoContainer.registerComponentInstance(GUIFactory.class.getName(), Class.forName("com.gisttemplates.gui.GUIFactory12").newInstance()); 64 | break; 65 | case V13: 66 | case V135: 67 | picoContainer.registerComponentInstance(GUIFactory.class.getName(), Class.forName("com.gisttemplates.gui.GUIFactory13").newInstance()); 68 | break; 69 | } 70 | } 71 | 72 | private void initIconsAdapter(MutablePicoContainer picoContainer) throws Exception { 73 | switch (getVersion()) { 74 | case V11: 75 | picoContainer.registerComponentInstance(Icons.class.getName(), Class.forName("com.gisttemplates.icons.Icons11").newInstance()); 76 | break; 77 | case V12: 78 | picoContainer.registerComponentInstance(Icons.class.getName(), Class.forName("com.gisttemplates.icons.Icons12").newInstance()); 79 | break; 80 | case V13: 81 | case V135: 82 | picoContainer.registerComponentInstance(Icons.class.getName(), Class.forName("com.gisttemplates.icons.Icons13").newInstance()); 83 | break; 84 | } 85 | } 86 | 87 | private void initGithubAdapter(MutablePicoContainer picoContainer) throws Exception { 88 | switch (getVersion()) { 89 | case V11: 90 | picoContainer.registerComponentInstance(GithubAdapter.class.getName(), Class.forName("com.gisttemplates.github.Github11").newInstance()); 91 | break; 92 | case V12: 93 | picoContainer.registerComponentInstance(GithubAdapter.class.getName(), Class.forName("com.gisttemplates.github.Github12").newInstance()); 94 | break; 95 | case V13: 96 | case V135: 97 | picoContainer.registerComponentInstance(GithubAdapter.class.getName(), Class.forName("com.gisttemplates.github.Github13").newInstance()); 98 | break; 99 | } 100 | } 101 | 102 | private IntelliJVersion getVersion() { 103 | int version = ApplicationInfo.getInstance().getBuild().getBaselineVersion(); 104 | if (version >= 135) { 105 | return IntelliJVersion.V135; 106 | } else if (version >= 130) { 107 | return IntelliJVersion.V13; 108 | } else if (version >= 120) { 109 | return IntelliJVersion.V12; 110 | } 111 | return IntelliJVersion.V11; 112 | } 113 | 114 | enum IntelliJVersion { 115 | V11, V12, V13, V135 116 | } 117 | 118 | public void disposeComponent() { 119 | } 120 | 121 | @NotNull 122 | public String getComponentName() { 123 | return "GistTemplatesApplication"; 124 | } 125 | 126 | } 127 | -------------------------------------------------------------------------------- /src/main/java/com/gisttemplates/action/GistTemplateEditorAction.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.action; 2 | 3 | import com.gisttemplates.adapter.EditorActionFactory; 4 | import com.gisttemplates.api.GistTemplate; 5 | import com.gisttemplates.api.GistService; 6 | import com.gisttemplates.api.LazyGistTemplate; 7 | import com.intellij.codeInsight.template.impl.TemplateImpl; 8 | import com.intellij.openapi.editor.actionSystem.EditorAction; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | /** 14 | * Date: 09/02/2014 15 | * Time: 16:01 16 | * 17 | * @author Geoffroy Warin (http://geowarin.github.io) 18 | */ 19 | public class GistTemplateEditorAction extends EditorAction { 20 | 21 | public GistTemplateEditorAction() { 22 | super(EditorActionFactory.getInstance().createActionHandler()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/gisttemplates/configuration/GistTemplatesConfigurable.form: -------------------------------------------------------------------------------- 1 | 2 |
    3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /src/main/java/com/gisttemplates/configuration/GistTemplatesConfigurable.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.configuration; 2 | 3 | import com.gisttemplates.adapter.GithubAdapter; 4 | import com.intellij.openapi.diagnostic.Logger; 5 | import com.intellij.openapi.options.Configurable; 6 | import com.intellij.openapi.options.ConfigurationException; 7 | import com.intellij.openapi.ui.Messages; 8 | import org.jetbrains.annotations.Nls; 9 | import org.jetbrains.annotations.NotNull; 10 | import org.jetbrains.annotations.Nullable; 11 | 12 | import javax.swing.*; 13 | import java.awt.event.ActionEvent; 14 | import java.awt.event.ActionListener; 15 | 16 | /** 17 | * Date: 09/02/2014 18 | * Time: 16:11 19 | * 20 | * @author Geoffroy Warin (http://geowarin.github.io) 21 | */ 22 | public class GistTemplatesConfigurable implements Configurable { 23 | 24 | private JPanel parentPanel; 25 | private JCheckBox useMyGithubAccountCheckBox; 26 | private final GistTemplatesSettings gistTemplatesSettings; 27 | private boolean isModified; 28 | 29 | private static final Logger LOG = Logger.getInstance(GistTemplatesConfigurable.class.getName()); 30 | 31 | 32 | public GistTemplatesConfigurable() { 33 | gistTemplatesSettings = GistTemplatesSettings.getInstance(); 34 | useMyGithubAccountCheckBox.addActionListener(new UseMyAccountActionListener()); 35 | init(gistTemplatesSettings); 36 | } 37 | 38 | private void init(GistTemplatesSettings settings) { 39 | useMyGithubAccountCheckBox.setSelected(settings.isUseGithubAccount() && GithubAdapter.getInstance().isCredentialsDefined()); 40 | } 41 | 42 | @Nls 43 | @Override 44 | public String getDisplayName() { 45 | return "Gist Templates"; 46 | } 47 | 48 | // No override because method is not in intellij 12+ 49 | @SuppressWarnings("UnusedDeclaration") 50 | public Icon getIcon() { 51 | return null; 52 | } 53 | 54 | @Override 55 | public boolean isModified() { 56 | return isModified; 57 | } 58 | 59 | @Nullable 60 | @Override 61 | public JComponent createComponent() { 62 | return parentPanel; 63 | } 64 | 65 | @Nullable 66 | @Override 67 | public String getHelpTopic() { 68 | return "preferences.gisttemplates"; 69 | } 70 | 71 | @Override 72 | public void apply() throws ConfigurationException { 73 | gistTemplatesSettings.setUseGithubAccount(useMyGithubAccountCheckBox.isSelected()); 74 | } 75 | 76 | @Override 77 | public void reset() { 78 | } 79 | 80 | @Override 81 | public void disposeUIResources() { 82 | } 83 | 84 | public static void main(String[] args) { 85 | JFrame frame = new JFrame("GistTemplatesConfigurable"); 86 | frame.setContentPane(new GistTemplatesConfigurable().parentPanel); 87 | frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 88 | frame.pack(); 89 | frame.setVisible(true); 90 | } 91 | 92 | private class UseMyAccountActionListener implements ActionListener { 93 | @Override 94 | public void actionPerformed(@NotNull ActionEvent actionEvent) { 95 | isModified = true; 96 | 97 | if (useMyGithubAccountCheckBox.isSelected()) { 98 | 99 | if (!GithubAdapter.getInstance().isCredentialsDefined()) { 100 | LOG.info("GithubSettings are not set"); 101 | Messages.showErrorDialog(parentPanel, "Github settings are not defined", "Login Failure"); 102 | 103 | useMyGithubAccountCheckBox.setSelected(false); 104 | isModified = false; 105 | } 106 | } 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/main/java/com/gisttemplates/configuration/GistTemplatesSettings.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.configuration; 2 | 3 | import com.intellij.openapi.components.PersistentStateComponent; 4 | import com.intellij.openapi.components.ServiceManager; 5 | import com.intellij.openapi.components.State; 6 | import com.intellij.openapi.components.Storage; 7 | import com.intellij.openapi.diagnostic.Logger; 8 | import org.jdom.Element; 9 | import org.jetbrains.annotations.Nullable; 10 | 11 | /** 12 | * Date: 18/02/2014 13 | * Time: 18:36 14 | * 15 | * @author Geoffroy Warin (http://geowarin.github.io) 16 | */ 17 | @State( 18 | name = "GistTemplatesSettings", 19 | storages = { 20 | @Storage( 21 | file = "$APP_CONFIG$/gisttemplates_settings.xml" 22 | )} 23 | ) 24 | public class GistTemplatesSettings implements PersistentStateComponent { 25 | 26 | private static final Logger LOG = Logger.getInstance(GistTemplatesSettings.class.getName()); 27 | private static final String GITHUB_TEMPLATES_SETTINGS_TAG = "githubTemplatesSettings"; 28 | private static final String USE_GITHUB_ACCOUNT = "useGithubAccount"; 29 | private boolean useGithubAccount; 30 | 31 | public static GistTemplatesSettings getInstance() { 32 | return ServiceManager.getService(GistTemplatesSettings.class); 33 | } 34 | 35 | 36 | @Nullable 37 | @Override 38 | public Element getState() { 39 | final Element element = new Element(GITHUB_TEMPLATES_SETTINGS_TAG); 40 | element.setAttribute(USE_GITHUB_ACCOUNT, String.valueOf(isUseGithubAccount())); 41 | return element; 42 | } 43 | 44 | @Override 45 | public void loadState(Element state) { 46 | try { 47 | setUseGithubAccount(Boolean.valueOf(state.getAttributeValue(USE_GITHUB_ACCOUNT))); 48 | } catch (Exception e) { 49 | LOG.error("Error happened while loading github settings: " + e); 50 | } 51 | } 52 | 53 | public boolean isUseGithubAccount() { 54 | return useGithubAccount; 55 | } 56 | 57 | public void setUseGithubAccount(boolean useGithubAccount) { 58 | this.useGithubAccount = useGithubAccount; 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /src/main/java/com/gisttemplates/gist/GistAccountFetcher.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.gist; 2 | 3 | import com.gisttemplates.api.GistTemplate; 4 | import com.intellij.notification.Notification; 5 | import com.intellij.notification.NotificationType; 6 | import com.intellij.notification.Notifications; 7 | import com.intellij.openapi.progress.ProgressManager; 8 | import com.intellij.openapi.project.Project; 9 | import org.eclipse.egit.github.core.client.GitHubClient; 10 | import org.eclipse.egit.github.core.client.RequestException; 11 | import org.eclipse.egit.github.core.service.GistService; 12 | 13 | import java.io.IOException; 14 | import java.util.Collections; 15 | import java.util.List; 16 | 17 | /** 18 | * Date: 09/02/2014 19 | * Time: 20:32 20 | * 21 | * @author Geoffroy Warin (http://geowarin.github.io) 22 | */ 23 | public class GistAccountFetcher { 24 | private final String githubUserName; 25 | private final boolean includeFavorites; 26 | 27 | public GistAccountFetcher(String githubUserName, boolean includeFavorites) { 28 | this.githubUserName = githubUserName; 29 | this.includeFavorites = includeFavorites; 30 | } 31 | 32 | public List fetchGistsList(GitHubClient githubClient) { 33 | try { 34 | return loadGistListInBackground(githubClient); 35 | } catch (IOException e) { 36 | notifyFetchError(e); 37 | } 38 | return Collections.emptyList(); 39 | } 40 | 41 | private void notifyFetchError(IOException e) { 42 | String content; 43 | if (e instanceof RequestException) { 44 | content = ((RequestException) e).formatErrors(); 45 | } else { 46 | content = e.getClass().getSimpleName() + " : " + e.getMessage(); 47 | } 48 | Notifications.Bus.notify(new Notification("GistTemplates", "Error while fetching gists for " + githubUserName, content, NotificationType.ERROR)); 49 | } 50 | 51 | private List loadGistListInBackground(GitHubClient githubClient) throws IOException { 52 | LoadGistListTask process = new LoadGistListTask(new GistService(githubClient), githubUserName, includeFavorites); 53 | return ProgressManager.getInstance().runProcessWithProgressSynchronously(process, "Loading gists for " + githubUserName, true, null); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/gisttemplates/gist/GistServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.gist; 2 | 3 | import com.gisttemplates.adapter.GithubAdapter; 4 | import com.gisttemplates.api.GistService; 5 | import com.gisttemplates.api.GistTemplate; 6 | import com.gisttemplates.configuration.GistTemplatesSettings; 7 | import com.intellij.notification.Notification; 8 | import com.intellij.notification.NotificationListener; 9 | import com.intellij.notification.NotificationType; 10 | import com.intellij.notification.Notifications; 11 | import org.eclipse.egit.github.core.Gist; 12 | import org.eclipse.egit.github.core.client.GitHubClient; 13 | import org.jetbrains.annotations.NotNull; 14 | 15 | import javax.swing.event.HyperlinkEvent; 16 | import java.io.IOException; 17 | import java.util.ArrayList; 18 | import java.util.Collections; 19 | import java.util.List; 20 | 21 | /** 22 | * Date: 16/03/2014 23 | * Time: 14:51 24 | * 25 | * @author Geoffroy Warin (http://geowarin.github.io) 26 | */ 27 | public class GistServiceImpl extends GistService { 28 | public static final String WEBSITE_URL = "http://geowarin.github.io/gist-templates-plugin"; 29 | private GistTemplatesSettings gistTemplatesSettings; 30 | 31 | public GistServiceImpl() { 32 | gistTemplatesSettings = GistTemplatesSettings.getInstance(); 33 | } 34 | 35 | @Override 36 | public void loadGist(GistTemplate gistTemplate) { 37 | try { 38 | Gist gist = loadGist(gistTemplate.getId()); 39 | gistTemplate.setGist(gist); 40 | } catch (IOException e) { 41 | notifyFetchError(e, gistTemplate.getId()); 42 | } 43 | } 44 | 45 | @Override 46 | public List fetchGistList() { 47 | List accountList = getAccountList(); 48 | if (accountList.isEmpty()) { 49 | notifyPluginNotConfigured(); 50 | return Collections.emptyList(); 51 | } 52 | 53 | List allGists = new ArrayList(); 54 | for (GistAccountFetcher cache : accountList) { 55 | List gistTemplates = cache.fetchGistsList(getGithubClient()); 56 | allGists.addAll(gistTemplates); 57 | } 58 | return allGists; 59 | } 60 | 61 | private void notifyPluginNotConfigured() { 62 | String message = 63 | String.format("To use gist templates plugin, please configure your github account in the options. More details here", 64 | WEBSITE_URL); 65 | Notifications.Bus.notify(new Notification("GistTemplates", "No account configured", message, NotificationType.WARNING, new MyNotificationListener())); 66 | } 67 | 68 | private class MyNotificationListener implements NotificationListener { 69 | @Override 70 | public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent event) { 71 | NotificationListener.URL_OPENING_LISTENER.hyperlinkUpdate(notification, event); 72 | } 73 | } 74 | 75 | 76 | private GitHubClient getGithubClient() { 77 | GithubAdapter githubAdapter = GithubAdapter.getInstance(); 78 | GitHubClient client = new GitHubClient(); 79 | if (usePersonalGithubAccount()) { 80 | client.setCredentials(githubAdapter.getLogin(), githubAdapter.getPassword()); 81 | } 82 | return client; 83 | } 84 | 85 | private void notifyFetchError(IOException e, String gistId) { 86 | Notifications.Bus.notify(new Notification("GistTemplates", "Error while fetching gist " + gistId, e.getMessage(), NotificationType.ERROR)); 87 | } 88 | 89 | private Gist loadGist(String gistId) throws IOException { 90 | org.eclipse.egit.github.core.service.GistService gistService = new org.eclipse.egit.github.core.service.GistService(getGithubClient()); 91 | return gistService.getGist(gistId); 92 | } 93 | 94 | private List getAccountList() { 95 | List caches = new ArrayList(); 96 | if (usePersonalGithubAccount()) { 97 | GistAccountFetcher cacheForGithubUser = new GistAccountFetcher(GithubAdapter.getInstance().getLogin(), true); 98 | caches.add(cacheForGithubUser); 99 | } 100 | return caches; 101 | } 102 | 103 | private boolean usePersonalGithubAccount() { 104 | return gistTemplatesSettings.isUseGithubAccount() 105 | && GithubAdapter.getInstance().isCredentialsDefined(); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/com/gisttemplates/gist/LoadGistListTask.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.gist; 2 | 3 | import com.gisttemplates.api.GistTemplate; 4 | import com.intellij.openapi.progress.ProgressManager; 5 | import com.intellij.openapi.util.ThrowableComputable; 6 | import org.eclipse.egit.github.core.Gist; 7 | import org.eclipse.egit.github.core.service.GistService; 8 | 9 | import java.io.IOException; 10 | import java.util.ArrayList; 11 | import java.util.Collections; 12 | import java.util.List; 13 | 14 | /** 15 | * Date: 16/03/2014 16 | * Time: 15:40 17 | * 18 | * @author Geoffroy Warin (http://geowarin.github.io) 19 | */ 20 | class LoadGistListTask implements ThrowableComputable, IOException> { 21 | private final GistService gistService; 22 | private final String githubUserName; 23 | private final boolean includeFavorites; 24 | 25 | LoadGistListTask(GistService gistService, String githubUserName, boolean includeFavorites) { 26 | this.gistService = gistService; 27 | this.githubUserName = githubUserName; 28 | this.includeFavorites = includeFavorites; 29 | } 30 | 31 | @Override 32 | public List compute() throws IOException { 33 | List gistList = gistService.getGists(githubUserName); 34 | ProgressManager.getInstance().getProgressIndicator().setFraction(0.5); 35 | List starredGists = includeFavorites ? gistService.getStarredGists() : Collections.emptyList(); 36 | ProgressManager.getInstance().getProgressIndicator().setFraction(1); 37 | return loadGists(gistList, starredGists); 38 | } 39 | 40 | private List loadGists(List gistList, List starredGists) throws IOException { 41 | List loadedGists = new ArrayList(gistList.size()); 42 | for (Gist gist : gistList) { 43 | loadedGists.add(new GistTemplate(gist, false)); 44 | } 45 | for (Gist gist : starredGists) { 46 | loadedGists.add(new GistTemplate(gist, true)); 47 | } 48 | return loadedGists; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/gisttemplates/gistfiles/GistFileCreator.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.gistfiles; 2 | 3 | import com.gisttemplates.adapter.Icons; 4 | import com.gisttemplates.api.GistTemplate; 5 | import com.gisttemplates.api.GistService; 6 | import com.google.common.collect.ComparisonChain; 7 | import com.intellij.openapi.application.ApplicationManager; 8 | import com.intellij.openapi.editor.Document; 9 | import com.intellij.openapi.fileEditor.FileDocumentManager; 10 | import com.intellij.openapi.fileTypes.StdFileTypes; 11 | import com.intellij.openapi.project.Project; 12 | import com.intellij.openapi.vcs.VcsShowConfirmationOption; 13 | import com.intellij.openapi.vfs.VirtualFile; 14 | import com.intellij.psi.PsiDirectory; 15 | import com.intellij.psi.PsiFile; 16 | import com.intellij.psi.PsiFileFactory; 17 | import com.intellij.psi.PsiManager; 18 | import com.intellij.util.ui.ConfirmationDialog; 19 | import org.eclipse.egit.github.core.GistFile; 20 | 21 | import java.util.*; 22 | 23 | /** 24 | * Date: 21/03/2014 25 | * Time: 18:53 26 | * 27 | * @author Geoffroy Warin (http://geowarin.github.io) 28 | */ 29 | public class GistFileCreator { 30 | private Project project; 31 | private VirtualFile parentDirectory; 32 | private final SortedSet filesToInsert; 33 | 34 | public GistFileCreator(Project project, VirtualFile parentDirectory, List gistFilesToInsert) { 35 | this.project = project; 36 | this.parentDirectory = parentDirectory; 37 | filesToInsert = new TreeSet(new Comparator() { 38 | @Override public int compare(GistFile gistFile1, GistFile gistFile2) { 39 | return ComparisonChain.start().compare(gistFile1.getRawUrl(), gistFile2.getRawUrl()).result(); 40 | } 41 | }); 42 | filesToInsert.addAll(gistFilesToInsert); 43 | } 44 | 45 | public void insertGistFiles(final GistTemplate parentGist) { 46 | GistService.getInstance().loadGist(parentGist); 47 | askToConfirmForReplacement(); 48 | createOrReplaceFiles(parentGist); 49 | } 50 | 51 | private void createOrReplaceFiles(final GistTemplate parentGist) { 52 | ApplicationManager.getApplication().runWriteAction(new Runnable() { 53 | @Override 54 | public void run() { 55 | for (GistFile gistFile : parentGist.getFiles()) { 56 | if (filesToInsert.contains(gistFile)) 57 | createOrReplaceFile(gistFile.getFilename(), gistFile.getContent()); 58 | } 59 | } 60 | }); 61 | } 62 | 63 | private void askToConfirmForReplacement() { 64 | Iterator iterator = filesToInsert.iterator(); 65 | while (iterator.hasNext()) { 66 | GistFile gistFile = iterator.next(); 67 | VirtualFile fileToInsert = parentDirectory.findChild(gistFile.getFilename()); 68 | if (fileToInsert != null && fileToInsert.exists()) { 69 | 70 | String message = String.format("File %s already exists. Would you like to replace it ?", fileToInsert.getName()); 71 | boolean replaceFile = ConfirmationDialog.requestForConfirmation(VcsShowConfirmationOption.STATIC_SHOW_CONFIRMATION, project, 72 | message, "File exists", Icons.getInstance().warning()); 73 | 74 | if (!replaceFile) 75 | iterator.remove(); 76 | } 77 | } 78 | } 79 | 80 | private VirtualFile createFile(String fileName, String text) { 81 | PsiFile javaFile = PsiFileFactory.getInstance(project).createFileFromText(fileName, StdFileTypes.PLAIN_TEXT, text); 82 | PsiDirectory root = PsiManager.getInstance(project).findDirectory(parentDirectory); 83 | PsiFile added = (PsiFile) root.add(javaFile); 84 | return added.getVirtualFile(); 85 | } 86 | 87 | private VirtualFile createOrReplaceFile(String fileName, String text) { 88 | 89 | VirtualFile file = parentDirectory.findChild(fileName); 90 | if (file != null && file.exists()) { 91 | replaceContent(file, text); 92 | return file; 93 | } 94 | 95 | return createFile(fileName, text); 96 | } 97 | 98 | private void replaceContent(VirtualFile file, String text) { 99 | Document docFile = FileDocumentManager.getInstance().getDocument(file); 100 | docFile.setText(text); 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /src/main/java/com/gisttemplates/gistfiles/GistFilesDialog.form: -------------------------------------------------------------------------------- 1 | 2 |
    3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 |
    61 | -------------------------------------------------------------------------------- /src/main/java/com/gisttemplates/gistfiles/GistFilesDialog.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.gistfiles; 2 | 3 | import com.gisttemplates.adapter.GUIFactory; 4 | import com.gisttemplates.api.GistTemplate; 5 | import com.gisttemplates.adapter.Icons; 6 | import com.intellij.openapi.project.Project; 7 | import com.intellij.openapi.ui.DialogWrapper; 8 | import com.intellij.ui.CheckboxTree; 9 | import com.intellij.ui.CheckedTreeNode; 10 | import com.intellij.ui.ListSpeedSearch; 11 | import com.intellij.ui.SimpleTextAttributes; 12 | import com.intellij.ui.components.JBList; 13 | import com.intellij.util.Function; 14 | import org.eclipse.egit.github.core.GistFile; 15 | import org.jetbrains.annotations.NotNull; 16 | import org.jetbrains.annotations.Nullable; 17 | 18 | import javax.swing.*; 19 | import javax.swing.event.ListSelectionEvent; 20 | import javax.swing.event.ListSelectionListener; 21 | import javax.swing.tree.DefaultMutableTreeNode; 22 | import javax.swing.tree.DefaultTreeModel; 23 | import java.awt.*; 24 | import java.util.Arrays; 25 | import java.util.List; 26 | 27 | /** 28 | * Date: 21/03/2014 29 | * Time: 11:14 30 | * 31 | * @author Geoffroy Warin (http://geowarin.github.io) 32 | */ 33 | public class GistFilesDialog extends DialogWrapper { 34 | private JPanel mainPanel; 35 | private JList templateList; 36 | private CheckboxTree filesTree; 37 | private JTextArea description; 38 | private List templates; 39 | 40 | public GistFilesDialog(@Nullable Project project, @NotNull List templates) { 41 | super(project); 42 | this.templates = templates; 43 | setTitle("Select Files to Insert"); 44 | mainPanel.setPreferredSize(new Dimension(500, 400)); 45 | init(); 46 | templateList.setSelectedIndex(0); 47 | } 48 | 49 | @Nullable @Override protected JComponent createCenterPanel() { 50 | return mainPanel; 51 | } 52 | 53 | @Nullable @Override public JComponent getPreferredFocusedComponent() { 54 | return templateList; 55 | } 56 | 57 | private void createUIComponents() { 58 | templateList = new JBList(templates); 59 | new ListSpeedSearch(templateList, new Function() { 60 | @Override public String fun(Object o) { 61 | return ((GistTemplate) o).getFilename(); 62 | } 63 | }); 64 | templateList.setCellRenderer(GUIFactory.getInstance().createGistTemplateColoredListCellRenderer()); 65 | templateList.addListSelectionListener(new MyListSelectionListener()); 66 | 67 | GistTemplate firstTemplate = templates.get(0); 68 | CheckedTreeNode checkedTreeNode = getCheckedTreeNode(firstTemplate); 69 | filesTree = new CheckboxTree(new MyCheckboxTreeCellRenderer(), checkedTreeNode); 70 | } 71 | 72 | private CheckedTreeNode getCheckedTreeNode(GistTemplate firstTemplate) { 73 | CheckedTreeNode checkedTreeNode = new CheckedTreeNode(firstTemplate); 74 | for (GistFile gistFile : firstTemplate.getFiles()) { 75 | checkedTreeNode.add(new CheckedTreeNode(gistFile)); 76 | } 77 | return checkedTreeNode; 78 | } 79 | 80 | public List getSelectedFiles() { 81 | return Arrays.asList(filesTree.getCheckedNodes(GistFile.class, null)); 82 | } 83 | 84 | public GistTemplate getSelectedTemplate() { 85 | return (GistTemplate) templateList.getSelectedValue(); 86 | } 87 | 88 | private static class MyCheckboxTreeCellRenderer extends CheckboxTree.CheckboxTreeCellRenderer { 89 | @Override 90 | public void customizeRenderer(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { 91 | DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value; 92 | Object userObject = treeNode.getUserObject(); 93 | if (userObject == null) { 94 | return; 95 | } 96 | 97 | if (userObject instanceof GistTemplate) { 98 | GistTemplate gistTemplate = (GistTemplate) userObject; 99 | getTextRenderer().append(gistTemplate.getFilename(), new SimpleTextAttributes(SimpleTextAttributes.STYLE_PLAIN, Color.BLACK)); 100 | getTextRenderer().setIcon(Icons.getInstance().github()); 101 | getTextRenderer().setToolTipText(gistTemplate.getDescription()); 102 | } else if (userObject instanceof GistFile) { 103 | GistFile gistFile = (GistFile) userObject; 104 | getTextRenderer().append(gistFile.getFilename(), new SimpleTextAttributes(SimpleTextAttributes.STYLE_PLAIN, Color.BLACK)); 105 | getTextRenderer().setIcon(Icons.getInstance().textFile()); 106 | } 107 | } 108 | } 109 | 110 | private class MyListSelectionListener implements ListSelectionListener { 111 | @Override public void valueChanged(@NotNull ListSelectionEvent e) { 112 | GistTemplate selectedTemplate = (GistTemplate) templateList.getSelectedValue(); 113 | if (e.getValueIsAdjusting() || selectedTemplate == null) { 114 | return; 115 | } 116 | 117 | CheckedTreeNode checkedTreeNode = getCheckedTreeNode(selectedTemplate); 118 | description.setText(selectedTemplate.getDescription()); 119 | filesTree.setModel(new DefaultTreeModel(checkedTreeNode)); 120 | } 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /src/main/java/com/gisttemplates/gistfiles/NewFileFromGistAction.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.gistfiles; 2 | 3 | import com.gisttemplates.api.GistTemplate; 4 | import com.gisttemplates.api.GistService; 5 | import com.gisttemplates.utils.MyDataKeys; 6 | import com.intellij.openapi.actionSystem.AnAction; 7 | import com.intellij.openapi.actionSystem.AnActionEvent; 8 | import com.intellij.openapi.actionSystem.DataContext; 9 | import com.intellij.openapi.project.Project; 10 | import com.intellij.openapi.vfs.VirtualFile; 11 | 12 | import java.util.List; 13 | 14 | /** 15 | * Date: 20/03/2014 16 | * Time: 11:33 17 | * 18 | * @author Geoffroy Warin (http://geowarin.github.io) 19 | */ 20 | public class NewFileFromGistAction extends AnAction { 21 | 22 | public void actionPerformed(AnActionEvent e) { 23 | DataContext dataContext = e.getDataContext(); 24 | Project project = MyDataKeys.PROJECT.getData(dataContext); 25 | VirtualFile selectedDirectory = getSelectedDirectory(project, MyDataKeys.VIRTUAL_FILE.getData(dataContext)); 26 | 27 | List gistTemplates = GistService.getInstance().fetchGistList(); 28 | if (gistTemplates.isEmpty()) { 29 | return; 30 | } 31 | GistFilesDialog gistFilesDialog = new GistFilesDialog(project, gistTemplates); 32 | gistFilesDialog.show(); 33 | 34 | if (gistFilesDialog.isOK()) { 35 | new GistFileCreator(project, selectedDirectory, gistFilesDialog.getSelectedFiles()) 36 | .insertGistFiles(gistFilesDialog.getSelectedTemplate()); 37 | } 38 | 39 | // JBPopupFactory popupFactory = JBPopupFactory.getInstance(); 40 | // List names = Arrays.asList("kikoo", "lol"); 41 | // popupFactory.createListPopup(new BaseListPopupStep("Select a gist", names) { 42 | // }).showInBestPositionFor(dataContext); 43 | } 44 | 45 | private static VirtualFile getSelectedDirectory(Project project, VirtualFile virtualFile) { 46 | if (virtualFile == null) { 47 | return project.getBaseDir(); 48 | } 49 | if (virtualFile.isDirectory()) 50 | return virtualFile; 51 | 52 | return virtualFile.getParent(); 53 | } 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/gisttemplates/utils/MyDataKeys.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.utils; 2 | 3 | import com.intellij.openapi.actionSystem.DataKey; 4 | import com.intellij.openapi.project.Project; 5 | import com.intellij.openapi.vfs.VirtualFile; 6 | 7 | /** 8 | * Date: 21/03/2014 9 | * Time: 23:34 10 | * 11 | * @author Geoffroy Warin (http://geowarin.github.io) 12 | */ 13 | public class MyDataKeys { 14 | 15 | public static final DataKey PROJECT = DataKey.create("project"); 16 | public static final DataKey VIRTUAL_FILE = DataKey.create("virtualFile"); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/com/gisttemplates/gistfiles/GistFilesDialogTest.java: -------------------------------------------------------------------------------- 1 | package com.gisttemplates.gistfiles; 2 | 3 | import com.gisttemplates.api.GistTemplate; 4 | import org.apache.commons.lang.RandomStringUtils; 5 | import org.eclipse.egit.github.core.Gist; 6 | import org.eclipse.egit.github.core.GistFile; 7 | 8 | import javax.swing.*; 9 | import java.util.*; 10 | 11 | /** 12 | * Date: 21/03/2014 13 | * Time: 18:33 14 | * 15 | * @author Geoffroy Warin (http://geowarin.github.io) 16 | */ 17 | public class GistFilesDialogTest { 18 | 19 | public static void main(String[] args) { 20 | SwingUtilities.invokeLater(new Runnable() { 21 | public final void run() { 22 | List gistTemplates = Arrays.asList(createFakeTemplate(), createFakeTemplate()); 23 | GistFilesDialog gistFilesDialog = new GistFilesDialog(null, gistTemplates); 24 | gistFilesDialog.show(); 25 | System.out.println(gistFilesDialog.getSelectedFiles()); 26 | } 27 | }); 28 | } 29 | 30 | public static GistTemplate createFakeTemplate() { 31 | return new GistTemplate(createFakeGist(), false); 32 | } 33 | 34 | private static Gist createFakeGist() { 35 | Gist gist = new Gist(); 36 | Map files = new LinkedHashMap(); 37 | 38 | String fileName = RandomStringUtils.randomAlphabetic(12); 39 | files.put(fileName, createGistFile(fileName)); 40 | files.put("AClass.java", createGistFile("AClass.java")); 41 | 42 | gist.setFiles(files); 43 | gist.setDescription("How dead. You blow like a cannon. Pieces o' death are forever small."); 44 | return gist; 45 | } 46 | 47 | private static GistFile createGistFile(String filename) { 48 | GistFile gistFile = new GistFile(); 49 | gistFile.setFilename(filename); 50 | gistFile.setContent("content"); 51 | return gistFile; 52 | } 53 | } 54 | --------------------------------------------------------------------------------