more appropriate way to collapse passed tasks (EDU-322)
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Fri, 24 Jun 2016 15:32:33 +0000 (18:32 +0300)
committerLiana Bakradze <liana.bakradze@jetbrains.com>
Fri, 24 Jun 2016 15:34:10 +0000 (18:34 +0300)
python/educational-core/student/src/com/jetbrains/edu/learning/actions/StudyTaskNavigationAction.java

index 97495aad8f0d97f159fe8302bc0267b8bc158b5c..e58e6d51bc526b737112275e166b732085cc0881 100644 (file)
@@ -8,6 +8,7 @@ import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.wm.ToolWindow;
 import com.intellij.openapi.wm.ToolWindowId;
 import com.intellij.openapi.wm.ToolWindowManager;
+import com.intellij.util.ui.tree.TreeUtil;
 import com.jetbrains.edu.learning.StudyState;
 import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.core.EduNames;
@@ -19,6 +20,8 @@ import org.jetbrains.annotations.Nullable;
 
 import javax.swing.*;
 import javax.swing.tree.TreePath;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 
 
@@ -63,19 +66,8 @@ abstract public class StudyTaskNavigationAction extends StudyActionWithShortcut
     }
 
     VirtualFile shouldBeActive = getFileToActivate(project, nextTaskFiles, taskDir);
-    JTree tree = ProjectView.getInstance(project).getCurrentProjectViewPane().getTree();
-    TreePath path = tree.getSelectionPath();
-    if (path != null) {
-      TreePath oldSelectionPath = path.getParentPath();
-      if (oldSelectionPath != null) {
-        tree.collapsePath(oldSelectionPath);
-        tree.fireTreeCollapsed(oldSelectionPath);
-      }
-    }
-    if (shouldBeActive != null) {
-      ProjectView.getInstance(project).select(shouldBeActive, shouldBeActive, false);
-      FileEditorManager.getInstance(project).openFile(shouldBeActive, true);
-    }
+
+    updateProjectView(project, shouldBeActive);
 
     ToolWindow runToolWindow = ToolWindowManager.getInstance(project).getToolWindow(ToolWindowId.RUN);
     if (runToolWindow != null) {
@@ -83,6 +75,41 @@ abstract public class StudyTaskNavigationAction extends StudyActionWithShortcut
     }
   }
 
+  private void updateProjectView(@NotNull Project project, VirtualFile shouldBeActive) {
+    JTree tree = ProjectView.getInstance(project).getCurrentProjectViewPane().getTree();
+    if (shouldBeActive != null) {
+      ProjectView.getInstance(project).selectCB(shouldBeActive, shouldBeActive, false).doWhenDone(() -> {
+        List<TreePath> paths = TreeUtil.collectExpandedPaths(tree);
+        List<TreePath> toCollapse = new ArrayList<TreePath>();
+        TreePath selectedPath = tree.getSelectionPath();
+        for (TreePath treePath : paths) {
+          if (treePath.isDescendant(selectedPath)) {
+            continue;
+          }
+          if (toCollapse.isEmpty()) {
+            toCollapse.add(treePath);
+            continue;
+          }
+          for (int i = 0; i < toCollapse.size(); i++) {
+            TreePath path = toCollapse.get(i);
+            if (treePath.isDescendant(path)) {
+              toCollapse.set(i, treePath);
+            }  else {
+              if (!path.isDescendant(treePath)) {
+                toCollapse.add(treePath);
+              }
+            }
+          }
+        }
+        for (TreePath path : toCollapse) {
+          tree.collapsePath(path);
+          tree.fireTreeCollapsed(path);
+        }
+      });
+      FileEditorManager.getInstance(project).openFile(shouldBeActive, true);
+    }
+  }
+
   @Nullable
   protected VirtualFile getFileToActivate(@NotNull Project project, Map<String, TaskFile> nextTaskFiles, VirtualFile taskDir) {
     VirtualFile shouldBeActive = null;