new editing mode for task description panel
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / actions / CCEditTaskTextAction.java
1 package com.jetbrains.edu.coursecreator.actions;
2
3 import com.intellij.icons.AllIcons;
4 import com.intellij.openapi.actionSystem.AnActionEvent;
5 import com.intellij.openapi.actionSystem.ToggleAction;
6 import com.intellij.openapi.diagnostic.Logger;
7 import com.intellij.openapi.editor.Document;
8 import com.intellij.openapi.fileEditor.FileDocumentManager;
9 import com.intellij.openapi.project.DumbAware;
10 import com.intellij.openapi.project.Project;
11 import com.intellij.openapi.vfs.VirtualFile;
12 import com.jetbrains.edu.coursecreator.CCProjectService;
13 import com.jetbrains.edu.learning.StudyState;
14 import com.jetbrains.edu.learning.StudyTaskManager;
15 import com.jetbrains.edu.learning.StudyUtils;
16 import com.jetbrains.edu.learning.core.EduNames;
17 import com.jetbrains.edu.learning.editor.StudyEditor;
18 import com.jetbrains.edu.learning.ui.StudyToolWindow;
19 import org.jetbrains.annotations.NotNull;
20
21 public class CCEditTaskTextAction extends ToggleAction implements DumbAware {
22   private static final Logger LOG = Logger.getInstance(CCEditTaskTextAction.class);
23
24   public CCEditTaskTextAction() {
25     super("Editing Mode", "Editing Mode", AllIcons.Modules.Edit);
26   }
27
28   @Override
29   public boolean isSelected(AnActionEvent e) {
30     Project project = e.getProject();
31     if (project == null) {
32       return false;
33     }
34     return StudyTaskManager.getInstance(project).getToolWindowMode() == StudyToolWindow.StudyToolWindowMode.EDITING;
35   }
36
37   @Override
38   public void setSelected(AnActionEvent e, boolean state) {
39     Project project = e.getProject();
40     if (project == null) {
41       return;
42     }
43     StudyToolWindow window = StudyUtils.getStudyToolWindow(project);
44     if (window == null) {
45       return;
46     }
47
48     final StudyEditor selectedEditor = StudyUtils.getSelectedStudyEditor(project);
49     if (selectedEditor == null) {
50       StudyTaskManager.getInstance(project).setTurnEditingMode(true);
51       return;
52     }
53     final StudyState studyState = new StudyState(selectedEditor);
54     VirtualFile taskTextFile = studyState.getTaskDir().findChild(EduNames.TASK_HTML);
55     if (taskTextFile == null) {
56       LOG.info("Failed to find task.html");
57       return;
58     }
59     Document document = FileDocumentManager.getInstance().getDocument(taskTextFile);
60     if (!state) {
61       if (document != null) {
62         FileDocumentManager.getInstance().saveDocument(document);
63       }
64       window.leaveEditingMode(project);
65       return;
66     }
67     window.enterEditingMode(taskTextFile, project);
68   }
69
70   @Override
71   public void update(@NotNull AnActionEvent e) {
72     Project project = e.getProject();
73     if (project == null) {
74       return;
75     }
76
77     if (CCProjectService.getInstance(project).getCourse() == null) {
78       e.getPresentation().setEnabledAndVisible(false);
79     }
80   }
81 }