title: Eclipse RCP E4: IStructuredSelection behaviour when the selected element is removed
body: <p>Eclipse RCP E4 application.</p>
  <p>The Send Proposal button <a href="https://i.stack.imgur.com/yDUsB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yDUsB.png" alt="Send Proposal Button" /></a> is enabled when the user selects any ready-to-send package <a href="https://i.stack.imgur.com/sMoOU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sMoOU.png" alt="Package ready" /></a>. For other options <a href="https://i.stack.imgur.com/L7DFE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/L7DFE.png" alt="Package" /></a> -<a href="https://i.stack.imgur.com/jRs1k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jRs1k.png" alt="GreenDot" /></a> - <a href="https://i.stack.imgur.com/mazqN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mazqN.png" alt="RedDot" /></a>, Send Proposal Button is disabled.</p>
  <p><a href="https://i.stack.imgur.com/OBisx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OBisx.png" alt="App Scenarios" /></a></p>
  <p>This is achieved with the following code (based on <a href="https://stackoverflow.com/questions/69159438/evaluation-of-canexecute-for-rcp-e4-application">question</a>):</p>
  <p>Navigator Part (View):</p>
  <pre class="lang-java prettyprint-override"><code>    Tree t = (Tree) viewer.getControl();
      t.addSelectionListener(new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
              IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
              selectionService.setSelection(viewer.getStructuredSelection());
              if (selection.getFirstElement() instanceof Package) {
                  broker.post(UIEvents.REQUEST_ENABLEMENT_UPDATE_TOPIC, UIEvents.ALL_ELEMENT_ID);
              }
          }
      });
  
      //To update viewer after the Send Proposal Button is pressed
      @Inject
      @Optional
      public void sentProposal(@UIEventTopic(MundiaurumEventConstants.PROPOSAL_SENT) Package env) {
          viewer.refresh();
      }
  </code></pre>
  <p>ModelServiceImpl:</p>
  <pre class="lang-java prettyprint-override"><code>@Override
  public boolean sendProposal(Package pack) {
      //model = list of packages
  
      model.remove(pack);
  
      //EventBroker to update Viewer after the Send Proposal Button is pressed
      broker.post(MundiaurumEventConstants.PROPOSAL_SENT, pack);
  
      //...
  }
  </code></pre>
  <p>The handler to execute the shipment:</p>
  <pre class="lang-java prettyprint-override"><code>public class SendProposalHandler {
      @Execute
      public void execute(Shell shell, IEnviosService model,
          @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, EModelService modelService,
          MApplication application, ESelectionService selectionService) {
  
          Package pack = (Package) selection.getFirstElement();
          model.sendProposal(pack);
  
          System.out.println(&quot;Selection service: &quot; + selection.getFirstElement().getClass().getName());
          System.out.println(&quot;Package ID: &quot; + ((Package) selection.getFirstElement()).getEnvioId());
          //This is to try to set the selection on the Navigator Part
          MPart navigatorPart = (MPart) modelService.find(&quot;com.mundiaurum.part.navigator&quot;, application);
          selectionService.setSelection(navigatorPart);
      }
  
      @CanExecute
      public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
          @Named(IServiceConstants.ACTIVE_PART) MPart part) {
          if (selection == null)
              return false;
          if (part == null)
              return false;
          if (selection.getFirstElement() instanceof Package) {
              Package pack = (Package) selection.getFirstElement();
              return pack.isReadyToSendProposal();
          }
          return false;
      }
  
  }
  </code></pre>
  <p>The button is enabled/disabled smooth when the user changes the selection by clicking on any part of the application.</p>
  <p>When the user click on Send Proposal button, the package is removed from the Navigator View, also the package is removed from the model,  but the button is still enabled.
  (next picture shows scenario after Send Prosal Button is pressed on Package 12).</p>
  <p><a href="https://i.stack.imgur.com/UKrEv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UKrEv.png" alt="enter image description here" /></a></p>
  <p>The issue is that the StructuredSelection is still keeping a reference of the package previous sent (and removed from model), and hence when @canExecute is evaluated the method returns true.</p>
  <p>I tried to set the StrucuturedSelction to the Navigator Part, to null, etc...</p>
  <p>But since the button is enabled if click on it immediately the following error is thrown:</p>
  <pre><code>org.eclipse.core.commands.ExecutionException: com.myapp.handlers.SendProposalHandler handler is missing @Execute
      at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:160)
      at org.eclipse.core.commands.Command.executeWithChecks(Command.java:488)
      at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:487)
      at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:213)
      at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.executeItem(HandledContributionItem.java:438)
      at org.eclipse.e4.ui.workbench.renderers.swt.AbstractContributionItem.handleWidgetSelection(AbstractContributionItem.java:449)
      at org.eclipse.e4.ui.workbench.renderers.swt.AbstractContributionItem.lambda$2(AbstractContributionItem.java:475)
      at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
      at org.eclipse.swt.widgets.Display.sendEvent(Display.java:5879)
      at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1427)
      at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:5121)
      at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:4599)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1157)
      at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
      at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1046)
      at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:155)
      at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:168)
      at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203)
      at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
      at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
      at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:401)
      at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:566)
      at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:654)
      at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)
      at org.eclipse.equinox.launcher.Main.run(Main.java:1462)
      at org.eclipse.equinox.launcher.Main.main(Main.java:1435)
  Caused by: org.eclipse.core.commands.NotHandledException: org.eclipse.e4.core.commands.internal.HandlerServiceHandler
      at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:161)
      ... 29 more
  </code></pre>
  <p>How can I modify the StructuredSelection in order to disable the button when the selected element is deleted from the model?</p>