part of EDU-331(improving lesson and task management)
[idea/community.git] / python / educational / course-creator / src / com / jetbrains / edu / coursecreator / projectView / CCDirectoryNode.java
1 package com.jetbrains.edu.coursecreator.projectView;
2
3 import com.intellij.ide.projectView.PresentationData;
4 import com.intellij.ide.projectView.ViewSettings;
5 import com.intellij.ide.projectView.impl.nodes.PsiDirectoryNode;
6 import com.intellij.openapi.project.Project;
7 import com.intellij.psi.PsiDirectory;
8 import com.intellij.ui.SimpleTextAttributes;
9 import com.jetbrains.edu.coursecreator.CCProjectService;
10 import com.jetbrains.edu.coursecreator.CCUtils;
11 import com.jetbrains.edu.coursecreator.format.Course;
12 import com.jetbrains.edu.coursecreator.format.Lesson;
13 import com.jetbrains.edu.coursecreator.format.Task;
14 import org.jetbrains.annotations.NotNull;
15
16 public class CCDirectoryNode extends PsiDirectoryNode {
17   private final PsiDirectory myValue;
18   private final Project myProject;
19
20   public CCDirectoryNode(@NotNull final Project project,
21                          PsiDirectory value,
22                          ViewSettings viewSettings) {
23     super(project, value, viewSettings);
24     myValue = value;
25     myProject = project;
26   }
27
28   @Override
29   protected void updateImpl(PresentationData data) {
30     //TODO:change presentable name for files with suffix _answer
31
32     String valueName = myValue.getName();
33     final Course course = CCProjectService.getInstance(myProject).getCourse();
34     if (course == null) return;
35     if (myProject.getBaseDir().equals(myValue.getVirtualFile())) {
36       data.clearText();
37       data.addText(valueName, SimpleTextAttributes.REGULAR_ATTRIBUTES);
38       data.addText(" (" + course.getName() + ")", SimpleTextAttributes.GRAYED_ATTRIBUTES);
39       return;
40     }
41     final Lesson lesson = course.getLesson(valueName);
42     if (lesson != null) {
43       data.clearText();
44       data.addText(valueName, SimpleTextAttributes.REGULAR_ATTRIBUTES);
45       data.addText(" (" + lesson.name + ")", SimpleTextAttributes.GRAYED_ATTRIBUTES);
46       return;
47     }
48     else {
49       final PsiDirectory parentDir = myValue.getParentDirectory();
50       if (parentDir != null) {
51         final Lesson parentLesson = course.getLesson(parentDir.getName());
52         if (parentLesson != null) {
53           final Task task = parentLesson.getTask(valueName);
54           if (task != null) {
55             data.clearText();
56             data.addText(valueName, SimpleTextAttributes.REGULAR_ATTRIBUTES);
57             data.addText(" (" + task.name + ")", SimpleTextAttributes.GRAYED_ATTRIBUTES);
58             return;
59           }
60         }
61       }
62     }
63     data.setPresentableText(valueName);
64   }
65
66   @Override
67   public int getTypeSortWeight(boolean sortByType) {
68     String name = myValue.getName();
69     String lessonDirName = "lesson";
70     String taskDirName = "task";
71     if (name.startsWith(lessonDirName) || name.startsWith(taskDirName)) {
72       String logicalName = name.contains(lessonDirName) ? lessonDirName : taskDirName;
73       int index = CCUtils.getIndex(name, logicalName) + 1;
74       return index != -1 ? index : 0;
75     }
76     return 0;
77   }
78 }