b8279abef378deb54408b080a325550cd4ba8784
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / projectView / CCTaskDirectoryNode.java
1 package com.jetbrains.edu.coursecreator.projectView;
2
3 import com.intellij.ide.projectView.ViewSettings;
4 import com.intellij.ide.util.treeView.AbstractTreeNode;
5 import com.intellij.openapi.project.Project;
6 import com.intellij.openapi.vfs.VirtualFile;
7 import com.intellij.psi.PsiDirectory;
8 import com.intellij.psi.PsiElement;
9 import com.intellij.psi.PsiFile;
10 import com.jetbrains.edu.coursecreator.CCUtils;
11 import com.jetbrains.edu.learning.StudyLanguageManager;
12 import com.jetbrains.edu.learning.StudyTaskManager;
13 import com.jetbrains.edu.learning.StudyUtils;
14 import com.jetbrains.edu.learning.core.EduNames;
15 import com.jetbrains.edu.learning.courseFormat.Course;
16 import com.jetbrains.edu.learning.courseFormat.Task;
17 import com.jetbrains.edu.learning.projectView.TaskDirectoryNode;
18 import org.jetbrains.annotations.NotNull;
19 import org.jetbrains.annotations.Nullable;
20
21 public class CCTaskDirectoryNode extends TaskDirectoryNode {
22   public CCTaskDirectoryNode(@NotNull Project project,
23                              PsiDirectory value,
24                              ViewSettings viewSettings,
25                              @NotNull Task task) {
26     super(project, value, viewSettings, task);
27   }
28
29   @Nullable
30   @Override
31   public AbstractTreeNode modifyChildNode(AbstractTreeNode childNode) {
32     AbstractTreeNode node = super.modifyChildNode(childNode);
33     if (node != null) {
34       return node;
35     }
36     Object value = childNode.getValue();
37     if (value instanceof PsiElement) {
38       PsiFile psiFile = ((PsiElement)value).getContainingFile();
39       VirtualFile virtualFile = psiFile.getVirtualFile();
40       if (virtualFile == null) {
41         return null;
42       }
43       if (StudyUtils.isTaskDescriptionFile(virtualFile.getName())) {
44         return null;
45       }
46       if (!CCUtils.isTestsFile(myProject, virtualFile) || !myTask.hasSubtasks()) {
47         return new CCStudentInvisibleFileNode(myProject, psiFile, myViewSettings);
48       }
49
50       Course course = StudyTaskManager.getInstance(myProject).getCourse();
51       if (course == null) {
52         return null;
53       }
54       StudyLanguageManager manager = StudyUtils.getLanguageManager(course);
55       if (manager == null) {
56         return new CCStudentInvisibleFileNode(myProject, psiFile, myViewSettings);
57       }
58       String testFileName = manager.getTestFileName();
59       return isActiveSubtaskTest(virtualFile) ? new CCStudentInvisibleFileNode(myProject, psiFile, myViewSettings, testFileName) : null;
60     }
61     return null;
62   }
63
64   private boolean isActiveSubtaskTest(VirtualFile virtualFile) {
65     if (!myTask.hasSubtasks()) {
66       return true;
67     }
68
69     boolean isSubtaskTestFile = virtualFile.getName().contains(EduNames.SUBTASK_MARKER);
70     if (myTask.getActiveSubtaskIndex() == 0) {
71       return !isSubtaskTestFile;
72     }
73     if (!isSubtaskTestFile) {
74       return false;
75     }
76     String nameWithoutExtension = virtualFile.getNameWithoutExtension();
77     int stepMarkerStart = nameWithoutExtension.indexOf(EduNames.SUBTASK_MARKER);
78     int stepIndex = Integer.valueOf(nameWithoutExtension.substring(EduNames.SUBTASK_MARKER.length() + stepMarkerStart));
79     return stepIndex == myTask.getActiveSubtaskIndex();
80   }
81 }