From d9079f3f97315330a9e716b4e20eb0c7221f6db6 Mon Sep 17 00:00:00 2001 From: Nadya Zabrodina Date: Wed, 13 Jul 2016 15:37:15 +0300 Subject: [PATCH] [patch]: cleanUp and small fixes * use ChangelistChooser as a default focused component; * do not choose default changelist as best variant, return null instead; * update changelist text only if name was not preselected manually; * apply from clipboard to default changelist instead of newly create with strange name; * cleanUp lambdas; --- .../openapi/vcs/changes/ChangeListUtil.java | 21 ++++------------- .../patch/ApplyPatchDifferentiatedDialog.java | 23 ++++++++++++++----- .../patch/ApplyPatchFromClipboardAction.java | 3 ++- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ChangeListUtil.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ChangeListUtil.java index 8d1b9cdedf46..ceb2f3518326 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ChangeListUtil.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ChangeListUtil.java @@ -16,13 +16,11 @@ package com.intellij.openapi.vcs.changes; import com.google.common.primitives.Ints; -import com.intellij.openapi.util.Condition; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Collections; -import java.util.Comparator; import java.util.List; public class ChangeListUtil { @@ -31,25 +29,16 @@ public class ChangeListUtil { public static LocalChangeList getPredefinedChangeList(@NotNull String defaultName, @NotNull ChangeListManager changeListManager) { final LocalChangeList sameNamedList = changeListManager.findChangeList(defaultName); if (sameNamedList != null) return sameNamedList; - LocalChangeList list = tryToMatchWithExistingChangelist(changeListManager, defaultName); - return list == null ? changeListManager.getDefaultChangeList() : list; + return tryToMatchWithExistingChangelist(changeListManager, defaultName); } @Nullable private static LocalChangeList tryToMatchWithExistingChangelist(@NotNull ChangeListManager changeListManager, @NotNull final String defaultName) { - List matched = ContainerUtil.findAll(changeListManager.getChangeListsCopy(), new Condition() { - @Override - public boolean value(LocalChangeList list) { - return defaultName.contains(list.getName().trim()); - } - }); + List matched = ContainerUtil.findAll(changeListManager.getChangeListsCopy(), + list -> defaultName.contains(list.getName().trim())); - return matched.isEmpty() ? null : Collections.max(matched, new Comparator() { - @Override - public int compare(LocalChangeList o1, LocalChangeList o2) { - return Ints.compare(o1.getName().trim().length(), o2.getName().trim().length()); - } - }); + return matched.isEmpty() ? null : Collections.max(matched, + (o1, o2) -> Ints.compare(o1.getName().trim().length(), o2.getName().trim().length())); } } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchDifferentiatedDialog.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchDifferentiatedDialog.java index 6d234ea67fd7..5475c5abde1e 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchDifferentiatedDialog.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchDifferentiatedDialog.java @@ -110,7 +110,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { private VirtualFileAdapter myListener; private final boolean myCanChangePatchFile; private String myHelpId = "reference.dialogs.vcs.patch.apply"; - private final String myCommitMessage; //may be provided externally; todo: parse with Additional Info Reader from patch meta information + private final boolean myShouldUpdateChangeListName; public ApplyPatchDifferentiatedDialog(final Project project, final ApplyPatchExecutor callback, final List executors, @NotNull final ApplyPatchMode applyPatchMode, @NotNull final VirtualFile patchFile) { @@ -178,7 +178,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { } new MyShowDiff().showDiff(); }); - + myShouldUpdateChangeListName = defaultList == null && externalCommitMessage == null; myUpdater = new MyUpdater(); myPatchFile = new TextFieldWithBrowseButton(); myPatchFile.addBrowseFolderListener(VcsBundle.message("patch.apply.select.title"), "", project, descriptor); @@ -189,7 +189,6 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { } }); - myCommitMessage = externalCommitMessage; myLoadQueue = new ZipperUpdater(500, Alarm.ThreadToUse.POOLED_THREAD, getDisposable()); myCanChangePatchFile = applyPatchMode.isCanChangePatchFile(); myReset = myCanChangePatchFile ? this::reset : EmptyRunnable.getInstance(); @@ -200,7 +199,12 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { }); ChangeListManager changeListManager = ChangeListManager.getInstance(project); myChangeListChooser.setChangeLists(changeListManager.getChangeListsCopy()); - myChangeListChooser.setDefaultSelection(defaultList != null ? defaultList : changeListManager.getDefaultChangeList()); + if (defaultList != null) { + myChangeListChooser.setDefaultSelection(defaultList); + } + else if (externalCommitMessage != null) { + myChangeListChooser.setDefaultName(externalCommitMessage); + } myChangeListChooser.init(); myInfoCalculator = new ChangesLegendCalculator(); @@ -340,6 +344,12 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { return myHelpId; } + @Nullable + @Override + public JComponent getPreferredFocusedComponent() { + return myChangeListChooser.getPreferredFocusedComponent(); + } + private void setPathFileChangeDefault() { myRecentPathFileChange.set(new FilePresentationModel(myPatchFile.getText())); } @@ -371,8 +381,9 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { new MatchPatchPaths(myProject).execute(filePatches, myUseProjectRootAsPredefinedBase); ApplicationManager.getApplication().invokeLater(() -> { - myChangeListChooser - .setDefaultName(myCommitMessage != null ? myCommitMessage : file.getNameWithoutExtension().replace('_', ' ').trim()); + if (myShouldUpdateChangeListName) { + myChangeListChooser.setDefaultName(file.getNameWithoutExtension().replace('_', ' ').trim()); + } myPatches.clear(); myPatches.addAll(matchedPatches); myReader = patchReader; diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchFromClipboardAction.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchFromClipboardAction.java index 701bc013be6c..a7f6d7c6cc34 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchFromClipboardAction.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchFromClipboardAction.java @@ -43,7 +43,8 @@ public class ApplyPatchFromClipboardAction extends DumbAwareAction { public MyApplyPatchFromClipboardDialog(@NotNull Project project, @NotNull String clipboardText) { super(project, new ApplyPatchDefaultExecutor(project), Collections.emptyList(), ApplyPatchMode.APPLY_PATCH_IN_MEMORY, - new LightVirtualFile("clipboardPatchFile", clipboardText)); + new LightVirtualFile("clipboardPatchFile", clipboardText), null, ChangeListManager.getInstance(project).getDefaultChangeList(), + null, null, null, false); } @Override