Eclipse RCP关闭窗口事件

以前曾见过这么一段有关SWT窗口关闭事件的代码:

import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class GoodLuck { public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.addListener(SWT.Close, new Listener() { public void handleEvent(Event event) { int style = SWT.APPLICATION_MODAL | SWT.YES | SWT.NO; MessageBox messageBox = new MessageBox(shell, style); messageBox.setText("Information "); messageBox.setMessage("Close the shell? "); event.doit = messageBox.open() == SWT.YES; } }); shell.setSize(400, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }  

 

然而把这段代码移植到RCP程序中,首先取得RCP的shell

Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); ,

然后套用上面代码的shell监听事件,却不灵了,一直也不知道什么原因。

 

 

今天在网上偶然间看到一篇文章,才明白了RCP程序的窗口关闭事件不是生搬硬套上面的代码的:

在ApplicationWorkbenchWindowAdvisor类中,重写preWindowShellClose()函数,即

@Override public boolean preWindowShellClose() { // TODO Auto-generated method stub MessageBox msgBox = new MessageBox(new Shell(), SWT.YES | SWT.NO | SWT.ICON_QUESTION); msgBox.setText("退出系统"); msgBox.setMessage("确定退出系统?"); if (msgBox.open() == SWT.YES) { return true; } return false; } 

 

你可能感兴趣的:(Eclipse RCP关闭窗口事件)