import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
+import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.IdeFocusManager;
-import com.jetbrains.edu.learning.StudyDocumentListener;
-import com.jetbrains.edu.learning.StudyTaskManager;
+import com.jetbrains.edu.learning.StudyState;
import com.jetbrains.edu.learning.StudyUtils;
import com.jetbrains.edu.learning.course.*;
import com.jetbrains.edu.learning.editor.StudyEditor;
public class StudyRefreshTaskFileAction extends DumbAwareAction {
public static final String ACTION_ID = "RefreshTaskAction";
public static final String SHORTCUT = "ctrl shift pressed X";
+ private static final Logger LOG = Logger.getInstance(StudyRefreshTaskFileAction.class.getName());
- public static void refresh(final Project project) {
+ public static void refresh(@NotNull final Project project) {
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
@Override
public void run() {
- final Editor editor = StudyEditor.getSelectedEditor(project);
- assert editor != null;
- final Document document = editor.getDocument();
- refreshFile(editor, document, project);
+ StudyEditor studyEditor = StudyEditor.getSelectedStudyEditor(project);
+ StudyState studyState = new StudyState(studyEditor);
+ if (studyEditor == null || !studyState.isValid()) {
+ LOG.info("RefreshTaskFileAction was invoked outside of Study Editor");
+ return;
+ }
+ refreshFile(studyState, project);
}
});
}
});
}
- public static void refreshFile(@NotNull final Editor editor, @NotNull final Document document, @NotNull final Project project) {
- StudyTaskManager taskManager = StudyTaskManager.getInstance(project);
- Course course = taskManager.getCourse();
- assert course != null;
- FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
- VirtualFile openedFile = fileDocumentManager.getFile(document);
- assert openedFile != null;
- final TaskFile selectedTaskFile = taskManager.getTaskFile(openedFile);
- assert selectedTaskFile != null;
- String openedFileName = openedFile.getName();
- Task currentTask = selectedTaskFile.getTask();
- resetTaskFile(document, project, course, selectedTaskFile, openedFileName, currentTask);
- selectedTaskFile.drawAllWindows(editor);
- selectedTaskFile.createGuardedBlocks(document, editor);
+ private static void refreshFile(@NotNull final StudyState studyState, @NotNull final Project project) {
+ final Editor editor = studyState.getEditor();
+ final TaskFile taskFile = studyState.getTaskFile();
+ if (!resetTaskFile(editor.getDocument(), project, taskFile, studyState.getVirtualFile().getName())) {
+ return;
+ }
+ taskFile.drawAllWindows(editor);
+ taskFile.createGuardedBlocks(editor);
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
IdeFocusManager.getInstance(project).requestFocus(editor.getContentComponent(), true);
}
});
- selectedTaskFile.navigateToFirstTaskWindow(editor);
- showBalloon(project);
+ taskFile.navigateToFirstTaskWindow(editor);
+ showBalloon(project, "You can start again now", MessageType.INFO);
}
- public static void resetTaskFile(Document document, Project project, Course course, TaskFile taskFile, String name, Task task) {
- resetDocument(document, course, name, task);
- updateLessonInfo(task);
+ private static boolean resetTaskFile(@NotNull final Document document,
+ @NotNull final Project project,
+ TaskFile taskFile,
+ String name) {
+ if (!resetDocument(project, document, taskFile, name)) {
+ return false;
+ }
+ updateLessonInfo(taskFile.getTask());
StudyUtils.updateStudyToolWindow(project);
resetTaskWindows(taskFile);
ProjectView.getInstance(project).refresh();
+ return true;
}
- private static void showBalloon(Project project) {
+ private static void showBalloon(@NotNull final Project project, String text, @NotNull final MessageType messageType) {
BalloonBuilder balloonBuilder =
- JBPopupFactory.getInstance().createHtmlTextBalloonBuilder("You can start again now", MessageType.INFO, null);
+ JBPopupFactory.getInstance().createHtmlTextBalloonBuilder(text, messageType, null);
final Balloon balloon = balloonBuilder.createBalloon();
StudyEditor selectedStudyEditor = StudyEditor.getSelectedStudyEditor(project);
assert selectedStudyEditor != null;
lessonInfo.update(StudyStatus.Unchecked, +1);
}
- private static void resetDocument(final Document document, Course course, String fileName, Task task) {
+ private static boolean resetDocument(@NotNull final Project project,
+ @NotNull final Document document,
+ @NotNull final TaskFile taskFile,
+ String fileName) {
StudyEditor.deleteGuardedBlocks(document);
- StudyDocumentListener listener = StudyEditor.getListener(document);
- if (listener != null) {
- document.removeDocumentListener(listener);
- }
+ taskFile.setTrackChanges(false);
clearDocument(document);
+ Task task = taskFile.getTask();
String lessonDir = Lesson.LESSON_DIR + String.valueOf(task.getLesson().getIndex() + 1);
String taskDir = Task.TASK_DIR + String.valueOf(task.getIndex() + 1);
+ Course course = task.getLesson().getCourse();
File resourceFile = new File(course.getResourcePath());
File resourceRoot = resourceFile.getParentFile();
+ if (!resourceFile.exists() || resourceRoot == null) {
+ showBalloon(project, "Course was deleted", MessageType.ERROR);
+ return false;
+ }
String patternPath = FileUtil.join(resourceRoot.getPath(), lessonDir, taskDir, fileName);
VirtualFile patternFile = VfsUtil.findFileByIoFile(new File(patternPath), true);
if (patternFile == null) {
- return;
+ return false;
}
- Document patternDocument = FileDocumentManager.getInstance().getDocument(patternFile);
+ final Document patternDocument = FileDocumentManager.getInstance().getDocument(patternFile);
if (patternDocument == null) {
- return;
+ return false;
}
document.setText(patternDocument.getCharsSequence());
- if (listener != null) {
- document.addDocumentListener(listener);
- }
+ taskFile.setTrackChanges(true);
+ return true;
}
- private static void clearDocument(final Document document) {
+ private static void clearDocument(@NotNull final Document document) {
final int lineCount = document.getLineCount();
if (lineCount != 0) {
CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
}
}
- public void actionPerformed(@NotNull AnActionEvent e) {
- refresh(e.getProject());
+ public void actionPerformed(@NotNull AnActionEvent event) {
+ final Project project = event.getProject();
+ if (project != null) {
+ refresh(project);
+ }
+ }
+
+ @Override
+ public void update(AnActionEvent event) {
+ final Project project = event.getProject();
+ if (project != null) {
+ StudyEditor studyEditor = StudyEditor.getSelectedStudyEditor(project);
+ StudyState studyState = new StudyState(studyEditor);
+ if (studyState.isValid()) {
+ StudyUtils.enableAction(event, true);
+ }
+ }
+ StudyUtils.enableAction(event, false);
}
}