Search in sources :

Example 1 with GlobalUndoableAction

use of com.intellij.openapi.command.undo.GlobalUndoableAction in project intellij-community by JetBrains.

the class DynamicDialog method doOKAction.

@Override
protected void doOKAction() {
    super.doOKAction();
    mySettings.setContainingClassName((String) myClassComboBox.getSelectedItem());
    mySettings.setStatic(myStaticCheckBox.isSelected());
    GrTypeElement typeElement = getEnteredTypeName();
    if (typeElement == null) {
        mySettings.setType(CommonClassNames.JAVA_LANG_OBJECT);
    } else {
        PsiType type = typeElement.getType();
        if (type instanceof PsiPrimitiveType) {
            type = TypesUtil.boxPrimitiveType(type, typeElement.getManager(), ProjectScope.getAllScope(myProject));
        }
        final String typeQualifiedName = type.getCanonicalText();
        if (typeQualifiedName != null) {
            mySettings.setType(typeQualifiedName);
        } else {
            mySettings.setType(type.getPresentableText());
        }
    }
    final Document document = PsiDocumentManager.getInstance(myProject).getDocument(myContext.getContainingFile());
    CommandProcessor.getInstance().executeCommand(myProject, () -> {
        UndoManager.getInstance(myProject).undoableActionPerformed(new GlobalUndoableAction(document) {

            @Override
            public void undo() throws UnexpectedUndoException {
                final DItemElement itemElement;
                if (mySettings.isMethod()) {
                    final List<ParamInfo> myPairList = mySettings.getParams();
                    final String[] argumentsTypes = QuickfixUtil.getArgumentsTypes(myPairList);
                    itemElement = myDynamicManager.findConcreteDynamicMethod(mySettings.getContainingClassName(), mySettings.getName(), argumentsTypes);
                } else {
                    itemElement = myDynamicManager.findConcreteDynamicProperty(mySettings.getContainingClassName(), mySettings.getName());
                }
                if (itemElement == null) {
                    Messages.showWarningDialog(myProject, GroovyInspectionBundle.message("Cannot.perform.undo.operation"), GroovyInspectionBundle.message("Undo.disable"));
                    return;
                }
                final DClassElement classElement = myDynamicManager.getClassElementByItem(itemElement);
                if (classElement == null) {
                    Messages.showWarningDialog(myProject, GroovyInspectionBundle.message("Cannot.perform.undo.operation"), GroovyInspectionBundle.message("Undo.disable"));
                    return;
                }
                removeElement(itemElement);
                if (classElement.getMethods().isEmpty() && classElement.getProperties().isEmpty()) {
                    myDynamicManager.removeClassElement(classElement);
                }
            }

            @Override
            public void redo() throws UnexpectedUndoException {
                addElement(mySettings);
            }
        });
        addElement(mySettings);
    }, "Add dynamic element", null);
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) DClassElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement) UnexpectedUndoException(com.intellij.openapi.command.undo.UnexpectedUndoException) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) List(java.util.List) Document(com.intellij.openapi.editor.Document) DItemElement(org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DItemElement)

Example 2 with GlobalUndoableAction

use of com.intellij.openapi.command.undo.GlobalUndoableAction in project intellij-community by JetBrains.

the class PsiPackageImplementationHelperImpl method handleQualifiedNameChange.

@Override
public void handleQualifiedNameChange(@NotNull final PsiPackage psiPackage, @NotNull final String newQualifiedName) {
    ApplicationManager.getApplication().assertWriteAccessAllowed();
    final String oldQualifedName = psiPackage.getQualifiedName();
    final boolean anyChanged = changePackagePrefixes(psiPackage, oldQualifedName, newQualifiedName);
    if (anyChanged) {
        UndoManager.getInstance(psiPackage.getProject()).undoableActionPerformed(new GlobalUndoableAction() {

            @Override
            public void undo() {
                changePackagePrefixes(psiPackage, newQualifiedName, oldQualifedName);
            }

            @Override
            public void redo() {
                changePackagePrefixes(psiPackage, oldQualifedName, newQualifiedName);
            }
        });
    }
}
Also used : GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction)

Example 3 with GlobalUndoableAction

use of com.intellij.openapi.command.undo.GlobalUndoableAction in project intellij-community by JetBrains.

the class ChangeFileEncodingAction method chosen.

// returns true if charset was changed, false if failed
protected boolean chosen(final Document document, final Editor editor, @Nullable final VirtualFile virtualFile, byte[] bytes, @NotNull final Charset charset) {
    if (virtualFile == null)
        return false;
    String text = document.getText();
    EncodingUtil.Magic8 isSafeToConvert = EncodingUtil.isSafeToConvertTo(virtualFile, text, bytes, charset);
    EncodingUtil.Magic8 isSafeToReload = EncodingUtil.isSafeToReloadIn(virtualFile, text, bytes, charset);
    final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile);
    final Charset oldCharset = virtualFile.getCharset();
    final Runnable undo;
    final Runnable redo;
    if (isSafeToConvert == EncodingUtil.Magic8.ABSOLUTELY && isSafeToReload == EncodingUtil.Magic8.ABSOLUTELY) {
        //change and forget
        undo = () -> EncodingManager.getInstance().setEncoding(virtualFile, oldCharset);
        redo = () -> EncodingManager.getInstance().setEncoding(virtualFile, charset);
    } else {
        IncompatibleEncodingDialog dialog = new IncompatibleEncodingDialog(virtualFile, charset, isSafeToReload, isSafeToConvert);
        dialog.show();
        if (dialog.getExitCode() == IncompatibleEncodingDialog.RELOAD_EXIT_CODE) {
            undo = () -> EncodingUtil.reloadIn(virtualFile, oldCharset);
            redo = () -> EncodingUtil.reloadIn(virtualFile, charset);
        } else if (dialog.getExitCode() == IncompatibleEncodingDialog.CONVERT_EXIT_CODE) {
            undo = () -> EncodingUtil.saveIn(document, editor, virtualFile, oldCharset);
            redo = () -> EncodingUtil.saveIn(document, editor, virtualFile, charset);
        } else {
            return false;
        }
    }
    final UndoableAction action = new GlobalUndoableAction(virtualFile) {

        @Override
        public void undo() {
            // invoke later because changing document inside undo/redo is not allowed
            Application application = ApplicationManager.getApplication();
            application.invokeLater(undo, ModalityState.NON_MODAL, (project == null ? application : project).getDisposed());
        }

        @Override
        public void redo() {
            // invoke later because changing document inside undo/redo is not allowed
            Application application = ApplicationManager.getApplication();
            application.invokeLater(redo, ModalityState.NON_MODAL, (project == null ? application : project).getDisposed());
        }
    };
    redo.run();
    CommandProcessor.getInstance().executeCommand(project, () -> {
        UndoManager undoManager = project == null ? UndoManager.getGlobalInstance() : UndoManager.getInstance(project);
        undoManager.undoableActionPerformed(action);
    }, "Change encoding for '" + virtualFile.getName() + "'", null, UndoConfirmationPolicy.REQUEST_CONFIRMATION);
    return true;
}
Also used : Project(com.intellij.openapi.project.Project) UndoManager(com.intellij.openapi.command.undo.UndoManager) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) Charset(java.nio.charset.Charset) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) Application(com.intellij.openapi.application.Application)

Example 4 with GlobalUndoableAction

use of com.intellij.openapi.command.undo.GlobalUndoableAction in project intellij-community by JetBrains.

the class Configuration method replaceInjectionsWithUndo.

public static <T> void replaceInjectionsWithUndo(final Project project, final T add, final T remove, final List<? extends PsiElement> psiElementsToRemove, final PairProcessor<T, T> actualProcessor) {
    final UndoableAction action = new GlobalUndoableAction() {

        @Override
        public void undo() {
            actualProcessor.process(remove, add);
        }

        @Override
        public void redo() {
            actualProcessor.process(add, remove);
        }
    };
    final List<PsiFile> psiFiles = ContainerUtil.mapNotNull(psiElementsToRemove, o -> o instanceof PsiCompiledElement ? null : o.getContainingFile());
    new WriteCommandAction.Simple(project, "Language Injection Configuration Update", PsiUtilCore.toPsiFileArray(psiFiles)) {

        @Override
        public void run() {
            for (PsiElement annotation : psiElementsToRemove) {
                if (!annotation.isValid())
                    continue;
                annotation.delete();
            }
            actualProcessor.process(add, remove);
            UndoManager.getInstance(project).undoableActionPerformed(action);
        }

        @Override
        protected UndoConfirmationPolicy getUndoConfirmationPolicy() {
            return UndoConfirmationPolicy.REQUEST_CONFIRMATION;
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) UndoConfirmationPolicy(com.intellij.openapi.command.UndoConfirmationPolicy) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) PsiCompiledElement(com.intellij.psi.PsiCompiledElement) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 5 with GlobalUndoableAction

use of com.intellij.openapi.command.undo.GlobalUndoableAction in project intellij-community by JetBrains.

the class ClearContextAction method actionPerformed.

public void actionPerformed(final AnActionEvent e) {
    final Project project = getProject(e);
    GlobalUndoableAction action = new GlobalUndoableAction() {

        public void undo() throws UnexpectedUndoException {
        }

        public void redo() throws UnexpectedUndoException {
            WorkingContextManager.getInstance(project).clearContext();
        }
    };
    UndoableCommand.execute(project, action, "Clear context", "Context");
}
Also used : Project(com.intellij.openapi.project.Project) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction)

Aggregations

GlobalUndoableAction (com.intellij.openapi.command.undo.GlobalUndoableAction)6 UndoableAction (com.intellij.openapi.command.undo.UndoableAction)3 Project (com.intellij.openapi.project.Project)3 UndoConfirmationPolicy (com.intellij.openapi.command.UndoConfirmationPolicy)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 Application (com.intellij.openapi.application.Application)1 Result (com.intellij.openapi.application.Result)1 UndoManager (com.intellij.openapi.command.undo.UndoManager)1 UnexpectedUndoException (com.intellij.openapi.command.undo.UnexpectedUndoException)1 Document (com.intellij.openapi.editor.Document)1 PsiCompiledElement (com.intellij.psi.PsiCompiledElement)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 Charset (java.nio.charset.Charset)1 List (java.util.List)1 DClassElement (org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DClassElement)1 DItemElement (org.jetbrains.plugins.groovy.annotator.intentions.dynamic.elements.DItemElement)1 GrTypeElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement)1