0

我正在做一个 eclipse-rcp 项目。我想实现一个 EventListener (或类似的东西),当用户按下窗口右上角的 x 时,它会被调用。知道在哪里/如何实现这个吗?

谢谢大家!

4

1 回答 1

2

有不同的方法可以做到这一点,具体取决于您的需要。如果您想在某些情况下禁止关闭主 shell,您可能需要preWindowShellClose()WorkbenchWindowAdvisor. 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
}

  }
}

希望这可以帮助。

于 2013-05-06T17:32:20.870 回答