Eclipse E4 - 菜单贡献和PersistedState

时间:2014-12-18 15:25:09

标签: java eclipse rcp e4

我在菜单贡献和持久状态方面遇到了问题。从VM args中删除-clearPersistedState标志之前没有问题。

现在,应用程序有一种奇怪的行为,每次执行代码时,菜单贡献都会开始堆积菜单条目。

这是处理器中包含的有罪代码段:

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);

1 个答案:

答案 0 :(得分:1)

您添加到应用程序模型的菜单项将被保留,因此您需要检查它们是否已存在于菜单中。您目前拥有的contains支票不会执行此操作。

您需要检查标签(或贡献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
 }
相关问题