eclipse插件开发中弹出操作的动态标签
我想创建一个简单的 Eclipse 插件,它的作用是:当您右键单击一个 java 项目时,它将显示一个弹出菜单,其中有一个项目的标签为“在该项目中找到 N 个 java 文件”,其中“N”是文件数。
我有一个想法,我可以更新“selectionChanged”中的标签:
public class CountAction implements IObjectActionDelegate {
public void selectionChanged(IAction action, ISelection selection) {
action.setText(countJavaFiles());
}
}
但如果我不单击该菜单项,它就不起作用,因为 CountAction
尚未加载,该 当您右键单击项目时,selectionChanged 不会被调用。
我在这个问题上花了很多时间,但没有解决。请帮我。
I want to create a simple eclipse plugin, which does: When you right click a java project, it will show a popup menu which has a item has label "N java files found in this project", where "N" is the file count.
I have an idea that I can update the label in "selectionChanged":
public class CountAction implements IObjectActionDelegate {
public void selectionChanged(IAction action, ISelection selection) {
action.setText(countJavaFiles());
}
}
But it doesn't work if I don't click that menu item, since the CountAction
has not been loaded, that selectionChanged
won't be invoked when you right-click on the project.
I have spent a lot of time on this, but not solved. Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@kett_chup 建议的文章的替代方法是使用
IElementUpdater
。只需处理程序
必须实现IElementUpdater
,handler.updateElement((UIElement element, Mapparameters)
必须使用元素设置所需的文本。 此新文本将显示在菜单和工具栏中
ICommandService.refreshElements(String commandId, Map filter) 更新命令文本时, 使用您的特定命令 ID - 全局命令服务通常就很好
IElementUpdater
接口也可用于更改选中状态 - 对于带有style=toggle
的命令 - 以及图标和工具提示。An alternative to the article suggested by @kett_chup, is to use
IElementUpdater
. Simplyhandler
must implementIElementUpdater
handler.updateElement((UIElement element, Map parameters)
must set the wanted text usingelement.setText("new text")
- this new text will show up in menus and toolbarsICommandService.refreshElements(String commandId, Map filter)
with your particular command ID - the global command service usually is just fineThe
IElementUpdater
interface can also be used to change the checked state - for commands withstyle=toggle
- as well as the icons and the tool tip.最后,我找到了一个非常简单的方法来实现这一点:
我不需要更改我的代码(有问题的示例代码),但我需要添加一个小的
startup
类:并添加以下内容到
plugin.xml
:此
MyStartUp
将在启动时加载该操作的实例,然后每次右键单击项目或文件时都会调用selectionChanged
。At last, I found a very easy way to implement this:
I don't need to change my code(the sample code in question), but I need to add a small
startup
class:And add following to
plugin.xml
:This
MyStartUp
will load an instance of that action at startup, thenselectionChanged
will be invoked each time when I right-click the projects or files.