Idea Plugin Component 介绍

2019-06-20  本文已影响0人  42cc8919e42f

Application level components

public class MyApplicationComponent implements BaseComponent {
    private final String COMPONENT_NAME = "ZhangkaiT1ApplicationName";

    @NotNull
    public static MyApplicationComponent getInstance() {
        return ApplicationManager.getApplication().getComponent(MyApplicationComponent.class);
    }

    @Override
    public void initComponent() {
        NotificationGroup group = new NotificationGroup("MyPluginNotification.UpdateCheck", NotificationDisplayType.BALLOON, true);
        Notification notification = group.createNotification("test", MessageType.INFO);
        Notifications.Bus.notify(notification);
    }

    @Override
    public void disposeComponent() {

    }

    @NotNull
    @Override
    public String getComponentName() {
        return COMPONENT_NAME;
    }
}

plugin.xml 中添加

<application-components>
        <component>
            <implementation-class>dev.zhangkai.t1.action.components.MyApplicationComponent</implementation-class>
        </component>
    </application-components>

上面的代码会在 idea 启动时生效

Project level components

public class MyProjectComponent implements ProjectComponent {
    private final String COMPONENT_NAME = "ZhangkaiT1ProjectComponent";
    private final int VERSION = 100;

    private final OkHttpClient client = new OkHttpClient();

    private final Request versionRequest = new Request.Builder().get().url("http://127.0.0.1:9000/version").build();

    private NotificationGroup group = new NotificationGroup("MyPluginNotification.UpdateCheck", NotificationDisplayType.BALLOON, true);
    private Project project;

    protected MyProjectComponent(@NotNull Project project) {
        this.project = project;
    }

    /**
     * 简单的插件升级检测
     */
    @Override
    public void projectOpened() {
        Call call = client.newCall(versionRequest);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                notifyMessage("please check your network connection", NotificationType.ERROR);
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                @NotNull String body = response.body().string();
                int version = Integer.valueOf(body);
                if (version > VERSION) {
                    notifyMessage("your plugin need update", NotificationType.INFORMATION);
                }
            }
        });
    }

    @Override
    public void projectClosed() {

    }

    @NotNull
    @Override
    public String getComponentName() {
        return COMPONENT_NAME;
    }

    private void notifyMessage(String message, NotificationType type) {
        Notification notification = group.createNotification("MyPlugin", "update check", message, type);
        Notifications.Bus.notify(notification, project);
    }
}

服务端是用go写的一个简单的接口,模拟返回当前最新版本号

func main() {
    http.HandleFunc("/version", pluginHandler)
    log.Fatal(http.ListenAndServe(":9000", nil))
}

func pluginHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "100")
}

实现的效果


image.png

Module level components

模块级别的组件,类似于前面的,各位看官自己尝试一下就会用了,不多赘述。

上一篇 下一篇

猜你喜欢

热点阅读