public static void invokeLaterIfNeeded(@NotNull ModalityState modalityState,
                                         @NotNull Runnable runnable) {
    Application app = ApplicationManager.getApplication();
    if (app.isDispatchThread()) {
      runnable.run();
    }
    else {
      app.invokeLater(runnable, modalityState);
    }
  }
public static void invokeLaterIfNeeded(@NotNull ModalityState modalityState,
                                         @NotNull Runnable runnable)