com.intellij.openapi.progress.util.BackgroundTaskUtil Java Examples

The following examples show how to use com.intellij.openapi.progress.util.BackgroundTaskUtil. 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: ANTLRv4PluginController.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void parseText(final VirtualFile grammarFile, String inputText) {
	// Wipes out the console and also any error annotations
	previewPanel.inputPanel.clearParseErrors();

	final PreviewState previewState = getPreviewState(grammarFile);

	abortCurrentParsing();

	// Parse text in a background thread to avoid freezing the UI if the grammar is badly written
	// an takes ages to interpret the input.
	parsingProgressIndicator = BackgroundTaskUtil.executeAndTryWait(
			(indicator) -> {
				long start = System.nanoTime();

				previewState.parsingResult = ParsingUtils.parseText(
						previewState.g, previewState.lg, previewState.startRuleName,
						grammarFile, inputText, project
				);

				return () -> previewPanel.onParsingCompleted(previewState, System.nanoTime() - start);
			},
			() -> previewPanel.notifySlowParsing(),
			ProgressWindow.DEFAULT_PROGRESS_DIALOG_POSTPONE_TIME_MILLIS,
			false
	);
}
 
Example #2
Source File: StartupManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
public final void scheduleBackgroundPostStartupActivities(@Nonnull UIAccess uiAccess) {
  if (myProject.isDisposedOrDisposeInProgress()) {
    return;
  }

  myBackgroundPostStartupScheduledFuture = AppExecutorUtil.getAppScheduledExecutorService().schedule(() -> {
    if (myProject.isDisposedOrDisposeInProgress()) {
      return;
    }

    List<StartupActivity.Background> activities = StartupActivity.BACKGROUND_POST_STARTUP_ACTIVITY.getExtensionList();
    //StartupActivity.BACKGROUND_POST_STARTUP_ACTIVITY.addExtensionPointListener(new ExtensionPointListener<StartupActivity.Background>() {
    //  @Override
    //  public void extensionAdded(@Nonnull StartupActivity.Background extension, @Nonnull PluginDescriptor pluginDescriptor) {
    //    extension.runActivity(myProject);
    //  }
    //}, this);

    BackgroundTaskUtil.runUnderDisposeAwareIndicator(this, () -> {
      for (StartupActivity activity : activities) {
        ProgressManager.checkCanceled();

        if (myProject.isDisposedOrDisposeInProgress()) {
          return;
        }

        activity.runActivity(uiAccess, myProject);
      }
    });
  }, Registry.intValue("ide.background.post.startup.activity.delay"), TimeUnit.MILLISECONDS);
}
 
Example #3
Source File: TFSVcs.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
@Override
public void enableIntegration() {
    BackgroundTaskUtil.executeOnPooledThread(myProject, () -> {
        Collection<VcsRoot> roots = ServiceManager.getService(myProject, VcsRootDetector.class).detect();
        new TfvcIntegrationEnabler(this).enable(roots);
    });
}
 
Example #4
Source File: DiffTaskQueue.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
public void executeAndTryWait(@Nonnull final Function<ProgressIndicator, Runnable> backgroundTask,
                              @Nullable final Runnable onSlowAction,
                              final int waitMillis,
                              final boolean forceEDT) {
  abort();
  myProgressIndicator = BackgroundTaskUtil.executeAndTryWait(backgroundTask, onSlowAction, waitMillis, forceEDT);
}
 
Example #5
Source File: DvcsUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static AccessToken workingTreeChangeStarted(@Nonnull Project project, @Nullable String activityName) {
  BackgroundTaskUtil.syncPublisher(BatchFileChangeListener.TOPIC).batchChangeStarted(project, activityName);
  return new AccessToken() {
    @Override
    public void finish() {
      BackgroundTaskUtil.syncPublisher(BatchFileChangeListener.TOPIC).batchChangeCompleted(project);
    }
  };
}
 
Example #6
Source File: VcsRepositoryManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void checkAndUpdateRepositoriesCollection(@Nullable VirtualFile checkedRoot) {
  Map<VirtualFile, Repository> repositories;
  try {
    MODIFY_LOCK.lock();
    try {
      REPO_LOCK.readLock().lock();
      if (myRepositories.containsKey(checkedRoot)) return;
      repositories = ContainerUtil.newHashMap(myRepositories);
    }
    finally {
      REPO_LOCK.readLock().unlock();
    }

    Collection<VirtualFile> invalidRoots = findInvalidRoots(repositories.keySet());
    repositories.keySet().removeAll(invalidRoots);
    Map<VirtualFile, Repository> newRoots = findNewRoots(repositories.keySet());
    repositories.putAll(newRoots);

    REPO_LOCK.writeLock().lock();
    try {
      if (!myDisposed) {
        myRepositories.clear();
        myRepositories.putAll(repositories);
      }
    }
    finally {
      REPO_LOCK.writeLock().unlock();
    }
    BackgroundTaskUtil.syncPublisher(myProject, VCS_REPOSITORY_MAPPING_UPDATED).mappingChanged();
  }
  finally {
    MODIFY_LOCK.unlock();
  }
}
 
Example #7
Source File: LineStatusMarkerPopup.java    From consulo with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
private List<DiffFragment> computeWordDiff() {
  if (!isShowInnerDifferences()) return null;
  if (myRange.getType() != Range.MODIFIED) return null;

  final CharSequence vcsContent = myTracker.getVcsContent(myRange);
  final CharSequence currentContent = myTracker.getCurrentContent(myRange);

  return BackgroundTaskUtil.tryComputeFast(new Function<ProgressIndicator, List<DiffFragment>>() {
    @Override
    public List<DiffFragment> fun(ProgressIndicator indicator) {
      return ByWord.compare(vcsContent, currentContent, ComparisonPolicy.DEFAULT, indicator);
    }
  }, Registry.intValue("diff.status.tracker.byword.delay"));
}
 
Example #8
Source File: AnnotateDiffViewerAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void doAnnotate(@Nonnull final ViewerAnnotator annotator) {
  final DiffViewerBase viewer = annotator.getViewer();
  final Project project = viewer.getProject();
  if (project == null) return;

  final FileAnnotationLoader loader = annotator.createAnnotationsLoader();
  if (loader == null) return;

  final DiffContextEx diffContext = ObjectUtils.tryCast(viewer.getContext(), DiffContextEx.class);

  annotator.getBackgroundableLock().lock();
  if (diffContext != null) diffContext.showProgressBar(true);

  BackgroundTaskUtil.executeOnPooledThread(viewer, () -> {
    try {
      loader.run();
    }
    finally {
      ApplicationManager.getApplication().invokeLater(() -> {
        if (diffContext != null) diffContext.showProgressBar(false);
        annotator.getBackgroundableLock().unlock();

        VcsException exception = loader.getException();
        if (exception != null) {
          Notification notification = VcsNotifier.IMPORTANT_ERROR_NOTIFICATION.createNotification("Can't Load Annotations", exception.getMessage(), NotificationType.ERROR, null);
          showNotification(viewer, notification);
          LOG.warn(exception);
          return;
        }

        if (loader.getResult() == null) return;
        if (viewer.isDisposed()) return;

        annotator.showAnnotation(loader.getResult());
      }, ProgressManager.getGlobalProgressIndicator().getModalityState());
    }
  });
}
 
Example #9
Source File: VcsSelectionHistoryDialog.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void start(@Nonnull Disposable disposable) {
  BackgroundTaskUtil.executeOnPooledThread(disposable, () -> {
    try {
      // first block is loaded in constructor
      for (int index = 1; index < myRevisions.size(); index++) {
        ProgressManager.checkCanceled();

        Block block = myBlocks.get(index - 1);
        VcsFileRevision revision = myRevisions.get(index);

        synchronized (LOCK) {
          myCurrentLoadingRevision = revision;
        }
        notifyUpdate();

        Block previousBlock = createBlock(block, revision);

        synchronized (LOCK) {
          myBlocks.add(previousBlock);
        }
        notifyUpdate();
      }
    }
    catch (VcsException e) {
      synchronized (LOCK) {
        myException = e;
      }
      notifyError(e);
    }
    finally {
      synchronized (LOCK) {
        myIsLoading = false;
        myCurrentLoadingRevision = null;
      }
      notifyUpdate();
    }
  });
}
 
Example #10
Source File: ZipperUpdater.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void queue(@Nonnull final Runnable runnable, final boolean urgent, final boolean anyModality) {
  synchronized (myLock) {
    if (myAlarm.isDisposed()) return;
    final boolean wasRaised = myRaised;
    myRaised = true;
    myIsEmpty = false;
    if (!wasRaised) {
      final Runnable request = new Runnable() {
        @Override
        public void run() {
          synchronized (myLock) {
            if (!myRaised) return;
            myRaised = false;
          }
          BackgroundTaskUtil.runUnderDisposeAwareIndicator(myAlarm, runnable);
          synchronized (myLock) {
            myIsEmpty = !myRaised;
          }
        }

        @Override
        public String toString() {
          return runnable.toString();
        }
      };
      if (Alarm.ThreadToUse.SWING_THREAD.equals(myThreadToUse)) {
        if (anyModality) {
          myAlarm.addRequest(request, urgent ? 0 : myDelay, ModalityState.any());
        }
        else if (!ApplicationManager.getApplication().isDispatchThread()) {
          myAlarm.addRequest(request, urgent ? 0 : myDelay, ModalityState.NON_MODAL);
        }
        else {
          myAlarm.addRequest(request, urgent ? 0 : myDelay);
        }
      }
      else {
        myAlarm.addRequest(request, urgent ? 0 : myDelay);
      }
    }
  }
}