0

I have got ditched in a problem with Menu Contribution and PersistedState. I had no problem before removing the -clearPersistedState flag from the VM args.

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);
greg-449
  • 109,219
  • 232
  • 102
  • 145
unclejohn00
  • 149
  • 10

1 Answers1

1

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.

You need to check for a match of the label (or the contribution URI, or the id), something like:

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
 }
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Thanks, just for my own curiosity, does the approach of not clearing the Persistent State create other weird behaviours? What I meant to say is what should I expect after the flag removal? – unclejohn00 Dec 18 '14 at 18:11
  • There were problems with fragments but these may have been resolved by now. – greg-449 Dec 18 '14 at 19:02
  • do you mean there were some bugs? Basically I think that the -clearPersistedState flag has been automatically added into my configuration by Eclipse itself, it's because I was wondering that... – unclejohn00 Dec 18 '14 at 21:17
  • When you are testing a new app you need -clearPersistedState so that the design changes you make to the Application.e4xmi are picked up – greg-449 Dec 19 '14 at 08:08
  • So are you suggesting to me to set back that flag? Otherwise if I amend the Application.e4xmi some changes won't take effect? – unclejohn00 Dec 19 '14 at 10:11