我正在做一个eclipse-rcp项目。我想实现一个EventListener (或者类似的东西),当用户按下窗口右上角的x时,它就会被调用。你知道我可以在哪里/如何实现它吗?
感谢所有人!
发布于 2013-05-07 01:32:21
有不同的方法可以做到这一点,这取决于您需要什么。如果您希望在某些情况下禁止关闭主shell,则可能需要在WorkbenchWindowAdvisor
中使用preWindowShellClose()
方法。http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fui%2Fapplication%2FWorkbenchWindowAdvisor.html。
如果你只想在主窗口关闭时执行一些操作,你可以像这样添加一个shutdownHook (另请参阅这个线程:What is the correct way to add a Shutdown Hook for an Eclipse RCP application?):
public class IPEApplication implements IApplication {
public Object start(IApplicationContext context) throws Exception {
final Display display = PlatformUI.createDisplay();
Runtime.getRuntime().addShutdownHook(new ShutdownHook()); }
// start workbench...
}
}
private class ShutdownHook extends Thread {
@Override
public void run() {
try {
final IWorkbench workbench = PlatformUI.getWorkbench();
final Display display = PlatformUI.getWorkbench()
.getDisplay();
if (workbench != null && !workbench.isClosing()) {
display.syncExec(new Runnable() {
public void run() {
IWorkbenchWindow [] workbenchWindows =
workbench.getWorkbenchWindows();
for(int i = 0;i < workbenchWindows.length;i++) {
IWorkbenchWindow workbenchWindow =
workbenchWindows[i];
if (workbenchWindow == null) {
// SIGTERM shutdown code must access
// workbench using UI thread!!
} else {
IWorkbenchPage[] pages = workbenchWindow
.getPages();
for (int j = 0; j < pages.length; j++) {
IEditorPart[] dirtyEditors = pages[j]
.getDirtyEditors();
for (int k = 0; k < dirtyEditors.length; k++) {
dirtyEditors[k]
.doSave(new NullProgressMonitor());
}
}
}
}
}
});
display.syncExec(new Runnable() {
public void run() {
workbench.close();
}
});
}
} catch (IllegalStateException e) {
// ignore
}
}
}
希望这能有所帮助。
https://stackoverflow.com/questions/16401691
复制相似问题