EDU-555 Add As Task File action should be undoable and revertable
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCAddAsTaskFile.java
1 package com.jetbrains.edu.coursecreator.actions;
2
3 import com.intellij.ide.projectView.ProjectView;
4 import com.intellij.openapi.command.undo.DocumentReference;
5 import com.intellij.openapi.command.undo.UndoableAction;
6 import com.intellij.openapi.command.undo.UnexpectedUndoException;
7 import com.intellij.openapi.project.Project;
8 import com.intellij.openapi.vfs.VirtualFile;
9 import com.jetbrains.edu.coursecreator.CCUtils;
10 import com.jetbrains.edu.learning.StudyUtils;
11 import com.jetbrains.edu.learning.core.EduUtils;
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 import org.jetbrains.annotations.Nullable;
16
17 public class CCAddAsTaskFile extends CCTaskFileActionBase {
18   public static final String ACTION_NAME = "Make Visible to Student";
19
20   public CCAddAsTaskFile() {
21     super(ACTION_NAME);
22   }
23
24
25   protected void performAction(VirtualFile file, Task task, Course course, Project project) {
26     EduUtils.runUndoableAction(project, ACTION_NAME, new AddTaskFile(file, null, course, project, task));
27   }
28
29   protected boolean isAvailable(Project project, VirtualFile file) {
30     return StudyUtils.getTaskFile(project, file) == null && !CCUtils.isTestsFile(project, file);
31   }
32
33   private static class AddTaskFile implements UndoableAction {
34     private final VirtualFile myFile;
35     private TaskFile myTaskFile;
36     private final Course myCourse;
37     private final Project myProject;
38     private final Task myTask;
39
40     public AddTaskFile(VirtualFile file, TaskFile taskFile, Course course, Project project, Task task) {
41       myFile = file;
42       myTaskFile = taskFile;
43       myCourse = course;
44       myProject = project;
45       myTask = task;
46     }
47
48     @Override
49     public void undo() throws UnexpectedUndoException {
50       CCHideFromStudent.hideFromStudent(myFile, myProject, myTask.getTaskFiles(), myTaskFile);
51       ProjectView.getInstance(myProject).refresh();
52     }
53
54     @Override
55     public void redo() throws UnexpectedUndoException {
56       if (myTaskFile != null) {
57         myTask.addTaskFile(myTaskFile);
58       } else {
59         myTask.addTaskFile(myFile.getName(), myTask.getTaskFiles().size());
60         myTaskFile = myTask.getTaskFile(myFile.getName());
61       }
62       CCUtils.createResourceFile(myFile, myCourse, StudyUtils.getTaskDir(myFile));
63       ProjectView.getInstance(myProject).refresh();
64     }
65
66     @Nullable
67     @Override
68     public DocumentReference[] getAffectedDocuments() {
69       return new DocumentReference[0];
70     }
71
72     @Override
73     public boolean isGlobal() {
74       return true;
75     }
76   }
77 }