IDEA-插件开发踩坑记录-第四坑-Action介绍与工具栏、弹出菜单中运用


场景

插件开发的时候,想把一个action在多个位置使用,可又不知道咋办。
找官网文档看Action详细使用方法:官网链接↗


一、Action的配置

官网介绍:

原文译文
“id” (required) - specifies a unique identifier for the action“id”(必填)-Action的唯一标识符
“class” (required) - specifies the FQN of the class implementing the action“class”(必填)实现Action行为的类全路径(FQN:完全限定名)
“text” (required) - specifies the default long-version text to be displayed for the action (tooltip for toolbar button or text for menu item)“text”(必填) 指定动作显示的默认文本(工具栏按钮提示或菜单项的文本)
“use-shortcut-of” (optional) - specifies the ID of the action whose keyboard shortcut this action will use“use-shortcut-of”(可选) 键盘快捷键ID,顾名思义:设置Action快捷键用的,但指定的是映射ID
“description” (optional) - specifies the text which is displayed in the status bar when the action is focused“description”(可选) 聚焦Action时,状态栏中显示的文本
“icon” (optional) - specifies the icon which is displayed on the toolbar button or next to the menu item“icon”(可选) 工具栏按钮上或菜单项旁边的图标

子设置项比较有用的:add-to-group 【这里就不赘述了】
group-id=“加入到的分组”
relative-to-action=“链到某个Action”
anchor=“位置”/>

二、Action可以用在哪

为了方便举例:准备一个Action

package org.intellij.sdk.action;

......省略import

public class DemoAction extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        Notification notify = NotifyGroupConstants.NOTIFICATION_GROUP
                .createNotification("你点击了DemoAction", NotificationType.INFORMATION);
        notify.notify(e.getProject());
    }
}

准备好plugin.xml配置,在plugin.xml添加如下内容:

<actions>
    <group id="MyGroup" text="自定的group"/>
    <action id="org.intellij.sdk.action.DemoAction"
            text="样例Action"
            class="org.intellij.sdk.action.DemoAction"
            icon="AllIcons.Actions.Refresh">
        <add-to-group group-id="EditorPopupMenu"/>
        <add-to-group group-id="MyGroup"/>
    </action>
</actions>

1、 ActionPopupMenu(弹出框包含菜单)

使用方法:ActionManager.createActionPopupMenu()

DefaultActionGroup group = 
  (DefaultActionGroup)ActionManager.getInstance().getAction("MyGroup");
ActionPopupMenu actionPopupMenu = ActionManager.getInstance().createActionPopupMenu(ActionPlaces.POPUP, group);
JPopupMenu component = actionPopupMenu.getComponent();
 myToolWindowContent.addMouseListener(new MouseAdapter() {
     @Override
     public void mouseClicked(@NotNull MouseEvent mouseEvent) {
         if (mouseEvent.getButton() == MouseEvent.BUTTON3) {
             component.show(myToolWindowContent, mouseEvent.getX(), mouseEvent.getY());
         }
     }
 });

效果图:
在这里插入图片描述

2、ActionToolbar(工具栏)

代码里为toolBar设定action:

DefaultActionGroup group = (DefaultActionGroup) ActionManager.getInstance().getAction("MyGroup");
ActionToolbar actionToolbar = ActionManager.getInstance()
        .createActionToolbar(ActionPlaces.TOOLBAR, group, true);
// 为了好看,设置下边框
topTools.setBorder(JBUI.Borders.customLine(JBColor.GRAY, 0, 0, 1, 0));
topTools.add(actionToolbar.getComponent());

效果图:
在这里插入图片描述

结语

此处展示的只是在弹出菜单和工具栏的运用,还有其他很多种用法。这里就不废话了,有其他想法的话,欢迎评论区一起讨论。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值