1. 程式人生 > 實用技巧 >java開發IDEA外掛入門

java開發IDEA外掛入門

簡介

IDEA是我們常用的開發工具,我們也會用到很多IDEA提供的外掛,如lombok外掛,checkstyle外掛等,今天我們就來開發一個自己的外掛。

環境準備

IDEA安裝PluginDevkit外掛,這是一個幫助我們開發外掛的外掛。

建立外掛專案

建立成功之後的專案結構為

plugin.xml為專案的核心配置檔案

<idea-plugin>
  <!--外掛Id-->
  <id>com.strongmore.secondplugin.id</id>
  <!--外掛名稱-->
  <name>SecondPlugin</name>
  <!--外掛版本號-->
  <version>1.0</version>
  <!--開發者資訊-->
  <vendor email="[email protected]" url="http://www.github.com">strongmore</vendor>
  <!--外掛描述資訊-->
  <description><![CDATA[
      this is my second plugin that to show in blog
    ]]></description>
  <!--外掛版本更新資訊-->
  <change-notes><![CDATA[
      this is my second plugin that to show in blog
    ]]>
  </change-notes>

  <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
  <idea-version since-build="173.0"/>

  <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
       on how to target different products -->
   <!--uncomment to enable plugin in all products-->
  <!--專案建立之後這個標籤是註釋的,需要開啟,不然不能部署到IDEA的外掛庫-->
  <depends>com.intellij.modules.lang</depends>

  <!--配置一些擴充套件功能,如工具窗-->
  <extensions defaultExtensionNs="com.intellij">
    <!-- Add your extensions here -->
  </extensions>

  <!--配置我們定義的動作命令,如右鍵點選-->
  <actions>
    <!-- Add your actions here -->
  </actions>

</idea-plugin>

開發功能

我們開發一個點選右鍵,每天一碗毒雞湯的小功能

建立我們的動作命令

建立成功之後自動在plugin.xml中新增配置

<!--配置我們定義的動作命令,如右鍵點選-->
  <actions>
    <!-- Add your actions here -->
    <action id="EditorPopupTauntId" class="com.strongmore.secondplugin.action.EditorPopupTaunt"
      text="毒雞湯" description="毒雞湯">
      <add-to-group group-id="EditorPopupMenu" anchor="first"/>
    </action>
  </actions>

當點選我們的按鈕時就會執行此方法

建立對話方塊

import com.intellij.openapi.ui.DialogWrapper;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.jetbrains.annotations.Nullable;

public class TauntDialog extends DialogWrapper {

  private JLabel jLabel;

  public TauntDialog() {
    super(true);
    setTitle("每天一碗毒雞湯");
    init();
  }

  @Nullable
  @Override
  protected JComponent createCenterPanel() {
    JPanel jPanel = new JPanel();
    jLabel = new JLabel();
    jLabel.setText("測試");
    jPanel.add(jLabel);
    return jPanel;
  }

  @Override
  protected JComponent createSouthPanel() {
    JPanel jPanel = new JPanel();
    JButton jButton = new JButton("再來一碗");
    jButton.addActionListener((e) -> {
      jLabel.setText("測試2");
    });
    jPanel.add(jButton);
    return jPanel;
  }
}

現在我們的毒雞湯內容是寫死的,需要不斷的變化,我們從公共的API獲取,毒雞湯API,網路請求我們使用spring-web的RestTemplate.

新增第三方jar包

使用網路請求更新毒雞湯內容

public class HttpUtility {

  public static String getTauntContent() {
    TauntResponse tauntResponse = httpGet("https://api.nextrt.com/api/dutang", TauntResponse.class);
    if (Objects.nonNull(tauntResponse) && tauntResponse.isSuccess()) {
      return tauntResponse.getData().getContent();
    }
    return null;
  }

  private static <T> T httpGet(String url, Class<T> clazz) {
    RestTemplate restTemplate = new RestTemplate();
    HttpEntity<String> request = new HttpEntity<>(new HttpHeaders());
    ResponseEntity<T> entity = restTemplate.exchange(url, HttpMethod.GET, request, clazz);
    if (entity.getStatusCode().is2xxSuccessful()) {
      return entity.getBody();
    }
    return null;
  }

}
public class TauntResponse {

  private String status;
  private String msg;
  private TauntInfo data;
  private Long timestamp;

  public void setStatus(String status) {
    this.status = status;
  }

  public void setData(TauntInfo data) {
    this.data = data;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  public void setTimestamp(Long timestamp) {
    this.timestamp = timestamp;
  }

  public Long getTimestamp() {
    return timestamp;
  }

  public String getMsg() {
    return msg;
  }

  public String getStatus() {
    return status;
  }

  public TauntInfo getData() {
    return data;
  }

  public boolean isSuccess() {
    return "1".equals(status);
  }
}
public class TauntInfo {

  private Integer id;
  private String content;

  public void setContent(String content) {
    this.content = content;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public Integer getId() {
    return id;
  }

  public String getContent() {
    return content;
  }
}

定義網路請求工具類和接收響應的資料結構

public class TauntDialog extends DialogWrapper {

  private JLabel jLabel;

  public TauntDialog() {
    super(true);
    setTitle("每天一碗毒雞湯");
    init();
  }

  @Nullable
  @Override
  protected JComponent createCenterPanel() {
    JPanel jPanel = new JPanel();
    jLabel = new JLabel();
    jLabel.setText(HttpUtility.getTauntContent());
    jPanel.add(jLabel);
    return jPanel;
  }

  @Override
  protected JComponent createSouthPanel() {
    JPanel jPanel = new JPanel();
    JButton jButton = new JButton("再來一碗");
    jButton.addActionListener((e) -> {
      jLabel.setText(HttpUtility.getTauntContent());
    });
    jPanel.add(jButton);
    return jPanel;
  }
}

執行除錯

執行之後會重新啟動一個IDEA介面

點選右鍵就會出現我們定義的action,點選

打包

打包之後會生成一個zip包

部署

我們可以將外掛安裝到我們的IDEA上

也可以釋出到IDEA的外掛庫,需要註冊IDEA賬號,註冊之後去我的外掛管理

選擇打包好的zip包上傳,等待稽核就好了

參考

Idea外掛開發-開發自己的第一款idea外掛
IDEA外掛開發