0

I have a Eclipse E4 application with multiple connections to different servers. Now when opening a MPart the part will call the server to receive his data. Also when the MPart is closed the client send the close to the server so that the client knows that the client does not need to have the updated data.

Now I need to find a way to let the MParts know that the client is shutting down. So that they do not send any messages to the server. This will speedup the shutdown of the client.

How can I send the shutdown command to the Mpart when the user clicked the close button?

JimmyD
  • 2,629
  • 4
  • 29
  • 58

1 Answers1

1

Use the event broker to send a message to the parts.

In the shutdown code send an event:

@Inject
IEventBroker eventBroker;

eventBroker.send("my/topic/shutdown", data);

Where data is any data you want to associate with the shudown event.

"my/topic/shutdown" is just a unique id for the event.

Use the send method to send the event synchronously, use post to send asynchronously.

Each part can subscribe to the event with:

@Inject
@Optional
public void shutdown(@EventTopic("my/topic/shutdown") Event event)
{
  ....
}

Event is org.osgi.service.event.Event

You can also use @UIEventTopic if you want the method to be guaranteed to run in the UI thread.

To handle a click on the application 'close' button you need to put an implementation of org.eclipse.e4.ui.workbench.modeling.IWindowCloseHandler in to the Eclipse Context of the the main window. You can do this in your life cycle class (if you have one). The application startup complete event is suitable for this:

@Optional
@Inject
public void appStartupComplete(@UIEventTopic(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE) Event event, MApplication app, EModelService modelService)
{
  MWindow window = (MWindow)modelService.find("window id", app);

  IEclipseContext windowContext = window.getContext();

  windowContext.set(IWindowCloseHandler.class, ContextInjectionFactory.make(AppCloseHandler.class, windowContext));
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • How can I hook into the close butun to send the event? – JimmyD Oct 13 '17 at 07:43
  • What close button? – greg-449 Oct 13 '17 at 07:46
  • The red cross on the right side of the screen (in WIndows). – JimmyD Oct 13 '17 at 07:48
  • Use an IWindowCloseHandler - added to answer – greg-449 Oct 13 '17 at 07:54
  • I added workbenchContext.set(IWindowCloseHandler.class, new EclipseCloseHandler()); to my postContextCreate in my application lifecycle. The handler willl not be called when pressing the close button. – JimmyD Oct 13 '17 at 08:30
  • It appears that despite the documentation you have to put this in the main window context rather than the application context. – greg-449 Oct 13 '17 at 08:38
  • The @UIEventTopic(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE) does not trigger. So the method will not be called – JimmyD Oct 13 '17 at 09:14
  • This is from code that I actually use, it definitely works when in the life cycle class. Make sure `Event` is `org.osgi.service.event.Event` there are lots of Event classes. – greg-449 Oct 13 '17 at 09:16