简体   繁体   English

Eclipse E4-菜单贡献和PersistedState

[英]Eclipse E4 - Menu contribution and PersistedState

I have got ditched in a problem with Menu Contribution and PersistedState. 我陷入了菜单贡献和PersistedState的问题。 I had no problem before removing the -clearPersistedState flag from the VM args. 从VM args删除-clearPersistedState标志之前,我没有任何问题。

Now, the app has a weird behaviour, the menu contribution starts to pile up a menu entry every time the code is executed. 现在,该应用程序具有怪异的行为,每次执行代码时,菜单项就会开始堆积菜单项。

Here it's the guilty snippet enclosed in a Processor: 这是包含在处理器中的内代码段:

MDirectMenuItem menuItem = MMenuFactory.INSTANCE.createDirectMenuItem();
    menuItem.setLabel("Another Exit");
    menuItem.setContributionURI("bundleclass://"
            + "com.telespazio.optsat.wizard/"
            + ExitHandlerWithCheck.class.getName());
    if (!menu.getChildren().contains(menuItem))
        menu.getChildren().add(menuItem);

The menu items you add to the application model will be persisted, so you need to check if they already exist in the menu. 您添加到应用程序模型中的菜单项将保留下来,因此您需要检查菜单中是否已经存在。 The contains check you currently have does not do this. 您当前拥有的contains支票不执行此操作。

You need to check for a match of the label (or the contribution URI, or the id), something like: 您需要检查标签(或贡献URI或ID)是否匹配,例如:

List<MMenuElement> children = menu.getChildren();

boolean gotExisting = false;

for (MMenuElement child : children)
 {
   if ("Another Exit".equals(child.getLabel())
    {
      gotExisting = true;
      break;
    }
 }

if (!gotExisting)
 {
   ... add to menu
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM