Search in sources :

Example 1 with StudyItem

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

the class CCUtils method updateHigherElements.

/**
   * This method decreases index and updates directory names of
   * all tasks/lessons that have higher index than specified object
   *
   * @param dirs         directories that are used to get tasks/lessons
   * @param getStudyItem function that is used to get task/lesson from VirtualFile. This function can return null
   * @param threshold    index is used as threshold
   * @param prefix       task or lesson directory name prefix
   */
public static void updateHigherElements(VirtualFile[] dirs, @NotNull final Function<VirtualFile, ? extends StudyItem> getStudyItem, final int threshold, final String prefix, final int delta) {
    ArrayList<VirtualFile> dirsToRename = new ArrayList<>(Collections2.filter(Arrays.asList(dirs), new Predicate<VirtualFile>() {

        @Override
        public boolean apply(VirtualFile dir) {
            final StudyItem item = getStudyItem.fun(dir);
            if (item == null) {
                return false;
            }
            int index = item.getIndex();
            return index > threshold;
        }
    }));
    Collections.sort(dirsToRename, (o1, o2) -> {
        StudyItem item1 = getStudyItem.fun(o1);
        StudyItem item2 = getStudyItem.fun(o2);
        //if we delete some dir we should start increasing numbers in dir names from the end
        return (-delta) * EduUtils.INDEX_COMPARATOR.compare(item1, item2);
    });
    for (final VirtualFile dir : dirsToRename) {
        final StudyItem item = getStudyItem.fun(dir);
        final int newIndex = item.getIndex() + delta;
        item.setIndex(newIndex);
        ApplicationManager.getApplication().runWriteAction(new Runnable() {

            @Override
            public void run() {
                try {
                    dir.rename(this, prefix + newIndex);
                } catch (IOException e) {
                    LOG.error(e);
                }
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) IOException(java.io.IOException) StudyItem(com.jetbrains.edu.learning.courseFormat.StudyItem) Predicate(com.google.common.base.Predicate)

Example 2 with StudyItem

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

the class CCCreateStudyItemActionBase method getItem.

@Nullable
protected StudyItem getItem(@NotNull final PsiDirectory sourceDirectory, @NotNull final Project project, @NotNull final Course course, @Nullable IdeView view, @Nullable StudyItem parentItem) {
    String itemName;
    int itemIndex;
    if (isAddedAsLast(sourceDirectory, project, course)) {
        itemIndex = getSiblingsSize(course, parentItem) + 1;
        String suggestedName = getItemName() + itemIndex;
        itemName = view == null ? suggestedName : Messages.showInputDialog("Name:", getTitle(), null, suggestedName, null);
    } else {
        StudyItem thresholdItem = getThresholdItem(course, sourceDirectory);
        if (thresholdItem == null) {
            return null;
        }
        final int index = thresholdItem.getIndex();
        CCCreateStudyItemDialog dialog = new CCCreateStudyItemDialog(project, getItemName(), thresholdItem.getName(), index);
        dialog.show();
        if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE) {
            return null;
        }
        itemName = dialog.getName();
        itemIndex = index + dialog.getIndexDelta();
    }
    if (itemName == null) {
        return null;
    }
    return createAndInitItem(course, parentItem, itemName, itemIndex);
}
Also used : CCCreateStudyItemDialog(com.jetbrains.edu.coursecreator.ui.CCCreateStudyItemDialog) StudyItem(com.jetbrains.edu.learning.courseFormat.StudyItem) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with StudyItem

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

the class CCCreateStudyItemActionBase method createItem.

@Nullable
public PsiDirectory createItem(@Nullable final IdeView view, @NotNull final Project project, @NotNull final PsiDirectory sourceDirectory, @NotNull final Course course) {
    StudyItem parentItem = getParentItem(course, sourceDirectory);
    final StudyItem item = getItem(sourceDirectory, project, course, view, parentItem);
    if (item == null) {
        return null;
    }
    final PsiDirectory parentDir = getParentDir(project, course, sourceDirectory);
    if (parentDir == null) {
        return null;
    }
    CCUtils.updateHigherElements(parentDir.getVirtualFile().getChildren(), getStudyOrderable(item), item.getIndex() - 1, getItemName(), 1);
    addItem(course, item);
    Collections.sort(getSiblings(course, parentItem), EduUtils.INDEX_COMPARATOR);
    return createItemDir(project, item, view, parentDir, course);
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) StudyItem(com.jetbrains.edu.learning.courseFormat.StudyItem) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with StudyItem

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

the class CCLessonMoveHandlerDelegate method doMove.

@Override
public void doMove(final Project project, PsiElement[] elements, @Nullable PsiElement targetContainer, @Nullable MoveCallback callback) {
    if (targetContainer == null || !(targetContainer instanceof PsiDirectory)) {
        return;
    }
    final Course course = StudyTaskManager.getInstance(project).getCourse();
    if (course == null) {
        return;
    }
    final PsiDirectory sourceDirectory = (PsiDirectory) elements[0];
    final Lesson sourceLesson = course.getLesson(sourceDirectory.getName());
    final Lesson targetLesson = course.getLesson(((PsiDirectory) targetContainer).getName());
    if (targetLesson == null) {
        Messages.showInfoMessage("Lessons can be moved only to other lessons", "Incorrect Target For Move");
        return;
    }
    final CCMoveStudyItemDialog dialog = new CCMoveStudyItemDialog(project, EduNames.LESSON, targetLesson.getName());
    dialog.show();
    if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE) {
        return;
    }
    ApplicationManager.getApplication().runWriteAction(new Runnable() {

        @Override
        public void run() {
            try {
                sourceDirectory.getVirtualFile().rename(this, "tmp");
            } catch (IOException e) {
                LOG.error(e);
            }
        }
    });
    final VirtualFile[] lessonDirs = project.getBaseDir().getChildren();
    final Function<VirtualFile, StudyItem> getStudyItem = file -> course.getLesson(file.getName());
    int sourceLessonIndex = sourceLesson.getIndex();
    sourceLesson.setIndex(-1);
    CCUtils.updateHigherElements(lessonDirs, getStudyItem, sourceLessonIndex, EduNames.LESSON, -1);
    final int newItemIndex = targetLesson.getIndex() + dialog.getIndexDelta();
    CCUtils.updateHigherElements(lessonDirs, getStudyItem, newItemIndex - 1, EduNames.LESSON, 1);
    sourceLesson.setIndex(newItemIndex);
    Collections.sort(course.getLessons(), EduUtils.INDEX_COMPARATOR);
    ApplicationManager.getApplication().runWriteAction(new Runnable() {

        @Override
        public void run() {
            try {
                sourceDirectory.getVirtualFile().rename(this, EduNames.LESSON + newItemIndex);
            } catch (IOException e) {
                LOG.error(e);
            }
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MoveHandlerDelegate(com.intellij.refactoring.move.MoveHandlerDelegate) DataContext(com.intellij.openapi.actionSystem.DataContext) VirtualFile(com.intellij.openapi.vfs.VirtualFile) IdeView(com.intellij.ide.IdeView) EduUtils(com.jetbrains.edu.learning.core.EduUtils) DialogWrapper(com.intellij.openapi.ui.DialogWrapper) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) Messages(com.intellij.openapi.ui.Messages) CommonDataKeys(com.intellij.openapi.actionSystem.CommonDataKeys) Logger(com.intellij.openapi.diagnostic.Logger) MoveCallback(com.intellij.refactoring.move.MoveCallback) CCMoveStudyItemDialog(com.jetbrains.edu.coursecreator.ui.CCMoveStudyItemDialog) PsiReference(com.intellij.psi.PsiReference) Lesson(com.jetbrains.edu.learning.courseFormat.Lesson) IOException(java.io.IOException) StudyTaskManager(com.jetbrains.edu.learning.StudyTaskManager) Editor(com.intellij.openapi.editor.Editor) DirectoryChooserUtil(com.intellij.ide.util.DirectoryChooserUtil) Nullable(org.jetbrains.annotations.Nullable) Function(com.intellij.util.Function) EduNames(com.jetbrains.edu.learning.core.EduNames) StudyItem(com.jetbrains.edu.learning.courseFormat.StudyItem) ApplicationManager(com.intellij.openapi.application.ApplicationManager) PsiDirectory(com.intellij.psi.PsiDirectory) LangDataKeys(com.intellij.openapi.actionSystem.LangDataKeys) Collections(java.util.Collections) CCUtils(com.jetbrains.edu.coursecreator.CCUtils) Course(com.jetbrains.edu.learning.courseFormat.Course) IOException(java.io.IOException) Lesson(com.jetbrains.edu.learning.courseFormat.Lesson) PsiDirectory(com.intellij.psi.PsiDirectory) Course(com.jetbrains.edu.learning.courseFormat.Course) CCMoveStudyItemDialog(com.jetbrains.edu.coursecreator.ui.CCMoveStudyItemDialog) StudyItem(com.jetbrains.edu.learning.courseFormat.StudyItem)

Aggregations

StudyItem (com.jetbrains.edu.learning.courseFormat.StudyItem)4 Nullable (org.jetbrains.annotations.Nullable)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiDirectory (com.intellij.psi.PsiDirectory)2 IOException (java.io.IOException)2 Predicate (com.google.common.base.Predicate)1 IdeView (com.intellij.ide.IdeView)1 DirectoryChooserUtil (com.intellij.ide.util.DirectoryChooserUtil)1 CommonDataKeys (com.intellij.openapi.actionSystem.CommonDataKeys)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1 LangDataKeys (com.intellij.openapi.actionSystem.LangDataKeys)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 DialogWrapper (com.intellij.openapi.ui.DialogWrapper)1 Messages (com.intellij.openapi.ui.Messages)1 PsiElement (com.intellij.psi.PsiElement)1 PsiReference (com.intellij.psi.PsiReference)1 MoveCallback (com.intellij.refactoring.move.MoveCallback)1