d4f39fbefd2f6358c29b48286e522b58f2006228
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / projectView / CCTreeStructureProvider.java
1 package com.jetbrains.edu.coursecreator.projectView;
2
3 import com.intellij.ide.projectView.TreeStructureProvider;
4 import com.intellij.ide.projectView.ViewSettings;
5 import com.intellij.ide.projectView.impl.nodes.PsiFileNode;
6 import com.intellij.ide.util.treeView.AbstractTreeNode;
7 import com.intellij.openapi.project.DumbAware;
8 import com.intellij.openapi.project.Project;
9 import com.intellij.openapi.vfs.VirtualFile;
10 import com.intellij.psi.PsiDirectory;
11 import com.jetbrains.edu.learning.core.EduNames;
12 import com.jetbrains.edu.coursecreator.CCProjectService;
13 import com.jetbrains.edu.coursecreator.CCUtils;
14 import org.jetbrains.annotations.NotNull;
15 import org.jetbrains.annotations.Nullable;
16
17 import java.util.ArrayList;
18 import java.util.Collection;
19
20 public class CCTreeStructureProvider implements TreeStructureProvider, DumbAware {
21   @NotNull
22   @Override
23   public Collection<AbstractTreeNode> modify(@NotNull AbstractTreeNode parent,
24                                              @NotNull Collection<AbstractTreeNode> children,
25                                              ViewSettings settings) {
26     if (!needModify(parent)) {
27       return children;
28     }
29     Collection<AbstractTreeNode> nodes = new ArrayList<AbstractTreeNode>();
30     for (AbstractTreeNode node : children) {
31       Project project = node.getProject();
32       if (project != null) {
33         if (node.getValue() instanceof PsiDirectory) {
34           String name = ((PsiDirectory)node.getValue()).getName();
35           if (CCUtils.GENERATED_FILES_FOLDER.equals(name)) {
36             continue;
37           }
38           PsiDirectory directory = (PsiDirectory)node.getValue();
39           nodes.add(new CCDirectoryNode(project, directory, settings));
40           continue;
41         }
42         if (node instanceof PsiFileNode) {
43           PsiFileNode fileNode = (PsiFileNode)node;
44           VirtualFile virtualFile = fileNode.getVirtualFile();
45           if (virtualFile == null) {
46             continue;
47           }
48           if (virtualFile.getName().contains(EduNames.WINDOWS_POSTFIX)) {
49             continue;
50           }
51
52           if (virtualFile.getParent().getName().contains(EduNames.TASK) && !CCProjectService.getInstance(project).isTaskFile(virtualFile)) {
53             nodes.add(new CCStudentInvisibleFileNode(project, ((PsiFileNode)node).getValue(), settings));
54             continue;
55           }
56         }
57         nodes.add(node);
58       }
59     }
60     return nodes;
61   }
62
63   private static boolean needModify(@NotNull final AbstractTreeNode parent) {
64     Project project = parent.getProject();
65     if (project != null) {
66       if (CCProjectService.getInstance(project).getCourse() == null) {
67         return false;
68       }
69     }
70     return true;
71   }
72
73   @Nullable
74   @Override
75   public Object getData(Collection<AbstractTreeNode> selected, String dataName) {
76     return null;
77   }
78 }