package com.intellij.execution.testframework.sm;

import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.ActionCallback;

import javax.swing.*;
import java.awt.event.ActionEvent;

/**
 * @author Roman Chernyatchik
 */
public class SMRunnerUtil {
  private static final Logger LOG = Logger.getInstance(SMRunnerUtil.class.getName());

  private SMRunnerUtil() {
  }

  /**
   * Adds runnable to Event Dispatch Queue
   * if we aren't in UnitTest of Headless environment mode
   * @param runnable Runnable
   */
  public static void addToInvokeLater(final Runnable runnable) {
    final Application application = ApplicationManager.getApplication();
    if (application.isHeadlessEnvironment() || application.isUnitTestMode()) {
      runnable.run();
    } else {
      SwingUtilities.invokeLater(runnable);
    }
  }

  public static void registerAsAction(final KeyStroke keyStroke,
                                      final String actionKey,
                                      final Runnable action, final JComponent component) {
    final InputMap inputMap = component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    inputMap.put(keyStroke, actionKey);
    component.getActionMap().put(inputMap.get(keyStroke), new AbstractAction() {
      public void actionPerformed(final ActionEvent e) {
        action.run();
      }
    });
  }

  public static ActionCallback runInEventDispatchThread(final Runnable runnable, final ModalityState state) {
    try {
      if (SwingUtilities.isEventDispatchThread()) {
        runnable.run();
      }
      else {
        final ActionCallback cb = new ActionCallback();
        ApplicationManager.getApplication().invokeAndWait(new Runnable() {
          public void run() {
            runnable.run();
            cb.setDone();
          }
        }, state);
        return cb;
      }
    }
    catch (Exception e) {
      LOG.warn(e);
    }
    return new ActionCallback.Done();
  }

}

@@ -261,57 +261,8 @@
 ger;
-%0Aimport com.intellij.openapi.util.ActionCallback;
 %0A%0Aim
@@ -1509,30 +1509,20 @@
  static 
-ActionCallback
+void
  runInEv
@@ -1704,64 +1704,8 @@
 e %7B%0A
-        final ActionCallback cb = new ActionCallback();%0A
     
@@ -1837,34 +1837,8 @@
 ();%0A
-            cb.setDone();%0A
     
@@ -1868,27 +1868,8 @@
 e);%0A
-        return cb;%0A
     
@@ -1927,52 +1927,14 @@
 e);%0A
+
     %7D%0A
-    return new ActionCallback.Done();%0A
   %7D%0A

remove unnecessary ActionCallback in call which is synchronous anyway