Intellij IDEA插件開發(fā)入門詳解
現(xiàn)今的IDE盡管有如“洪水猛獸”般強(qiáng)大,但要知道再?gòu)?qiáng)大的IDE也沒法提供給使用者想要的一切功能,所以IDE一般都提供有API接口供開發(fā)者自行擴(kuò)展。下面以Intellij IDEA 12下的插件開發(fā)為例,來看一下如何進(jìn)一步增強(qiáng)IDE以適應(yīng)開發(fā)者的需求。
1.創(chuàng)建Plugin工程
如果Module SDK中沒有可選的SDK,那么點(diǎn)擊New新添加一個(gè)SDK,目錄就選擇Intellij的安裝位置即可。
創(chuàng)建出的Plugin項(xiàng)目結(jié)構(gòu)很簡(jiǎn)單,只是在META-INF下多了一個(gè)plugin.xml配置文件,后文會(huì)介紹到它的用處。
2.讓插件Say哈嘍
2.1添加Component
在src目錄上Alt+Insert,可以看到New對(duì)話框中列出有三種組件,分別對(duì)應(yīng)三種級(jí)別:Application、Project、Module Component。這里我們選擇Application Component作為實(shí)例,在彈出框中輸入一個(gè)名字例如MyComponent,這樣一個(gè)組件就創(chuàng)建出來了。
然后在MyComponent中添加一個(gè)SayHello的方法,其他方法暫不實(shí)現(xiàn),源代碼如下所示:
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
現(xiàn)在需要添加一個(gè)Action讓使用我們插件的用戶可以通過菜單或其他方式點(diǎn)擊到插件。
Action主要工作是創(chuàng)建一個(gè)Application和MyComponent對(duì)象,代碼如下:
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配置文件
其實(shí)前面兩步新建Component和Action的同時(shí),IDEA在幫我們自動(dòng)將它們注冊(cè)到META-INF/plugin.xml中。
我們剛才添加的Application Component和Action會(huì)在<application-components>結(jié)點(diǎn)下,plugin.xml最終是下面的樣子:
<idea-plugin version="2"> <id>com.cdai.plugin.rapidg</id> <name>CDai's Rapid Generator Plugin</name> <version>1.0</version> <vendor email="dc_726@163.com" url="http://www.yourcompany.com">CDai</vendor> <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.運(yùn)行調(diào)試
打開Run/Debug配置對(duì)話框,新加一個(gè)Plugin類型的,Use classpath of module選擇剛才的示例項(xiàng)目。
運(yùn)行起來就會(huì)發(fā)現(xiàn),原來會(huì)啟動(dòng)一個(gè)新的Intellij IDEA實(shí)例,重新走一遍啟動(dòng)配置過程,可以看到插件的名字就是plugin.xml中<name>中的值。我們可以只選中我們剛開發(fā)的插件,忽略掉其他的?,F(xiàn)在通過Window->Say Hello!就可以觸發(fā)我們的插件了,效果就是會(huì)彈出個(gè)對(duì)話框。
有趣的是,plugin.xml中其他的一些描述會(huì)在插件崩潰時(shí)顯示給用戶,將問題報(bào)告給插件作者。
4.插件配置面板
很多插件都是在Settings中有配置頁的,現(xiàn)在簡(jiǎn)單介紹一下如何為我們的插件添加一個(gè)配置頁。
首先改造一下MyComponent類,主要變化就是多實(shí)現(xiàn)了一個(gè)Configurable接口。這個(gè)接口中有一個(gè)createComponent方法,這個(gè)方法返回Swing的JComponent對(duì)象就會(huì)顯示到Settings里。另外使用IDEA提供的Swing Designer設(shè)計(jì)器還是挺方便的,自動(dòng)生成的樣式和布局代碼為了避免被修改,也不會(huì)被我們看到(與NetBeans不同),所以最終代碼很簡(jiǎn)潔。
最終效果就是這樣的了,我們?cè)谠O(shè)計(jì)器里設(shè)計(jì)的面板嵌入到了右邊。
5.帶對(duì)話框的插件
一種常見的插件就是點(diǎn)擊插件對(duì)應(yīng)的菜單項(xiàng)后,彈出一個(gè)對(duì)話框(例如搜索工作空間里的類、提交SVN前的代碼確認(rèn)等等)。其實(shí)很簡(jiǎn)單,實(shí)現(xiàn)方法就是先創(chuàng)建一個(gè)Dialog,然后在Swing設(shè)計(jì)器中設(shè)計(jì)好Dialog中的控件布局,最后在Action中顯示出對(duì)話框。具體代碼就不列舉了,有需求可以找我索取。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java concurrency集合之LinkedBlockingDeque_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
LinkedBlockingDeque是雙向鏈表實(shí)現(xiàn)的雙向并發(fā)阻塞隊(duì)列。該阻塞隊(duì)列同時(shí)支持FIFO和FILO兩種操作方式,即可以從隊(duì)列的頭和尾同時(shí)操作(插入/刪除);并且,該阻塞隊(duì)列是支持線程安全。2017-06-06Java Springboot之Spring家族的技術(shù)體系
今天帶大家來學(xué)習(xí)Spring家族的技術(shù)體系,文中有非常詳細(xì)的圖文介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05java實(shí)現(xiàn)二叉樹的創(chuàng)建及5種遍歷方法(總結(jié))
下面小編就為大家?guī)硪黄猨ava實(shí)現(xiàn)二叉樹的創(chuàng)建及5種遍歷方法(總結(jié))。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04淺談Java實(shí)現(xiàn)面向?qū)ο缶幊蘪ava oop
這篇文章主要介紹了淺談Java實(shí)現(xiàn)面向?qū)ο缶幊蘪ava oop,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07SpringBoot整合Echarts實(shí)現(xiàn)用戶人數(shù)和性別展示功能(詳細(xì)步驟)
這篇文章主要介紹了SpringBoot整合Echarts實(shí)現(xiàn)用戶人數(shù)和性別展示,通過數(shù)據(jù)庫(kù)設(shè)計(jì)、實(shí)現(xiàn)數(shù)據(jù)訪問層、業(yè)務(wù)邏輯層和控制層的代碼編寫,以及前端頁面的開發(fā),本文詳細(xì)地介紹了SpringBoot整合Echarts的實(shí)現(xiàn)步驟和代碼,需要的朋友可以參考下2023-05-05