@Override
  public void runWhenProjectIsInitialized(@NotNull final Runnable action) {
    final Application application = ApplicationManager.getApplication();
    if (application == null) return;

    //noinspection SynchronizeOnThis
    synchronized (this) {
      // in tests which simulate project opening, post-startup activities could have been run
      // already.
      // Then we should act as if the project was initialized
      boolean initialized =
          myProject.isInitialized()
              || application.isUnitTestMode() && myPostStartupActivitiesPassed;
      if (!initialized) {
        registerPostStartupActivity(action);
        return;
      }
    }

    Runnable runnable =
        new Runnable() {
          @Override
          public void run() {
            if (!myProject.isDisposed()) {
              action.run();
            }
          }
        };
    if (application.isDispatchThread() && ModalityState.current() == ModalityState.NON_MODAL) {
      runnable.run();
    } else {
      application.invokeLater(runnable, ModalityState.NON_MODAL);
    }
  }
  private static void runActivity(Runnable runnable) {
    ProgressManager.checkCanceled();

    try {
      runnable.run();
    } catch (ProcessCanceledException e) {
      throw e;
    } catch (Throwable ex) {
      LOG.error(ex);
    }
  }
 public void runWithoutProgress() throws IncorrectOperationException {
   final Runnable runnable = preprocessFile(myFile, myProcessChangedTextOnly);
   runnable.run();
 }
  private void runLayoutCodeProcess(
      final Runnable readAction, final Runnable writeAction, final boolean globalAction) {
    final ProgressWindow progressWindow = new ProgressWindow(true, myProject);
    progressWindow.setTitle(myCommandName);
    progressWindow.setText(myProgressText);

    final ModalityState modalityState = ModalityState.current();

    final Runnable process =
        new Runnable() {
          @Override
          public void run() {
            ApplicationManager.getApplication().runReadAction(readAction);
          }
        };

    Runnable runnable =
        new Runnable() {
          @Override
          public void run() {
            try {
              ProgressManager.getInstance().runProcess(process, progressWindow);
            } catch (ProcessCanceledException e) {
              return;
            } catch (IndexNotReadyException e) {
              return;
            }

            final Runnable writeRunnable =
                new Runnable() {
                  @Override
                  public void run() {
                    CommandProcessor.getInstance()
                        .executeCommand(
                            myProject,
                            new Runnable() {
                              @Override
                              public void run() {
                                if (globalAction)
                                  CommandProcessor.getInstance()
                                      .markCurrentCommandAsGlobal(myProject);
                                try {
                                  writeAction.run();

                                  if (myPostRunnable != null) {
                                    ApplicationManager.getApplication().invokeLater(myPostRunnable);
                                  }
                                } catch (IndexNotReadyException ignored) {
                                }
                              }
                            },
                            myCommandName,
                            null);
                  }
                };

            if (ApplicationManager.getApplication().isUnitTestMode()) {
              writeRunnable.run();
            } else {
              ApplicationManager.getApplication()
                  .invokeLater(writeRunnable, modalityState, myProject.getDisposed());
            }
          }
        };

    if (ApplicationManager.getApplication().isUnitTestMode()) {
      runnable.run();
    } else {
      ApplicationManager.getApplication().executeOnPooledThread(runnable);
    }
  }