new editing mode for task description panel
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / actions / StudyRefreshAnswerPlaceholder.java
1 package com.jetbrains.edu.learning.actions;
2
3 import com.intellij.icons.AllIcons;
4 import com.intellij.openapi.actionSystem.AnActionEvent;
5 import com.intellij.openapi.application.ApplicationManager;
6 import com.intellij.openapi.command.CommandProcessor;
7 import com.intellij.openapi.editor.Document;
8 import com.intellij.openapi.editor.Editor;
9 import com.intellij.openapi.project.DumbAwareAction;
10 import com.intellij.openapi.project.Project;
11 import com.intellij.openapi.util.TextRange;
12 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
13 import com.jetbrains.edu.learning.courseFormat.TaskFile;
14 import com.jetbrains.edu.learning.StudyState;
15 import com.jetbrains.edu.learning.StudyUtils;
16 import com.jetbrains.edu.learning.editor.StudyEditor;
17 import org.jetbrains.annotations.Nullable;
18
19 public class StudyRefreshAnswerPlaceholder extends DumbAwareAction {
20
21   public static final String NAME = "Refresh Answer Placeholder";
22
23   public StudyRefreshAnswerPlaceholder() {
24     super(NAME, NAME, AllIcons.Actions.Refresh);
25   }
26
27   @Override
28   public void actionPerformed(AnActionEvent e) {
29     Project project = e.getProject();
30     if (project == null) {
31       return;
32     }
33     final AnswerPlaceholder answerPlaceholder = getAnswerPlaceholder(e);
34     if (answerPlaceholder == null) {
35       return;
36     }
37     StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
38     final StudyState studyState = new StudyState(studyEditor);
39     Document patternDocument = StudyUtils.getPatternDocument(answerPlaceholder.getTaskFile(), studyState.getVirtualFile().getName());
40     if (patternDocument == null) {
41       return;
42     }
43     AnswerPlaceholder.MyInitialState initialState = answerPlaceholder.getInitialState();
44     int startOffset = patternDocument.getLineStartOffset(initialState.myLine) + initialState.myStart;
45     final String text = patternDocument.getText(new TextRange(startOffset, startOffset + initialState.myLength));
46     CommandProcessor.getInstance().executeCommand(project, new Runnable() {
47       @Override
48       public void run() {
49         ApplicationManager.getApplication().runWriteAction(new Runnable() {
50           @Override
51           public void run() {
52             Document document = studyState.getEditor().getDocument();
53             int offset = answerPlaceholder.getRealStartOffset(document);
54             document.deleteString(offset, offset + answerPlaceholder.getLength());
55             document.insertString(offset, text);
56           }
57         });
58       }
59     }, NAME, null);
60   }
61
62   @Override
63   public void update(AnActionEvent e) {
64     if (getAnswerPlaceholder(e) == null) {
65       e.getPresentation().setEnabledAndVisible(false);
66     }
67   }
68
69   @Nullable
70   private static AnswerPlaceholder getAnswerPlaceholder(AnActionEvent e) {
71     final Project project = e.getProject();
72     if (project == null) {
73       return null;
74     }
75     StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
76     final StudyState studyState = new StudyState(studyEditor);
77     if (studyEditor == null || !studyState.isValid()) {
78       return null;
79     }
80     final Editor editor = studyState.getEditor();
81     final TaskFile taskFile = studyState.getTaskFile();
82     return taskFile.getAnswerPlaceholder(editor.getDocument(), editor.getCaretModel().getLogicalPosition());
83   }
84 }