Search in sources :

Example 11 with ItemResourceValue

use of com.android.ide.common.rendering.api.ItemResourceValue in project android by JetBrains.

the class ThemeResolverTest method testLocalThemes.

public void testLocalThemes() throws IOException {
    VirtualFile myLayout = myFixture.copyFileToProject("themeEditor/layout.xml", "res/layout/layout.xml");
    VirtualFile myStyleFile = myFixture.copyFileToProject("themeEditor/styles.xml", "res/values/styles.xml");
    ConfigurationManager configurationManager = myFacet.getConfigurationManager();
    Configuration configuration = configurationManager.getConfiguration(myLayout);
    ThemeResolver themeResolver = new ThemeResolver(configuration);
    // We don't have any libraries so this will only include the project theme
    assertEquals(1, themeResolver.getLocalThemes().size());
    // No library themes
    assertEquals(0, themeResolver.getExternalLibraryThemes().size());
    assertNull("The theme is an app theme and shouldn't be returned for the android namespace", themeResolver.getTheme("android:Theme.MyTheme"));
    ConfiguredThemeEditorStyle theme = themeResolver.getTheme("Theme.MyTheme");
    assertEquals("Theme.MyTheme", theme.getName());
    assertEquals("Theme", theme.getParent().getName());
    assertEquals(1, theme.getConfiguredValues().size());
    ConfiguredElement<ItemResourceValue> value = Iterables.get(theme.getConfiguredValues(), 0);
    assertEquals("windowBackground", value.getElement().getName());
    assertEquals("@drawable/pic", value.getElement().getValue());
    // Modify a value.
    theme.setValue("android:windowBackground", "@drawable/other");
    FileDocumentManager.getInstance().saveAllDocuments();
    assertFalse(new String(myStyleFile.contentsToByteArray(), "UTF-8").contains("@drawable/pic"));
    assertTrue(new String(myStyleFile.contentsToByteArray(), "UTF-8").contains("@drawable/other"));
    // Add a value.
    theme.setValue("android:windowBackground2", "@drawable/second_background");
    FileDocumentManager.getInstance().saveAllDocuments();
    assertTrue(new String(myStyleFile.contentsToByteArray(), "UTF-8").contains("@drawable/other"));
    assertTrue(new String(myStyleFile.contentsToByteArray(), "UTF-8").contains("@drawable/second_background"));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Configuration(com.android.tools.idea.configurations.Configuration) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)

Example 12 with ItemResourceValue

use of com.android.ide.common.rendering.api.ItemResourceValue in project android by JetBrains.

the class ResolutionUtils method getAttrType.

@Nullable
public static /*if we can't work out the type, e.g a 'reference' with a '@null' value or enum*/
ResourceType getAttrType(@NotNull ItemResourceValue item, @NotNull Configuration configuration) {
    ResourceResolver resolver = configuration.getResourceResolver();
    assert resolver != null;
    ResourceValue resolvedValue = resolver.resolveResValue(item);
    ResourceType attrType = resolvedValue.getResourceType();
    if (attrType != null) {
        return attrType;
    } else {
        AttributeDefinition def = getAttributeDefinition(configuration, item);
        if (def != null) {
            for (AttributeFormat attrFormat : def.getFormats()) {
                attrType = AndroidDomUtil.getResourceType(attrFormat);
                if (attrType != null) {
                    return attrType;
                }
            }
        }
    }
    // sometimes we won't find the type of the attr, this means it's either a reference that points to @null, or a enum
    return null;
}
Also used : AttributeFormat(org.jetbrains.android.dom.attrs.AttributeFormat) ResourceResolver(com.android.ide.common.resources.ResourceResolver) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) ResourceType(com.android.resources.ResourceType) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with ItemResourceValue

use of com.android.ide.common.rendering.api.ItemResourceValue in project android by JetBrains.

the class ColorUtils method getContrastItems.

/**
   * @param styleAttributeName the name of a style attribute we want to check for contrast issues
   * Returns the set of {@link ItemResourceValue} that have to be checked in the current theme for contrast against a particular attribute.
   */
@NotNull
public static ImmutableSet<ItemResourceValue> getContrastItems(@NotNull ThemeEditorContext context, @NotNull String styleAttributeName) {
    Set<String> contrastColorSet = CONTRAST_MAP.get(styleAttributeName);
    if (contrastColorSet == null) {
        return ImmutableSet.of();
    }
    ImmutableSet.Builder<ItemResourceValue> contrastItemsBuilder = ImmutableSet.builder();
    ConfiguredThemeEditorStyle currentTheme = context.getCurrentTheme();
    assert currentTheme != null;
    for (String contrastColor : contrastColorSet) {
        ItemResourceValue contrastItem = ThemeEditorUtils.resolveItemFromParents(currentTheme, contrastColor, false);
        if (contrastItem == null) {
            contrastItem = ThemeEditorUtils.resolveItemFromParents(currentTheme, contrastColor, true);
        }
        if (contrastItem != null) {
            contrastItemsBuilder.add(contrastItem);
        }
    }
    return contrastItemsBuilder.build();
}
Also used : ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with ItemResourceValue

use of com.android.ide.common.rendering.api.ItemResourceValue in project android by JetBrains.

the class GraphicalResourceRendererEditor method updateComponentInternal.

/**
   * Sets the UI state of the passed {@link ResourceComponent} based on the given {@link EditedStyleItem}
   */
private static void updateComponentInternal(@NotNull ResourceComponent component, @NotNull final EditedStyleItem item) {
    final String currentVariantColor = ColorUtil.toHex(ThemeEditorConstants.CURRENT_VARIANT_COLOR);
    final String notSelectedVariantColor = ColorUtil.toHex(ThemeEditorConstants.NOT_SELECTED_VARIANT_COLOR);
    FolderConfiguration restrictedConfig = RestrictedConfiguration.restrict(item.getSelectedItemResourceValue(), item.getAllConfiguredItems());
    String description = String.format(ThemeEditorConstants.CURRENT_VARIANT_TEMPLATE, currentVariantColor, item.getSelectedValueConfiguration().toShortDisplayString());
    VariantsComboItem selectedItem = new VariantsComboItem(description, restrictedConfig != null ? restrictedConfig : item.getSelectedValueConfiguration(), item.getSelectedValueConfiguration());
    // All the not selected elements are sorted alphabetically
    TreeSet<VariantsComboItem> notSelectedItems = Sets.newTreeSet(VARIANTS_COMBO_ITEM_COMPARATOR);
    for (ConfiguredElement<ItemResourceValue> configuredItem : item.getNonSelectedItemResourceValues()) {
        restrictedConfig = RestrictedConfiguration.restrict(configuredItem, item.getAllConfiguredItems());
        if (restrictedConfig == null) {
            // This type is not visible
            LOG.warn(String.format("For item '%1$s': Folder configuration '%2$s' can never be selected. There are no qualifiers combination that would allow selecting it.", item.getName(), configuredItem.getConfiguration()));
            continue;
        }
        description = String.format(ThemeEditorConstants.NOT_SELECTED_VARIANT_TEMPLATE, notSelectedVariantColor, configuredItem.getConfiguration().toShortDisplayString(), " - " + configuredItem.getElement().getValue());
        notSelectedItems.add(new VariantsComboItem(description, restrictedConfig, configuredItem.getConfiguration()));
    }
    ImmutableList<VariantsComboItem> variantList = ImmutableList.<VariantsComboItem>builder().add(selectedItem).addAll(notSelectedItems).build();
    component.setVariantsModel(new CollectionComboBoxModel<VariantsComboItem>(variantList, selectedItem));
}
Also used : ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) VariantsComboItem(com.android.tools.idea.editors.theme.attributes.variants.VariantsComboItem)

Example 15 with ItemResourceValue

use of com.android.ide.common.rendering.api.ItemResourceValue in project android by JetBrains.

the class StyleListPaletteCellRenderer method customizeCellRenderer.

@Override
protected void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus) {
    super.customizeCellRenderer(list, value, index, selected, hasFocus);
    if (!(value instanceof String) || ThemesListModel.isSpecialOption((String) value)) {
        myColorPaletteComponent.reset();
        return;
    }
    ThemeResolver themeResolver = myContext.getThemeResolver();
    final ConfiguredThemeEditorStyle theme = themeResolver.getTheme((String) value);
    if (theme == null) {
        myColorPaletteComponent.reset();
        setIcon(null);
        return;
    }
    final boolean isFrameworkAttr = !ThemeEditorUtils.isAppCompatTheme(theme);
    ItemResourceValue primaryResourceValue = ThemeEditorUtils.resolveItemFromParents(theme, PRIMARY_MATERIAL, isFrameworkAttr);
    ItemResourceValue primaryDarkResourceValue = ThemeEditorUtils.resolveItemFromParents(theme, PRIMARY_DARK_MATERIAL, isFrameworkAttr);
    ItemResourceValue accentResourceValue = ThemeEditorUtils.resolveItemFromParents(theme, ACCENT_MATERIAL, isFrameworkAttr);
    //Check needed in case the xml files are inconsistent and have an item, but not a value
    if (primaryResourceValue != null && primaryDarkResourceValue != null && accentResourceValue != null) {
        Configuration configuration = theme.getConfiguration();
        ResourceResolver resourceResolver = configuration.getConfigurationManager().getResolverCache().getResourceResolver(configuration.getRealTarget(), theme.getStyleResourceUrl(), configuration.getFullConfig());
        Color primaryColor = ResourceHelper.resolveColor(resourceResolver, primaryResourceValue, myContext.getProject());
        Color primaryDarkColor = ResourceHelper.resolveColor(resourceResolver, primaryDarkResourceValue, myContext.getProject());
        Color accentColor = ResourceHelper.resolveColor(resourceResolver, accentResourceValue, myContext.getProject());
        if (primaryColor != null && primaryDarkColor != null && accentColor != null) {
            myColorPaletteComponent.setValues(primaryColor, primaryDarkColor, accentColor);
        }
        setIcon(myColorPaletteComponent);
    } else {
        myColorPaletteComponent.reset();
        setIcon(null);
    }
    if (selected) {
        myThemeChangedListener.themeChanged(theme.getQualifiedName());
    }
}
Also used : ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) Configuration(com.android.tools.idea.configurations.Configuration) ThemeResolver(com.android.tools.idea.editors.theme.ThemeResolver) ResourceResolver(com.android.ide.common.resources.ResourceResolver) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)

Aggregations

ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)28 ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)11 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 NotNull (org.jetbrains.annotations.NotNull)8 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)7 Configuration (com.android.tools.idea.configurations.Configuration)7 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)5 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)4 EditedStyleItem (com.android.tools.idea.editors.theme.datamodels.EditedStyleItem)4 ResourceItem (com.android.ide.common.res2.ResourceItem)3 ResourceResolver (com.android.ide.common.resources.ResourceResolver)3 ResourceType (com.android.resources.ResourceType)3 ResourceUrl (com.android.ide.common.resources.ResourceUrl)2 ThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ThemeEditorStyle)2 RestrictedConfiguration (com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration)2 Nullable (org.jetbrains.annotations.Nullable)2 Nullable (com.android.annotations.Nullable)1 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)1 ResourceFile (com.android.ide.common.resources.ResourceFile)1 Configurable (com.android.ide.common.resources.configuration.Configurable)1