Java Code Examples for com.intellij.openapi.application.Application#invokeAndWait()

The following examples show how to use com.intellij.openapi.application.Application#invokeAndWait() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdepsFileWriter.java    From intellij with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a jdeps proto for the given blaze target and writes it to the jdeps file specified
 * in the target's JavaIdeInfo.
 */
private void writeJdepsFile(String execRoot, TargetMap targetMap, TargetIdeInfo target) {
  if (target.getJavaIdeInfo() == null || target.getJavaIdeInfo().getJdepsFile() == null) {
    return;
  }
  Deps.Dependencies jdeps = getJdeps(targetMap, target);
  ArtifactLocation jdepsArtifact = target.getJavaIdeInfo().getJdepsFile();
  VirtualFile jdepsFile =
      fileSystem.createFile(execRoot + "/" + jdepsArtifact.getExecutionRootRelativePath());
  Application application = ApplicationManager.getApplication();
  application.invokeAndWait(
      () ->
          application.runWriteAction(
              () -> {
                try (OutputStream jdepsOutputStream = jdepsFile.getOutputStream(this)) {
                  jdeps.writeTo(jdepsOutputStream);
                } catch (IOException e) {
                  fail(e.getMessage());
                }
              }));
}
 
Example 2
Source File: IdeaHelper.java    From azure-devops-intellij with MIT License 6 votes vote down vote up
public static void runOnUIThread(final Runnable runnable, final boolean wait, final ModalityState modalityState) {
    Application application = ApplicationManager.getApplication();
    if (application != null && !application.isDispatchThread() && !application.isUnitTestMode()) {
        if (wait) {
            application.invokeAndWait(runnable, modalityState);
            // TODO: use this instead after deprecating IDEA 15: TransactionGuard.getInstance().submitTransactionAndWait(runnable);
        } else {
            application.invokeLater(runnable, modalityState);
            // TODO: use this instead after deprecating IDEA 15: TransactionGuard.getInstance().submitTransaction(runnable);
        }
    } else {
        // If we are already on the dispatch thread then we can run it here
        // If we don't have an application or we are testing, just run the runnable here
        runnable.run();
    }
}
 
Example 3
Source File: PantsSystemProjectResolver.java    From intellij-pants-plugin with Apache License 2.0 6 votes vote down vote up
private static void clearPantsModules(@NotNull Project project, String projectPath, DataNode<ProjectData> projectDataNode) {
  Runnable clearModules = () -> {
    Set<String> importedModules = projectDataNode.getChildren().stream()
      .map(node -> node.getData(ProjectKeys.MODULE))
      .filter(Objects::nonNull)
      .map(ModuleData::getInternalName)
      .collect(Collectors.toSet());

    Module[] modules = ModuleManager.getInstance(project).getModules();
    for (Module module : modules) {
      boolean hasPantsProjectPath = Objects.equals(module.getOptionValue(PantsConstants.PANTS_OPTION_LINKED_PROJECT_PATH), Paths.get(projectPath).normalize().toString());
      boolean isNotBeingImported = !importedModules.contains(module.getName());
      if (hasPantsProjectPath && isNotBeingImported) {
        ModuleManager.getInstance(project).disposeModule(module);
      }
    }
  };

  Application application = ApplicationManager.getApplication();
  application.invokeAndWait(() -> application.runWriteAction(clearModules));
}
 
Example 4
Source File: ExternalSystemUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static VirtualFile waitForTheFile(@Nullable final String path) {
  if (path == null) return null;

  final VirtualFile[] file = new VirtualFile[1];
  final Application app = ApplicationManager.getApplication();
  Runnable action = new Runnable() {
    public void run() {
      app.runWriteAction(new Runnable() {
        public void run() {
          file[0] = LocalFileSystem.getInstance().refreshAndFindFileByPath(path);
        }
      });
    }
  };
  if (app.isDispatchThread()) {
    action.run();
  }
  else {
    app.invokeAndWait(action, ModalityState.defaultModalityState());
  }
  return file[0];
}
 
Example 5
Source File: ModuleManagerComponent.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
protected void fireModulesAdded() {
  Runnable runnableWithProgress = () -> {
    for (final Module module : myModuleModel.getModules()) {
      final Application app = ApplicationManager.getApplication();
      final Runnable swingRunnable = () -> fireModuleAddedInWriteAction(module);
      if (app.isDispatchThread() || app.isHeadlessEnvironment()) {
        swingRunnable.run();
      }
      else {
        ProgressIndicator pi = ProgressManager.getInstance().getProgressIndicator();
        app.invokeAndWait(swingRunnable, pi.getModalityState());
      }
    }
  };

  ProgressIndicator progressIndicator = myProgressManager.getProgressIndicator();
  if (progressIndicator == null) {
    myProgressManager.runProcessWithProgressSynchronously(runnableWithProgress, "Loading modules", false, myProject);
  }
  else {
    runnableWithProgress.run();
  }
}
 
Example 6
Source File: VcsUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static VirtualFile waitForTheFile(final String path) {
  final VirtualFile[] file = new VirtualFile[1];
  final Application app = ApplicationManager.getApplication();
  Runnable action = new Runnable() {
    @Override
    public void run() {
      app.runWriteAction(new Runnable() {
        @Override
        public void run() {
          file[0] = LocalFileSystem.getInstance().refreshAndFindFileByPath(path);
        }
      });
    }
  };

  app.invokeAndWait(action, ModalityState.defaultModalityState());

  return file[0];
}
 
Example 7
Source File: MicroProfileAssert.java    From intellij-quarkus with Eclipse Public License 2.0 5 votes vote down vote up
public static void saveFile(String name, String content, Module javaProject) throws IOException {
    String modulePath = ModuleUtilCore.getModuleDirPath(javaProject);
    VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByPath(modulePath + "/src/main/resources/" + name);
    Application application = ApplicationManager.getApplication();
    application.invokeAndWait(() -> application.runWriteAction(() -> {
        try {
            file.setBinaryContent(content.getBytes(file.getCharset()));
        } catch (IOException e) {}
    }));
}
 
Example 8
Source File: AsyncUtils.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void invokeAndWait(Runnable runnable) throws ProcessCanceledException {
  final Application app = ApplicationManager.getApplication();
  if (app == null || app.isUnitTestMode()) {
    try {
      // This case existing to support unit testing.
      SwingUtilities.invokeAndWait(runnable);
    }
    catch (InterruptedException | InvocationTargetException e) {
      throw new ProcessCanceledException(e);
    }
  }
  else {
    app.invokeAndWait(runnable);
  }
}
 
Example 9
Source File: AsyncUtils.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void invokeAndWait(Runnable runnable) throws ProcessCanceledException {
  final Application app = ApplicationManager.getApplication();
  if (app == null || app.isUnitTestMode()) {
    try {
      // This case existing to support unit testing.
      SwingUtilities.invokeAndWait(runnable);
    }
    catch (InterruptedException | InvocationTargetException e) {
      throw new ProcessCanceledException(e);
    }
  }
  else {
    app.invokeAndWait(runnable);
  }
}
 
Example 10
Source File: HaxelibProjectUpdater.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
/**
 * Cause a synchronous write action to be run on the AWT thread.
 *
 * @param action action to run.
 */
private static void doWriteAction(final Runnable action) {
  final Application application = ApplicationManager.getApplication();
  application.invokeAndWait(new Runnable() {
    @Override
    public void run() {
      application.runWriteAction(action);
    }
  }, application.getDefaultModalityState());
}
 
Example 11
Source File: HaxelibProjectUpdater.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
/**
 * Cause a synchronous read action to be run.  Blocks if a write action is
 * currently running in the AWT thread.  Also blocks write actions from
 * occuring while this is being run.  So don't let tasks take too long, or
 * the UI gets choppy.
 *
 * @param action action to run.
 */
private static void doReadAction(final Runnable action) {
  final Application application = ApplicationManager.getApplication();
  application.invokeAndWait(new Runnable() {
    @Override
    public void run() {
      application.runReadAction(action);
    }
  }, application.getDefaultModalityState());
}
 
Example 12
Source File: WaitForProgressToShow.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void runOrInvokeAndWaitAboveProgress(final Runnable command, @Nullable final ModalityState modalityState) {
  final Application application = ApplicationManager.getApplication();
  if (application.isDispatchThread()) {
    command.run();
  } else {
    final ProgressIndicator pi = ProgressManager.getInstance().getProgressIndicator();
    if (pi != null) {
      execute(pi);
      application.invokeAndWait(command, pi.getModalityState());
    } else {
      final ModalityState notNullModalityState = modalityState == null ? ModalityState.NON_MODAL : modalityState;
      application.invokeAndWait(command, notNullModalityState);
    }
  }
}
 
Example 13
Source File: HttpConfigurable.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void runAboveAll(@Nonnull final Runnable runnable) {
  ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
  if (progressIndicator != null && progressIndicator.isModal()) {
    WaitForProgressToShow.runOrInvokeAndWaitAboveProgress(runnable);
  }
  else {
    Application app = ApplicationManager.getApplication();
    app.invokeAndWait(runnable, ModalityState.any());
  }
}