Search in sources :

Example 1 with MoveCallback

use of com.intellij.refactoring.move.MoveCallback in project intellij-plugins by StepicOrg.

the class AbstractMoveHandlerDelegate method doMove.

@Override
public void doMove(Project project, PsiElement[] elements, @Nullable PsiElement targetContainer, @Nullable MoveCallback callback) {
    List<PsiFileSystemItem> sources = Arrays.stream(elements).filter(psiElement -> isNotMovableOrRenameElement(psiElement, acceptableClasses)).map(ProjectPsiFilesUtils::getFile).filter(Objects::nonNull).collect(Collectors.toList());
    StringBuilder message = new StringBuilder();
    if (sources.size() > 1) {
        message.append("You can not move the following elements:");
    } else {
        PsiFileSystemItem source = sources.get(0);
        message.append("You can not move the ").append(source.isDirectory() ? "directory" : "file").append(":");
    }
    for (PsiFileSystemItem file : sources) {
        message.append("\n").append(file.getVirtualFile().getPath());
    }
    MessagesEx.error(project, message.toString(), "Move").showNow();
}
Also used : MoveHandlerDelegate(com.intellij.refactoring.move.MoveHandlerDelegate) Arrays(java.util.Arrays) ProjectPsiFilesUtils.isNotMovableOrRenameElement(org.stepik.plugin.utils.ProjectPsiFilesUtils.isNotMovableOrRenameElement) MoveCallback(com.intellij.refactoring.move.MoveCallback) MessagesEx(com.intellij.openapi.ui.ex.MessagesEx) Set(java.util.Set) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) NotNull(org.jetbrains.annotations.NotNull) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) ProjectPsiFilesUtils.isCanNotBeTarget(org.stepik.plugin.utils.ProjectPsiFilesUtils.isCanNotBeTarget) ProjectPsiFilesUtils(org.stepik.plugin.utils.ProjectPsiFilesUtils) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) ProjectPsiFilesUtils(org.stepik.plugin.utils.ProjectPsiFilesUtils)

Example 2 with MoveCallback

use of com.intellij.refactoring.move.MoveCallback 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

Project (com.intellij.openapi.project.Project)2 PsiElement (com.intellij.psi.PsiElement)2 MoveCallback (com.intellij.refactoring.move.MoveCallback)2 MoveHandlerDelegate (com.intellij.refactoring.move.MoveHandlerDelegate)2 Nullable (org.jetbrains.annotations.Nullable)2 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 DialogWrapper (com.intellij.openapi.ui.DialogWrapper)1 Messages (com.intellij.openapi.ui.Messages)1 MessagesEx (com.intellij.openapi.ui.ex.MessagesEx)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)1 PsiReference (com.intellij.psi.PsiReference)1