我想为IntelliJ Idea创建一个插件,它将一个小按钮放入状态栏.
AFAIK的代码必须如下所示:
final StatusBar statusBar = WindowManager.getInstance().getStatusBar(project); final StatusBarWidget widget = new WordCounterWidget(); statusBar.addWidget(widget);
WordCounterWidget是我想要放入状态栏的按钮.
我试着说出来
>当我选择一个特定的菜单项时,调用action
>进入project service的构造函数.
这些都没有带来预期的结果 – 按钮没有显示.
用于插入按钮的上述代码(或其他可显示文本且可点击的内容)是否正确?如果是的话,我应该把它放在哪里,以便在项目打开,重新打开或创建时执行它(状态栏小部件应该在Idea中打开项目时始终可见)?
更新1:尝试将WordCounterServiceImpl的代码更改为
public class WordCounterServiceImpl implements WordCounterService { private final Project project; public WordCounterServiceImpl(final Project project) { this.project = project; } @Override public void projectOpened() { final StatusBar statusBar = WindowManager.getInstance().getStatusBar(project); final StatusBarWidget widget = new WordCounterWidget(); statusBar.addWidget(widget); } }
该按钮仍未显示.
解决方法
com.intellij.openapi.components.ProjectComponent#projectOpened
见:https://www.jetbrains.org/intellij/sdk/docs/basics/plugin_structure/plugin_components.html