Search in sources :

Example 11 with ResourceElement

use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.

the class AndroidResourceUtil method addValueResource.

private static boolean addValueResource(@NotNull Project project, @NotNull VirtualFile resDir, @NotNull final String resourceName, @NotNull final ResourceType resourceType, @NotNull String fileName, @NotNull List<String> dirNames, @Nullable final String resourceValue, @NotNull final Processor<ResourceElement> afterAddedProcessor) throws Exception {
    if (dirNames.size() == 0) {
        return false;
    }
    final VirtualFile[] resFiles = new VirtualFile[dirNames.size()];
    for (int i = 0, n = dirNames.size(); i < n; i++) {
        String dirName = dirNames.get(i);
        resFiles[i] = WriteAction.compute(() -> findOrCreateResourceFile(project, resDir, fileName, dirName));
        if (resFiles[i] == null) {
            return false;
        }
    }
    if (!ReadonlyStatusHandler.ensureFilesWritable(project, resFiles)) {
        return false;
    }
    final Resources[] resourcesElements = new Resources[resFiles.length];
    for (int i = 0; i < resFiles.length; i++) {
        final Resources resources = AndroidUtils.loadDomElement(project, resFiles[i], Resources.class);
        if (resources == null) {
            AndroidUtils.reportError(project, AndroidBundle.message("not.resource.file.error", fileName));
            return false;
        }
        resourcesElements[i] = resources;
    }
    List<PsiFile> psiFiles = Lists.newArrayListWithExpectedSize(resFiles.length);
    PsiManager manager = PsiManager.getInstance(project);
    for (VirtualFile file : resFiles) {
        PsiFile psiFile = manager.findFile(file);
        if (psiFile != null) {
            psiFiles.add(psiFile);
        }
    }
    PsiFile[] files = psiFiles.toArray(new PsiFile[psiFiles.size()]);
    WriteCommandAction<Void> action = new WriteCommandAction<Void>(project, "Add Resource", files) {

        @Override
        protected void run(@NotNull Result<Void> result) {
            for (Resources resources : resourcesElements) {
                final ResourceElement element = addValueResource(resourceType, resources, resourceValue);
                element.getName().setValue(resourceName);
                afterAddedProcessor.process(element);
            }
        }
    };
    action.execute();
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) Resources(org.jetbrains.android.dom.resources.Resources)

Example 12 with ResourceElement

use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.

the class ResourceManager method findValueResources.

// not recommended to use, because it is too slow
@NotNull
public List<ResourceElement> findValueResources(@NotNull String resourceType, @NotNull String resourceName, boolean distinguishDelimitersInName) {
    final List<ValueResourceInfoImpl> resources = findValueResourceInfos(resourceType, resourceName, distinguishDelimitersInName, false);
    final List<ResourceElement> result = new ArrayList<ResourceElement>();
    for (ValueResourceInfoImpl resource : resources) {
        final ResourceElement domElement = resource.computeDomElement();
        if (domElement != null) {
            result.add(domElement);
        }
    }
    return result;
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with ResourceElement

use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.

the class ResourceManager method getValueResources.

protected List<ResourceElement> getValueResources(@NotNull final ResourceType resourceType, @Nullable Set<VirtualFile> files) {
    final List<ResourceElement> result = new ArrayList<ResourceElement>();
    List<Pair<Resources, VirtualFile>> resourceFiles = getResourceElements(files);
    for (final Pair<Resources, VirtualFile> pair : resourceFiles) {
        final Resources resources = pair.getFirst();
        ApplicationManager.getApplication().runReadAction(new Runnable() {

            @Override
            public void run() {
                if (!resources.isValid() || myProject.isDisposed()) {
                    return;
                }
                final List<ResourceElement> valueResources = AndroidResourceUtil.getValueResourcesFromElement(resourceType, resources);
                for (ResourceElement valueResource : valueResources) {
                    final String resName = valueResource.getName().getValue();
                    if (resName != null && isResourcePublic(resourceType.getName(), resName)) {
                        result.add(valueResource);
                    }
                }
            }
        });
    }
    return result;
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Resources(org.jetbrains.android.dom.resources.Resources) Pair(com.intellij.openapi.util.Pair)

Example 14 with ResourceElement

use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.

the class AndroidColorAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (element instanceof XmlTag) {
        XmlTag tag = (XmlTag) element;
        String tagName = tag.getName();
        if ((ResourceType.COLOR.getName().equals(tagName) || ResourceType.DRAWABLE.getName().equals(tagName) || ResourceType.MIPMAP.getName().equals(tagName))) {
            DomElement domElement = DomManager.getDomManager(element.getProject()).getDomElement(tag);
            if (domElement instanceof ResourceElement || ApplicationManager.getApplication().isUnitTestMode()) {
                String value = tag.getValue().getText().trim();
                annotateXml(element, holder, value);
            }
        } else if (TAG_ITEM.equals(tagName)) {
            XmlTagValue value = tag.getValue();
            String text = value.getText();
            annotateXml(element, holder, text);
        }
    } else if (element instanceof XmlAttributeValue) {
        XmlAttributeValue v = (XmlAttributeValue) element;
        String value = v.getValue();
        if (value == null || value.isEmpty()) {
            return;
        }
        annotateXml(element, holder, value);
    } else if (element instanceof PsiReferenceExpression) {
        ResourceReferenceType referenceType = AndroidPsiUtils.getResourceReferenceType(element);
        if (referenceType != ResourceReferenceType.NONE) {
            // (isResourceReference will return true for both "R.drawable.foo" and the foo literal leaf in the
            // same expression, which would result in both elements getting annotated and the icon showing up
            // in the gutter twice. Instead we only count the outer one.
            ResourceType type = AndroidPsiUtils.getResourceType(element);
            if (type == ResourceType.COLOR || type == ResourceType.DRAWABLE || type == ResourceType.MIPMAP) {
                String name = AndroidPsiUtils.getResourceName(element);
                annotateResourceReference(type, holder, element, name, referenceType == ResourceReferenceType.FRAMEWORK);
            }
        }
    }
}
Also used : ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) DomElement(com.intellij.util.xml.DomElement) XmlTagValue(com.intellij.psi.xml.XmlTagValue) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) ResourceReferenceType(com.android.tools.idea.AndroidPsiUtils.ResourceReferenceType) ResourceType(com.android.resources.ResourceType) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag)

Example 15 with ResourceElement

use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.

the class CreateXmlResourceDialog method checkIfResourceAlreadyExists.

@Nullable
public static ValidationInfo checkIfResourceAlreadyExists(@NotNull Project project, @NotNull VirtualFile resourceDir, @NotNull String resourceName, @NotNull ResourceType resourceType, @NotNull List<String> dirNames, @NotNull String fileName) {
    if (resourceName.length() == 0 || dirNames.size() == 0 || fileName.length() == 0) {
        return null;
    }
    for (String directoryName : dirNames) {
        final VirtualFile resourceSubdir = resourceDir.findChild(directoryName);
        if (resourceSubdir == null) {
            continue;
        }
        final VirtualFile resFile = resourceSubdir.findChild(fileName);
        if (resFile == null) {
            continue;
        }
        if (resFile.getFileType() != StdFileTypes.XML) {
            return new ValidationInfo("File " + FileUtil.toSystemDependentName(resFile.getPath()) + " is not XML file");
        }
        final Resources resources = AndroidUtils.loadDomElement(project, resFile, Resources.class);
        if (resources == null) {
            return new ValidationInfo(AndroidBundle.message("not.resource.file.error", FileUtil.toSystemDependentName(resFile.getPath())));
        }
        for (ResourceElement element : AndroidResourceUtil.getValueResourcesFromElement(resourceType, resources)) {
            if (resourceName.equals(element.getName().getValue())) {
                return new ValidationInfo("resource '" + resourceName + "' already exists in " + FileUtil.toSystemDependentName(resFile.getPath()));
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceElement(org.jetbrains.android.dom.resources.ResourceElement) ValidationInfo(com.intellij.openapi.ui.ValidationInfo) Resources(org.jetbrains.android.dom.resources.Resources) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ResourceElement (org.jetbrains.android.dom.resources.ResourceElement)16 XmlTag (com.intellij.psi.xml.XmlTag)7 NotNull (org.jetbrains.annotations.NotNull)7 Result (com.intellij.openapi.application.Result)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)6 DomElement (com.intellij.util.xml.DomElement)6 Nullable (org.jetbrains.annotations.Nullable)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Resources (org.jetbrains.android.dom.resources.Resources)4 Style (org.jetbrains.android.dom.resources.Style)4 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)3 ResourceType (com.android.resources.ResourceType)2 ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)2 Project (com.intellij.openapi.project.Project)2 PsiFile (com.intellij.psi.PsiFile)2 XmlAttribute (com.intellij.psi.xml.XmlAttribute)2 Processor (com.intellij.util.Processor)2 Item (org.jetbrains.android.dom.resources.Item)2 StyleItem (org.jetbrains.android.dom.resources.StyleItem)2 LocalResourceManager (org.jetbrains.android.resourceManagers.LocalResourceManager)2