use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ResourceFolderRepositoryTest method testAddIntegerArrayItemElements.
public void testAddIntegerArrayItemElements() throws Exception {
resetScanCounter();
VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "integers"));
ResourceItem array = getOnlyItem(resources, ResourceType.ARRAY, "integers");
ResourceValue resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("10", resourceValue.getValue());
assertTrue(resourceValue instanceof ArrayResourceValue);
ArrayResourceValue arv = (ArrayResourceValue) resourceValue;
assertEquals(2, arv.getElementCount());
assertEquals("10", arv.getElement(0));
assertEquals("20", arv.getElement(1));
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int offset = document.getText().indexOf("<item>10</item>");
document.insertString(offset, "<item>5</item>");
documentManager.commitDocument(document);
}
});
// First edit won't be incremental (file -> Psi).
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "integers"));
array = getOnlyItem(resources, ResourceType.ARRAY, "integers");
resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("5", resourceValue.getValue());
assertTrue(resourceValue instanceof ArrayResourceValue);
arv = (ArrayResourceValue) resourceValue;
assertEquals(3, arv.getElementCount());
assertEquals("5", arv.getElement(0));
assertEquals("10", arv.getElement(1));
assertEquals("20", arv.getElement(2));
resetScanCounter();
// Now try a second edit.
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int offset = document.getText().indexOf("<item>5</item>");
document.insertString(offset, "<item>2</item>");
documentManager.commitDocument(document);
}
});
assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "integers"));
array = getOnlyItem(resources, ResourceType.ARRAY, "integers");
resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("2", resourceValue.getValue());
assertTrue(resourceValue instanceof ArrayResourceValue);
arv = (ArrayResourceValue) resourceValue;
assertEquals(4, arv.getElementCount());
assertEquals("2", arv.getElement(0));
assertEquals("5", arv.getElement(1));
assertEquals("10", arv.getElement(2));
assertEquals("20", arv.getElement(3));
// Shouldn't have done any full file rescans during the above edits
ensureIncremental();
}
use of com.android.ide.common.res2.ResourceItem in project paraphrase by JakeWharton.
the class ValueResourceParser method getResource.
/**
* Returns a new ResourceItem object for a given node.
* @param node the node representing the resource.
* @return a ResourceItem object or null.
*/
static ResourceItem getResource(Node node) {
ResourceType type = getType(node);
String name = getName(node);
if (type != null && name != null) {
return new ResourceItem(name, type, node);
}
return null;
}
use of com.android.ide.common.res2.ResourceItem in project paraphrase by JakeWharton.
the class ValueResourceParser method parseFile.
/**
* Parses the file and returns a list of {@link ResourceItem} objects.
* @return a list of resources.
*
* @throws MergingException if a merging exception happens
*/
@NonNull
private List<ResourceItem> parseFile() throws MergingException {
Document document = parseDocument(file);
// get the root node
Node rootNode = document.getDocumentElement();
if (rootNode == null) {
return Collections.emptyList();
}
NodeList nodes = rootNode.getChildNodes();
final int count = nodes.getLength();
// list containing the result
List<ResourceItem> resources = Lists.newArrayListWithExpectedSize(count);
for (int i = 0, n = nodes.getLength(); i < n; i++) {
Node node = nodes.item(i);
if (node.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
ResourceItem resource = getResource(node);
if (resource != null) {
resources.add(resource);
}
}
return resources;
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class Parameter method existsResourceFile.
/**
* @deprecated Replaced by {@link AssetStudioUtils#resourceExists(AndroidProjectPaths, ResourceFolderType, String)}
* TODO: After Wizard migration, delete this
*/
public static boolean existsResourceFile(@Nullable SourceProvider sourceProvider, @Nullable Module module, @NotNull ResourceFolderType resourceFolderType, @NotNull ResourceType resourceType, @Nullable String name) {
if (name == null || name.isEmpty() || sourceProvider == null) {
return false;
}
AndroidFacet facet = module != null ? AndroidFacet.getInstance(module) : null;
for (File resDir : sourceProvider.getResDirectories()) {
if (facet != null) {
VirtualFile virtualResDir = VfsUtil.findFileByIoFile(resDir, false);
if (virtualResDir != null) {
ResourceFolderRepository folderRepository = ResourceFolderRegistry.get(facet, virtualResDir);
List<ResourceItem> resourceItemList = folderRepository.getResourceItem(resourceType, name);
if (resourceItemList != null && !resourceItemList.isEmpty()) {
return true;
}
}
} else if (existsResourceFile(resDir, resourceFolderType, name)) {
return true;
}
}
return false;
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class StringResourceData method setTranslation.
public boolean setTranslation(@NotNull String key, @Nullable Locale locale, @NotNull String value) {
StringResource stringResource = getStringResource(key);
ResourceItem currentItem = locale == null ? stringResource.getDefaultValueAsResourceItem() : stringResource.getTranslationAsResourceItem(locale);
if (currentItem != null) {
// modify existing item
CharSequence oldText = locale == null ? stringResource.getDefaultValueAsString() : stringResource.getTranslationAsString(locale);
if (!StringUtil.equals(oldText, value)) {
boolean changed = StringsWriteUtils.setItemText(myFacet.getModule().getProject(), currentItem, value);
if (changed) {
if (value.isEmpty()) {
if (locale == null) {
stringResource.removeDefaultValue();
} else {
stringResource.removeTranslation(locale);
}
} else {
if (locale == null) {
stringResource.setDefaultValue(currentItem, value);
} else {
stringResource.putTranslation(locale, currentItem, value);
}
}
}
return changed;
}
} else {
// create new item
@SuppressWarnings("deprecation") VirtualFile primaryResourceDir = myFacet.getPrimaryResourceDir();
assert primaryResourceDir != null;
boolean translatable = stringResource.isTranslatable();
ResourceItem item = StringsWriteUtils.createItem(myFacet, primaryResourceDir, locale, key, value, translatable);
if (item != null) {
if (locale == null) {
stringResource.setDefaultValue(item, value);
} else {
stringResource.putTranslation(locale, item, value);
}
return true;
}
return false;
}
return false;
}
Aggregations