如何在Eclipse(e4)RCP中实现IWindowCloseHandler? [英] How to implement IWindowCloseHandler in Eclipse (e4) RCP?

查看:996
本文介绍了如何在Eclipse(e4)RCP中实现IWindowCloseHandler?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现 IWindowCloseHandler ,以便在关闭应用程序之前显示 MessageDialog

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

这是我的代码:

编辑

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

推荐答案

IWindowCloseHandler 必须注册您想要控制的 MWindow 的Eclipse上下文( IEclipseContext )。

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);

如果要在 LifeCycle 类有一些工作要做,因为生命周期方法在应用程序启动时被称为太早,才能够直接在上下文中设置值。有必要等待应用程序启动完成事件:

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);        
    }
  }
}

这篇关于如何在Eclipse(e4)RCP中实现IWindowCloseHandler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆