如何在Eclipse (e4) RCP中实现IWindowCloseHandler ?

时间:2022-07-14 19:00:19

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

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

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

伊斯梅尔

2 个解决方案

#1


5  

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

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

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:

如果您想在生命周期类中设置这个,那么需要做一些工作,因为在应用程序启动时,生命周期方法被调用得太早,因此可以直接在上下文中设置值。需要等待app启动完成事件:

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


1  

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

使用依赖注入和注释对@greg-449的答案进行更改。将这个类注册为应用程序中的addon。

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

#1


5  

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

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

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:

如果您想在生命周期类中设置这个,那么需要做一些工作,因为在应用程序启动时,生命周期方法被调用得太早,因此可以直接在上下文中设置值。需要等待app启动完成事件:

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


1  

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

使用依赖注入和注释对@greg-449的答案进行更改。将这个类注册为应用程序中的addon。

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