如何在关闭应用程序之前实现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;
}
}
}
伊斯梅尔
最佳答案
IWindowCloseHandler
必须在要控制的IEclipseContext
的Eclipse上下文(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);
}
}
}