天天看點

IDEA插件開發彙總

作者:成名之路

IDEA插件開發環境搭建和入門示例,可以看之前的文章 《IDEA插件開發入門指南》

這裡來說一下開發插件的一些相關知識

gradle task 運作 打包

1、打開gradle面闆

IDEA插件開發彙總

gradle面闆

2、runIde,啟動沙盒idea,運作插件

3、buildPlugin,打包建構插件,打包好後,插件在項目的build > distributions檔案裡面

插件的通路入口

Action

IDEA插件開發彙總

MyFirstAction示例

1、建立Action

Action作為插件的常用入口,建立方式很簡單

IDEA插件開發彙總

建立Action

也可以自己編寫一個Action類,繼承AnAction,重寫actionPerformed方法,在actionPerformed方法裡編寫插件的業務代碼

2、注冊Action

通過idea建立action時,會彈出建立框,來填寫action的資訊,填寫完點确定,idea會自動将建立好的action注冊到resources/META-INF/plugin.xml裡,它會在在actions節點下,添加一個action的配置節點,當然,也可以自己去plugin.xml裡面去增加,action配置示例如下:

<actions>
        <action id="com.abc.action.MyFirstAction"
                class="com.abc.action.MyFirstAction"
                text="MyFirstAction"
                description="一個示例">
            <add-to-group group-id="ToolsMenu" anchor="first"/>
            <keyboard-shortcut keymap="$default" first-keystroke="ctrl F" second-keystroke="ctrl A"/>
        </action>
    </actions>           

配置說明:

屬性 說明
id action的唯一辨別符,推薦使用action的全路徑,必填
class action的實作類路徑,必填
text action在idea中顯示的名字,必填
description action的描述
group-id action加在哪個菜單組裡,示例是加在Tools菜單組裡
anchor action在菜單組的位置,示例是加在tools菜單裡的第一個
first-keystroke 快捷鍵1
second-keystroke 快捷鍵2

Tool Window

就是在idea的左邊,右邊,下邊顯示的工具視窗(插件視窗)

借用官網的一段說明:Tool windows provide access to development tasks: viewing your project structure, running and debugging your application, integration with version control systems and other external tools, code analysis, search, navigation, and so on. By default, tool windows are attached to the bottom and sides of the main window. However, you can rearrange and even detach them to use as separate windows, for example, on another monitor.
  1. Project tool window
  2. Structure tool window
  3. Run tool window
  4. Maven tool window
IDEA插件開發彙總

Tool Window

1、建立tool window

通過實作ToolWindowFactory建立tool window,然後将我們定義好的元件,添加進去,例如按鈕,下拉框等

說明:idea插件UI元件使用Java swing開發的,需要了解一些swing元件的用法

import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class MyFirstToolWindow implements ToolWindowFactory {

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();//擷取内容面闆工廠
        Content content = contentFactory.createContent(pluginUI(), "", false);//添加自定義的面闆
        toolWindow.getContentManager().addContent(content);//添加到tool window管理器中
    }

    private JPanel pluginUI() {
        JPanel panel = new JPanel();
        JLabel label = new JLabel("hello");
        panel.add(label);
        return panel;
    }
}           

2、注冊tool window

同樣的是在plugin.xml中配置

<extensions defaultExtensionNs="com.intellij">
        <toolWindow factoryClass="com.abc.toolwin.MyFirstToolWindow" id="plugin tool window"/>
</extensions>
           
IDEA插件開發彙總

tool window顯示效果

插件資料存儲

1、建立State

插件開發中,會涉及到有些資料需要存儲起來,通過實作PersistentStateComponent接口來實作,資料會以xml的格式存放在項目的idea檔案夾下,插件加載時會擷取資料并設值,代碼示例

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;


@State(name = "my-data", storages = {@Storage(value = "my-data.xml")})
public class MyData implements PersistentStateComponent<MyData> {
    private String name;

    //擷取資料
    @Override
    public @Nullable MyData getState() {
        return this;
    }

    //加載資料
    @Override
    public void loadState(@NotNull MyData state) {
        this.name = state.getName();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}           

2、配置State

同樣的是在plugin.xml中配置

<extensions defaultExtensionNs="com.intellij">
        <toolWindow factoryClass="com.abc.toolwin.MyFirstToolWindow" id="plugin tool window"/>
        <!--projectService時項目級,applicationService應用級-->
        <projectService serviceImplementation="com.abc.data.MyData"/>
    </extensions>           

3、使用State的資料

import com.abc.data.MyData;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;

public class MyFirstToolWindow implements ToolWindowFactory {

    public static Project project;

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        MyFirstToolWindow.project = project;
        ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();//擷取内容面闆工廠
        Content content = contentFactory.createContent(pluginUI(), "", false);//添加自定義的面闆
        toolWindow.getContentManager().addContent(content);//添加到tool window管理器中
    }

    private JPanel pluginUI() {
        saveData();
        MyData data = getData();
        JPanel panel = new JPanel();
        JLabel label = new JLabel("hello");
        panel.add(label);
        JTextField field = new JTextField();
        field.setText(data.getName());
        field.setPreferredSize(new Dimension(300, 30));
        panel.add(field);
        return panel;
    }

    //擷取資料
    private MyData getData() {
        return project.getService(MyData.class).getState();
    }

  	//儲存資料
    public void saveData() {
        MyData state = project.getService(MyData.class).getState();
        state.setName("張三");
    }
}           
IDEA插件開發彙總

常用元件

Idea插件是基于Java swing開發,是以swing的元件都可以使用,idea在swing元件的基礎上進行了擴充,例如常見的JBLabel -> JLabel,JBTextField ->JTextField。這裡寫一下idea的一些獨有的元件

1、通知元件notify

在 IDEA 右下角通知使用者

IDEA插件開發彙總

通知

JButton button = new JButton("通知");
        button.addActionListener(e -> {
            Notifications.Bus.notify(new Notification(Notifications.SYSTEM_MESSAGES_GROUP_ID, "通知标題", "通知内容", NotificationType.INFORMATION), null);
        });
        panel.add(button);           

通知類型常用的有INFORMATION(資訊),WARNING(警告),ERROR(錯誤)

2、彈出提示框

IDEA插件開發彙總

提示框

IDEA插件開發彙總

是,否,取消彈框

JButton alertError = new JButton("彈出提示");
        alertError.addActionListener(e -> {
            Messages.showErrorDialog("錯誤資訊", "标題");
        });
        panel.add(alertError);

        JButton alertYesOrNo = new JButton("是否提示框");
        alertYesOrNo.addActionListener(e -> {
            int retVal = Messages.showYesNoCancelDialog("消息内容", "标題", Messages.getQuestionIcon());
            if (Messages.YES == retVal) {
                Messages.showErrorDialog("點選了【是】", "标題");
            } else if (Messages.NO == retVal) {
                Messages.showErrorDialog("點選了【否】", "标題");
            } else if (Messages.CANCEL == retVal) {
                Messages.showErrorDialog("點選了【取消】", "标題");
            }
        });
        panel.add(alertYesOrNo);           

結束

喜歡的朋友,點贊關注收藏吧,這是很大的驅動力

憑誰問,廉頗老矣,尚能飯否?

繼續閱讀