Search in sources :

Example 21 with ResourceItem

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

the class ThemeEditorUtilsTest method testCopyThemeVersionOverride.

/**
   * Tests copyTheme method for following cases:
   * 1. copyTheme(21, "values-en-night")
   * 2. copyTheme(21, "values-v19")
   */
public void testCopyThemeVersionOverride() {
    myFixture.copyFileToProject("themeEditor/styles_1.xml", "res/values-en-night/styles.xml");
    myFixture.copyFileToProject("themeEditor/styles_1.xml", "res/values-v19/styles.xml");
    LocalResourceRepository repository = AppResourceRepository.getAppResources(myModule, true);
    assertNotNull(repository);
    final List<ResourceItem> styleItems = repository.getResourceItem(ResourceType.STYLE, "AppTheme");
    assertNotNull(styleItems);
    assertEquals(2, styleItems.size());
    new WriteCommandAction.Simple(myModule.getProject(), "Copy a theme") {

        @Override
        protected void run() throws Throwable {
            for (ResourceItem styleItem : styleItems) {
                XmlTag styleTag = LocalResourceRepository.getItemTag(getProject(), styleItem);
                assertNotNull(styleTag);
                ThemeEditorUtils.copyTheme(21, styleTag);
            }
        }
    }.execute();
    myFixture.checkResultByFile("res/values-en-night-v21/styles.xml", "themeEditor/styles_1.xml", true);
    myFixture.checkResultByFile("res/values-v21/styles.xml", "themeEditor/styles_1.xml", true);
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag)

Example 22 with ResourceItem

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

the class StringResourceParser method parseUnderReadLock.

@NotNull
private static StringResourceData parseUnderReadLock(AndroidFacet facet, LocalResourceRepository repository) {
    List<String> keys = Lists.newArrayList(repository.getItemsOfType(ResourceType.STRING));
    Collections.sort(keys);
    Map<String, StringResource> keyToResourceMap = new HashMap<>();
    Project project = facet.getModule().getProject();
    for (String key : keys) {
        List<ResourceItem> items = repository.getResourceItem(ResourceType.STRING, key);
        if (items == null) {
            continue;
        }
        StringResource stringResource = new StringResource(key);
        for (ResourceItem item : items) {
            XmlTag tag = LocalResourceRepository.getItemTag(project, item);
            if (tag != null && SdkConstants.VALUE_FALSE.equals(tag.getAttributeValue(SdkConstants.ATTR_TRANSLATABLE))) {
                stringResource.setTranslatable(false);
            }
            String itemStringRepresentation = resourceToString(project, item);
            FolderConfiguration config = item.getConfiguration();
            LocaleQualifier qualifier = config == null ? null : config.getLocaleQualifier();
            if (qualifier == null) {
                stringResource.setDefaultValue(item, itemStringRepresentation);
            } else {
                Locale locale = Locale.create(qualifier);
                stringResource.putTranslation(locale, item, itemStringRepresentation);
            }
        }
        keyToResourceMap.put(key, stringResource);
    }
    return new StringResourceData(facet, keyToResourceMap);
}
Also used : Locale(com.android.tools.idea.rendering.Locale) HashMap(java.util.HashMap) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Project(com.intellij.openapi.project.Project) ResourceItem(com.android.ide.common.res2.ResourceItem) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with ResourceItem

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

the class StringsWriteUtils method getStringResourceItem.

@Nullable
private static ResourceItem getStringResourceItem(@NotNull AndroidFacet facet, @NotNull String key, @Nullable Locale locale) {
    LocalResourceRepository repository = facet.getModuleResources(true);
    // Ensure that items *just* created are processed by the resource repository
    repository.sync();
    List<ResourceItem> items = repository.getResourceItem(ResourceType.STRING, key);
    if (items == null) {
        return null;
    }
    for (ResourceItem item : items) {
        FolderConfiguration config = item.getConfiguration();
        LocaleQualifier qualifier = config == null ? null : config.getLocaleQualifier();
        if (qualifier == null) {
            if (locale == null) {
                return item;
            } else {
                continue;
            }
        }
        Locale l = Locale.create(qualifier);
        if (l.equals(locale)) {
            return item;
        }
    }
    return null;
}
Also used : Locale(com.android.tools.idea.rendering.Locale) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with ResourceItem

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

the class StringsWriteUtils method createItem.

/**
   * Creates a string resource in the specified locale.
   *
   * @return the resource item that was created, null if it wasn't created or could not be read back
   */
@Nullable
public static ResourceItem createItem(@NotNull final AndroidFacet facet, @NotNull VirtualFile resFolder, @Nullable final Locale locale, @NotNull final String name, @NotNull final String value, final boolean translatable) {
    Project project = facet.getModule().getProject();
    XmlFile resourceFile = getStringResourceFile(project, resFolder, locale);
    if (resourceFile == null) {
        return null;
    }
    final XmlTag root = resourceFile.getRootTag();
    if (root == null) {
        return null;
    }
    new WriteCommandAction.Simple(project, "Creating string " + name, resourceFile) {

        @Override
        public void run() {
            XmlTag child = root.createChildTag(ResourceType.STRING.getName(), root.getNamespace(), escapeResourceStringAsXml(value), false);
            child.setAttribute(SdkConstants.ATTR_NAME, name);
            // XmlTagImpl handles a null value by deleting the attribute, which is our desired behavior
            //noinspection ConstantConditions
            child.setAttribute(SdkConstants.ATTR_TRANSLATABLE, translatable ? null : SdkConstants.VALUE_FALSE);
            root.addSubTag(child, false);
        }
    }.execute();
    if (ApplicationManager.getApplication().isReadAccessAllowed()) {
        return getStringResourceItem(facet, name, locale);
    } else {
        return ApplicationManager.getApplication().runReadAction(new Computable<ResourceItem>() {

            @Override
            public ResourceItem compute() {
                return getStringResourceItem(facet, name, locale);
            }
        });
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with ResourceItem

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

the class StringsWriteUtils method setAttributeForItems.

/**
   * Sets the value of an attribute for resource items.  If SdkConstants.ATTR_NAME is set to null or "", the items are deleted.
   *
   * @param attribute The attribute whose value we wish to change
   * @param value     The desired attribute value
   * @param items     The resource items
   * @return True if the value was successfully set, false otherwise
   */
public static boolean setAttributeForItems(@NotNull Project project, @NotNull final String attribute, @Nullable final String value, @NotNull List<ResourceItem> items) {
    if (items.isEmpty()) {
        return false;
    }
    final List<XmlTag> tags = Lists.newArrayListWithExpectedSize(items.size());
    final Set<PsiFile> files = Sets.newHashSetWithExpectedSize(items.size());
    for (ResourceItem item : items) {
        XmlTag tag = LocalResourceRepository.getItemTag(project, item);
        if (tag == null) {
            return false;
        }
        tags.add(tag);
        files.add(tag.getContainingFile());
    }
    final boolean deleteTag = attribute.equals(SdkConstants.ATTR_NAME) && (value == null || value.isEmpty());
    new WriteCommandAction.Simple(project, "Setting attribute " + attribute, files.toArray(new PsiFile[files.size()])) {

        @Override
        public void run() {
            for (XmlTag tag : tags) {
                if (deleteTag) {
                    tag.delete();
                } else {
                    // XmlTagImpl handles a null value by deleting the attribute, which is our desired behavior
                    //noinspection ConstantConditions
                    tag.setAttribute(attribute, value);
                }
            }
        }
    }.execute();
    return true;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag)

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