Search in sources :

Example 1 with RenameXmlAttributeProcessor

use of com.intellij.refactoring.rename.RenameXmlAttributeProcessor in project android by JetBrains.

the class AndroidResourceRenameResourceProcessor method renameElement.

@Override
public void renameElement(PsiElement element, final String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    if (element instanceof PsiField) {
        new RenameJavaVariableProcessor().renameElement(element, newName, usages, listener);
    } else {
        if (element instanceof PsiNamedElement) {
            super.renameElement(element, newName, usages, listener);
            if (element instanceof PsiFile) {
                VirtualFile virtualFile = ((PsiFile) element).getVirtualFile();
                if (virtualFile != null && !LocalHistory.getInstance().isUnderControl(virtualFile)) {
                    DocumentReference ref = DocumentReferenceManager.getInstance().create(virtualFile);
                    UndoManager.getInstance(element.getProject()).nonundoableActionPerformed(ref, false);
                }
            }
        } else if (element instanceof XmlAttributeValue) {
            new RenameXmlAttributeProcessor().renameElement(element, newName, usages, listener);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) RenameJavaVariableProcessor(com.intellij.refactoring.rename.RenameJavaVariableProcessor) RenameXmlAttributeProcessor(com.intellij.refactoring.rename.RenameXmlAttributeProcessor) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) DocumentReference(com.intellij.openapi.command.undo.DocumentReference)

Example 2 with RenameXmlAttributeProcessor

use of com.intellij.refactoring.rename.RenameXmlAttributeProcessor in project android by JetBrains.

the class AndroidApplicationPackageRenameProcessor method renameElement.

@Override
public void renameElement(PsiElement element, String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
    if (element instanceof PsiPackage) {
        final Map<GenericAttributeValue, String> newAttrValues = new HashMap<GenericAttributeValue, String>();
        final Project project = element.getProject();
        final String oldPackageQName = ((PsiPackage) element).getQualifiedName();
        final String newPackageQName = PsiUtilCore.getQualifiedNameAfterRename(oldPackageQName, newName);
        for (Module module : ModuleManager.getInstance(project).getModules()) {
            final AndroidFacet facet = AndroidFacet.getInstance(module);
            final Manifest manifest = facet != null ? facet.getManifest() : null;
            if (manifest != null) {
                final XmlElement manifestElement = manifest.getXmlElement();
                final PsiFile manifestPsiFile = manifestElement != null ? manifestElement.getContainingFile() : null;
                if (manifestPsiFile instanceof XmlFile) {
                    final String basePackage = manifest.getPackage().getValue();
                    if (basePackage == null) {
                        continue;
                    }
                    processAllAttributesToUpdate((XmlFile) manifestPsiFile, basePackage, oldPackageQName, newPackageQName, new Processor<Pair<GenericAttributeValue, String>>() {

                        @Override
                        public boolean process(Pair<GenericAttributeValue, String> pair) {
                            newAttrValues.put(pair.getFirst(), pair.getSecond());
                            return true;
                        }
                    });
                }
            }
        }
        new RenamePsiPackageProcessor().renameElement(element, newName, usages, listener);
        for (Map.Entry<GenericAttributeValue, String> e : newAttrValues.entrySet()) {
            //noinspection unchecked
            e.getKey().setStringValue(e.getValue());
        }
        return;
    }
    final PsiFile file = element.getContainingFile();
    if (!(file instanceof XmlFile)) {
        return;
    }
    final Map<GenericAttributeValue, PsiClass> attr2class = buildAttr2ClassMap((XmlFile) file);
    new RenameXmlAttributeProcessor().renameElement(element, newName, usages, listener);
    for (Map.Entry<GenericAttributeValue, PsiClass> e : attr2class.entrySet()) {
        //noinspection unchecked
        e.getKey().setValue(e.getValue());
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) HashMap(com.intellij.util.containers.HashMap) RenamePsiPackageProcessor(com.intellij.refactoring.rename.RenamePsiPackageProcessor) Manifest(org.jetbrains.android.dom.manifest.Manifest) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) RenameXmlAttributeProcessor(com.intellij.refactoring.rename.RenameXmlAttributeProcessor) XmlElement(com.intellij.psi.xml.XmlElement) Module(com.intellij.openapi.module.Module) HashMap(com.intellij.util.containers.HashMap) Map(java.util.Map) GenericAttributeValue(com.intellij.util.xml.GenericAttributeValue) Pair(com.intellij.openapi.util.Pair)

Aggregations

RenameXmlAttributeProcessor (com.intellij.refactoring.rename.RenameXmlAttributeProcessor)2 DocumentReference (com.intellij.openapi.command.undo.DocumentReference)1 Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1 Pair (com.intellij.openapi.util.Pair)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 XmlElement (com.intellij.psi.xml.XmlElement)1 XmlFile (com.intellij.psi.xml.XmlFile)1 RenameJavaVariableProcessor (com.intellij.refactoring.rename.RenameJavaVariableProcessor)1 RenamePsiPackageProcessor (com.intellij.refactoring.rename.RenamePsiPackageProcessor)1 HashMap (com.intellij.util.containers.HashMap)1 GenericAttributeValue (com.intellij.util.xml.GenericAttributeValue)1 Map (java.util.Map)1 Manifest (org.jetbrains.android.dom.manifest.Manifest)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1