0

I am working on an eclipse-rcp project. I want to implement an EventListener (or something like that), which is called, when the user presses the x on the top right corner of the window. Any idea where/how I can implement this?

Thanks to all!

antumin
  • 167
  • 1
  • 12

1 Answers1

2

There are different ways to do so, depending on what you need. If you want to forbid closing of the main shell under some circumstances, you might want to use preWindowShellClose() method in your WorkbenchWindowAdvisor. http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fui%2Fapplication%2FWorkbenchWindowAdvisor.html .

If you just want to perform some action when main window is closed, you could add a shutdownHook like this (see also this thread: 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
}

  }
}

Hope this helps.

Community
  • 1
  • 1
Alex K.
  • 3,294
  • 4
  • 29
  • 41
  • Thank you for your answer. I think preWindowShellClose() should work for me. However I can't find the WorkbenchWindowAdvisor Object in my project. Any clue where I can find/create it? – antumin May 07 '13 at 14:48
  • 1
    Try to search for a class which extends WorkbenchWindowAdvisor. When you create your application, in the class which implements IPlatformRunnable, you pass an instance of WorkbenchAdvisor (some thing like that PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());) And in the WorkbenchAdvisor you should override createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) method which create instance of WorkbenchWindowAdwisor you are looking for. Please accept the main answer, if this have helped you. – Alex K. May 07 '13 at 19:20