From 79971282d4fe861a30a054e5ac0bfb91d65c3b38 Mon Sep 17 00:00:00 2001 From: irengrig Date: Tue, 2 Feb 2010 10:43:25 +0300 Subject: [PATCH] VCS: shelved changes: navigatable show diff --- .../changes/shelf/DiffShelvedChangesAction.java | 55 +++++++++++++++++++--- .../changes/shelf/ShelvedChangesViewManager.java | 23 ++++----- .../openapi/vcs/changes/ui/ChangesBrowser.java | 11 ++--- .../openapi/vcs/changes/ui/ChangesComparator.java | 36 ++++++++++++++ 4 files changed, 98 insertions(+), 27 deletions(-) create mode 100644 platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesComparator.java diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/DiffShelvedChangesAction.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/DiffShelvedChangesAction.java index 5754a3e037..248ebc5401 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/DiffShelvedChangesAction.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/DiffShelvedChangesAction.java @@ -15,10 +15,7 @@ */ package com.intellij.openapi.vcs.changes.shelf; -import com.intellij.openapi.actionSystem.ActionManager; -import com.intellij.openapi.actionSystem.AnAction; -import com.intellij.openapi.actionSystem.AnActionEvent; -import com.intellij.openapi.actionSystem.PlatformDataKeys; +import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.diff.DiffRequestFactory; import com.intellij.openapi.diff.MergeRequest; import com.intellij.openapi.diff.impl.patch.ApplyPatchContext; @@ -30,12 +27,15 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vcs.changes.Change; import com.intellij.openapi.vcs.changes.ContentRevision; +import com.intellij.openapi.vcs.changes.actions.ShowDiffAction; import com.intellij.openapi.vcs.changes.patch.ApplyPatchAction; import com.intellij.openapi.vcs.changes.patch.PatchMergeRequestFactory; +import com.intellij.openapi.vcs.changes.ui.ChangesComparator; import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; import java.io.IOException; +import java.util.Arrays; import java.util.List; /** @@ -43,7 +43,8 @@ import java.util.List; */ public class DiffShelvedChangesAction extends AnAction implements DumbAware { public void actionPerformed(final AnActionEvent e) { - Project project = e.getData(PlatformDataKeys.PROJECT); + showShelvedChangesDiff(e.getDataContext()); + /*Project project = e.getData(PlatformDataKeys.PROJECT); final ShelvedChangeList[] changeLists = e.getData(ShelvedChangesViewManager.SHELVED_CHANGELIST_KEY); List shelvedChanges = e.getData(ShelvedChangesViewManager.SHELVED_CHANGE_KEY); if ((shelvedChanges == null || shelvedChanges.isEmpty()) && changeLists != null && changeLists.length > 0) { @@ -63,7 +64,49 @@ public class DiffShelvedChangesAction extends AnAction implements DumbAware { } } } - ActionManager.getInstance().getAction("ChangesView.Diff").actionPerformed(e); + ActionManager.getInstance().getAction("ChangesView.Diff").actionPerformed(e);*/ + } + + public static void showShelvedChangesDiff(final DataContext dc) { + Project project = PlatformDataKeys.PROJECT.getData(dc); + ShelvedChangeList[] changeLists = ShelvedChangesViewManager.SHELVED_CHANGELIST_KEY.getData(dc); + if (changeLists == null) { + changeLists = ShelvedChangesViewManager.SHELVED_RECYCLED_CHANGELIST_KEY.getData(dc); + } + + // selected changes inside lists + List shelvedChanges = ShelvedChangesViewManager.SHELVED_CHANGE_KEY.getData(dc); + + if (changeLists == null) return; + + Change toSelect = null; + final List changesFromFirstList = changeLists[0].getChanges(); + if (shelvedChanges != null) { + for (final ShelvedChange fromList : changesFromFirstList) { + for (ShelvedChange shelvedChange : shelvedChanges) { + if (fromList.equals(shelvedChange)) { + toSelect = fromList.getChange(project); + break; + } + } + if (toSelect != null) break; + } + } + + final Change[] changes = new Change[changesFromFirstList.size()]; + for (int i = 0; i < changesFromFirstList.size(); i++) { + final ShelvedChange shelvedChange = changesFromFirstList.get(i); + changes[i] = shelvedChange.getChange(project); + } + Arrays.sort(changes, ChangesComparator.getInstance()); + + int toSelectIdx = 0; + for (int i = 0; i < changes.length; i++) { + if (toSelect == changes[i]) { + toSelectIdx = i; + } + } + ShowDiffAction.showDiffForChange(changes, toSelectIdx, project); } private static boolean showConflictingChangeDiff(final Project project, final ShelvedChange c) throws PatchSyntaxException, IOException { diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedChangesViewManager.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedChangesViewManager.java index cb1d6a80f8..1b6336c366 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedChangesViewManager.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedChangesViewManager.java @@ -38,7 +38,6 @@ import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.Pair; import com.intellij.openapi.vcs.*; import com.intellij.openapi.vcs.changes.Change; -import com.intellij.openapi.vcs.changes.actions.ShowDiffAction; import com.intellij.openapi.vcs.changes.issueLinks.IssueLinkRenderer; import com.intellij.openapi.vcs.changes.issueLinks.TreeLinkMouseListener; import com.intellij.openapi.vcs.changes.patch.RelativePathCalculator; @@ -60,7 +59,10 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; -import javax.swing.tree.*; +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.DefaultTreeModel; +import javax.swing.tree.TreeModel; +import javax.swing.tree.TreePath; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; @@ -112,25 +114,16 @@ public class ShelvedChangesViewManager implements ProjectComponent { myTree.setCellRenderer(new ShelfTreeCellRenderer(project, myMoveRenameInfo)); new TreeLinkMouseListener(new ShelfTreeCellRenderer(project, myMoveRenameInfo)).install(myTree); - ActionManager.getInstance().getAction("ShelvedChanges.Diff").registerCustomShortcutSet(CommonShortcuts.getDiff(), myTree); + final AnAction showDiffAction = ActionManager.getInstance().getAction("ShelvedChanges.Diff"); + showDiffAction.registerCustomShortcutSet(CommonShortcuts.getDiff(), myTree); PopupHandler.installPopupHandler(myTree, "ShelvedChangesPopupMenu", ActionPlaces.UNKNOWN); myTree.addMouseListener(new MouseAdapter() { public void mouseClicked(final MouseEvent e) { if (e.getClickCount() != 2) return; - if (myTree.getPathForLocation(e.getX(), e.getY()) == null) return; - final TreePath selectionPath = myTree.getSelectionPath(); - if (selectionPath == null) return; - final Object lastPathComponent = selectionPath.getLastPathComponent(); - if (((TreeNode) lastPathComponent).isLeaf()) { - DataContext context = DataManager.getInstance().getDataContext(myTree); - final Change[] changes = VcsDataKeys.CHANGES.getData(context); - if (changes != null && changes.length > 0) { - ShowDiffAction.showDiffForChange(changes, 0, myProject); - } - e.consume(); - } + + DiffShelvedChangesAction.showShelvedChangesDiff(DataManager.getInstance().getDataContext(myTree)); } }); } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesBrowser.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesBrowser.java index b0284ab55d..f873de6745 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesBrowser.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesBrowser.java @@ -32,7 +32,9 @@ import javax.swing.*; import javax.swing.tree.DefaultTreeModel; import java.awt.*; import java.io.File; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; /** @@ -292,11 +294,7 @@ public class ChangesBrowser extends JPanel implements TypeSafeDataProvider { } protected static List sortChanges(final List list) { - Collections.sort(list, new Comparator() { - public int compare(final Change o1, final Change o2) { - return ChangesUtil.getFilePath(o1).getName().compareToIgnoreCase(ChangesUtil.getFilePath(o2).getName()); - } - }); + Collections.sort(list, ChangesComparator.getInstance()); return list; } @@ -346,4 +344,5 @@ public class ChangesBrowser extends JPanel implements TypeSafeDataProvider { LOCAL_CHANGES, COMMITTED_CHANGES } + } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesComparator.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesComparator.java new file mode 100644 index 0000000000..58be91b63b --- /dev/null +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesComparator.java @@ -0,0 +1,36 @@ +/* + * Copyright 2000-2010 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.openapi.vcs.changes.ui; + +import com.intellij.openapi.vcs.changes.Change; +import com.intellij.openapi.vcs.changes.ChangesUtil; + +import java.util.Comparator; + +public class ChangesComparator implements Comparator { + private static final ChangesComparator ourInstance = new ChangesComparator(); + + public static ChangesComparator getInstance() { + return ourInstance; + } + + private ChangesComparator() { + } + + public int compare(final Change o1, final Change o2) { + return ChangesUtil.getFilePath(o1).getName().compareToIgnoreCase(ChangesUtil.getFilePath(o2).getName()); + } +} -- 2.11.4.GIT