Example usage for com.intellij.openapi.command.undo GlobalUndoableAction GlobalUndoableAction

List of usage examples for com.intellij.openapi.command.undo GlobalUndoableAction GlobalUndoableAction

Introduction

In this page you can find the example usage for com.intellij.openapi.command.undo GlobalUndoableAction GlobalUndoableAction.

Prototype

public GlobalUndoableAction(VirtualFile @NotNull... files) 

Source Link

Usage

From source file:org.jetbrains.plugins.groovy.annotator.intentions.dynamic.ui.DynamicDialog.java

License:Apache License

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 {//w  w  w.  ja  v  a 2  s.  c  o  m
        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, new Runnable() {
        public void run() {
            UndoManager.getInstance(myProject).undoableActionPerformed(new GlobalUndoableAction(document) {
                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().size() == 0 && classElement.getProperties().size() == 0) {
                        myDynamicManager.removeClassElement(classElement);
                    }
                }

                public void redo() throws UnexpectedUndoException {
                    addElement(mySettings);
                }
            });

            addElement(mySettings);
        }
    }, "Add dynamic element", null);
}