3

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

这是我的代码:

编辑

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

}

伊斯梅尔

4

2 回答 2

6

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

MWindow window = get the window

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

如果要在LifeCycle类中进行设置,则需要做一些工作,因为生命周期方法在应用程序启动时调用得太早,无法直接在上下文中设置值。需要等待应用启动完成事件:

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);        
    }
  }
}
于 2014-03-04T14:01:28.760 回答
1

@greg-449 使用依赖注入和注释的答案的变体。将此类注册为 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;
    }
}
于 2014-07-22T11:23:04.040 回答