2014-03-04 111 views
2

如何在执行关闭应用程序之前执行IWindowCloseHandler以显示MessageDialog如何在Eclipse(e4)RCP中实现IWindowCloseHandler?

这里是我的代码:

编辑

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

}

伊斯梅尔

+0

如果这是纯粹的e4应用程序,则不能使用PlatformUI。 –

+0

好@格雷格-449,我现在可以更好地理解e4 RCP应用程序。所以,在我最近的编辑中,我使用'EModelService'和'MApplication'来查找我的应用程序的主窗口,并在'processAddictions()'中获得了我的窗口。 LifeCycle类位于'plugin.xml'中。现在,我只需要将窗口传递给'close'方法,但是我不能,我错过了什么去做最后一步? –

+0

正如我已经说过的那样,将'IWindowCloseHandler'放在窗口上下文中,Eclipse将在需要时调用'close'方法。您必须等到应用程序启动事件触发才能将条目放入上下文中。我已更新答案以匹配您的代码 –

回答

5

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

我想控制主窗口。所以为了得到它,我做了:'MWindow window =(MWindow)PlatformUI.getWorkbench()。getActiveWorkbenchWindow()。getShell();'。现在,'LifeCycle'类是一个普通的类(不再在'plugin.xml'中。现在,我不能'getContext'!。请参阅我的编辑。 –

+1

您的LifeCycle'仍然必须在plugin.xml作为生命周期类,你不能使用PlatformUI,你必须使用'EModelService'找到窗口。 –

+0

我不能使用IEventBroker,并且没有建议导入,对于EventHandler也是一样的。 –

1

使用依赖注入和注释@格雷格-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; 
    } 
} 
+0

试过这个,但'window.getContext()。set(...)'因为getContext()返回null而崩溃。可能是什么问题?我正在使用Eclipse Kepler版本4.3.1。 – skowski

+0

@skowski你确定你的window.id存在。 – kdoteu

相关问题