IntelliJ插件开发-status bar

简介

IntelliJ平台允许插件通过自定义小部件扩展IDE状态栏。状态栏小部件是小型的UI元素,允许为当前文件、项目、IDE等提供有用的信息和设置。例如,状态栏包含显示当前文件编码或项目的当 前版本控制分支的小部件。由于其突出的展示和有限的空间,它们应仅用于显示足够相关的信息或设置。

扩展状态栏以添加新的小部件的起点是StatusBarWidgetFactory接口,该接口在com.intellij.statusBarWidgetFactory扩展点中注册。 注意:在plugin.xml中的注册中必须提供id属性,并与StatusBarWidgetFactory.getId()的值匹配。如果小部件提供与编辑器文件相关的信息或功能,请考虑扩展StatusBarEditorBasedWidgetFactory类。

使用示例

  1. 实现StatusBarWidgetFactory接口。
    如果你的status bar需要依赖Editor,可以直接继承StatusBarEditorBasedWidgetFactory类,它提供了getTextEditor方法可以获取当前选中的编辑器。
public class EditorStatusBarWidgetFactory extends StatusBarEditorBasedWidgetFactory {

	private final static String ID = "MyStatusBar";
	private final static String NAME = "my status bar";

    @Override
    public @NonNls @NotNull String getId() {
        return ID;
    }

    @Override
    public @Nls @NotNull String getDisplayName() {
        return NAME;
    }

    @Override
    public @NotNull StatusBarWidget createWidget(@NotNull Project project) {
        return new MyStatusBarWidget(project);
    }

    @Override
    public void disposeWidget(@NotNull StatusBarWidget widget) {}
}
  1. 实现StatusBarWidget接口
public class MyStatusBarWidget implements StatusBarWidget {
    private final Project project;

    public MyStatusBarWidget(@NotNull Project project) {
        this.project = project;
    }

    @Nullable
    @Override
    public WidgetPresentation getPresentation() {
        // 返回StatusBar组件的展示样式
        return new CustomStatusBarPresentation();
    }

    @Override
    public void install(@NotNull StatusBar statusBar) {
        // 安装StatusBar组件
    }

    @Override
    public void dispose() {
        // 清理资源
    }

    private class MyStatusBarPresentation implements StatusBarWidget.IconPresentation {
        @Nullable
        @Override
        public Icon getIcon() {
            // 返回StatusBar组件的图标
            return null;
        }

        @Nullable
        @Override
        public String getTooltipText() {
            // 返回StatusBar组件的工具提示文本
            return "Custom Status Bar Widget";
        }

        @Nullable
        @Override
        public String getText() {
            // 返回StatusBar组件的文本
            return "Custom Widget";
        }

        @Override
        public StatusBarWidget.Alignment getAlignment() {
            // 返回StatusBar组件的对齐方式
            return Alignment.LEFT;
        }
    }
}

除了直接实现StatusBarWidget接口外,IntelliJ还提供了一个CustomStatusBarWidget接口继承自StatusBarWidget,它提供了getComponent方法,允许用户返回自定义的JComponent展示到status bar上。EditorBasedStatusBarPopup类就是实现了这个接口,它自己画了个JPanel,并添加了点击事件,达到点击后弹出菜单的效果。

public class MyStatusBarWidget extends EditorBasedStatusBarPopup {

    private static final String WIDGET_ID = "MyStatusBar";
    static String statusBarText = "status bar text";

    public MyStatusBarWidget(@NotNull Project project) {
        super(project, false);
    }

    @Override
    protected @NotNull WidgetState getWidgetState(@Nullable VirtualFile file) {
        String toolTip = statusBarText;
        EditorBasedStatusBarPopup.WidgetState state = new EditorBasedStatusBarPopup.WidgetState(toolTip, statusBarText, true);
        state.setIcon(IconLoader.getIcon("/iconPath", MyStatusBarWidget.class));
        return state;
    }

    @Override
    protected @Nullable ListPopup createPopup(DataContext context) {
		boolean showDisabledActions = false;
        AnAction action1 = ActionManager.getInstance().getAction("MyPlugin.Action1");
		AnAction action2 = ActionManager.getInstance().getAction("MyPlugin.Action2");
		DefaultActionGroup actionGroup = new DefaultActionGroup(List.of(action1, action2));
		return JBPopupFactory.getInstance().createActionGroupPopup("popup title", group, context
			, JBPopupFactory.ActionSelectionAid.SPEEDSEARCH, showDisabledActions);
    }

    @Override
    protected @NotNull StatusBarWidget createInstance(@NotNull Project project) {
        return new MyStatusBarWidget(project);
    }

    @Override
    public @NonNls @NotNull String ID() {
        return WIDGET_ID;
    }

    public static void update(@NotNull Project project) {
        EditorStatusBarWidget widget = findWidget(project);
        if (widget != null)
            widget.update(() -> widget.myStatusBar.updateWidget(WIDGET_ID));
    }

    public static void update(@NotNull Project project, String statusBarText) {
        EditorStatusBarWidget.statusBarText = statusBarText;
        EditorStatusBarWidget widget = findWidget(project);
        if (widget != null)
            widget.update(() -> widget.myStatusBar.updateWidget(WIDGET_ID));
    }

    @Nullable
    private static EditorStatusBarWidget findWidget(@NotNull Project project) {
        StatusBar bar = WindowManager.getInstance().getStatusBar(project);
        if (bar != null) {
            StatusBarWidget widget = bar.getWidget(WIDGET_ID);
            if (widget instanceof EditorStatusBarWidget)
                return (EditorStatusBarWidget)widget;
        }
        return null;
    }
	
	// 提供给外部的静态方法:更新statusBar的text
    public static void update(@NotNull Project project, String statusBarText) {
        EditorStatusBarWidget.statusBarText = statusBarText;
        EditorStatusBarWidget widget = findWidget(project);
        if (widget != null)
            widget.update(() -> widget.myStatusBar.updateWidget(WIDGET_ID));
    }
}

3.注册StatusBarWidgetFactory

<idea-plugin>
    <extensions defaultExtensionNs="com.intellij">
		<statusBarWidgetFactory order="first" id="MyStatusBar" implementation="com.demo.MyStatusBarWidgetFactory"/>
	extensions>
idea-plugin>

参考文献

Status Bar Widgets

你可能感兴趣的:(#,Intellij插件,java,intellij,idea)