Search in sources :

Example 46 with ResourceItem

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();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) ResourceItem(com.android.ide.common.res2.ResourceItem) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 47 with ResourceItem

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;
}
Also used : ResourceType(com.android.resources.ResourceType) ResourceItem(com.android.ide.common.res2.ResourceItem)

Example 48 with ResourceItem

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;
}
Also used : Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) ResourceItem(com.android.ide.common.res2.ResourceItem) NonNull(com.android.annotations.NonNull)

Example 49 with ResourceItem

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceFolderRepository(com.android.tools.idea.res.ResourceFolderRepository) ResourceItem(com.android.ide.common.res2.ResourceItem) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 50 with ResourceItem

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceItem(com.android.ide.common.res2.ResourceItem)

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