diff options
Diffstat (limited to 'python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/actions/CreateLesson.java')
-rw-r--r-- | python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/actions/CreateLesson.java | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/actions/CreateLesson.java b/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/actions/CreateLesson.java new file mode 100644 index 000000000000..15d9f8367db9 --- /dev/null +++ b/python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/actions/CreateLesson.java @@ -0,0 +1,89 @@ +package org.jetbrains.plugins.coursecreator.actions; + +import com.intellij.ide.IdeView; +import com.intellij.ide.util.DirectoryChooserUtil; +import com.intellij.ide.util.DirectoryUtil; +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.project.DumbAwareAction; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.Messages; +import com.intellij.psi.PsiDirectory; +import com.intellij.util.PlatformIcons; +import org.jetbrains.plugins.coursecreator.CCProjectService; +import org.jetbrains.plugins.coursecreator.format.Course; +import org.jetbrains.plugins.coursecreator.format.Lesson; + +public class CreateLesson extends DumbAwareAction { + public CreateLesson() { + super("Lesson", "Create new Lesson", PlatformIcons.DIRECTORY_CLOSED_ICON); + } + + @Override + public void actionPerformed(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 directory = DirectoryChooserUtil.getOrChooseDirectory(view); + if (directory == null) return; + + final CCProjectService service = CCProjectService.getInstance(project); + final Course course = service.getCourse(); + final int size = course.getLessons().size(); + final String lessonName = Messages.showInputDialog("Name:", "Lesson Name", null, "lesson" + (size+1), null); + if (lessonName == null) return; + + ApplicationManager.getApplication().runWriteAction(new Runnable() { + @Override + public void run() { + final PsiDirectory lessonDirectory = DirectoryUtil.createSubdirectories("lesson" + (size+1), directory, "\\/"); + if (lessonDirectory != null) { + view.selectElement(lessonDirectory); + final Lesson lesson = new Lesson(lessonName); + lesson.setIndex(size + 1); + course.addLesson(lesson, lessonDirectory); + } + } + }); + } + + @Override + public void update(AnActionEvent event) { + final Presentation presentation = event.getPresentation(); + final Project project = event.getData(CommonDataKeys.PROJECT); + if (project == null) { + presentation.setVisible(false); + presentation.setEnabled(false); + return; + } + + final IdeView view = event.getData(LangDataKeys.IDE_VIEW); + if (view == null) { + presentation.setVisible(false); + presentation.setEnabled(false); + return; + } + + final PsiDirectory[] directories = view.getDirectories(); + if (directories.length == 0) { + presentation.setVisible(false); + presentation.setEnabled(false); + return; + } + final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view); + if (directory != null && !project.getBaseDir().equals(directory.getVirtualFile())) { + presentation.setVisible(false); + presentation.setEnabled(false); + return; + } + presentation.setVisible(true); + presentation.setEnabled(true); + + } +}
\ No newline at end of file |