Search in sources :

Example 51 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class StringResourceData method validateTranslation.

@Nullable
public String validateTranslation(@NotNull String key, @Nullable Locale locale) {
    if (!myKeyToResourceMap.keySet().contains(key)) {
        throw new IllegalArgumentException("Key " + key + " does not exist.");
    }
    StringResource stringResource = getStringResource(key);
    if (locale == null) {
        ResourceItem item = stringResource.getDefaultValueAsResourceItem();
        return (item == null) ? String.format("Key '%1$s' is missing the default value", key) : null;
    }
    final boolean translationMissing = stringResource.isTranslationMissing(locale);
    final boolean doNotTranslate = !stringResource.isTranslatable();
    if (translationMissing && !doNotTranslate) {
        return String.format("Key '%1$s' is missing %2$s translation", key, getLabel(locale));
    } else if (doNotTranslate && !translationMissing) {
        return "Key '" + key + "' is marked as untranslatable and should not be translated to " + getLabel(locale);
    }
    return null;
}
Also used : ResourceItem(com.android.ide.common.res2.ResourceItem) Nullable(org.jetbrains.annotations.Nullable)

Example 52 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class StringResourceData method setTranslatable.

public boolean setTranslatable(String key, boolean translatable) {
    StringResource stringResource = getStringResource(key);
    ResourceItem item = stringResource.getDefaultValueAsResourceItem();
    if (item != null) {
        String translatableAsString;
        if (translatable) {
            translatableAsString = null;
            stringResource.setTranslatable(true);
        } else {
            translatableAsString = SdkConstants.VALUE_FALSE;
            stringResource.setTranslatable(false);
        }
        return StringsWriteUtils.setAttributeForItems(myFacet.getModule().getProject(), SdkConstants.ATTR_TRANSLATABLE, translatableAsString, Collections.singletonList(item));
    }
    return false;
}
Also used : ResourceItem(com.android.ide.common.res2.ResourceItem)

Example 53 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class DynamicResourceValueRepository method addValues.

private void addValues(Map<String, ClassField> resValues) {
    for (Map.Entry<String, ClassField> entry : resValues.entrySet()) {
        ClassField field = entry.getValue();
        String name = field.getName();
        assert entry.getKey().equals(name) : entry.getKey() + " vs " + name;
        ResourceType type = ResourceType.getEnum(field.getType());
        if (type == null) {
            LOG.warn("Ignoring field " + name + "(" + field + "): unknown type " + field.getType());
            continue;
        }
        ListMultimap<String, ResourceItem> map = mItems.get(type);
        if (map == null) {
            map = ArrayListMultimap.create();
            mItems.put(type, map);
        } else if (map.containsKey(name)) {
            // Masked by higher priority source provider
            continue;
        }
        ResourceItem item = new DynamicResourceValueItem(type, field);
        map.put(name, item);
    }
}
Also used : ResourceType(com.android.resources.ResourceType) ResourceItem(com.android.ide.common.res2.ResourceItem) Map(java.util.Map) ClassField(com.android.builder.model.ClassField)

Example 54 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class ThemeEditorStyle method getStyleResourceItems.

/**
   * Returns all the {@link ResourceItem} where this style is defined. This includes all the definitions in the
   * different resource folders.
   */
@NotNull
protected Collection<ResourceItem> getStyleResourceItems() {
    assert !isFramework();
    Collection<ResourceItem> resultItems;
    final Module module = myManager.getModule();
    if (isProjectStyle()) {
        AndroidFacet facet = AndroidFacet.getInstance(module);
        assert facet != null : module.getName() + " module doesn't have AndroidFacet";
        // We need to keep a Set of ResourceItems to override them. The key is the folder configuration + the name
        final HashMap<String, ResourceItem> resourceItems = Maps.newHashMap();
        ThemeEditorUtils.acceptResourceResolverVisitor(facet, new ThemeEditorUtils.ResourceFolderVisitor() {

            @Override
            public void visitResourceFolder(@NotNull LocalResourceRepository resources, String moduleName, @NotNull String variantName, boolean isSourceSelected) {
                if (!isSourceSelected) {
                    // TODO: Process all source sets
                    return;
                }
                List<ResourceItem> items = resources.getResourceItem(ResourceType.STYLE, myQualifiedName);
                if (items == null) {
                    return;
                }
                for (ResourceItem item : items) {
                    String key = item.getConfiguration().toShortDisplayString() + "/" + item.getName();
                    resourceItems.put(key, item);
                }
            }
        });
        resultItems = ImmutableList.copyOf(resourceItems.values());
    } else {
        LocalResourceRepository resourceRepository = AppResourceRepository.getAppResources(module, true);
        assert resourceRepository != null;
        List<ResourceItem> items = resourceRepository.getResourceItem(ResourceType.STYLE, myQualifiedName);
        if (items != null) {
            resultItems = items;
        } else {
            resultItems = Collections.emptyList();
        }
    }
    return resultItems;
}
Also used : AndroidFacet(org.jetbrains.android.facet.AndroidFacet) ThemeEditorUtils(com.android.tools.idea.editors.theme.ThemeEditorUtils) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) ResourceItem(com.android.ide.common.res2.ResourceItem) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 55 with ResourceItem

use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.

the class ThemeEditorStyle method getNamePsiElement.

/**
   * Returns a PsiElement of the name attribute for this theme
   * made from a RANDOM sourceXml
   */
@Nullable
public PsiElement getNamePsiElement() {
    Collection<ResourceItem> resources = getStyleResourceItems();
    if (resources.isEmpty()) {
        return null;
    }
    // Any sourceXml will do to get the name attribute from
    final XmlTag sourceXml = LocalResourceRepository.getItemTag(myManager.getProject(), resources.iterator().next());
    assert sourceXml != null;
    final XmlAttribute nameAttribute = sourceXml.getAttribute("name");
    if (nameAttribute == null) {
        return null;
    }
    XmlAttributeValue attributeValue = nameAttribute.getValueElement();
    if (attributeValue == null) {
        return null;
    }
    return new ValueResourceElementWrapper(attributeValue);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag) ValueResourceElementWrapper(org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ResourceItem (com.android.ide.common.res2.ResourceItem)83 VirtualFile (com.intellij.openapi.vfs.VirtualFile)44 PsiFile (com.intellij.psi.PsiFile)35 Document (com.intellij.openapi.editor.Document)26 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)26 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)11 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)11 XmlTag (com.intellij.psi.xml.XmlTag)11 NotNull (org.jetbrains.annotations.NotNull)11 IOException (java.io.IOException)10 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)8 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)8 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)7 ResourceFile (com.android.ide.common.res2.ResourceFile)7 ResourceType (com.android.resources.ResourceType)7 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)7 Nullable (org.jetbrains.annotations.Nullable)7 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)6 Project (com.intellij.openapi.project.Project)6 File (java.io.File)6