[hg]: move BackgroundTaskUtil to platform-impl

* reuse for hg appropriate command execution instead of simple execution on application PolledThread

* style: refactor static methods signatures to be able call not only from AWT
This commit is contained in:
Nadya Zabrodina
2016-03-09 14:29:26 +03:00
parent e479b24e05
commit f82f0820a0
9 changed files with 44 additions and 32 deletions
@@ -16,6 +16,7 @@
package com.intellij.diff.util;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.util.BackgroundTaskUtil;
import com.intellij.util.Function;
import org.jetbrains.annotations.CalledInAwt;
import org.jetbrains.annotations.NotNull;
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.diff.util;
package com.intellij.openapi.progress.util;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
@@ -28,6 +28,8 @@ import com.intellij.openapi.util.EmptyRunnable;
import com.intellij.openapi.util.Ref;
import com.intellij.util.Consumer;
import com.intellij.util.Function;
import com.intellij.util.NullableConsumer;
import org.jetbrains.annotations.CalledInAny;
import org.jetbrains.annotations.CalledInAwt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -170,6 +172,25 @@ public class BackgroundTaskUtil {
@NotNull
public static ProgressIndicator executeOnPooledThread(@NotNull final Consumer<ProgressIndicator> task, @NotNull Disposable parent) {
final ModalityState modalityState = ModalityState.current();
return executeOnPooledThread(task, parent, modalityState);
}
@NotNull
@CalledInAny
public static ProgressIndicator executeOnPooledThread(@NotNull final Runnable runnable,
@NotNull Disposable parent) {
return executeOnPooledThread(new NullableConsumer<ProgressIndicator>() {
@Override
public void consume(@Nullable ProgressIndicator indicator) {
runnable.run();
}
}, parent, ModalityState.NON_MODAL);
}
@NotNull
@CalledInAny
public static ProgressIndicator executeOnPooledThread(@NotNull final Consumer<ProgressIndicator> task,
@NotNull Disposable parent, final ModalityState modalityState) {
final ProgressIndicator indicator = new EmptyProgressIndicator() {
@NotNull
@Override
@@ -177,7 +198,6 @@ public class BackgroundTaskUtil {
return modalityState;
}
};
indicator.start();
final Disposable disposable = new Disposable() {
@Override
@@ -186,6 +206,7 @@ public class BackgroundTaskUtil {
}
};
Disposer.register(parent, disposable);
indicator.start();
ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
@Override
@@ -29,7 +29,6 @@ import com.intellij.diff.tools.util.base.DiffViewerBase;
import com.intellij.diff.tools.util.base.DiffViewerListener;
import com.intellij.diff.tools.util.side.OnesideTextDiffViewer;
import com.intellij.diff.tools.util.side.TwosideTextDiffViewer;
import com.intellij.diff.util.BackgroundTaskUtil;
import com.intellij.diff.util.Side;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
@@ -43,6 +42,7 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.localVcs.UpToDateLineNumberProvider;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.util.BackgroundTaskUtil;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.Balloon;
@@ -21,7 +21,6 @@ import com.intellij.codeInsight.hint.HintManagerImpl;
import com.intellij.diff.comparison.ByWord;
import com.intellij.diff.comparison.ComparisonPolicy;
import com.intellij.diff.fragments.DiffFragment;
import com.intellij.diff.util.BackgroundTaskUtil;
import com.intellij.diff.util.DiffDrawUtil;
import com.intellij.diff.util.DiffUtil;
import com.intellij.diff.util.TextDiffType;
@@ -46,6 +45,7 @@ import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.util.BackgroundTaskUtil;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.registry.Registry;
@@ -406,12 +406,12 @@ public class HgVcs extends AbstractVcs<CommittedChangeList> {
@Override
@CalledInAwt
public void enableIntegration() {
ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
HgUtil.executeOnPooledThread(new Runnable() {
public void run() {
Collection<VcsRoot> roots = ServiceManager.getService(myProject, VcsRootDetector.class).detect();
new HgIntegrationEnabler(HgVcs.this).enable(roots);
}
});
}, myProject);
}
@Override
@@ -12,7 +12,6 @@
// limitations under the License.
package org.zmlx.hg4idea.command;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
@@ -57,12 +56,12 @@ public class HgAddCommand {
}.queue();
}
else {
ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
HgUtil.executeOnPooledThread(new Runnable() {
@Override
public void run() {
executeInCurrentThread(files);
}
});
}, myProject);
}
}
@@ -70,9 +69,7 @@ public class HgAddCommand {
final Map<VirtualFile, Collection<VirtualFile>> sorted = HgUtil.sortByHgRoots(myProject, files);
for (Map.Entry<VirtualFile, Collection<VirtualFile>> entry : sorted.entrySet()) {
if (indicator != null) {
if (indicator.isCanceled()) {
return;
}
if (indicator.isCanceled()) return;
indicator.setFraction(0);
indicator.setText2("Adding files to " + entry.getKey().getPresentableUrl());
}
@@ -85,9 +82,7 @@ public class HgAddCommand {
int currentChunk = 0;
for (List<String> paths : chunks) {
if (indicator != null) {
if (indicator.isCanceled()) {
return;
}
if (indicator.isCanceled()) return;
indicator.setFraction((double)currentChunk / chunks.size());
currentChunk++;
}
@@ -98,7 +98,7 @@ public class HgCommandExecutor {
public void execute(@Nullable final VirtualFile repo, @NotNull final String operation, @Nullable final List<String> arguments,
@Nullable final HgCommandResultHandler handler) {
HgUtil.executeOnPooledThreadIfNeeded(new Runnable() {
HgUtil.executeOnPooledThread(new Runnable() {
@Override
public void run() {
HgCommandResult result = executeInCurrentThread(repo, operation, arguments);
@@ -106,7 +106,7 @@ public class HgCommandExecutor {
handler.process(result);
}
}
});
}, myProject);
}
public HgCommandResult executeInCurrentThread(@Nullable final VirtualFile repo,
@@ -18,7 +18,6 @@ package org.zmlx.hg4idea.repo;
import com.intellij.dvcs.repo.RepositoryImpl;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Condition;
@@ -228,13 +227,11 @@ public class HgRepositoryImpl extends RepositoryImpl implements HgRepository {
myOpenedBranches = HgBranchesCommand.collectNames(branchCommandResult);
}
ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
HgUtil.executeOnPooledThread(new Runnable() {
public void run() {
if (!project.isDisposed()) {
project.getMessageBus().syncPublisher(HgVcs.STATUS_TOPIC).update(project, getRoot());
}
project.getMessageBus().syncPublisher(HgVcs.STATUS_TOPIC).update(project, getRoot());
}
});
}, project);
}
}
@@ -13,9 +13,12 @@
package org.zmlx.hg4idea.util;
import com.intellij.dvcs.DvcsUtil;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.util.BackgroundTaskUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Couple;
@@ -55,11 +58,9 @@ import org.zmlx.hg4idea.provider.HgChangeProvider;
import org.zmlx.hg4idea.repo.HgRepository;
import org.zmlx.hg4idea.repo.HgRepositoryManager;
import java.awt.*;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -423,12 +424,9 @@ public abstract class HgUtil {
return sorted;
}
public static void executeOnPooledThreadIfNeeded(Runnable runnable) {
if (EventQueue.isDispatchThread() && !ApplicationManager.getApplication().isUnitTestMode()) {
ApplicationManager.getApplication().executeOnPooledThread(runnable);
} else {
runnable.run();
}
@NotNull
public static ProgressIndicator executeOnPooledThread(@NotNull Runnable runnable, @NotNull Disposable parentDisposable) {
return BackgroundTaskUtil.executeOnPooledThread(runnable, parentDisposable);
}
/**