From fdb22f8fb5a595988eae6621e5bdcaea4739ed19 Mon Sep 17 00:00:00 2001 From: "liana.bakradze" Date: Mon, 24 Aug 2015 15:21:48 +0300 Subject: [PATCH] EDU-402 Add As Task File action --- .../resources/META-INF/plugin.xml | 3 + .../actions/CCAddAsTaskFile.java | 68 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 python/educational/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAsTaskFile.java diff --git a/python/educational/course-creator/resources/META-INF/plugin.xml b/python/educational/course-creator/resources/META-INF/plugin.xml index fb9c60ba782b..84bc4ac32c0f 100644 --- a/python/educational/course-creator/resources/META-INF/plugin.xml +++ b/python/educational/course-creator/resources/META-INF/plugin.xml @@ -59,6 +59,9 @@ + + + diff --git a/python/educational/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAsTaskFile.java b/python/educational/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAsTaskFile.java new file mode 100644 index 000000000000..43ea51084510 --- /dev/null +++ b/python/educational/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAsTaskFile.java @@ -0,0 +1,68 @@ +package com.jetbrains.edu.coursecreator.actions; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.actionSystem.Presentation; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; +import com.jetbrains.edu.courseFormat.Task; +import com.jetbrains.edu.coursecreator.CCProjectService; + +import java.io.IOException; + +public class CCAddAsTaskFile extends AnAction { + private static final Logger LOG = Logger.getInstance(CCAddAsTaskFile.class); + + @Override + public void actionPerformed(final AnActionEvent e) { + Project project = e.getProject(); + if (project == null) { + return; + } + final VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext()); + if (file == null) { + return; + } + Task task = CCProjectService.getInstance(project).getTask(file); + if (task == null) { + return; + } + task.addTaskFile(file.getName(), task.getTaskFiles().size()); + ApplicationManager.getApplication().runWriteAction(new Runnable() { + @Override + public void run() { + String name = file.getNameWithoutExtension(); + String extension = file.getExtension(); + try { + file.rename(this, name + ".answer." + extension); + } + catch (IOException e1) { + LOG.error(e1); + } + } + }); + } + + + @Override + public void update(AnActionEvent e) { + Project project = e.getProject(); + Presentation presentation = e.getPresentation(); + if (project == null) { + presentation.setEnabledAndVisible(false); + return; + } + VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext()); + if (file == null || file.isDirectory() || CCProjectService.getInstance(project).isAnswerFile(file)) { + presentation.setEnabledAndVisible(false); + return; + } + Task task = CCProjectService.getInstance(project).getTask(file); + if (task == null) { + presentation.setEnabledAndVisible(false); + } + } +}