551d344e37721c0a589ec5e283ac4c34ed8b31c7
[idea/community.git] / python / educational-core / 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.learning.core.EduNames;
10 import com.jetbrains.edu.learning.core.EduUtils;
11 import com.jetbrains.edu.learning.courseFormat.Course;
12 import com.jetbrains.edu.learning.courseFormat.Lesson;
13 import com.jetbrains.edu.learning.courseFormat.Task;
14 import com.jetbrains.edu.coursecreator.CCProjectService;
15 import icons.InteractiveLearningIcons;
16 import org.jetbrains.annotations.NotNull;
17
18 public class CCDirectoryNode extends PsiDirectoryNode {
19   private final PsiDirectory myValue;
20   private final Project myProject;
21
22   public CCDirectoryNode(@NotNull final Project project,
23                          PsiDirectory value,
24                          ViewSettings viewSettings) {
25     super(project, value, viewSettings);
26     myValue = value;
27     myProject = project;
28   }
29
30   @Override
31   protected void updateImpl(PresentationData data) {
32     String valueName = myValue.getName();
33     final CCProjectService service = CCProjectService.getInstance(myProject);
34     final Course course = service.getCourse();
35     if (course == null) return;
36     if (myProject.getBaseDir().equals(myValue.getVirtualFile())) {
37       data.clearText();
38       data.setIcon(InteractiveLearningIcons.Course);
39       data.addText(course.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
40       data.addText(" (" + valueName + ")", SimpleTextAttributes.GRAYED_ATTRIBUTES);
41       return;
42     }
43     final Lesson lesson = course.getLesson(valueName);
44     if (lesson != null) {
45       data.clearText();
46       data.setIcon(InteractiveLearningIcons.Lesson);
47       data.addText(lesson.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
48       return;
49     }
50     else {
51       final PsiDirectory parentDir = myValue.getParentDirectory();
52       if (parentDir != null) {
53         final Lesson parentLesson = course.getLesson(parentDir.getName());
54         if (parentLesson != null) {
55           final Task task = parentLesson.getTask(valueName);
56           if (task != null) {
57             data.clearText();
58             data.setIcon(InteractiveLearningIcons.Task);
59             data.addText(task.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
60             return;
61           }
62         }
63       }
64     }
65     data.setPresentableText(valueName);
66   }
67
68   @Override
69   public int getTypeSortWeight(boolean sortByType) {
70     String name = myValue.getName();
71     if (name.startsWith(EduNames.LESSON) || name.startsWith(EduNames.TASK)) {
72       String logicalName = name.contains(EduNames.LESSON) ? EduNames.LESSON : EduNames.TASK;
73       int index = EduUtils.getIndex(name, logicalName) + 1;
74       return index != -1 ? index + 1: 0;
75     }
76     return 0;
77   }
78
79   @Override
80   public String getNavigateActionText(boolean focusEditor) {
81     return null;
82   }
83 }