Search in sources :

Example 31 with XmlElement

use of com.intellij.psi.xml.XmlElement in project android by JetBrains.

the class AndroidProcessChooserDialog method collectAllProcessNames.

@NotNull
private static Set<String> collectAllProcessNames(Project project) {
    final List<AndroidFacet> facets = ProjectFacetManager.getInstance(project).getFacets(AndroidFacet.ID);
    final Set<String> result = new HashSet<String>();
    for (AndroidFacet facet : facets) {
        final String packageName = AndroidCompileUtil.getAaptManifestPackage(facet);
        if (packageName != null) {
            result.add(packageName.toLowerCase());
        }
        final Manifest manifest = facet.getManifest();
        if (manifest != null) {
            final XmlElement xmlElement = manifest.getXmlElement();
            if (xmlElement != null) {
                collectProcessNames(xmlElement, result);
            }
        }
        final AndroidModel androidModel = facet.getAndroidModel();
        if (androidModel != null) {
            result.addAll(androidModel.getAllApplicationIds());
        }
    }
    return result;
}
Also used : AndroidModel(com.android.tools.idea.model.AndroidModel) XmlElement(com.intellij.psi.xml.XmlElement) Manifest(org.jetbrains.android.dom.manifest.Manifest) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) HashSet(com.intellij.util.containers.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 32 with XmlElement

use of com.intellij.psi.xml.XmlElement in project android by JetBrains.

the class ManifestInnerClass method getFields.

@NotNull
@Override
public PsiField[] getFields() {
    if (myFieldsCache == null) {
        myFieldsCache = CachedValuesManager.getManager(getProject()).createCachedValue(new CachedValueProvider<PsiField[]>() {

            @Override
            public Result<PsiField[]> compute() {
                final Manifest manifest = myFacet.getManifest();
                if (manifest == null) {
                    return Result.create(PsiField.EMPTY_ARRAY, PsiModificationTracker.MODIFICATION_COUNT);
                }
                final List<Pair<String, String>> pairs = doGetFields(manifest);
                final PsiField[] result = new PsiField[pairs.size()];
                final PsiClassType stringType = PsiType.getJavaLangString(myManager, GlobalSearchScope.allScope(getProject()));
                final PsiElementFactory factory = JavaPsiFacade.getElementFactory(getProject());
                int i = 0;
                for (Pair<String, String> pair : pairs) {
                    final AndroidLightField field = new AndroidLightField(pair.getFirst(), ManifestInnerClass.this, stringType, true, pair.getSecond());
                    field.setInitializer(factory.createExpressionFromText("\"" + pair.getSecond() + "\"", field));
                    result[i++] = field;
                }
                final XmlElement xmlElement = manifest.getXmlElement();
                final PsiFile psiManifestFile = xmlElement != null ? xmlElement.getContainingFile() : null;
                return Result.create(result, psiManifestFile != null ? psiManifestFile : PsiModificationTracker.MODIFICATION_COUNT);
            }
        });
    }
    return myFieldsCache.getValue();
}
Also used : CachedValueProvider(com.intellij.psi.util.CachedValueProvider) Manifest(org.jetbrains.android.dom.manifest.Manifest) XmlElement(com.intellij.psi.xml.XmlElement) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with XmlElement

use of com.intellij.psi.xml.XmlElement in project android by JetBrains.

the class AndroidGotoDeclarationHandler method collectManifestElements.

private static void collectManifestElements(@NotNull String nestedClassName, @NotNull String fieldName, @NotNull AndroidFacet facet, @NotNull List<PsiElement> result) {
    final Manifest manifest = facet.getManifest();
    if (manifest == null) {
        return;
    }
    List<? extends ManifestElementWithRequiredName> list;
    if ("permission".equals(nestedClassName)) {
        list = manifest.getPermissions();
    } else if ("permission_group".equals(nestedClassName)) {
        list = manifest.getPermissionGroups();
    } else {
        return;
    }
    for (ManifestElementWithRequiredName domElement : list) {
        final AndroidAttributeValue<String> nameAttribute = domElement.getName();
        final String name = nameAttribute.getValue();
        if (AndroidUtils.equal(name, fieldName, false)) {
            final XmlElement psiElement = nameAttribute.getXmlAttributeValue();
            if (psiElement != null) {
                result.add(psiElement);
            }
        }
    }
}
Also used : ManifestElementWithRequiredName(org.jetbrains.android.dom.manifest.ManifestElementWithRequiredName) XmlElement(com.intellij.psi.xml.XmlElement) Manifest(org.jetbrains.android.dom.manifest.Manifest)

Example 34 with XmlElement

use of com.intellij.psi.xml.XmlElement 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)

Example 35 with XmlElement

use of com.intellij.psi.xml.XmlElement in project android by JetBrains.

the class AbstractRegisterComponentAction method invoke.

@Override
public final void invoke(@NotNull Project project, @Nullable Editor editor, @Nullable PsiFile file) throws IncorrectOperationException {
    final PsiClass psiClass = editor == null ? null : extractClass(editor, file);
    if (psiClass == null) {
        return;
    }
    final AndroidFacet facet = AndroidFacet.getInstance(file);
    final Manifest manifest = facet == null ? null : facet.getManifest();
    if (manifest == null) {
        return;
    }
    final XmlElement element = manifest.getXmlElement();
    final PsiFile manifestFile = element == null ? null : element.getContainingFile();
    if (manifestFile == null) {
        return;
    }
    new WriteCommandAction.Simple(project, file, manifestFile) {

        @Override
        protected void run() throws Throwable {
            invoke(psiClass, manifest);
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlElement(com.intellij.psi.xml.XmlElement) Manifest(org.jetbrains.android.dom.manifest.Manifest) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Aggregations

XmlElement (com.intellij.psi.xml.XmlElement)69 PsiElement (com.intellij.psi.PsiElement)16 NotNull (org.jetbrains.annotations.NotNull)16 Nullable (org.jetbrains.annotations.Nullable)11 XmlTag (com.intellij.psi.xml.XmlTag)10 PsiFile (com.intellij.psi.PsiFile)7 PsiReference (com.intellij.psi.PsiReference)6 XmlAttribute (com.intellij.psi.xml.XmlAttribute)6 XmlFile (com.intellij.psi.xml.XmlFile)6 Manifest (org.jetbrains.android.dom.manifest.Manifest)5 Module (com.intellij.openapi.module.Module)4 Project (com.intellij.openapi.project.Project)4 DomElement (com.intellij.util.xml.DomElement)4 ArrayList (java.util.ArrayList)4 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)3 CompositePsiElement (com.intellij.psi.impl.source.tree.CompositePsiElement)3 SmartList (com.intellij.util.SmartList)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 ResourceType (com.android.resources.ResourceType)2 Result (com.intellij.openapi.application.Result)2