Intellij IDEA插件开发入门
2013-11-04 21:03
197 查看
现今的IDE尽管有如“洪水猛兽”般强大,但要知道再强大的IDE也没法提供给使用者想要的一切功能,
所以IDE一般都提供有API接口供开发者自行扩展。下面以IntellijIDEA12下的插件开发为例,来看一下如何进一步增强IDE以适应开发者的需求。
1.创建Plugin工程
如果ModuleSDK中没有可选的SDK,那么点击New新添加一个SDK,目录就选择Intellij的安装位置即可。
创建出的Plugin项目结构很简单,只是在META-INF下多了一个plugin.xml配置文件,后文会介绍到它的用处。
2.让插件Say哈喽
2.1添加Component
在src目录上Alt+Insert,可以看到New对话框中列出有三种组件,分别对应三种级别:Application、Project、ModuleComponent。
这里我们选择ApplicationComponent作为实例,在弹出框中输入一个名字例如MyComponent,这样一个组件就创建出来了。
然后在MyComponent中添加一个SayHello的方法,其他方法暂不实现,源代码如下所示:
2.2添加Action
现在需要添加一个Action让使用我们插件的用户可以通过菜单或其他方式点击到插件。
Action主要工作是创建一个Application和MyComponent对象,代码如下:
2.3配置文件
其实前面两步新建Component和Action的同时,IDEA在帮我们自动将它们注册到META-INF/plugin.xml中。
我们刚才添加的ApplicationComponent和Action会在<application-components>结点下,plugin.xml最终是下面的样子:
3.运行调试
打开Run/Debug配置对话框,新加一个Plugin类型的,Useclasspathofmodule选择刚才的示例项目。
运行起来就会发现,原来会启动一个新的IntellijIDEA实例,重新走一遍启动配置过程,可以看到插件的名字就是plugin.xml中<name>中的值。
我们可以只选中我们刚开发的插件,忽略掉其他的。现在通过Window->SayHello!就可以触发我们的插件了,效果就是会弹出个对话框。
有趣的是,plugin.xml中其他的一些描述会在插件崩溃时显示给用户,将问题报告给插件作者。
4.插件配置面板
很多插件都是在Settings中有配置页的,现在简单介绍一下如何为我们的插件添加一个配置页。
首先改造一下MyComponent类,主要变化就是多实现了一个Configurable接口。这个接口中有一个createComponent方法,
这个方法返回Swing的JComponent对象就会显示到Settings里。另外使用IDEA提供的SwingDesigner设计器还是挺方便的,
自动生成的样式和布局代码为了避免被修改,也不会被我们看到(与NetBeans不同),所以最终代码很简洁。
最终效果就是这样的了,我们在设计器里设计的面板嵌入到了右边。
5.带对话框的插件
一种常见的插件就是点击插件对应的菜单项后,弹出一个对话框(例如搜索工作空间里的类、提交SVN前的代码确认等等)。
其实很简单,实现方法就是先创建一个Dialog,然后在Swing设计器中设计好Dialog中的控件布局,最后在Action中显示出对话框。
具体代码就不列举了,有需求可以找我索取。
参考资料
Wehavetriedtoanswersomeofthesequestionsinthenewdemo:GoogleSearch
PluginforIntelliJIDEA.Thisdemoexplainsthebasicsofpluginwritingandpublishing.
Moreexperiencedplugindeveloperscanfindanswersinthesetwoarticles:
1.The
BasicsofPluginDevelopmentforIntelliJIDEAbyAlexeyEfimov(forbeginners)
2.Developing
CustomLanguagePluginsforIntelliJIDEA5.0byDmitryJemerov(formoreadvanceddevelopers)
AlsoIntelliJIDEAtriestoeasetheplugindevelopmentbyprovidingspecificcodegeneration,completion,etc.Justlookwhatwasdonefor5.0
and5.1releasesforOpen
API&Plugins.
所以IDE一般都提供有API接口供开发者自行扩展。下面以IntellijIDEA12下的插件开发为例,来看一下如何进一步增强IDE以适应开发者的需求。
1.创建Plugin工程
如果ModuleSDK中没有可选的SDK,那么点击New新添加一个SDK,目录就选择Intellij的安装位置即可。
创建出的Plugin项目结构很简单,只是在META-INF下多了一个plugin.xml配置文件,后文会介绍到它的用处。
2.让插件Say哈喽
2.1添加Component
在src目录上Alt+Insert,可以看到New对话框中列出有三种组件,分别对应三种级别:Application、Project、ModuleComponent。
这里我们选择ApplicationComponent作为实例,在弹出框中输入一个名字例如MyComponent,这样一个组件就创建出来了。
然后在MyComponent中添加一个SayHello的方法,其他方法暂不实现,源代码如下所示:
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 | package com.cdai.plugin.rapidg; import com.intellij.openapi.components.ApplicationComponent; import com.intellij.openapi.ui.Messages; import org.jetbrains.annotations.NotNull; /** *MyComponent *User:cdai *Date:13-11-4 *Time:上午10:08 */ public class MyComponent implements ApplicationComponent{ public MyComponent(){ } public void initComponent(){ //TODO:insertcomponentinitializationlogichere } public void disposeComponent(){ //TODO:insertcomponentdisposallogichere } @NotNull public StringgetComponentName(){ return "MyComponent" ; } public void sayHello(){ //Showdialogwithmessage Messages.showMessageDialog( "HelloWorld!" , "Sample" , Messages.getInformationIcon() ); } } |
现在需要添加一个Action让使用我们插件的用户可以通过菜单或其他方式点击到插件。
Action主要工作是创建一个Application和MyComponent对象,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.cdai.plugin.rapidg; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.application.Application; import com.intellij.openapi.application.ApplicationManager; /** *SayHelloAction *User:cdai *Date:13-11-4 *Time:上午10:16 */ public class SayHelloAction extends AnAction{ @Override public void actionPerformed(AnActionEvente){ Applicationapplication=ApplicationManager.getApplication(); MyComponentmyComponent=application.getComponent(MyComponent. class ); myComponent.sayHello(); } } |
其实前面两步新建Component和Action的同时,IDEA在帮我们自动将它们注册到META-INF/plugin.xml中。
我们刚才添加的ApplicationComponent和Action会在<application-components>结点下,plugin.xml最终是下面的样子:
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 | < idea-plugin version = "2" > < id >com.cdai.plugin.rapidg</ id > < name >CDai'sRapidGeneratorPlugin</ name > < version >1.0</ version > < vendor = "dc_726@163.com" url = " >CDai</ vendor > < description > <![CDATA[ Entershortdescriptionforyourpluginhere.<br> <small>mostHTMLtagsmaybeused</small> ]]> </ description > < change-notes > <![CDATA[ Addchangenoteshere.<br> <small>mostHTMLtagsmaybeused</small> ]]> </ change-notes > <!--pleasesee < idea-version since-build = "107.105" /> <!--pleasesee onhowtotargetdifferentproducts--> <!--uncommenttoenableplugininallproducts <depends>com.intellij.modules.lang</depends> --> < application-components > <!--Addyourapplicationcomponentshere--> < component > < implementation-class >com.cdai.plugin.rapidg.MyComponent</ implementation-class > </ component > </ application-components > < project-components > <!--Addyourprojectcomponentshere--> </ project-components > < actions > <!--Addyouractionshere--> < action id = "SayHello" class = "com.cdai.plugin.rapidg.SayHelloAction" text = "SayHello!" > < add-to-group group-id = "WindowMenu" anchor = "first" /> </ action > </ actions > < extensions defaultExtensionNs = "com.intellij" > <!--Addyourextensionshere--> </ extensions > </ idea-plugin > |
打开Run/Debug配置对话框,新加一个Plugin类型的,Useclasspathofmodule选择刚才的示例项目。
运行起来就会发现,原来会启动一个新的IntellijIDEA实例,重新走一遍启动配置过程,可以看到插件的名字就是plugin.xml中<name>中的值。
我们可以只选中我们刚开发的插件,忽略掉其他的。现在通过Window->SayHello!就可以触发我们的插件了,效果就是会弹出个对话框。
有趣的是,plugin.xml中其他的一些描述会在插件崩溃时显示给用户,将问题报告给插件作者。
4.插件配置面板
很多插件都是在Settings中有配置页的,现在简单介绍一下如何为我们的插件添加一个配置页。
首先改造一下MyComponent类,主要变化就是多实现了一个Configurable接口。这个接口中有一个createComponent方法,
这个方法返回Swing的JComponent对象就会显示到Settings里。另外使用IDEA提供的SwingDesigner设计器还是挺方便的,
自动生成的样式和布局代码为了避免被修改,也不会被我们看到(与NetBeans不同),所以最终代码很简洁。
最终效果就是这样的了,我们在设计器里设计的面板嵌入到了右边。
5.带对话框的插件
一种常见的插件就是点击插件对应的菜单项后,弹出一个对话框(例如搜索工作空间里的类、提交SVN前的代码确认等等)。
其实很简单,实现方法就是先创建一个Dialog,然后在Swing设计器中设计好Dialog中的控件布局,最后在Action中显示出对话框。
具体代码就不列举了,有需求可以找我索取。
参考资料
Wehavetriedtoanswersomeofthesequestionsinthenewdemo:GoogleSearch
PluginforIntelliJIDEA.Thisdemoexplainsthebasicsofpluginwritingandpublishing.
Moreexperiencedplugindeveloperscanfindanswersinthesetwoarticles:
1.The
BasicsofPluginDevelopmentforIntelliJIDEAbyAlexeyEfimov(forbeginners)
2.Developing
CustomLanguagePluginsforIntelliJIDEA5.0byDmitryJemerov(formoreadvanceddevelopers)
AlsoIntelliJIDEAtriestoeasetheplugindevelopmentbyprovidingspecificcodegeneration,completion,etc.Justlookwhatwasdonefor5.0
and5.1releasesforOpen
API&Plugins.
相关文章推荐
- Intellij IDEA插件开发入门
- Intellij IDEA插件开发入门
- 搜索引擎使用入门--如果我想开发chrome插件应该怎么做
- Android Studio 插件开发详解一:入门练手
- 【Intellij Idea 插件从入门到深坑】系列目录
- Chrome浏览器插件开发入门
- discuz插件开发新手入门 超详细
- Eclipse插件开发快速入门(二)
- chrome插件开发入门
- Eclipse插件开发入门(一)
- K3BOS插件开发入门教程
- Babel插件开发入门指南
- MS IDE RAD技术入门----VC6插件开发入门(1)----使用VC来可行性
- 一个设计性很强的入门插件开发例子
- IntelliJ IDEA插件开发入门教程(一)
- Eclipse插件开发之新手入门
- firefox 插件扩展开发 入门记录
- 干货|IntelliJ IDEA插件开发
- Windows Live Writer 插件开发入门
- grafana插件开发入门