插件开发的时候,想把一个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
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>
使用方法: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());
}
}
});
效果图:
代码里为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());
效果图:
此处展示的只是在弹出菜单和工具栏的运用,还有其他很多种用法。这里就不废话了,有其他想法的话,欢迎评论区一起讨论。