f794e69d0ea21bd2c45e182c206ca19e5cf37f98
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCHideFromStudent.java
1 package com.jetbrains.edu.coursecreator.actions;
2
3 import com.intellij.openapi.application.ApplicationManager;
4 import com.intellij.openapi.diagnostic.Logger;
5 import com.intellij.openapi.editor.Editor;
6 import com.intellij.openapi.fileEditor.FileEditor;
7 import com.intellij.openapi.fileEditor.FileEditorManager;
8 import com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorImpl;
9 import com.intellij.openapi.project.Project;
10 import com.intellij.openapi.vfs.VirtualFile;
11 import com.jetbrains.edu.learning.StudyUtils;
12 import com.jetbrains.edu.learning.courseFormat.Course;
13 import com.jetbrains.edu.learning.courseFormat.Task;
14 import com.jetbrains.edu.learning.courseFormat.TaskFile;
15
16 import java.io.IOException;
17 import java.util.Map;
18
19 public class CCHideFromStudent extends CCTaskFileActionBase {
20
21   private static final Logger LOG = Logger.getInstance(CCHideFromStudent.class);
22
23   @Override
24   protected void performAction(VirtualFile file, Task task, Course course, Project project) {
25     Map<String, TaskFile> taskFiles = task.getTaskFiles();
26     TaskFile taskFile = StudyUtils.getTaskFile(project, file);
27     if (taskFile == null) {
28       return;
29     }
30     if (!taskFile.getAnswerPlaceholders().isEmpty() && FileEditorManager.getInstance(project).isFileOpen(file)) {
31       for (FileEditor fileEditor : FileEditorManager.getInstance(project).getEditors(file)) {
32         if (fileEditor instanceof PsiAwareTextEditorImpl) {
33           Editor editor = ((PsiAwareTextEditorImpl)fileEditor).getEditor();
34           editor.getMarkupModel().removeAllHighlighters();
35         }
36       }
37     }
38     String name = file.getName();
39     VirtualFile patternFile = StudyUtils.getPatternFile(taskFile, name);
40     ApplicationManager.getApplication().runWriteAction(() -> {
41       if (patternFile != null) {
42         try {
43           patternFile.delete(CCHideFromStudent.class);
44         }
45         catch (IOException e) {
46           LOG.info(e);
47         }
48       }
49     });
50     taskFiles.remove(name);
51   }
52
53   @Override
54   protected boolean isAvailable(Project project, VirtualFile file) {
55     return StudyUtils.getTaskFile(project, file) != null;
56   }
57 }