2014-09-23 83 views
0

我已经将一个下拉列表动态添加到Eclipse工具栏中的下拉按钮中,方法如下:Programmatically add options to pulldown button in eclipse如何在Eclipse工具栏中更新下拉按钮的下拉列表?

现在,我需要更新下拉列表。我的想法是删除旧列表,然后向下拉按钮添加一个新列表。我尝试了以下方法:removeContributionFactory(AbstractContributionFactory factory)和IMenuService的dispose(),但它们都不起作用。任何人都可以给我一些如何实现目标的提示吗?

这里是我使用的代码:

(1)在类A,I调用的方法,以向下列表中添加一滴下拉按钮(命令)

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"); 
.. 
+0

任何人都知道如何更新它? – chygo 2014-09-23 13:31:33

+0

把你试过的代码与 – 2014-09-24 03:44:09

+0

嗨,我已经添加了我用过的代码 – chygo 2014-09-24 04:51:57

回答

0

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