IDEA-插件开发踩坑记录-第三坑-自定义事件


前言

做IDEA插件,发现一个数据变动好几处需要配合改动。感觉好麻烦,总不能每次改动都在修改数据的地方加上处理吧。想起来有个监听器模式,想必IDEA肯定也是有相应机制的。官网手册➹


一、IDEA的消息传递简介

事件组成元素:

  1. TOPIC:消息主题
  2. Listener:消息监听器
    IDEA消息主要组成部分
    消息广播:ProjectBus是ApplicationBus的子集,每个topic都可以有多个监听器;
  3. 消息通过ApplicationBus发送到topic1;
  4. Handler1收到消息通知;
  5. 消息传递给ProjectBus中topic1的订阅者(handler2和handler3);
    在这里插入图片描述

二、发布消息与处理消息

  1. 创建Listener
public interface DateRefreshListener {
    void refresh(String date,Project fireProject);
}
  1. 创建Publisher及其Topic
public class DateRefreshMessagePublisher {

    public static final Topic<DateRefreshListener> TOPIC = new Topic<>("date refresh events", DateRefreshListener.class);

    public static DateRefreshMessagePublisher getInstance(Project project) {
        return (DateRefreshMessagePublisher) project.getPicoContainer().getComponentInstanceOfType(DateRefreshMessagePublisher.class);
    }

    /**
     * 推送刷新事件
     */
    public void fireDateRefreshExecute(String date, Project project) {
        getPublisher(project).refresh(date, project);
    }

    @NotNull
    private static DateRefreshListener getPublisher(Project project) {
        return project.getMessageBus().syncPublisher(TOPIC);
    }
}
  1. 在Plugin.xml注册Service、Listener
public class DateRefreshNotifyListener implements DateRefreshListener {

    @Override
    public void refresh(String date, Project fireProject) {
        Notification notify = NotifyGroupConstants.NOTIFICATION_GROUP
                .createNotification("通过plugin.xml定义的-Listener给" + fireProject.getName() + "发通知" + date, NotificationType.INFORMATION);
        notify.notify(fireProject);
    }
}
    <extensions defaultExtensionNs="com.intellij">

        <projectService serviceImplementation="org.intellij.sdk.service.DateRefreshNotifyListener"/>
        <projectService serviceImplementation="org.intellij.sdk.listener.DateRefreshMessagePublisher"/>

        <toolWindow id="Sample Calendar" secondary="true" icon="AllIcons.General.Modified" anchor="right"
                    factoryClass="org.intellij.sdk.toolWindow.MyToolWindowFactory"/>
    </extensions>

    <projectListeners>
        <listener class="org.intellij.sdk.service.DateRefreshNotifyListener"
                  topic="org.intellij.sdk.listener.DateRefreshListener"/>
    </projectListeners>
    <idea-version since-build="193.0"/>
  1. 【演示用】在toolwindow创建时,增加监听方法
public class MyToolWindowFactory implements ToolWindowFactory {
    AtomicBoolean icon = new AtomicBoolean(true);

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        MyToolWindow myToolWindow = new MyToolWindow(toolWindow, project);
        ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
        Content content = contentFactory.createContent(myToolWindow.getContent(), "", false);
        toolWindow.getContentManager().addContent(content);

        project.getMessageBus().connect().subscribe(DateRefreshMessagePublisher.TOPIC, (date, fireProject) -> {
            if (icon.get()) {
                toolWindow.setIcon(AllIcons.Actions.Cancel);
                icon.set(false);
            } else {
                toolWindow.setIcon(AllIcons.Actions.Refresh);
                icon.set(true);
            }

            Notification notify = NotifyGroupConstants.NOTIFICATION_GROUP
                    .createNotification("修改toolWindow图标的同时,给" + fireProject.getName() + "发通知" + date, NotificationType.INFORMATION);
            notify.notify(fireProject);
        });
    }
}

效果图

在这里插入图片描述


系列文章

第一坑-创建gradle工程编译失败
第二坑-Action的Icon死活加不上去

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值