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