Search in sources :

Example 11 with CourseInfo

use of com.jetbrains.edu.learning.courseFormat.CourseInfo in project intellij-community by JetBrains.

the class StudyNewProjectPanel method setupBrowseButton.

private void setupBrowseButton() {
    if (SystemInfo.isMac && !UIUtil.isUnderDarcula())
        myBrowseButton.putClientProperty("JButton.buttonType", null);
    myBrowseButton.setIcon(InteractiveLearningIcons.InterpreterGear);
    final FileChooserDescriptor fileChooser = new FileChooserDescriptor(true, false, false, true, false, false) {

        @Override
        public boolean isFileVisible(VirtualFile file, boolean showHiddenFiles) {
            return file.isDirectory() || StudyUtils.isZip(file.getName());
        }

        @Override
        public boolean isFileSelectable(VirtualFile file) {
            return StudyUtils.isZip(file.getName());
        }
    };
    myBrowseButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            final BaseListPopupStep<String> popupStep = new BaseListPopupStep<String>("", "Add local course", LOGIN_TO_STEPIC) {

                @Override
                public PopupStep onChosen(final String selectedValue, boolean finalChoice) {
                    return doFinalStep(() -> {
                        if ("Add local course".equals(selectedValue)) {
                            Project[] projects = ProjectManager.getInstance().getOpenProjects();
                            FileChooser.chooseFile(fileChooser, null, projects.length == 0 ? null : projects[0].getBaseDir(), file -> {
                                String fileName = file.getPath();
                                int oldSize = myAvailableCourses.size();
                                CourseInfo courseInfo = myGenerator.addLocalCourse(fileName);
                                if (courseInfo != null) {
                                    if (oldSize != myAvailableCourses.size()) {
                                        myCoursesComboBox.addItem(courseInfo);
                                    }
                                    myCoursesComboBox.setSelectedItem(courseInfo);
                                    setOK();
                                } else {
                                    setError(INVALID_COURSE);
                                    myCoursesComboBox.removeAllItems();
                                    myCoursesComboBox.addItem(CourseInfo.INVALID_COURSE);
                                    for (CourseInfo course : myAvailableCourses) {
                                        myCoursesComboBox.addItem(course);
                                    }
                                    myCoursesComboBox.setSelectedItem(CourseInfo.INVALID_COURSE);
                                }
                            });
                        } else if (LOGIN_TO_STEPIC.equals(selectedValue)) {
                            showLoginDialog(true, "Signing In And Getting Stepik Course List");
                        }
                    });
                }
            };
            final ListPopup popup = JBPopupFactory.getInstance().createListPopup(popupStep);
            popup.showInScreenCoordinates(myBrowseButton, myBrowseButton.getLocationOnScreen());
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) UIUtil(com.intellij.util.ui.UIUtil) AllIcons(com.intellij.icons.AllIcons) ActionListener(java.awt.event.ActionListener) VirtualFile(com.intellij.openapi.vfs.VirtualFile) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) StudyUtils(com.jetbrains.edu.learning.StudyUtils) DefaultProjectFactory(com.intellij.openapi.project.DefaultProjectFactory) ArrayList(java.util.ArrayList) FacetValidatorsManager(com.intellij.facet.ui.FacetValidatorsManager) DefaultProjectFactoryImpl(com.intellij.openapi.project.DefaultProjectFactoryImpl) ProjectManager(com.intellij.openapi.project.ProjectManager) StudyProjectGenerator(com.jetbrains.edu.learning.courseGeneration.StudyProjectGenerator) Project(com.intellij.openapi.project.Project) FileChooserDescriptor(com.intellij.openapi.fileChooser.FileChooserDescriptor) AncestorEvent(javax.swing.event.AncestorEvent) ComboBox(com.intellij.openapi.ui.ComboBox) ProgressManager(com.intellij.openapi.progress.ProgressManager) InteractiveLearningIcons(icons.InteractiveLearningIcons) com.jetbrains.edu.learning.stepic(com.jetbrains.edu.learning.stepic) StringUtil(com.intellij.openapi.util.text.StringUtil) ValidationResult(com.intellij.facet.ui.ValidationResult) FixedSizeButton(com.intellij.openapi.ui.FixedSizeButton) ActionEvent(java.awt.event.ActionEvent) SystemInfo(com.intellij.openapi.util.SystemInfo) JBScrollPane(com.intellij.ui.components.JBScrollPane) VerticalFlowLayout(com.intellij.openapi.ui.VerticalFlowLayout) java.awt(java.awt) ListPopup(com.intellij.openapi.ui.popup.ListPopup) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) AncestorListenerAdapter(com.intellij.ui.AncestorListenerAdapter) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) CourseInfo(com.jetbrains.edu.learning.courseFormat.CourseInfo) PopupStep(com.intellij.openapi.ui.popup.PopupStep) ApplicationManager(com.intellij.openapi.application.ApplicationManager) PanelWithAnchor(com.intellij.ui.PanelWithAnchor) LabeledComponent(com.intellij.openapi.ui.LabeledComponent) NotNull(org.jetbrains.annotations.NotNull) FileChooser(com.intellij.openapi.fileChooser.FileChooser) Course(com.jetbrains.edu.learning.courseFormat.Course) javax.swing(javax.swing) FileChooserDescriptor(com.intellij.openapi.fileChooser.FileChooserDescriptor) ActionEvent(java.awt.event.ActionEvent) ListPopup(com.intellij.openapi.ui.popup.ListPopup) CourseInfo(com.jetbrains.edu.learning.courseFormat.CourseInfo) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep) PopupStep(com.intellij.openapi.ui.popup.PopupStep) ActionListener(java.awt.event.ActionListener) BaseListPopupStep(com.intellij.openapi.ui.popup.util.BaseListPopupStep)

Example 12 with CourseInfo

use of com.jetbrains.edu.learning.courseFormat.CourseInfo in project intellij-community by JetBrains.

the class StudyProjectGenerator method getCoursesFromCache.

public static List<CourseInfo> getCoursesFromCache() {
    List<CourseInfo> courses = new ArrayList<>();
    final File cacheFile = new File(OUR_COURSES_DIR, CACHE_NAME);
    if (!cacheFile.exists()) {
        return courses;
    }
    try {
        final FileInputStream inputStream = new FileInputStream(cacheFile);
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            try {
                String line;
                while ((line = reader.readLine()) != null) {
                    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
                    final CourseInfo courseInfo = gson.fromJson(line, CourseInfo.class);
                    courses.add(courseInfo);
                }
            } catch (IOException | JsonSyntaxException e) {
                LOG.error(e.getMessage());
            } finally {
                StudyUtils.closeSilently(reader);
            }
        } catch (UnsupportedEncodingException e) {
            LOG.error(e.getMessage());
        } finally {
            StudyUtils.closeSilently(inputStream);
        }
    } catch (FileNotFoundException e) {
        LOG.error(e.getMessage());
    }
    return courses;
}
Also used : CourseInfo(com.jetbrains.edu.learning.courseFormat.CourseInfo) VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) PsiFile(com.intellij.psi.PsiFile) NewVirtualFile(com.intellij.openapi.vfs.newvfs.NewVirtualFile)

Example 13 with CourseInfo

use of com.jetbrains.edu.learning.courseFormat.CourseInfo in project intellij-community by JetBrains.

the class CCPushCourse method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
    final Project project = e.getData(CommonDataKeys.PROJECT);
    if (view == null || project == null) {
        return;
    }
    final Course course = StudyTaskManager.getInstance(project).getCourse();
    if (course == null) {
        return;
    }
    if (course.getId() > 0) {
        ProgressManager.getInstance().run(new Task.Modal(project, "Updating Course", true) {

            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                for (Lesson lesson : course.getLessons()) {
                    if (lesson.getId() > 0) {
                        CCStepicConnector.updateLesson(project, lesson, indicator);
                    } else {
                        final CourseInfo info = CourseInfo.fromCourse(course);
                        final int lessonId = CCStepicConnector.postLesson(project, lesson, indicator);
                        if (lessonId != -1) {
                            final List<Integer> sections = info.getSections();
                            final Integer sectionId = sections.get(sections.size() - 1);
                            CCStepicConnector.postUnit(lessonId, lesson.getIndex(), sectionId);
                        }
                    }
                }
            }
        });
    } else {
        CCStepicConnector.postCourseWithProgress(project, course);
    }
    EduUsagesCollector.courseUploaded();
}
Also used : Project(com.intellij.openapi.project.Project) Task(com.intellij.openapi.progress.Task) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) IdeView(com.intellij.ide.IdeView) Course(com.jetbrains.edu.learning.courseFormat.Course) Lesson(com.jetbrains.edu.learning.courseFormat.Lesson) CourseInfo(com.jetbrains.edu.learning.courseFormat.CourseInfo)

Example 14 with CourseInfo

use of com.jetbrains.edu.learning.courseFormat.CourseInfo in project intellij-community by JetBrains.

the class CCPushLesson method actionPerformed.

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
    final Project project = e.getData(CommonDataKeys.PROJECT);
    if (view == null || project == null) {
        return;
    }
    final Course course = StudyTaskManager.getInstance(project).getCourse();
    if (course == null) {
        return;
    }
    PsiDirectory lessonDir = DirectoryChooserUtil.getOrChooseDirectory(view);
    if (lessonDir == null || !lessonDir.getName().contains("lesson")) {
        return;
    }
    final Lesson lesson = course.getLesson(lessonDir.getName());
    if (lesson == null) {
        return;
    }
    ProgressManager.getInstance().run(new Task.Modal(project, "Uploading Lesson", true) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            indicator.setText("Uploading lesson to " + EduStepicNames.STEPIC_URL);
            if (lesson.getId() > 0) {
                CCStepicConnector.updateLesson(project, lesson, indicator);
            } else {
                final CourseInfo info = CourseInfo.fromCourse(course);
                final int lessonId = CCStepicConnector.postLesson(project, lesson, indicator);
                final List<Integer> sections = info.getSections();
                final Integer sectionId = sections.get(sections.size() - 1);
                CCStepicConnector.postUnit(lessonId, lesson.getIndex(), sectionId);
            }
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) Task(com.intellij.openapi.progress.Task) PsiDirectory(com.intellij.psi.PsiDirectory) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) IdeView(com.intellij.ide.IdeView) Course(com.jetbrains.edu.learning.courseFormat.Course) Lesson(com.jetbrains.edu.learning.courseFormat.Lesson) CourseInfo(com.jetbrains.edu.learning.courseFormat.CourseInfo)

Example 15 with CourseInfo

use of com.jetbrains.edu.learning.courseFormat.CourseInfo in project intellij-community by JetBrains.

the class StudyStepicFormatTest method testAvailableCourses.

@Test
public void testAvailableCourses() throws IOException {
    String responseString = FileUtil.loadFile(new File(getTestDataPath(), "courses.json"));
    StepicWrappers.CoursesContainer container = EduStepicClient.deserializeStepicResponse(StepicWrappers.CoursesContainer.class, responseString);
    assertNotNull(container.courses);
    assertTrue("Incorrect number of courses", container.courses.size() == 4);
    List<CourseInfo> filtered = ContainerUtil.filter(container.courses, info -> EduStepicConnector.canBeOpened(info));
    assertEquals(ContainerUtil.newArrayList("Adaptive Python", "Introduction to Python", "format2"), ContainerUtil.map(filtered, CourseInfo::getName));
}
Also used : TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) File(java.io.File) CourseInfo(com.jetbrains.edu.learning.courseFormat.CourseInfo) Test(org.junit.Test)

Aggregations

CourseInfo (com.jetbrains.edu.learning.courseFormat.CourseInfo)16 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)7 NewVirtualFile (com.intellij.openapi.vfs.newvfs.NewVirtualFile)5 PsiFile (com.intellij.psi.PsiFile)5 Course (com.jetbrains.edu.learning.courseFormat.Course)4 File (java.io.File)4 Project (com.intellij.openapi.project.Project)3 Lesson (com.jetbrains.edu.learning.courseFormat.Lesson)3 List (java.util.List)3 Nullable (org.jetbrains.annotations.Nullable)3 IdeView (com.intellij.ide.IdeView)2 Notification (com.intellij.notification.Notification)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 Task (com.intellij.openapi.progress.Task)2 AncestorListenerAdapter (com.intellij.ui.AncestorListenerAdapter)2 ArrayList (java.util.ArrayList)2 JsonReader (com.google.gson.stream.JsonReader)1 FacetValidatorsManager (com.intellij.facet.ui.FacetValidatorsManager)1 ValidationResult (com.intellij.facet.ui.ValidationResult)1