現今的IDE盡管有如“洪水猛獸”般強大,但要知道再強大的IDE也沒法提供給使用者想要的一切功能,所以IDE一般都提供有API接口供開發者自行擴展。下面以Intellij IDEA 12下的插件開發為例,來看一下如何進一步增強IDE以適應開發者的需求。
1.創建Plugin工程
如果Module SDK中沒有可選的SDK,那么點擊New新添加一個SDK,目錄就選擇Intellij的安裝位置即可。
創建出的Plugin項目結構很簡單,只是在META-INF下多了一個plugin.xml配置文件,后文會介紹到它的用處。
2.讓插件Say哈嘍
2.1添加Component
在src目錄上Alt+Insert,可以看到New對話框中列出有三種組件,分別對應三種級別:Application、Project、Module Component。這里我們選擇Application Component作為實例,在彈出框中輸入一個名字例如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
|
package com.cdai.plugin.rapidg; import com.intellij.openapi.components.ApplicationComponent; import com.intellij.openapi.ui.Messages; import org.jetbrains.annotations.NotNull; /** * My Component * User: cdai * Date: 13-11-4 * Time: 上午10:08 */ public class MyComponent implements ApplicationComponent { public MyComponent() { } public void initComponent() { // TODO: insert component initialization logic here } public void disposeComponent() { // TODO: insert component disposal logic here } @NotNull public String getComponentName() { return "MyComponent" ; } public void sayHello() { // Show dialog with message Messages.showMessageDialog( "Hello World!" , "Sample" , Messages.getInformationIcon() ); } } |
2.2添加Action
現在需要添加一個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
|
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; /** * Say Hello Action * User: cdai * Date: 13-11-4 * Time: 上午10:16 */ public class SayHelloAction extends AnAction { @Override public void actionPerformed(AnActionEvent e) { Application application = ApplicationManager.getApplication(); MyComponent myComponent = application.getComponent(MyComponent. class ); myComponent.sayHello(); } } |
2.3配置文件
其實前面兩步新建Component和Action的同時,IDEA在幫我們自動將它們注冊到META-INF/plugin.xml中。
我們剛才添加的Application Component和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
49
50
|
< id >com.cdai.plugin.rapidg</ id > < name >CDai's Rapid Generator Plugin</ name > < version >1.0</ version > < description > <![CDATA[ Enter short description for your plugin here.<br> <small>most HTML tags may be used</small> ]]> </ description > < change-notes > <![CDATA[ Add change notes here.<br> <small>most HTML tags may be used</small> ]]> </ change-notes > <!-- please see http://confluence.jetbrains.net/display/IDEADEV/Build+Number+Ranges for description --> < idea-version since-build = "107.105" /> <!-- please see http://confluence.jetbrains.net/display/IDEADEV/Plugin+Compatibility+with+IntelliJ+Platform+Products on how to target different products --> <!-- uncomment to enable plugin in all products <depends>com.intellij.modules.lang</depends> --> < application-components > <!-- Add your application components here --> < component > < implementation-class >com.cdai.plugin.rapidg.MyComponent</ implementation-class > </ component > </ application-components > < project-components > <!-- Add your project components here --> </ project-components > < actions > <!-- Add your actions here --> < action id = "SayHello" class = "com.cdai.plugin.rapidg.SayHelloAction" text = "Say Hello!" > < add-to-group group-id = "WindowMenu" anchor = "first" /> </ action > </ actions > < extensions defaultExtensionNs = "com.intellij" > <!-- Add your extensions here --> </ extensions > </ idea-plugin > |
3.運行調試
打開Run/Debug配置對話框,新加一個Plugin類型的,Use classpath of module選擇剛才的示例項目。
運行起來就會發現,原來會啟動一個新的Intellij IDEA實例,重新走一遍啟動配置過程,可以看到插件的名字就是plugin.xml中<name>中的值。我們可以只選中我們剛開發的插件,忽略掉其他的。現在通過Window->Say Hello!就可以觸發我們的插件了,效果就是會彈出個對話框。
有趣的是,plugin.xml中其他的一些描述會在插件崩潰時顯示給用戶,將問題報告給插件作者。
4.插件配置面板
很多插件都是在Settings中有配置頁的,現在簡單介紹一下如何為我們的插件添加一個配置頁。
首先改造一下MyComponent類,主要變化就是多實現了一個Configurable接口。這個接口中有一個createComponent方法,這個方法返回Swing的JComponent對象就會顯示到Settings里。另外使用IDEA提供的Swing Designer設計器還是挺方便的,自動生成的樣式和布局代碼為了避免被修改,也不會被我們看到(與NetBeans不同),所以最終代碼很簡潔。
最終效果就是這樣的了,我們在設計器里設計的面板嵌入到了右邊。
5.帶對話框的插件
一種常見的插件就是點擊插件對應的菜單項后,彈出一個對話框(例如搜索工作空間里的類、提交SVN前的代碼確認等等)。其實很簡單,實現方法就是先創建一個Dialog,然后在Swing設計器中設計好Dialog中的控件布局,最后在Action中顯示出對話框。具體代碼就不列舉了,有需求可以找我索取。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/dc_726/article/details/14139155