[git] Init dialog in EDT + UIUtil.invokeAndWait(Computable)

The pattern is very frequent => introduce a utility method to avoid
extra Ref<T>-s.
This commit is contained in:
Kirill Likhodedov
2014-06-24 16:29:05 +04:00
parent b29e6aaa5f
commit 5adcafb458
4 changed files with 38 additions and 20 deletions
@@ -2096,6 +2096,17 @@ public class UIUtil {
}
}
public static <T> T invokeAndWaitIfNeeded(@NotNull final Computable<T> computable) {
final Ref<T> result = Ref.create();
invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
result.set(computable.compute());
}
});
return result.get();
}
public static void invokeAndWaitIfNeeded(@NotNull final ThrowableRunnable runnable) throws Throwable {
if (SwingUtilities.isEventDispatchThread()) {
runnable.run();
@@ -21,6 +21,7 @@ import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.VerticalFlowLayout;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.VcsNotifier;
import com.intellij.openapi.vcs.changes.Change;
@@ -125,20 +126,21 @@ public class GitBranchUiHandlerImpl implements GitBranchUiHandler {
}
@Override
public boolean showUntrackedFilesDialogWithRollback(@NotNull String operationName, @NotNull String rollbackProposal,
@NotNull Collection<VirtualFile> untrackedFiles) {
String title = "Could not " + StringUtil.capitalize(operationName);
String description = UntrackedFilesNotifier.createUntrackedFilesOverwrittenDescription(operationName, false);
public boolean showUntrackedFilesDialogWithRollback(@NotNull String operationName, @NotNull final String rollbackProposal,
@NotNull final Collection<VirtualFile> untrackedFiles) {
final String title = "Could not " + StringUtil.capitalize(operationName);
final String description = UntrackedFilesNotifier.createUntrackedFilesOverwrittenDescription(operationName, false);
final SelectFilesDialog dialog = new UntrackedFilesDialog(myProject, untrackedFiles, StringUtil.stripHtml(description, true), rollbackProposal);
dialog.setTitle(title);
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
return UIUtil.invokeAndWaitIfNeeded(new Computable<Boolean>() {
@Override
public void run() {
public Boolean compute() {
SelectFilesDialog dialog = new UntrackedFilesDialog(myProject, untrackedFiles,
StringUtil.stripHtml(description, true), rollbackProposal);
dialog.setTitle(title);
myFacade.showDialog(dialog);
return dialog.isOK();
}
});
return dialog.isOK();
}
@NotNull
@@ -22,7 +22,9 @@ import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Couple;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.CheckinProjectPanel;
import com.intellij.openapi.vcs.FilePath;
@@ -131,14 +133,17 @@ public class GitCheckinHandlerFactory extends VcsCheckinHandlerFactory {
}
if (crlfHelper.get().shouldWarn()) {
final GitCrlfDialog dialog = new GitCrlfDialog(myProject);
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
Pair<Integer, Boolean> codeAndDontWarn = UIUtil.invokeAndWaitIfNeeded(new Computable<Pair<Integer, Boolean>>() {
@Override
public void run() {
public Pair<Integer, Boolean> compute() {
final GitCrlfDialog dialog = new GitCrlfDialog(myProject);
dialog.show();
return Pair.create(dialog.getExitCode(), dialog.dontWarnAgain());
}
});
int decision = dialog.getExitCode();
int decision = codeAndDontWarn.first;
boolean dontWarnAgain = codeAndDontWarn.second;
if (decision == GitCrlfDialog.CANCEL) {
return ReturnResult.CANCEL;
}
@@ -148,7 +153,7 @@ public class GitCheckinHandlerFactory extends VcsCheckinHandlerFactory {
setCoreAutoCrlfAttribute(anyRoot);
}
else {
if (dialog.dontWarnAgain()) {
if (dontWarnAgain) {
settings.setWarnAboutCrlf(false);
}
}
@@ -119,15 +119,15 @@ public class GitMergeUpdater extends GitUpdater {
LOG.info("Local changes would be overwritten by merge");
final List<FilePath> paths = getFilesOverwrittenByMerge(mergeLineListener.getOutput());
final Collection<Change> changes = getLocalChangesFilteredByFiles(paths);
final ChangeListViewerDialog dialog = new ChangeListViewerDialog(myProject, changes, false) {
@Override protected String getDescription() {
return "Your local changes to the following files would be overwritten by merge.<br/>" +
"Please, commit your changes or stash them before you can merge.";
}
};
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
ChangeListViewerDialog dialog = new ChangeListViewerDialog(myProject, changes, false) {
@Override protected String getDescription() {
return "Your local changes to the following files would be overwritten by merge.<br/>" +
"Please, commit your changes or stash them before you can merge.";
}
};
dialog.show();
}
});