48425e247100938014dc46891a205968394bb1b2
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCAnswerPlaceholderAction.java
1 package com.jetbrains.edu.coursecreator.actions;
2
3 import com.intellij.openapi.actionSystem.AnActionEvent;
4 import com.intellij.openapi.actionSystem.CommonDataKeys;
5 import com.intellij.openapi.actionSystem.Presentation;
6 import com.intellij.openapi.editor.Editor;
7 import com.intellij.openapi.project.DumbAwareAction;
8 import com.intellij.openapi.project.Project;
9 import com.intellij.openapi.vfs.VirtualFile;
10 import com.intellij.psi.PsiFile;
11 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
12 import com.jetbrains.edu.learning.courseFormat.TaskFile;
13 import com.jetbrains.edu.coursecreator.CCProjectService;
14 import org.jetbrains.annotations.NotNull;
15 import org.jetbrains.annotations.Nullable;
16
17 import javax.swing.*;
18
19 abstract public class CCAnswerPlaceholderAction extends DumbAwareAction {
20
21   protected CCAnswerPlaceholderAction(@Nullable String text, @Nullable String description, @Nullable Icon icon) {
22     super(text, description, icon);
23   }
24
25   @Nullable
26   private static CCState getState(@NotNull AnActionEvent e) {
27     final Project project = e.getProject();
28     if (project == null || CCProjectService.getInstance(project).getCourse() == null) {
29       return null;
30     }
31     final PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(e.getDataContext());
32     if (psiFile == null) {
33       return null;
34     }
35     VirtualFile virtualFile = psiFile.getVirtualFile();
36     if (virtualFile == null) {
37       return null;
38     }
39     final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
40     if (editor == null) {
41       return null;
42     }
43     TaskFile taskFile = CCProjectService.getInstance(project).getTaskFile(virtualFile);
44     if (taskFile == null) {
45       return null;
46     }
47     AnswerPlaceholder answerPlaceholder = taskFile.getAnswerPlaceholder(editor.getDocument(),
48                                                                         editor.getCaretModel().getLogicalPosition(),
49                                                                         true);
50     if (answerPlaceholder == null) {
51       return null;
52     }
53     return new CCState(taskFile, answerPlaceholder, psiFile, editor, project);
54   }
55
56   @Override
57   public void update(@NotNull AnActionEvent e) {
58     Presentation presentation = e.getPresentation();
59     boolean isAvailable = getState(e) != null;
60     presentation.setEnabled(isAvailable);
61     presentation.setVisible(isAvailable);
62   }
63
64   @Override
65   public void actionPerformed(@NotNull AnActionEvent e) {
66     CCState state = getState(e);
67     if (state == null) {
68       return;
69     }
70     performAnswerPlaceholderAction(state);
71   }
72
73   protected abstract void performAnswerPlaceholderAction(@NotNull final CCState state);
74
75   protected static class CCState {
76     private TaskFile myTaskFile;
77     private AnswerPlaceholder myAnswerPlaceholder;
78     private PsiFile myFile;
79     private Editor myEditor;
80     private Project myProject;
81
82     public CCState(@NotNull final TaskFile taskFile,
83                    @NotNull final AnswerPlaceholder answerPlaceholder,
84                    @NotNull final PsiFile file,
85                    @NotNull final Editor editor,
86                    @NotNull final Project project) {
87       myTaskFile = taskFile;
88       myAnswerPlaceholder = answerPlaceholder;
89       myFile = file;
90       myEditor = editor;
91       myProject = project;
92     }
93
94     @NotNull
95     public TaskFile getTaskFile() {
96       return myTaskFile;
97     }
98
99     @NotNull
100     public AnswerPlaceholder getAnswerPlaceholder() {
101       return myAnswerPlaceholder;
102     }
103
104     @NotNull
105     public PsiFile getFile() {
106       return myFile;
107     }
108
109     @NotNull
110     public Editor getEditor() {
111       return myEditor;
112     }
113
114     @NotNull
115     public Project getProject() {
116       return myProject;
117     }
118   }
119 }