初試IDEA插件開發

這裏我們寫一個自己的插件,僅僅是簡單的彈出一個對話框。

1、新建一個插件項目

  • File->new->project->intellij platform plugin
  • 選擇好project sdk:如果project sdk爲空,只需要new一個,選擇你的idea安裝目錄即可。
  • 點擊下一步,選擇好安裝路徑完成新建。
    這裏寫圖片描述
  • 建好的目錄結構
    這裏寫圖片描述

2、實現彈出對話框的功能

  • 新建一個Application Component:在src目錄上右鍵->new->Application Component。
  • 添加一個方法sayHello()。其他需要實現的接口方法暫且不管。具體代碼(注意import的包名):
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;

/**
 * Created by mislead on 2015/6/4.
 */
public class HelloComponent implements ApplicationComponent {
    public HelloComponent() {
    }

    public void initComponent() {
        // TODO: insert component initialization logic here
    }

    public void disposeComponent() {
        // TODO: insert component disposal logic here
    }

    @NotNull
    public String getComponentName() {
        return "HelloComponent";
    }

    public void sayHello() {
        Messages.showMessageDialog("hello, plugin", "hello", Messages.getInformationIcon());
    }
}
  • 新建一個Action
    這裏寫圖片描述
  • Action具體代碼:
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;

/**
 * Created by mislead on 2015/6/4.
 */
public class SayHelloAction extends AnAction {
    public void actionPerformed(AnActionEvent e) {
        Application application = ApplicationManager.getApplication();
        HelloComponent component = application.getComponent(HelloComponent.class);
        component.sayHello();

    }
}
  • 完成之後運行或者調試這裏寫圖片描述:運行會重新運行一個IDEA 的實例程序,第一次可能會花點時間,會重新提示你進行設置和註冊。
  • 插件效果:
    這裏寫圖片描述 這裏寫圖片描述
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章