use util method
[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.editor.Document;
7 import com.intellij.openapi.project.Project;
8 import com.jetbrains.edu.learning.StudyState;
9 import com.jetbrains.edu.learning.StudyTaskManager;
10 import com.jetbrains.edu.learning.StudyUtils;
11 import com.jetbrains.edu.learning.core.EduNames;
12 import com.jetbrains.edu.learning.core.EduUtils;
13 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
14 import com.jetbrains.edu.learning.courseFormat.Course;
15 import com.jetbrains.edu.learning.courseFormat.TaskFile;
16 import com.jetbrains.edu.learning.editor.StudyEditor;
17
18 public class StudyFillPlaceholdersAction extends AnAction {
19   @Override
20   public void actionPerformed(AnActionEvent e) {
21     final Project project = e.getProject();
22     if (project != null) {
23       StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
24       StudyState studyState = new StudyState(studyEditor);
25       if (!studyState.isValid()) {
26         return;
27       }
28       TaskFile taskFile = studyState.getTaskFile();
29       final Document document = studyState.getEditor().getDocument();
30       for (AnswerPlaceholder placeholder : taskFile.getActivePlaceholders()) {
31         String answer = placeholder.getPossibleAnswer();
32         if (answer == null) {
33           continue;
34         }
35         EduUtils.replaceAnswerPlaceholder(project, document, placeholder, placeholder.getRealLength(), answer);
36       }
37     }
38   }
39
40   @Override
41   public void update(AnActionEvent e) {
42     StudyUtils.updateAction(e);
43     final Project project = e.getProject();
44     if (project != null) {
45
46       Course course = StudyTaskManager.getInstance(project).getCourse();
47       Presentation presentation = e.getPresentation();
48       if (course != null && !EduNames.STUDY.equals(course.getCourseMode())) {
49         presentation.setEnabled(false);
50         presentation.setVisible(true);
51         return;
52       }
53       StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
54       StudyState studyState = new StudyState(studyEditor);
55       if (!studyState.isValid()) {
56         presentation.setEnabledAndVisible(false);
57         return;
58       }
59       TaskFile taskFile = studyState.getTaskFile();
60       if (taskFile.getActivePlaceholders().isEmpty()) {
61         presentation.setEnabledAndVisible(false);
62       }
63     }
64   }
65 }