Fix EDU-780 Null task text for adaptive courses loaded from cache
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / StudyUtils.java
1 package com.jetbrains.edu.learning;
2
3 import com.intellij.execution.RunContentExecutor;
4 import com.intellij.execution.configurations.GeneralCommandLine;
5 import com.intellij.execution.process.ProcessHandler;
6 import com.intellij.lang.Language;
7 import com.intellij.openapi.actionSystem.AnActionEvent;
8 import com.intellij.openapi.actionSystem.CommonDataKeys;
9 import com.intellij.openapi.actionSystem.DataContext;
10 import com.intellij.openapi.actionSystem.Presentation;
11 import com.intellij.openapi.application.ApplicationManager;
12 import com.intellij.openapi.diagnostic.Logger;
13 import com.intellij.openapi.editor.Document;
14 import com.intellij.openapi.editor.Editor;
15 import com.intellij.openapi.editor.RangeMarker;
16 import com.intellij.openapi.editor.actionSystem.EditorActionManager;
17 import com.intellij.openapi.editor.colors.EditorColors;
18 import com.intellij.openapi.editor.colors.EditorColorsManager;
19 import com.intellij.openapi.editor.impl.DocumentImpl;
20 import com.intellij.openapi.fileEditor.FileDocumentManager;
21 import com.intellij.openapi.fileEditor.FileEditor;
22 import com.intellij.openapi.fileEditor.FileEditorManager;
23 import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx;
24 import com.intellij.openapi.fileEditor.impl.LoadTextUtil;
25 import com.intellij.openapi.progress.ProgressManager;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.openapi.project.ProjectManager;
28 import com.intellij.openapi.projectRoots.Sdk;
29 import com.intellij.openapi.ui.MessageType;
30 import com.intellij.openapi.ui.popup.Balloon;
31 import com.intellij.openapi.ui.popup.JBPopupFactory;
32 import com.intellij.openapi.util.Disposer;
33 import com.intellij.openapi.util.io.FileUtil;
34 import com.intellij.openapi.util.text.StringUtil;
35 import com.intellij.openapi.vfs.LocalFileSystem;
36 import com.intellij.openapi.vfs.VfsUtil;
37 import com.intellij.openapi.vfs.VirtualFile;
38 import com.intellij.openapi.wm.IdeFocusManager;
39 import com.intellij.openapi.wm.ToolWindow;
40 import com.intellij.openapi.wm.ToolWindowAnchor;
41 import com.intellij.openapi.wm.ToolWindowManager;
42 import com.intellij.psi.PsiDirectory;
43 import com.intellij.psi.PsiElement;
44 import com.intellij.psi.PsiFile;
45 import com.intellij.ui.JBColor;
46 import com.intellij.ui.awt.RelativePoint;
47 import com.intellij.ui.content.Content;
48 import com.intellij.util.ObjectUtils;
49 import com.intellij.util.TimeoutUtil;
50 import com.intellij.util.containers.ContainerUtil;
51 import com.intellij.util.text.MarkdownUtil;
52 import com.intellij.util.ui.UIUtil;
53 import com.jetbrains.edu.learning.checker.StudyExecutor;
54 import com.jetbrains.edu.learning.checker.StudyTestRunner;
55 import com.jetbrains.edu.learning.core.EduAnswerPlaceholderDeleteHandler;
56 import com.jetbrains.edu.learning.core.EduAnswerPlaceholderPainter;
57 import com.jetbrains.edu.learning.core.EduNames;
58 import com.jetbrains.edu.learning.core.EduUtils;
59 import com.jetbrains.edu.learning.courseFormat.*;
60 import com.jetbrains.edu.learning.courseGeneration.StudyProjectGenerator;
61 import com.jetbrains.edu.learning.editor.StudyEditor;
62 import com.jetbrains.edu.learning.ui.StudyToolWindow;
63 import com.jetbrains.edu.learning.ui.StudyToolWindowFactory;
64 import com.petebevin.markdown.MarkdownProcessor;
65 import org.jetbrains.annotations.NotNull;
66 import org.jetbrains.annotations.Nullable;
67
68 import javax.swing.*;
69 import java.awt.*;
70 import java.io.*;
71 import java.util.ArrayList;
72 import java.util.Collection;
73 import java.util.Iterator;
74 import java.util.List;
75 import java.util.concurrent.Callable;
76 import java.util.concurrent.ExecutionException;
77 import java.util.concurrent.Future;
78
79 public class StudyUtils {
80   private StudyUtils() {
81   }
82
83   private static final Logger LOG = Logger.getInstance(StudyUtils.class.getName());
84   private static final String EMPTY_TASK_TEXT = "Please, open any task to see task description";
85   private static final String ourPrefix = "<html><head><script type=\"text/x-mathjax-config\">\n" +
86                                           "            MathJax.Hub.Config({\n" +
87                                           "                tex2jax: {\n" +
88                                           "                    inlineMath: [ ['$','$'], [\"\\\\(\",\"\\\\)\"] ],\n" +
89                                           "                    displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"] ],\n" +
90                                           "                    processEscapes: true,\n" +
91                                           "                    processEnvironments: true\n" +
92                                           "                },\n" +
93                                           "                displayAlign: 'center',\n" +
94                                           "                \"HTML-CSS\": {\n" +
95                                           "                    styles: {'#mydiv': {\"font-size\": %s}},\n" +
96                                           "                    preferredFont: null,\n" +
97                                           "                    linebreaks: { automatic: true }\n" +
98                                           "                }\n" +
99                                           "            });\n" +
100                                           "</script><script type=\"text/javascript\"\n" +
101                                           " src=\"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full\">\n" +
102                                           " </script></head><body><div id=\"mydiv\">";
103
104   private static final String ourPostfix = "</div></body></html>";
105
106   public static void closeSilently(@Nullable final Closeable stream) {
107     if (stream != null) {
108       try {
109         stream.close();
110       }
111       catch (IOException e) {
112         // close silently
113       }
114     }
115   }
116
117   public static boolean isZip(String fileName) {
118     return fileName.contains(".zip");
119   }
120
121   @Nullable
122   public static <T> T getFirst(@NotNull final Iterable<T> container) {
123     Iterator<T> iterator = container.iterator();
124     if (!iterator.hasNext()) {
125       return null;
126     }
127     return iterator.next();
128   }
129
130   public static boolean indexIsValid(int index, @NotNull final Collection collection) {
131     int size = collection.size();
132     return index >= 0 && index < size;
133   }
134
135   @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
136   @Nullable
137   public static String getFileText(@Nullable final String parentDir, @NotNull final String fileName, boolean wrapHTML,
138                                    @NotNull final String encoding) {
139     final File inputFile = parentDir != null ? new File(parentDir, fileName) : new File(fileName);
140     if (!inputFile.exists()) return null;
141     final StringBuilder taskText = new StringBuilder();
142     BufferedReader reader = null;
143     try {
144       reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), encoding));
145       String line;
146       while ((line = reader.readLine()) != null) {
147         taskText.append(line).append("\n");
148         if (wrapHTML) {
149           taskText.append("<br>");
150         }
151       }
152       return wrapHTML ? UIUtil.toHtml(taskText.toString()) : taskText.toString();
153     }
154     catch (IOException e) {
155       LOG.info("Failed to get file text from file " + fileName, e);
156     }
157     finally {
158       closeSilently(reader);
159     }
160     return null;
161   }
162
163   public static void updateAction(@NotNull final AnActionEvent e) {
164     final Presentation presentation = e.getPresentation();
165     presentation.setEnabled(false);
166     final Project project = e.getProject();
167     if (project != null) {
168       final StudyEditor studyEditor = getSelectedStudyEditor(project);
169       if (studyEditor != null) {
170         presentation.setEnabledAndVisible(true);
171       }
172     }
173   }
174
175   public static void updateToolWindows(@NotNull final Project project) {
176     final StudyToolWindow studyToolWindow = getStudyToolWindow(project);
177     if (studyToolWindow != null) {
178       String taskText = getTaskText(project);
179       if (taskText != null) {
180         studyToolWindow.setTaskText(taskText, null, project);
181       }
182       else {
183         LOG.warn("Task text is null");
184       }
185       studyToolWindow.updateCourseProgress(project);
186     }
187   }
188
189   public static void initToolWindows(@NotNull final Project project) {
190     final ToolWindowManager windowManager = ToolWindowManager.getInstance(project);
191     windowManager.getToolWindow(StudyToolWindowFactory.STUDY_TOOL_WINDOW).getContentManager().removeAllContents(false);
192     StudyToolWindowFactory factory = new StudyToolWindowFactory();
193     factory.createToolWindowContent(project, windowManager.getToolWindow(StudyToolWindowFactory.STUDY_TOOL_WINDOW));
194
195   }
196
197   @Nullable
198   public static StudyToolWindow getStudyToolWindow(@NotNull final Project project) {
199     if (project.isDisposed()) return null;
200     
201     ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(StudyToolWindowFactory.STUDY_TOOL_WINDOW);
202     if (toolWindow != null) {
203       Content[] contents = toolWindow.getContentManager().getContents();
204       for (Content content: contents) {
205         JComponent component = content.getComponent();
206         if (component != null && component instanceof StudyToolWindow) {
207           return (StudyToolWindow)component;
208         }
209       }
210     }
211     return null;
212   }
213
214   public static void deleteFile(@NotNull final VirtualFile file) {
215     try {
216       file.delete(StudyUtils.class);
217     }
218     catch (IOException e) {
219       LOG.error(e);
220     }
221   }
222
223   public static File copyResourceFile(@NotNull final String sourceName, @NotNull final String copyName, @NotNull final Project project,
224                                       @NotNull final Task task)
225     throws IOException {
226     final StudyTaskManager taskManager = StudyTaskManager.getInstance(project);
227     final Course course = taskManager.getCourse();
228     int taskNum = task.getIndex();
229     int lessonNum = task.getLesson().getIndex();
230     assert course != null;
231     final String pathToResource = FileUtil.join(course.getCourseDirectory(), EduNames.LESSON + lessonNum, EduNames.TASK + taskNum);
232     final File resourceFile = new File(pathToResource, copyName);
233     FileUtil.copy(new File(pathToResource, sourceName), resourceFile);
234     return resourceFile;
235   }
236
237   @Nullable
238   public static Sdk findSdk(@NotNull final Task task, @NotNull final Project project) {
239     final Language language = task.getLesson().getCourse().getLanguageById();
240     return StudyExecutor.INSTANCE.forLanguage(language).findSdk(project);
241   }
242
243   @NotNull
244   public static StudyTestRunner getTestRunner(@NotNull final Task task, @NotNull final VirtualFile taskDir) {
245     final Language language = task.getLesson().getCourse().getLanguageById();
246     return StudyExecutor.INSTANCE.forLanguage(language).getTestRunner(task, taskDir);
247   }
248
249   public static RunContentExecutor getExecutor(@NotNull final Project project, @NotNull final Task currentTask,
250                                                @NotNull final ProcessHandler handler) {
251     final Language language = currentTask.getLesson().getCourse().getLanguageById();
252     return StudyExecutor.INSTANCE.forLanguage(language).getExecutor(project, handler);
253   }
254
255   public static void setCommandLineParameters(@NotNull final GeneralCommandLine cmd,
256                                               @NotNull final Project project,
257                                               @NotNull final String filePath,
258                                               @NotNull final String sdkPath,
259                                               @NotNull final Task currentTask) {
260     final Language language = currentTask.getLesson().getCourse().getLanguageById();
261     StudyExecutor.INSTANCE.forLanguage(language).setCommandLineParameters(cmd, project, filePath, sdkPath, currentTask);
262   }
263
264   public static void showNoSdkNotification(@NotNull final Task currentTask, @NotNull final Project project) {
265     final Language language = currentTask.getLesson().getCourse().getLanguageById();
266     StudyExecutor.INSTANCE.forLanguage(language).showNoSdkNotification(project);
267   }
268
269
270   /**
271    * shows pop up in the center of "check task" button in study editor
272    */
273   public static void showCheckPopUp(@NotNull final Project project, @NotNull final Balloon balloon) {
274     final StudyEditor studyEditor = getSelectedStudyEditor(project);
275     assert studyEditor != null;
276
277     balloon.show(computeLocation(studyEditor.getEditor()), Balloon.Position.above);
278     Disposer.register(project, balloon);
279   }
280
281   public static RelativePoint computeLocation(Editor editor){
282
283     final Rectangle visibleRect = editor.getComponent().getVisibleRect();
284     Point point = new Point(visibleRect.x + visibleRect.width + 10,
285                             visibleRect.y + 10);
286     return new RelativePoint(editor.getComponent(), point);
287   }
288
289
290   /**
291    * returns language manager which contains all the information about language specific file names
292    */
293   @Nullable
294   public static StudyLanguageManager getLanguageManager(@NotNull final Course course) {
295     Language language = course.getLanguageById();
296     return language == null ? null : StudyLanguageManager.INSTANCE.forLanguage(language);
297   }
298
299   public static boolean isTestsFile(@NotNull Project project, @NotNull final String name) {
300     Course course = StudyTaskManager.getInstance(project).getCourse();
301     if (course == null) {
302       return false;
303     }
304     StudyLanguageManager manager = getLanguageManager(course);
305     if (manager == null) {
306       return false;
307     }
308     return manager.getTestFileName().equals(name);
309   }
310
311   @Nullable
312   public static TaskFile getTaskFile(@NotNull final Project project, @NotNull final VirtualFile file) {
313     final Course course = StudyTaskManager.getInstance(project).getCourse();
314     if (course == null) {
315       return null;
316     }
317     VirtualFile taskDir = file.getParent();
318     if (taskDir == null) {
319       return null;
320     }
321     //need this because of multi-module generation
322     if (EduNames.SRC.equals(taskDir.getName())) {
323       taskDir = taskDir.getParent();
324       if (taskDir == null) {
325         return null;
326       }
327     }
328     final String taskDirName = taskDir.getName();
329     if (taskDirName.contains(EduNames.TASK)) {
330       final VirtualFile lessonDir = taskDir.getParent();
331       if (lessonDir != null) {
332         int lessonIndex = EduUtils.getIndex(lessonDir.getName(), EduNames.LESSON);
333         List<Lesson> lessons = course.getLessons();
334         if (!indexIsValid(lessonIndex, lessons)) {
335           return null;
336         }
337         final Lesson lesson = lessons.get(lessonIndex);
338         int taskIndex = EduUtils.getIndex(taskDirName, EduNames.TASK);
339         final List<Task> tasks = lesson.getTaskList();
340         if (!indexIsValid(taskIndex, tasks)) {
341           return null;
342         }
343         final Task task = tasks.get(taskIndex);
344         return task.getFile(file.getName());
345       }
346     }
347     return null;
348   }
349
350   public static void drawAllWindows(Editor editor, TaskFile taskFile) {
351     editor.getMarkupModel().removeAllHighlighters();
352     final Project project = editor.getProject();
353     if (project == null) return;
354     final StudyTaskManager taskManager = StudyTaskManager.getInstance(project);
355     for (AnswerPlaceholder answerPlaceholder : taskFile.getAnswerPlaceholders()) {
356       final JBColor color = taskManager.getColor(answerPlaceholder);
357       EduAnswerPlaceholderPainter.drawAnswerPlaceholder(editor, answerPlaceholder, color);
358     }
359     final Document document = editor.getDocument();
360     EditorActionManager.getInstance()
361       .setReadonlyFragmentModificationHandler(document, new EduAnswerPlaceholderDeleteHandler(editor));
362     EduAnswerPlaceholderPainter.createGuardedBlocks(editor, taskFile);
363     editor.getColorsScheme().setColor(EditorColors.READONLY_FRAGMENT_BACKGROUND_COLOR, null);
364   }
365
366   @Nullable
367   public static StudyEditor getSelectedStudyEditor(@NotNull final Project project) {
368     try {
369       final FileEditor fileEditor = FileEditorManagerEx.getInstanceEx(project).getSplitters().getCurrentWindow().
370         getSelectedEditor().getSelectedEditorWithProvider().getFirst();
371       if (fileEditor instanceof StudyEditor) {
372         return (StudyEditor)fileEditor;
373       }
374     }
375     catch (Exception e) {
376       return null;
377     }
378     return null;
379   }
380
381   @Nullable
382   public static Editor getSelectedEditor(@NotNull final Project project) {
383     final StudyEditor studyEditor = getSelectedStudyEditor(project);
384     if (studyEditor != null) {
385       return studyEditor.getEditor();
386     }
387     return null;
388   }
389
390   public static void deleteGuardedBlocks(@NotNull final Document document) {
391     if (document instanceof DocumentImpl) {
392       final DocumentImpl documentImpl = (DocumentImpl)document;
393       List<RangeMarker> blocks = documentImpl.getGuardedBlocks();
394       for (final RangeMarker block : blocks) {
395         ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().runWriteAction(() -> document.removeGuardedBlock(block)));
396       }
397     }
398   }
399
400
401   @Nullable
402   public static VirtualFile getPatternFile(@NotNull TaskFile taskFile, String name) {
403     Task task = taskFile.getTask();
404     String lessonDir = EduNames.LESSON + String.valueOf(task.getLesson().getIndex());
405     String taskDir = EduNames.TASK + String.valueOf(task.getIndex());
406     Course course = task.getLesson().getCourse();
407     File resourceFile = new File(course.getCourseDirectory());
408     if (!resourceFile.exists()) {
409       return null;
410     }
411     String patternPath = FileUtil.join(resourceFile.getPath(), lessonDir, taskDir, name);
412     VirtualFile patternFile = VfsUtil.findFileByIoFile(new File(patternPath), true);
413     if (patternFile == null) {
414       return null;
415     }
416     return patternFile;
417   }
418
419   @Nullable
420   public static Document getPatternDocument(@NotNull final TaskFile taskFile, String name) {
421     VirtualFile patternFile = getPatternFile(taskFile, name);
422     if (patternFile == null) {
423       return null;
424     }
425     return FileDocumentManager.getInstance().getDocument(patternFile);
426   }
427
428   public static boolean isRenameableOrMoveable(@NotNull final Project project, @NotNull final Course course, @NotNull final PsiElement element) {
429     if (element instanceof PsiFile) {
430       VirtualFile virtualFile = ((PsiFile)element).getVirtualFile();
431       if (project.getBaseDir().equals(virtualFile.getParent())) {
432         return false;
433       }
434       TaskFile file = getTaskFile(project, virtualFile);
435       if (file != null) {
436         return false;
437       }
438       String name = virtualFile.getName();
439       return !isTestsFile(project, name) && !isTaskDescriptionFile(name);
440     }
441     if (element instanceof PsiDirectory) {
442       VirtualFile virtualFile = ((PsiDirectory)element).getVirtualFile();
443       VirtualFile parent = virtualFile.getParent();
444       if (parent == null) {
445         return true;
446       }
447       if (project.getBaseDir().equals(parent)) {
448         return false;
449       }
450       Lesson lesson = course.getLesson(parent.getName());
451       if (lesson != null) {
452         Task task = lesson.getTask(virtualFile.getName());
453         if (task != null) {
454           return false;
455         }
456       }
457     }
458     return true;
459   }
460
461   public static boolean canRenameOrMove(DataContext dataContext) {
462     Project project = CommonDataKeys.PROJECT.getData(dataContext);
463     PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
464     if (element == null || project == null) {
465       return false;
466     }
467     Course course = StudyTaskManager.getInstance(project).getCourse();
468     if (course == null || !EduNames.STUDY.equals(course.getCourseMode())) {
469       return false;
470     }
471
472     if (!isRenameableOrMoveable(project, course, element)) {
473       return true;
474     }
475     return false;
476   }
477
478   @Nullable
479   public static String getTaskTextFromTask(@Nullable final VirtualFile taskDirectory, @Nullable final Task task) {
480     if (task == null || task.getLesson() == null || task.getLesson().getCourse() == null) {
481       return null;
482     }
483     final Course course = task.getLesson().getCourse();
484     String text = task.getText() != null ? task.getText() : getTaskTextFromTaskName(taskDirectory, EduNames.TASK_MD);
485     
486     if (text == null) return null;
487     if (course.isAdaptive()) text = wrapAdaptiveCourseText(text);
488
489     return wrapTextToDisplayLatex(text);
490   }
491
492   private static String wrapAdaptiveCourseText(@NotNull String text) {
493     return text + "\n\n<b>Note</b>: Use standard input to obtain input for the task.";
494   }
495
496   public static String wrapTextToDisplayLatex(String taskTextFileHtml) {
497     final String prefix = String.format(ourPrefix, EditorColorsManager.getInstance().getGlobalScheme().getEditorFontSize());
498     return prefix + taskTextFileHtml + ourPostfix;
499   }
500
501   @Nullable
502   private static String getTaskTextFromTaskName(@Nullable VirtualFile taskDirectory, @NotNull String taskTextFilename) {
503     if (taskDirectory == null) return null;
504     taskDirectory.refresh(false, true);
505     VirtualFile taskTextFile = ObjectUtils.chooseNotNull(taskDirectory.findChild(EduNames.TASK_HTML), 
506                                                          taskDirectory.findChild(EduNames.TASK_MD));
507     if (taskTextFile == null) {
508       VirtualFile srcDir = taskDirectory.findChild(EduNames.SRC);
509       if (srcDir != null) {
510          taskTextFile = srcDir.findChild(taskTextFilename);
511       }
512     }
513     if (taskTextFile != null) {
514       return String.valueOf(LoadTextUtil.loadText(taskTextFile));
515     }
516     return null;
517   }
518
519   @Nullable
520   public static StudyPluginConfigurator getConfigurator(@NotNull final Project project) {
521     StudyPluginConfigurator[] extensions = StudyPluginConfigurator.EP_NAME.getExtensions();
522     for (StudyPluginConfigurator extension: extensions) {
523       if (extension.accept(project)) {
524         return extension;
525       }
526     }
527     return null;
528   }
529
530   @Nullable
531   public static StudyTwitterPluginConfigurator getTwitterConfigurator(@NotNull final Project project) {
532     StudyTwitterPluginConfigurator[] extensions = StudyTwitterPluginConfigurator.EP_NAME.getExtensions();
533     for (StudyTwitterPluginConfigurator extension: extensions) {
534       if (extension.accept(project)) {
535         return extension;
536       }
537     }
538     return null;
539   }
540
541   @Nullable
542   public static String getTaskText(@NotNull final Project project) {
543     TaskFile taskFile = getSelectedTaskFile(project);
544     if (taskFile == null) {
545       return EMPTY_TASK_TEXT;
546     }
547     final Task task = taskFile.getTask();
548     if (task != null) {
549       return getTaskTextFromTask(task.getTaskDir(project), task);
550     }
551     return null;
552   }
553   @Nullable
554   public static TaskFile getSelectedTaskFile(@NotNull Project project) {
555     VirtualFile[] files = FileEditorManager.getInstance(project).getSelectedFiles();
556     TaskFile taskFile = null;
557     for (VirtualFile file : files) {
558       taskFile = getTaskFile(project, file);
559       if (taskFile != null) {
560         break;
561       }
562     }
563     return taskFile;
564   }
565   
566   @Nullable
567   public static Task getCurrentTask(@NotNull final Project project) {
568     final TaskFile taskFile = getSelectedTaskFile(project);
569     return taskFile != null ? taskFile.getTask() : null;
570   }
571
572   public static boolean isStudyProject(@NotNull Project project) {
573     return StudyTaskManager.getInstance(project).getCourse() != null;
574   }
575
576   @Nullable
577   public static Project getStudyProject() {
578     Project studyProject = null;
579     Project[] openProjects = ProjectManager.getInstance().getOpenProjects();
580     for (Project project : openProjects) {
581       if (StudyTaskManager.getInstance(project).getCourse() != null) {
582          studyProject = project;
583       }
584     }
585     return studyProject;
586   }
587
588   @NotNull
589   public static File getCourseDirectory(@NotNull Project project, Course course) {
590     final File courseDirectory;
591     if (course.isAdaptive()) {
592       courseDirectory = new File(StudyProjectGenerator.OUR_COURSES_DIR,
593                                  StudyProjectGenerator.ADAPTIVE_COURSE_PREFIX + course.getName()
594                                  + "_" + StudyTaskManager.getInstance(project).getUser().getEmail());
595     }
596     else {
597       courseDirectory = new File(StudyProjectGenerator.OUR_COURSES_DIR, course.getName());
598     }
599     return courseDirectory;
600   }
601
602   public static boolean hasJavaFx() {
603     try {
604       Class.forName("javafx.application.Platform");
605       return true;
606     }
607     catch (ClassNotFoundException e) {
608       return false;
609     }
610   }
611
612   @Nullable
613   public static Task getTask(@NotNull Project project, @NotNull VirtualFile taskVF) {
614     Course course = StudyTaskManager.getInstance(project).getCourse();
615     if (course == null) {
616       return null;
617     }
618     VirtualFile lessonVF = taskVF.getParent();
619     if (lessonVF == null) {
620       return null;
621     }
622     Lesson lesson = course.getLesson(lessonVF.getName());
623     if (lesson == null) {
624       return null;
625     }
626     return lesson.getTask(taskVF.getName());
627   }
628
629   @Nullable
630   public static VirtualFile getTaskDir(@NotNull VirtualFile taskFile) {
631     VirtualFile parent = taskFile.getParent();
632     if (parent == null) {
633       return null;
634     }
635     String name = parent.getName();
636     if (name.contains(EduNames.TASK)) {
637       return parent;
638     }
639     if (EduNames.SRC.equals(name)) {
640       return parent.getParent();
641     }
642     return null;
643   }
644
645   @Nullable
646   public static Task getTaskForFile(@NotNull Project project, @NotNull VirtualFile taskFile) {
647     VirtualFile taskDir = getTaskDir(taskFile);
648     if (taskDir == null) {
649       return null;
650     }
651     return getTask(project, taskDir);
652   }
653
654   // supposed to be called under progress
655   @Nullable
656   public static <T> T execCancelable(@NotNull final Callable<T> callable) {
657     final Future<T> future = ApplicationManager.getApplication().executeOnPooledThread(callable);
658
659     while (!future.isCancelled() && !future.isDone()) {
660       ProgressManager.checkCanceled();
661       TimeoutUtil.sleep(500);
662     }
663     T result = null;
664     try {
665       result = future.get();
666     }
667     catch (InterruptedException | ExecutionException e) {
668       LOG.warn(e.getMessage());
669     }
670     return result;
671   }
672
673   @Nullable
674   public static Task getTaskFromSelectedEditor(Project project) {
675     final StudyEditor editor = getSelectedStudyEditor(project);
676     Task task = null;
677     if (editor != null) {
678       final TaskFile file = editor.getTaskFile();
679       task = file.getTask();
680     }
681     return task;
682   }
683
684   private static String convertToHtml(@NotNull final String content) {
685     ArrayList<String> lines = ContainerUtil.newArrayList(content.split("\n|\r|\r\n"));
686     MarkdownUtil.replaceHeaders(lines);
687     MarkdownUtil.replaceCodeBlock(lines);
688     
689     return new MarkdownProcessor().markdown(StringUtil.join(lines, "\n"));
690   }
691   
692   public static boolean isTaskDescriptionFile(@NotNull final String fileName) {
693     return EduNames.TASK_HTML.equals(fileName) || EduNames.TASK_MD.equals(fileName);
694   }
695   
696   @Nullable
697   public static VirtualFile findTaskDescriptionVirtualFile(@NotNull VirtualFile taskDir) {
698     return ObjectUtils.chooseNotNull(taskDir.findChild(EduNames.TASK_HTML), taskDir.findChild(EduNames.TASK_MD));
699   }
700   
701   @NotNull
702   public static String getTaskDescriptionFileName(final boolean useHtml) {
703     return useHtml ? EduNames.TASK_HTML : EduNames.TASK_MD;    
704   }
705
706   @Nullable
707   public static Document getDocument(String basePath, int lessonIndex, int taskIndex, String fileName) {
708     String taskPath = FileUtil.join(basePath, EduNames.LESSON + lessonIndex, EduNames.TASK + taskIndex);
709     VirtualFile taskFile = LocalFileSystem.getInstance().findFileByPath(FileUtil.join(taskPath, fileName));
710     if (taskFile == null) {
711       taskFile = LocalFileSystem.getInstance().findFileByPath(FileUtil.join(taskPath, EduNames.SRC, fileName));
712     }
713     if (taskFile == null) {
714       return null;
715     }
716     return FileDocumentManager.getInstance().getDocument(taskFile);
717   }
718
719   public static void showErrorPopupOnToolbar(@NotNull Project project) {
720     final Balloon balloon =
721       JBPopupFactory.getInstance().createHtmlTextBalloonBuilder("Couldn't post your reaction", MessageType.ERROR, null).createBalloon();
722     showCheckPopUp(project, balloon);
723   }
724
725   public static void selectFirstAnswerPlaceholder(@Nullable final StudyEditor studyEditor, @NotNull final Project project) {
726     if (studyEditor == null) return;
727     final Editor editor = studyEditor.getEditor();
728     IdeFocusManager.getInstance(project).requestFocus(editor.getContentComponent(), true);
729     final List<AnswerPlaceholder> placeholders = studyEditor.getTaskFile().getAnswerPlaceholders();
730     if (placeholders.isEmpty()) return;
731     final AnswerPlaceholder placeholder = placeholders.get(0);
732     int startOffset = placeholder.getOffset();
733     editor.getSelectionModel().setSelection(startOffset, startOffset + placeholder.getRealLength());
734   }
735
736   public static void registerStudyToolWindow(@Nullable final Course course, Project project) {
737     if (course != null && "PyCharm".equals(course.getCourseType())) {
738       final ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
739       registerToolWindows(toolWindowManager, project);
740       final ToolWindow studyToolWindow = toolWindowManager.getToolWindow(StudyToolWindowFactory.STUDY_TOOL_WINDOW);
741       if (studyToolWindow != null) {
742         studyToolWindow.show(null);
743         initToolWindows(project);
744       }
745     }
746   }
747
748   private static void registerToolWindows(@NotNull final ToolWindowManager toolWindowManager, Project project) {
749     final ToolWindow toolWindow = toolWindowManager.getToolWindow(StudyToolWindowFactory.STUDY_TOOL_WINDOW);
750     if (toolWindow == null) {
751       toolWindowManager.registerToolWindow(StudyToolWindowFactory.STUDY_TOOL_WINDOW, true, ToolWindowAnchor.RIGHT, project, true);
752     }
753   }
754 }