从Eclipse插件以编程方式更改菜单项 [英] Change Menu Items Programmatically From Eclipse Plugin
问题描述
我希望能够在启动我的eclipse插件应用程序时完全删除菜单项。我想做的是能够根据用户操作的业务逻辑稍后添加这些菜单项。有没有办法做到这一点?我看过使用贡献,但我觉得这不是我想要的。
I would like to be able to completely remove menu items upon startup of my eclipse plugin application. What I want to do is be able to add these menu items later depending on business logic based off of the user's actions. Is there a way to do this? I've looked at using contributions, but I feel like it's not going to be exactly what I want.
如果它可以做我所做的需要做,我该如何去使用它们?感谢您的任何帮助。
If it can do what I need it to do, how do I go about using them? Thanks in advance for any assistance.
推荐答案
您可以从MenuManager获取菜单,然后修改贡献。此代码段显示了如何访问菜单管理器并删除命名项目。
You can obtain the Menu from the MenuManager and then modify the contributions. This snippet shows how to access the menu manager and remove a named item.
您需要跟踪已删除的项目和项目索引来恢复它们。唯一的问题是indexOf方法是不可见的。将此片段添加到与MenuManager相同的包中的类型,并将其添加到片段是一种方法。
You'll need to keep track of the removed items and item indices to restore them. The only trouble is that the indexOf method is not visible. Adding this snippet to a type in the same package as MenuManager and adding it to a fragment is one way round that.
IWorkbenchWindow window = Workbench.getInstance().getActiveWorkbenchWindow()
if(window instanceof WorkbenchWindow) {
MenuManager menuManager = ((WorkbenchWindow)window).getMenuManager();
//TODO you may need to remove items from the coolbar as well
ICoolBarManager coolBarManager = null;
if(((WorkbenchWindow) window).getCoolBarVisible()) {
coolBarManager = ((WorkbenchWindow)window).getCoolBarManager2();
}
Menu menu = menuManager.getMenu();
//you'll need to find the id for the item
String itemId = "menuId";
IContributionItem item = menuManager.find(itemId);
// remember position, TODO this is protected
int controlIdx = menu.indexOf(mySaveAction.getId());
if (item != null) {
// clean old one
menuManager.remove(item);
// refresh menu gui
menuManager.update();
}
}
这篇关于从Eclipse插件以编程方式更改菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!