From 93e5e035ba7b247eba8d2ae71fa01522cfc4121f Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Wed, 7 Jan 2015 15:03:11 +0300 Subject: [PATCH] [vcs] ChangesTreeList: extract method for searching for file in tree --- .../vcs/changes/ui/ChangesTreeList.java | 54 +++++++++++-------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesTreeList.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesTreeList.java index b4c8fbb9f90e..ed72f047c26c 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesTreeList.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/ui/ChangesTreeList.java @@ -26,6 +26,7 @@ import com.intellij.openapi.project.DumbAware; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.EmptyRunnable; import com.intellij.openapi.util.Pair; +import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.registry.Registry; @@ -400,28 +401,10 @@ public abstract class ChangesTreeList extends JPanel implements TypeSafeDataP } } else { if (toSelect != null) { - ChangesBrowserNode root = (ChangesBrowserNode)model.getRoot(); - final int[] rowToSelect = new int[] {-1}; - TreeUtil.traverse(root, new TreeUtil.Traverse() { - @Override - public boolean accept(Object node) { - if (node instanceof DefaultMutableTreeNode) { - Object userObject = ((DefaultMutableTreeNode)node).getUserObject(); - if (userObject instanceof Change) { - Change change = (Change)userObject; - VirtualFile virtualFile = change.getVirtualFile(); - if ((virtualFile != null && virtualFile.equals(toSelect)) || seemsToBeMoved(change, toSelect)) { - TreeNode[] path = ((DefaultMutableTreeNode)node).getPath(); - rowToSelect[0] = myTree.getRowForPath(new TreePath(path)); - } - } - } - - return rowToSelect[0] == -1; - } - }); - - scrollRow = rowToSelect[0] == -1 ? scrollRow : rowToSelect[0]; + int rowInTree = findRowContainingFile((TreeNode)model.getRoot(), toSelect); + if (rowInTree > -1) { + scrollRow = rowInTree; + } } } @@ -443,6 +426,33 @@ public abstract class ChangesTreeList extends JPanel implements TypeSafeDataP } } + private int findRowContainingFile(@NotNull TreeNode root, @Nullable final VirtualFile toSelect) { + if (toSelect == null) { + return -1; + } + + final Ref row = Ref.create(-1); + TreeUtil.traverse(root, new TreeUtil.Traverse() { + @Override + public boolean accept(Object node) { + if (node instanceof DefaultMutableTreeNode) { + Object userObject = ((DefaultMutableTreeNode)node).getUserObject(); + if (userObject instanceof Change) { + Change change = (Change)userObject; + VirtualFile virtualFile = change.getVirtualFile(); + if ((virtualFile != null && virtualFile.equals(toSelect)) || seemsToBeMoved(change, toSelect)) { + TreeNode[] path = ((DefaultMutableTreeNode)node).getPath(); + row.set(myTree.getRowForPath(new TreePath(path))); + } + } + } + + return row.get() == -1; + } + }); + return row.get(); + } + private static boolean seemsToBeMoved(Change change, VirtualFile toSelect) { ContentRevision afterRevision = change.getAfterRevision(); if (afterRevision == null) return false;