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

时间:2014-09-23 03:39:47

标签: eclipse plugins toolbar

我已按照说明动态向Eclipse工具栏中的下拉按钮添加了一个下拉列表:Programmatically add options to pulldown button in eclipse

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

1 个答案:

答案 0 :(得分:0)

我想我找到了答案,我使用的方法,对于大自然,下拉列表无法更新。要添加可刷新的下拉列表,应使用动态元素:Static and dynamic pulldown menus