如何更新Eclipse工具栏中下拉按钮的下拉列表



我按照以下说明,在Eclipse工具栏的下拉按钮中动态添加了一个下拉列表:在Eclipse 中以编程方式向下拉按钮添加选项

现在,我需要更新下拉列表。我的想法是删除旧列表,然后在下拉按钮中添加一个新列表。我尝试了IMenuService的removeContributionFactory(AbstractContributionFactory工厂)和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");
..

我想我已经找到了答案,我使用的方法,对于自然,下拉列表无法更新。要添加可刷新的下拉列表,应使用动态元素:静态和动态下拉菜单

最新更新