【问题标题】:How to implement IWindowCloseHandler in Eclipse (e4) RCP?如何在 Eclipse (e4) RCP 中实现 IWindowCloseHandler?
【发布时间】:2014-04-06 01:23:57
【问题描述】:

如何实现 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;
    } 
 }

}

伊斯梅尔

【问题讨论】:

  • 如果这是一个纯e4应用你不能使用PlatformUI。
  • 好的 @greg-449,我现在可以更好地理解 e4 RCP 应用程序了。所以,在我最新的编辑中,我使用EModelServiceMApplication 来查找我的应用程序的主窗口,我的窗口位于processAddictions()。 LifeCycle 类位于plugin.xml 中。现在,我只需要将窗口传递给close 方法,但我不能,最后一步我缺少什么?
  • 正如我已经说过的,您将IWindowCloseHandler 放在窗口上下文中,Eclipse 将在需要时调用close 方法。您必须等到应用启动事件触发后才能将条目放入上下文中。我已更新答案以匹配您的代码
  • 我看不到你的更新(抱歉),我猜我在编辑的同时。我也添加了新的修改。 . .我在这里得到NullPointerExceptionwindow.getContext().set(IWindowCloseHandler.class, closeHandler);。我的新编辑是否正确?为什么我收到 NullPointerException ? .再次抱歉没有看到您的修改
  • 调用@ProcessAdditions 时上下文尚未设置,您必须在应用启动完成事件中执行此操作,如我所示。

标签: java eclipse rcp e4 messagedialog


【解决方案1】:

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

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

【讨论】:

  • 我想控制主窗口。所以为了得到它,我做到了:MWindow window= (MWindow) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();。现在,LifeCycle 类是普通类(plugin.xml 中不再存在。现在,我不能 getContext !请看我的编辑。
  • LifeCycle 仍然必须在 plugin.xml 中声明为生命周期类。不能使用PlatformUI,必须使用EModelService找到窗口。
  • 我不能使用IEventBroker 并且不建议导入,EventHandler 也是如此
  • IEventBrokerorg.eclipse.e4.core.services 插件中
  • EventHandlerorg.eclipse.osgi.services
【解决方案2】:

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

【讨论】:

  • 试过了,但window.getContext().set(...) 崩溃了,因为getContext() 返回了null。任何想法可能是什么问题?我正在使用 Eclipse Kepler 版本 4.3.1。
  • @skowski 你确定你的 window.id 存在吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
  • 2013-04-25
  • 2016-10-15
  • 1970-01-01
相关资源
最近更新 更多