VCS: shelved changes: navigatable show diff
authorirengrig <Irina.Chernushina@jetbrains.com>
Tue, 2 Feb 2010 07:43:25 +0000 (10:43 +0300)
committerirengrig <Irina.Chernushina@jetbrains.com>
Tue, 2 Feb 2010 07:43:25 +0000 (10:43 +0300)
platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/DiffShelvedChangesAction.java
platform/vcs-impl/src/com/intellij/openapi/vcs/changes/shelf/ShelvedChangesViewManager.java
platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesBrowser.java
platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesComparator.java [new file with mode: 0644]

index 5754a3e037f04946f7e7bab37583c1761c862dc4..248ebc54016d4d02436ebbd6342171cde9e2ce5b 100644 (file)
  */
 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<ShelvedChange> 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<ShelvedChange> shelvedChanges = ShelvedChangesViewManager.SHELVED_CHANGE_KEY.getData(dc);
+
+    if (changeLists == null) return;
+
+    Change toSelect = null;
+    final List<ShelvedChange> 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 {
index cb1d6a80f8e6f1158d82cb46433a89aaae75add5..1b6336c366618938f22f311c08103cdcf5d62891 100644 (file)
@@ -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));
       }
     });
   }
index b0284ab55d6fd6c98ada9a67be6be7f8728f9aa0..f873de6745f524cc65739ed2bc091f343fef3f7c 100644 (file)
@@ -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<Change> sortChanges(final List<Change> list) {
-    Collections.sort(list, new Comparator<Change>() {
-      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 (file)
index 0000000..58be91b
--- /dev/null
@@ -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<Change> {
+  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());
+  }
+}