Search in sources :

Example 1 with UndoConfirmationPolicy

use of com.intellij.openapi.command.UndoConfirmationPolicy 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 2 with UndoConfirmationPolicy

use of com.intellij.openapi.command.UndoConfirmationPolicy in project android by JetBrains.

the class AndroidExtractStyleAction method doExtractStyle.

@Nullable
public static String doExtractStyle(@NotNull Module module, @NotNull final XmlTag viewTag, final boolean addStyleAttributeToTag, @Nullable MyTestConfig testConfig) {
    final PsiFile file = viewTag.getContainingFile();
    if (file == null) {
        return null;
    }
    final String dialogTitle = AndroidBundle.message("android.extract.style.title");
    final String fileName = AndroidResourceUtil.getDefaultResourceFileName(ResourceType.STYLE);
    assert fileName != null;
    final List<String> dirNames = Collections.singletonList(ResourceFolderType.VALUES.getName());
    final List<XmlAttribute> extractableAttributes = getExtractableAttributes(viewTag);
    final Project project = module.getProject();
    if (extractableAttributes.size() == 0) {
        AndroidUtils.reportError(project, "The tag doesn't contain any attributes that can be extracted", dialogTitle);
        return null;
    }
    final LayoutViewElement viewElement = getLayoutViewElement(viewTag);
    assert viewElement != null;
    final ResourceValue parentStyleValue = viewElement.getStyle().getValue();
    final String parentStyle;
    boolean supportImplicitParent = false;
    if (parentStyleValue != null) {
        parentStyle = parentStyleValue.getResourceName();
        if (ResourceType.STYLE != parentStyleValue.getType() || parentStyle == null || parentStyle.length() == 0) {
            AndroidUtils.reportError(project, "Invalid parent style reference " + parentStyleValue.toString(), dialogTitle);
            return null;
        }
        supportImplicitParent = parentStyleValue.getNamespace() == null;
    } else {
        parentStyle = null;
    }
    final String styleName;
    final List<XmlAttribute> styledAttributes;
    final VirtualFile chosenDirectory;
    final boolean searchStyleApplications;
    if (testConfig == null) {
        final ExtractStyleDialog dialog = new ExtractStyleDialog(module, fileName, supportImplicitParent ? parentStyle : null, dirNames, extractableAttributes);
        dialog.setTitle(dialogTitle);
        if (!dialog.showAndGet()) {
            return null;
        }
        searchStyleApplications = dialog.isToSearchStyleApplications();
        chosenDirectory = dialog.getResourceDirectory();
        if (chosenDirectory == null) {
            AndroidUtils.reportError(project, AndroidBundle.message("check.resource.dir.error", module.getName()));
            return null;
        }
        styledAttributes = dialog.getStyledAttributes();
        styleName = dialog.getStyleName();
    } else {
        testConfig.validate(extractableAttributes);
        chosenDirectory = testConfig.getResourceDirectory();
        styleName = testConfig.getStyleName();
        final Set<String> attrsToExtract = new HashSet<String>(Arrays.asList(testConfig.getAttributesToExtract()));
        styledAttributes = new ArrayList<XmlAttribute>();
        for (XmlAttribute attribute : extractableAttributes) {
            if (attrsToExtract.contains(attribute.getName())) {
                styledAttributes.add(attribute);
            }
        }
        searchStyleApplications = false;
    }
    final boolean[] success = { false };
    final Ref<Style> createdStyleRef = Ref.create();
    final boolean finalSupportImplicitParent = supportImplicitParent;
    new WriteCommandAction(project, "Extract Android Style '" + styleName + "'", file) {

        @Override
        protected void run(@NotNull final Result result) throws Throwable {
            final List<XmlAttribute> attributesToDelete = new ArrayList<XmlAttribute>();
            if (!AndroidResourceUtil.createValueResource(project, chosenDirectory, styleName, null, ResourceType.STYLE, fileName, dirNames, new Processor<ResourceElement>() {

                @Override
                public boolean process(ResourceElement element) {
                    assert element instanceof Style;
                    final Style style = (Style) element;
                    createdStyleRef.set(style);
                    for (XmlAttribute attribute : styledAttributes) {
                        if (SdkConstants.NS_RESOURCES.equals(attribute.getNamespace())) {
                            final StyleItem item = style.addItem();
                            item.getName().setStringValue("android:" + attribute.getLocalName());
                            item.setStringValue(attribute.getValue());
                            attributesToDelete.add(attribute);
                        }
                    }
                    if (parentStyleValue != null && (!finalSupportImplicitParent || !styleName.startsWith(parentStyle + "."))) {
                        final String aPackage = parentStyleValue.getNamespace();
                        style.getParentStyle().setStringValue((aPackage != null ? aPackage + ":" : "") + parentStyle);
                    }
                    return true;
                }
            })) {
                return;
            }
            ApplicationManager.getApplication().runWriteAction(new Runnable() {

                @Override
                public void run() {
                    for (XmlAttribute attribute : attributesToDelete) {
                        attribute.delete();
                    }
                    if (addStyleAttributeToTag) {
                        final LayoutViewElement viewElement = getLayoutViewElement(viewTag);
                        assert viewElement != null;
                        viewElement.getStyle().setStringValue("@style/" + styleName);
                    }
                }
            });
            success[0] = true;
        }

        @Override
        protected UndoConfirmationPolicy getUndoConfirmationPolicy() {
            return UndoConfirmationPolicy.REQUEST_CONFIRMATION;
        }
    }.execute();
    if (!success[0]) {
        return null;
    }
    final Style createdStyle = createdStyleRef.get();
    final XmlTag createdStyleTag = createdStyle != null ? createdStyle.getXmlTag() : null;
    if (createdStyleTag != null) {
        final AndroidFindStyleApplicationsAction.MyStyleData createdStyleData = AndroidFindStyleApplicationsAction.getStyleData(createdStyleTag);
        if (createdStyleData != null && searchStyleApplications) {
            ApplicationManager.getApplication().invokeLater(new Runnable() {

                @Override
                public void run() {
                    AndroidFindStyleApplicationsAction.doRefactoringForTag(createdStyleTag, createdStyleData, file, null);
                }
            });
        }
    }
    return styleName;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlAttribute(com.intellij.psi.xml.XmlAttribute) LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) StyleItem(org.jetbrains.android.dom.resources.StyleItem) Result(com.intellij.openapi.application.Result) UndoConfirmationPolicy(com.intellij.openapi.command.UndoConfirmationPolicy) ResourceValue(org.jetbrains.android.dom.resources.ResourceValue) Style(org.jetbrains.android.dom.resources.Style) PsiFile(com.intellij.psi.PsiFile) HashSet(com.intellij.util.containers.HashSet) ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) Project(com.intellij.openapi.project.Project) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with UndoConfirmationPolicy

use of com.intellij.openapi.command.UndoConfirmationPolicy in project android by JetBrains.

the class AndroidInlineUtil method doInlineStyleDeclaration.

static void doInlineStyleDeclaration(@NotNull Project project, @NotNull MyStyleData data, @Nullable final StyleUsageData usageData, @NotNull ErrorReporter errorReporter, @Nullable AndroidInlineTestConfig testConfig) {
    final Style style = data.myStyleElement;
    final Map<AndroidAttributeInfo, String> attributeValues = AndroidRefactoringUtil.computeAttributeMap(style, errorReporter, AndroidBundle.message("android.inline.style.title"));
    if (attributeValues == null) {
        return;
    }
    final StyleRefData parentStyleRef = AndroidRefactoringUtil.getParentStyle(style);
    boolean inlineThisOnly;
    if (testConfig != null) {
        inlineThisOnly = testConfig.isInlineThisOnly();
    } else {
        final boolean invokedOnReference = usageData != null;
        final AndroidInlineStyleDialog dialog = new AndroidInlineStyleDialog(project, data.myReferredElement, style.getXmlTag(), data.myStyleName, attributeValues, parentStyleRef, invokedOnReference, invokedOnReference);
        if (!dialog.showAndGet()) {
            return;
        }
        inlineThisOnly = dialog.isInlineThisOnly();
    }
    if (inlineThisOnly) {
        assert usageData != null;
        final PsiFile file = usageData.getFile();
        if (file == null) {
            return;
        }
        new WriteCommandAction(project, AndroidBundle.message("android.inline.style.command.name", data.myStyleName), file) {

            @Override
            protected void run(@NotNull final Result result) throws Throwable {
                usageData.inline(attributeValues, parentStyleRef);
            }

            @Override
            protected UndoConfirmationPolicy getUndoConfirmationPolicy() {
                return UndoConfirmationPolicy.REQUEST_CONFIRMATION;
            }
        }.execute();
    } else if (testConfig != null) {
        final AndroidInlineAllStyleUsagesProcessor processor = new AndroidInlineAllStyleUsagesProcessor(project, data.myReferredElement, style.getXmlTag(), data.myStyleName, attributeValues, parentStyleRef, testConfig);
        processor.setPreviewUsages(false);
        processor.run();
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Result(com.intellij.openapi.application.Result) UndoConfirmationPolicy(com.intellij.openapi.command.UndoConfirmationPolicy) Style(org.jetbrains.android.dom.resources.Style) PsiFile(com.intellij.psi.PsiFile)

Example 4 with UndoConfirmationPolicy

use of com.intellij.openapi.command.UndoConfirmationPolicy in project android by JetBrains.

the class AndroidDbManager method processAddOrRemove.

public void processAddOrRemove(final AndroidDataSource dataSource, final boolean add) {
    final Project project = myDbFacade.getProject();
    final UndoableAction action = new GlobalUndoableAction() {

        public void undo() throws UnexpectedUndoException {
            if (add) {
                removeDataSourceInner(project, dataSource);
            } else {
                addDataSourceInner(project, dataSource);
            }
        }

        public void redo() throws UnexpectedUndoException {
            if (add) {
                addDataSourceInner(project, dataSource);
            } else {
                removeDataSourceInner(project, dataSource);
            }
        }
    };
    final String commandName = add ? DatabaseMessages.message("command.name.add.data.source") : DatabaseMessages.message("command.name.remove.data.source");
    new WriteCommandAction(project, commandName) {

        protected void run(@NotNull final Result result) throws Throwable {
            action.redo();
            UndoManager.getInstance(project).undoableActionPerformed(action);
        }

        @Override
        protected UndoConfirmationPolicy getUndoConfirmationPolicy() {
            return UndoConfirmationPolicy.REQUEST_CONFIRMATION;
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) 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) Result(com.intellij.openapi.application.Result)

Aggregations

UndoConfirmationPolicy (com.intellij.openapi.command.UndoConfirmationPolicy)4 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)4 Result (com.intellij.openapi.application.Result)3 PsiFile (com.intellij.psi.PsiFile)3 GlobalUndoableAction (com.intellij.openapi.command.undo.GlobalUndoableAction)2 UndoableAction (com.intellij.openapi.command.undo.UndoableAction)2 Project (com.intellij.openapi.project.Project)2 Style (org.jetbrains.android.dom.resources.Style)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiCompiledElement (com.intellij.psi.PsiCompiledElement)1 PsiElement (com.intellij.psi.PsiElement)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlTag (com.intellij.psi.xml.XmlTag)1 HashSet (com.intellij.util.containers.HashSet)1 LayoutViewElement (org.jetbrains.android.dom.layout.LayoutViewElement)1 ResourceElement (org.jetbrains.android.dom.resources.ResourceElement)1 ResourceValue (org.jetbrains.android.dom.resources.ResourceValue)1 StyleItem (org.jetbrains.android.dom.resources.StyleItem)1 Nullable (org.jetbrains.annotations.Nullable)1