3ce85b39d7f8d81cbc04dc66080df4d5f4f89765
[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.ide.projectView.ProjectView;
4 import com.intellij.openapi.application.ApplicationManager;
5 import com.intellij.openapi.command.undo.DocumentReference;
6 import com.intellij.openapi.command.undo.UndoableAction;
7 import com.intellij.openapi.command.undo.UnexpectedUndoException;
8 import com.intellij.openapi.diagnostic.Logger;
9 import com.intellij.openapi.editor.Editor;
10 import com.intellij.openapi.fileEditor.FileEditor;
11 import com.intellij.openapi.fileEditor.FileEditorManager;
12 import com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorImpl;
13 import com.intellij.openapi.project.Project;
14 import com.intellij.openapi.vfs.VirtualFile;
15 import com.jetbrains.edu.coursecreator.CCUtils;
16 import com.jetbrains.edu.learning.StudyUtils;
17 import com.jetbrains.edu.learning.core.EduUtils;
18 import com.jetbrains.edu.learning.courseFormat.Course;
19 import com.jetbrains.edu.learning.courseFormat.Task;
20 import com.jetbrains.edu.learning.courseFormat.TaskFile;
21 import org.jetbrains.annotations.Nullable;
22
23 import java.io.IOException;
24 import java.util.Map;
25
26 public class CCHideFromStudent extends CCTaskFileActionBase {
27
28   private static final Logger LOG = Logger.getInstance(CCHideFromStudent.class);
29   private static final String ACTION_NAME = "Hide from Student";
30
31   public CCHideFromStudent() {
32     super(ACTION_NAME);
33   }
34
35   @Override
36   protected void performAction(VirtualFile file, Task task, Course course, Project project) {
37     TaskFile taskFile = StudyUtils.getTaskFile(project, file);
38     if (taskFile == null) {
39       return;
40     }
41     EduUtils.runUndoableAction(project, ACTION_NAME, new HideTaskFile(project, course, file, task, taskFile));
42   }
43
44   private static class HideTaskFile implements UndoableAction {
45
46     private final Project myProject;
47     private final Course myCourse;
48     private final VirtualFile myFile;
49     private final Task myTask;
50     private final TaskFile myTaskFile;
51
52     public HideTaskFile(Project project, Course course, VirtualFile file, Task task, TaskFile taskFile) {
53       myProject = project;
54       myCourse = course;
55       myFile = file;
56       myTask = task;
57       myTaskFile = taskFile;
58     }
59
60     @Override
61     public void undo() throws UnexpectedUndoException {
62       myTask.getTaskFiles().put(myFile.getName(), myTaskFile);
63       CCUtils.createResourceFile(myFile, myCourse, StudyUtils.getTaskDir(myFile));
64       if (!myTaskFile.getAnswerPlaceholders().isEmpty() && FileEditorManager.getInstance(myProject).isFileOpen(myFile)) {
65         for (FileEditor fileEditor : FileEditorManager.getInstance(myProject).getEditors(myFile)) {
66           if (fileEditor instanceof PsiAwareTextEditorImpl) {
67             Editor editor = ((PsiAwareTextEditorImpl)fileEditor).getEditor();
68             StudyUtils.drawAllWindows(editor, myTaskFile);
69           }
70         }
71       }
72       ProjectView.getInstance(myProject).refresh();
73     }
74
75     @Override
76     public void redo() throws UnexpectedUndoException {
77       hideFromStudent(myFile, myProject, myTask.getTaskFiles(), myTaskFile);
78       ProjectView.getInstance(myProject).refresh();
79     }
80
81     @Nullable
82     @Override
83     public DocumentReference[] getAffectedDocuments() {
84       return new DocumentReference[0];
85     }
86
87     @Override
88     public boolean isGlobal() {
89       return true;
90     }
91   }
92
93   public static void hideFromStudent(VirtualFile file, Project project, Map<String, TaskFile> taskFiles, TaskFile taskFile) {
94     if (!taskFile.getAnswerPlaceholders().isEmpty() && FileEditorManager.getInstance(project).isFileOpen(file)) {
95       for (FileEditor fileEditor : FileEditorManager.getInstance(project).getEditors(file)) {
96         if (fileEditor instanceof PsiAwareTextEditorImpl) {
97           Editor editor = ((PsiAwareTextEditorImpl)fileEditor).getEditor();
98           editor.getMarkupModel().removeAllHighlighters();
99         }
100       }
101     }
102     String name = file.getName();
103     VirtualFile patternFile = StudyUtils.getPatternFile(taskFile, name);
104     ApplicationManager.getApplication().runWriteAction(() -> {
105       if (patternFile != null) {
106         try {
107           patternFile.delete(CCHideFromStudent.class);
108         }
109         catch (IOException e) {
110           LOG.info(e);
111         }
112       }
113     });
114     taskFiles.remove(name);
115   }
116
117   @Override
118   protected boolean isAvailable(Project project, VirtualFile file) {
119     return StudyUtils.getTaskFile(project, file) != null;
120   }
121 }