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;
}
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;
}
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);
}
}
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;
}
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);
}
Aggregations