-->

Add System Tray and Active Workbech Shell referenc

2019-08-14 02:39发布

问题:

I am new in E4 application development. I add System tray icon in RCP 3.7.x successfully. to add a system tray icon in e4 application. I am using the e4 application life cycle to add a system tray icon in this way:

public class LifeCycleManager {
    @PostContextCreate
    void postContextCreate(IApplicationContext appContext, Display display) {
    SystemNotifier icon= new SystemNotifier(shell);
    SystemNotifier.trayItem = icon.initTaskItem(shell);
    if (SystemNotifier.trayItem != null) {      
        icon.hookPopupMenu();
    }
  }  

}

How to get reference of Active Workbench Shell in e4 application. Which annotation use of e4 application life cycle to add System Tray

回答1:

The application shell is not available when @PostContextCreate runs. You need to wait for the application startup complete event, something like:

@PostContextCreate
void postContextCreate(IEclipseContext context, IEventBroker eventBroker)
{
  eventBroker.subscribe(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE, new AppStartupCompleteEventHandler(eventBroker, context));
}


private static final class AppStartupCompleteEventHandler implements EventHandler
{
  private final IEventBroker _eventBroker;
  private final IEclipseContext _context;


  AppStartupCompleteEventHandler(IEventBroker eventBroker, IEclipseContext context)
  {
    _eventBroker = eventBroker;
    _context = context;
  }


  @Override
  public void handleEvent(final Event event)
  {
    _eventBroker.unsubscribe(this);

    Shell shell = (Shell)_context.get(IServiceConstants.ACTIVE_SHELL);

    ... your code ...
  }

}