【发布时间】:2014-09-23 03:39:47
【问题描述】:
我已按照说明在 Eclipse 工具栏中的下拉按钮中动态添加了一个下拉列表:Programmatically add options to pulldown button in eclipse
现在,我需要更新下拉列表。我的想法是删除旧列表,然后在下拉按钮中添加一个新列表。我尝试了IMenuService的removeContributionFactory(AbstractContributionFactory factory)和dispose()方法,但都不起作用。谁能给我一些关于如何实现目标的提示?
这是我使用的代码:
(1)在A类中,我调用方法给下拉按钮添加下拉列表(命令)
Class A {
public static ContextSwitchContributionFactory contextFactory =
new ContextSwitchContributionFactory("menu:"+"SwitchContext", null);
public static IMenuService menuService =
(IMenuService)PlatformUI.getWorkbench().getService(IMenuService.class);
...
method A () {
...
ContextSwitchContributionFactory.updateContextMenu(menuService, contextFactory, "SwitchContext");
...
}
(2)ContextSwitchContributionFactory的定义:
public class ContextSwitchContributionFactory extends
AbstractContributionFactory {
private ContextData contextData = new ContextData();
private ContextsCollector contextList;
}
static public void updateContextMenu (IMenuService service, final AbstractContributionFactory factory, final String menuId) {
service.dispose();
service.addContributionFactory(factory);
}
public ContextSwitchContributionFactory(String location, String namespace) {
super(location, namespace);
// this is to read the file and update the data for creating the drop down list
contextData.readContextsFile();
contextList = contextData.getContextsCollector();
}
@Override
public void createContributionItems(IServiceLocator serviceLocator,
IContributionRoot additions) {
Set<IContext> cxtset = contextList.getContextList();
Iterator<IContext> iterator = cxtset.iterator();
while (iterator.hasNext()) {
IContext context = iterator.next();
CommandContributionItemParameter menuitem = new CommandContributionItemParameter(
serviceLocator, null,
"coms.sample.command.context",
CommandContributionItem.STYLE_PUSH);
menuitem.label = context.getName();
menuitem.visibleEnabled = true;
if (context.isSelected()) {
ImageDescriptor image = MechanicPlugin.getImageDescriptor("icons/ticking_icon.png");
menuitem.icon = image;
}
additions.addContributionItem(new CommandContributionItem(menuitem), null);
}
}
}
(3)在C类的方法C中,我要更新下拉列表:
class C {
...
method C {
...
// A.menuService.dispose(); (doesn't work)
// remove the old one
A.menuService.removeContributionFactory(A.contextFactory);
// create a new one
A.contextFactory =
new ContextSwitchContributionFactory("menu:"+"SwitchContext", null);
// after executing this statement, the old drop down list is still there, and the new one is added after the old list.
A.menuService.addContributionFactory(A.contextFactory);
ContextSwitchContributionFactory.updateContextMenu(menuService, contextFactory, "SwitchContext");
..
【问题讨论】:
-
有人知道怎么更新吗?
-
输入你尝试过的代码
-
嗨,我已经添加了我使用的代码