EDU-602 Disable Fill Answer Placeholders action for course creator projects
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / actions / StudyFillPlaceholdersAction.java
1 package com.jetbrains.edu.learning.actions;
2
3 import com.intellij.openapi.actionSystem.AnAction;
4 import com.intellij.openapi.actionSystem.AnActionEvent;
5 import com.intellij.openapi.actionSystem.Presentation;
6 import com.intellij.openapi.application.ApplicationManager;
7 import com.intellij.openapi.command.CommandProcessor;
8 import com.intellij.openapi.editor.Document;
9 import com.intellij.openapi.project.Project;
10 import com.jetbrains.edu.learning.StudyTaskManager;
11 import com.jetbrains.edu.learning.core.EduNames;
12 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
13 import com.jetbrains.edu.learning.courseFormat.Course;
14 import com.jetbrains.edu.learning.courseFormat.TaskFile;
15 import com.jetbrains.edu.learning.StudyState;
16 import com.jetbrains.edu.learning.StudyUtils;
17 import com.jetbrains.edu.learning.editor.StudyEditor;
18
19 public class StudyFillPlaceholdersAction extends AnAction {
20   @Override
21   public void actionPerformed(AnActionEvent e) {
22     final Project project = e.getProject();
23     if (project != null) {
24       StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
25       StudyState studyState = new StudyState(studyEditor);
26       if (!studyState.isValid()) {
27         return;
28       }
29       final TaskFile taskFile = studyState.getTaskFile();
30       final Document document = studyState.getEditor().getDocument();
31       CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
32         @Override
33         public void run() {
34           ApplicationManager.getApplication().runWriteAction(new Runnable() {
35             @Override
36             public void run() {
37               for (AnswerPlaceholder placeholder : taskFile.getAnswerPlaceholders()) {
38                 String answer = placeholder.getPossibleAnswer();
39                 if (answer == null) {
40                   continue;
41                 }
42                 int offset = placeholder.getRealStartOffset(document);
43                 document.deleteString(offset, offset + placeholder.getRealLength());
44                 document.insertString(offset, answer);
45               }
46             }
47           });
48         }
49       });
50     }
51   }
52
53   @Override
54   public void update(AnActionEvent e) {
55     StudyUtils.updateAction(e);
56     final Project project = e.getProject();
57     if (project != null) {
58
59       Course course = StudyTaskManager.getInstance(project).getCourse();
60       Presentation presentation = e.getPresentation();
61       if (course != null && !EduNames.STUDY.equals(course.getCourseMode())) {
62         presentation.setEnabled(false);
63         presentation.setVisible(true);
64         return;
65       }
66       StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
67       StudyState studyState = new StudyState(studyEditor);
68       if (!studyState.isValid()) {
69         presentation.setEnabledAndVisible(false);
70         return;
71       }
72       TaskFile taskFile = studyState.getTaskFile();
73       if (taskFile.getAnswerPlaceholders().isEmpty()) {
74         presentation.setEnabledAndVisible(false);
75       }
76     }
77   }
78 }