add ability to hide file from student
authorliana.bakradze <liana.bakradze@jetbrains.com>
Mon, 18 Apr 2016 10:42:33 +0000 (13:42 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Mon, 18 Apr 2016 18:13:03 +0000 (21:13 +0300)
python/educational-core/course-creator/resources/META-INF/plugin.xml
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAsTaskFile.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCCreateTaskFile.java [deleted file]
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCHideFromStudent.java [new file with mode: 0644]
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCTaskFileActionBase.java [new file with mode: 0644]

index def669884579cbe90fe401665d8e2a6bdfee6da4..ed0795f78ba35808bce2b7367838a00446080bb5 100644 (file)
     <action id="CreateTask" class="com.jetbrains.edu.coursecreator.actions.CCCreateTask">
       <add-to-group group-id="NewGroup" anchor="before" relative-to-action="NewFile"/>
     </action>
-    <action id="CreateTaskFile" class="com.jetbrains.edu.coursecreator.actions.CCCreateTaskFile">
-      <add-to-group group-id="NewGroup" anchor="before" relative-to-action="NewFile"/>
-    </action>
 
     <group id="CCProjectViewGroup">
-      <action id="AddTaskFile" class="com.jetbrains.edu.coursecreator.actions.CCAddAsTaskFile" text="Add As Task File"/>
+      <action id="AddTaskFile" class="com.jetbrains.edu.coursecreator.actions.CCAddAsTaskFile" text="Add as Task File"/>
+      <action id="HideTaskFile" class="com.jetbrains.edu.coursecreator.actions.CCHideFromStudent" text="Hide from Student"/>
       <action id="ShowPreview" class="com.jetbrains.edu.coursecreator.actions.CCShowPreview">
         <add-to-group group-id="EditorTabPopupMenu"/>
       </action>
index fa5423922e37443eb1b9328ccb9874138286fe20..c62bd5e1e069935658d820f8a6e93e5e4547e6df 100644 (file)
@@ -1,46 +1,20 @@
 package com.jetbrains.edu.coursecreator.actions;
 
-import com.intellij.ide.projectView.ProjectView;
-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.project.Project;
 import com.intellij.openapi.vfs.VirtualFile;
+import com.jetbrains.edu.coursecreator.CCUtils;
 import com.jetbrains.edu.learning.StudyUtils;
+import com.jetbrains.edu.learning.courseFormat.Course;
 import com.jetbrains.edu.learning.courseFormat.Task;
 
-public class CCAddAsTaskFile extends AnAction {
-  @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 = StudyUtils.getTask(project, file);
-    if (task == null) {
-      return;
-    }
+public class CCAddAsTaskFile extends CCTaskFileActionBase {
+
+  protected void performAction(VirtualFile file, Task task, Course course, Project project) {
     task.addTaskFile(file.getName(), task.getTaskFiles().size());
-    ProjectView.getInstance(project).refresh();
+    CCUtils.createResourceFile(file, course, StudyUtils.getTaskDir(file));
   }
 
-
-  @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() || StudyUtils.getTaskFile(project, file) != null) {
-      presentation.setEnabledAndVisible(false);
-    }
+  protected boolean isAvailable(Project project, VirtualFile file) {
+    return StudyUtils.getTaskFile(project, file) == null && !CCUtils.isTestsFile(project, file);
   }
 }
diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCCreateTaskFile.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCCreateTaskFile.java
deleted file mode 100644 (file)
index 88e1449..0000000
+++ /dev/null
@@ -1,129 +0,0 @@
-package com.jetbrains.edu.coursecreator.actions;
-
-import com.intellij.icons.AllIcons;
-import com.intellij.ide.IdeView;
-import com.intellij.ide.fileTemplates.FileTemplate;
-import com.intellij.ide.fileTemplates.FileTemplateUtil;
-import com.intellij.ide.projectView.ProjectView;
-import com.intellij.ide.util.DirectoryChooserUtil;
-import com.intellij.ide.util.EditorHelper;
-import com.intellij.openapi.actionSystem.AnActionEvent;
-import com.intellij.openapi.actionSystem.CommonDataKeys;
-import com.intellij.openapi.actionSystem.LangDataKeys;
-import com.intellij.openapi.actionSystem.Presentation;
-import com.intellij.openapi.application.ApplicationManager;
-import com.intellij.openapi.fileEditor.FileEditorManager;
-import com.intellij.openapi.fileTypes.FileType;
-import com.intellij.openapi.project.DumbAwareAction;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.vfs.VirtualFile;
-import com.intellij.psi.PsiDirectory;
-import com.intellij.psi.PsiElement;
-import com.jetbrains.edu.learning.StudyTaskManager;
-import com.jetbrains.edu.learning.core.EduNames;
-import com.jetbrains.edu.learning.courseFormat.Course;
-import com.jetbrains.edu.learning.courseFormat.Lesson;
-import com.jetbrains.edu.learning.courseFormat.Task;
-import com.jetbrains.edu.coursecreator.CCLanguageManager;
-import com.jetbrains.edu.coursecreator.CCUtils;
-import com.jetbrains.edu.coursecreator.ui.CreateTaskFileDialog;
-import org.jetbrains.annotations.NotNull;
-
-import static com.intellij.openapi.ui.DialogWrapper.OK_EXIT_CODE;
-
-public class CCCreateTaskFile extends DumbAwareAction {
-
-  public CCCreateTaskFile() {
-    super("Task File", "Create new Task File", AllIcons.FileTypes.Text);
-  }
-
-  @Override
-  public void actionPerformed(final AnActionEvent e) {
-    final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
-    final Project project = e.getData(CommonDataKeys.PROJECT);
-
-    if (view == null || project == null) {
-      return;
-    }
-    final PsiDirectory taskDir = DirectoryChooserUtil.getOrChooseDirectory(view);
-    if (taskDir == null) return;
-    PsiDirectory lessonDir = taskDir.getParent();
-    if (lessonDir == null) {
-      return;
-    }
-    final Course course = StudyTaskManager.getInstance(project).getCourse();
-    final Lesson lesson = course.getLesson(lessonDir.getName());
-    final Task task = lesson.getTask(taskDir.getName());
-
-    final int index = task.getTaskFiles().size() + 1;
-    String generatedName = "file" + index;
-    CreateTaskFileDialog dialog = new CreateTaskFileDialog(project, generatedName, course);
-    dialog.show();
-    if (dialog.getExitCode() != OK_EXIT_CODE) {
-      return;
-    }
-    final String name = dialog.getFileName();
-    if (name == null) return;
-    FileType type = dialog.getFileType();
-    if (type == null) {
-      return;
-    }
-    final CCLanguageManager CCLanguageManager = CCUtils.getStudyLanguageManager(course);
-    if (CCLanguageManager == null) {
-      return;
-    }
-    final String extension = type.getDefaultExtension();
-    ApplicationManager.getApplication().runWriteAction(new Runnable() {
-      @Override
-      public void run() {
-        final FileTemplate taskTemplate = CCLanguageManager.getTaskFileTemplateForExtension(project, extension);
-        final String taskFileName = name + "." + extension;
-        try {
-          if (taskTemplate == null) {
-            VirtualFile file = taskDir.getVirtualFile().createChildData(this, taskFileName);
-            ProjectView.getInstance(project).select(file, file, false);
-            FileEditorManager.getInstance(project).openFile(file, true);
-          }
-          else {
-            final PsiElement taskFile = FileTemplateUtil.createFromTemplate(taskTemplate, taskFileName, null, taskDir);
-            ApplicationManager.getApplication().invokeLater(new Runnable() {
-              @Override
-              public void run() {
-                EditorHelper.openInEditor(taskFile, false);
-                view.selectElement(taskFile);
-              }
-            });
-          }
-          task.addTaskFile(taskFileName, index);
-        }
-        catch (Exception ignored) {
-        }
-      }
-    });
-  }
-
-  @Override
-  public void update(@NotNull AnActionEvent event) {
-    final Presentation presentation = event.getPresentation();
-    presentation.setEnabledAndVisible(false);
-    final Project project = event.getData(CommonDataKeys.PROJECT);
-    if (project == null) {
-      return;
-    }
-    final IdeView view = event.getData(LangDataKeys.IDE_VIEW);
-    if (view == null) {
-      return;
-    }
-
-    final PsiDirectory[] directories = view.getDirectories();
-    if (directories.length == 0) {
-      return;
-    }
-    final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
-    final Course course = StudyTaskManager.getInstance(project).getCourse();
-    if (course != null && directory != null && !directory.getName().contains(EduNames.TASK)) {
-      return;
-    }
-    presentation.setEnabledAndVisible(true);
-  }
-}
diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCHideFromStudent.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCHideFromStudent.java
new file mode 100644 (file)
index 0000000..07cd46c
--- /dev/null
@@ -0,0 +1,45 @@
+package com.jetbrains.edu.coursecreator.actions;
+
+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.learning.StudyUtils;
+import com.jetbrains.edu.learning.courseFormat.Course;
+import com.jetbrains.edu.learning.courseFormat.Task;
+import com.jetbrains.edu.learning.courseFormat.TaskFile;
+
+import java.io.IOException;
+import java.util.Map;
+
+public class CCHideFromStudent extends CCTaskFileActionBase {
+
+  private static final Logger LOG = Logger.getInstance(CCHideFromStudent.class);
+
+  @Override
+  protected void performAction(VirtualFile file, Task task, Course course, Project project) {
+    Map<String, TaskFile> taskFiles = task.getTaskFiles();
+    TaskFile taskFile = StudyUtils.getTaskFile(project, file);
+    if (taskFile == null) {
+      return;
+    }
+    String name = file.getName();
+    VirtualFile patternFile = StudyUtils.getPatternFile(taskFile, name);
+    ApplicationManager.getApplication().runWriteAction(() -> {
+      if (patternFile != null) {
+        try {
+          patternFile.delete(CCHideFromStudent.class);
+        }
+        catch (IOException e) {
+          LOG.info(e);
+        }
+      }
+    });
+    taskFiles.remove(name);
+  }
+
+  @Override
+  protected boolean isAvailable(Project project, VirtualFile file) {
+    return StudyUtils.getTaskFile(project, file) != null;
+  }
+}
diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCTaskFileActionBase.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCTaskFileActionBase.java
new file mode 100644 (file)
index 0000000..4f0632e
--- /dev/null
@@ -0,0 +1,59 @@
+package com.jetbrains.edu.coursecreator.actions;
+
+import com.intellij.ide.projectView.ProjectView;
+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.project.Project;
+import com.intellij.openapi.vfs.VirtualFile;
+import com.jetbrains.edu.coursecreator.CCUtils;
+import com.jetbrains.edu.learning.StudyTaskManager;
+import com.jetbrains.edu.learning.StudyUtils;
+import com.jetbrains.edu.learning.courseFormat.Course;
+import com.jetbrains.edu.learning.courseFormat.Task;
+
+public abstract class CCTaskFileActionBase extends AnAction {
+
+  @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;
+    }
+    VirtualFile taskVF = file.getParent();
+    if (taskVF == null) {
+      return;
+    }
+    Task task = StudyUtils.getTask(project, taskVF);
+    if (task == null) {
+      return;
+    }
+    Course course = StudyTaskManager.getInstance(project).getCourse();
+    performAction(file, task, course, project);
+    ProjectView.getInstance(project).refresh();
+  }
+
+  protected abstract void performAction(VirtualFile file, Task task, Course course, Project project);
+
+
+  @Override
+  public void update(AnActionEvent e) {
+    Project project = e.getProject();
+    Presentation presentation = e.getPresentation();
+    if (project == null || !CCUtils.isCourseCreator(project)) {
+      presentation.setEnabledAndVisible(false);
+      return;
+    }
+    VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
+    if (file == null || file.isDirectory() || !isAvailable(project, file)) {
+      presentation.setEnabledAndVisible(false);
+    }
+  }
+
+  protected abstract boolean isAvailable(Project project, VirtualFile file);
+}