How to implement IWindowCloseHandler in Eclipse (e4) RCP?

喜夏-厌秋 提交于 2019-12-11 01:56:35

问题


How can I implement IWindowCloseHandler in order to display a MessageDialog before closing the application ?

Here is my code :

EDIT

public class LifeCycle {    

 @PostContextCreate
 public void postContextCreate()
  {
    // TODO start up code here

     System.out.println("open");

  }

 @ProcessAdditions
  void processAdditions(MApplication app, EModelService modelService)
  {
     WindowCloseHandler closeHandler=new WindowCloseHandler();
    MWindow window = (MWindow)modelService.find("uploadcenter.source.trimmedwindow.0", app);
    window.getContext().set(IWindowCloseHandler.class, closeHandler);
  }
 private static class WindowCloseHandler implements IWindowCloseHandler{

    @Override
    public boolean close(MWindow window) {
        // TODO Auto-generated method stub
        Shell shell = new Shell();

        if (MessageDialog.openConfirm(shell, "Confirmation",
                "Do you want to exit?")) {
            return true;
        }
        return false;
    } 
 }

}

Ismail


回答1:


The IWindowCloseHandler must be registered in the Eclipse context (IEclipseContext) for the MWindow which you want to control.

MWindow window = get the window

window.getContext().set(IWindowCloseHandler.class, handler);

If you want to set this up in the LifeCycle class there is a bit of work to do because the life cycle methods are called too early in the application start up to be able to set the value in the context directly. It is necessary to wait for the app startup complete event:

public class LifeCycle
{
  @ProcessAdditions
  public void processAdditions(IEventBroker eventBroker, MApplication app, EModelService modelService)
  {
     MWindow window =(MWindow)modelService.find("uploadcenter.source.trimmedwindow.0", app);

     eventBroker.subscribe(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE, 
                          new AppStartupCompleteEventHandler(window));
  }


  private static class AppStartupCompleteEventHandler implements EventHandler
  {
    private MWindow theWindow;

    AppStartupCompleteEventHandler(MWindow window)
    {
       theWindow = window;
    }


    @Override
    public void handleEvent(final Event event)
    {
      theWindow.getContext().set(IWindowCloseHandler.class, handler);        
    }
  }
}



回答2:


A variation on @greg-449's answer using dependency injection and annotation. Register this class as an addon in your Application.e4xmi.

public class ExampleWindowCloseAddon implements IWindowCloseHandler
{
    @Inject
    @Optional
    public void startupComplete(@UIEventTopic(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE) MApplication application,
            EModelService modelService)
    {
        MWindow window = (MWindow) modelService.find("my.window.id", application);
        window.getContext().set(IWindowCloseHandler.class, this);
    }

    @Override
    public boolean close(MWindow window)
    {
        // Your code goes here
        return true;
    }
}


来源:https://stackoverflow.com/questions/22173143/how-to-implement-iwindowclosehandler-in-eclipse-e4-rcp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!