简体   繁体   中英

Eclipse PDE: Programmatically detect opened dialog and close it

On Eclipse Luna, I select a server and click the Start button on Servers view, then the server (for example Tomcat8) will get started. If something is wrong during the start-up process, a dialog will be populated to display the error messages (for example time-out). The dialog is modeless in this test case.

Now I need to start the server programmatically from a plugin. In case that errors occur, how could I programmatically detect that a dialog has been opened and how to close it?

You could use the Display.addFilter method to listen for all SWT.Activate events which will tell you about all Shells (and other things) being activated. You can then detect the shells you want to close.

Something like:

Display.getDefault().addFilter(SWT.Activate, new Listener()
  {
    @Override
    public void handleEvent(final Event event)
    {
      // Is this a Shell being activated?

      if (event.widget instanceof Shell)
       {
         final Shell shell = (Shell)event.widget;

         // Look at the shell title to see if it is the one we want

         if ("About".equals(shell.getText()))
          {
            // Close the shell after it has finished initializing

            Display.getDefault().asyncExec(new Runnable()
             {
               @Override
               public void run()
               {
                 shell.close();
               }
             });
          }
       }
    }
  });

which closes a dialog called 'About'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM