Search in sources :

Example 1 with ConfiguredElement

use of com.android.tools.idea.editors.theme.datamodels.ConfiguredElement in project android by JetBrains.

the class ParentRendererEditor method updateVariantsCombo.

private void updateVariantsCombo() {
    if (myItem == null) {
        myVariantsComboBox.setVisible(false);
        return;
    }
    myVariantsComboBox.setVisible(true);
    Collection<ConfiguredElement<String>> allParents = myItem.getParentNames();
    final String currentVariantColor = ColorUtil.toHex(ThemeEditorConstants.CURRENT_VARIANT_COLOR);
    final String notSelectedVariantColor = ColorUtil.toHex(ThemeEditorConstants.NOT_SELECTED_VARIANT_COLOR);
    final ArrayList<VariantsComboItem> variants = Lists.newArrayListWithCapacity(allParents.size());
    ConfiguredThemeEditorStyle currentParent = myItem.getParent(myContext.getThemeResolver());
    ConfiguredElement<String> selectedElement = null;
    if (currentParent != null) {
        //noinspection unchecked
        selectedElement = (ConfiguredElement<String>) currentParent.getConfiguration().getFullConfig().findMatchingConfigurable(ImmutableList.copyOf(allParents));
    }
    if (selectedElement == null) {
        selectedElement = allParents.iterator().next();
    }
    for (ConfiguredElement<String> configuredParent : allParents) {
        FolderConfiguration restrictedConfig = RestrictedConfiguration.restrict(configuredParent, allParents);
        String parentName = configuredParent.getElement();
        if (restrictedConfig == null) {
            // This type is not visible
            LOG.warn(String.format("For style '%1$s': Folder configuration '%2$s' can never be selected. There are no qualifiers combination that would allow selecting it.", parentName, configuredParent.getConfiguration()));
            continue;
        }
        if (configuredParent.getConfiguration().equals(selectedElement.getConfiguration())) {
            // This is the selected parent
            variants.add(0, new VariantsComboItem(String.format(ThemeEditorConstants.CURRENT_VARIANT_TEMPLATE, currentVariantColor, configuredParent.getConfiguration().toShortDisplayString()), restrictedConfig, configuredParent.getConfiguration()));
        } else {
            variants.add(new VariantsComboItem(String.format(ThemeEditorConstants.NOT_SELECTED_VARIANT_TEMPLATE, notSelectedVariantColor, configuredParent.getConfiguration().toShortDisplayString(), " - " + parentName), restrictedConfig, configuredParent.getConfiguration()));
        }
    }
    myVariantsComboBox.setModel(new CollectionComboBoxModel<VariantsComboItem>(variants, variants.get(0)));
}
Also used : ConfiguredElement(com.android.tools.idea.editors.theme.datamodels.ConfiguredElement) VariantsComboItem(com.android.tools.idea.editors.theme.attributes.variants.VariantsComboItem) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)

Example 2 with ConfiguredElement

use of com.android.tools.idea.editors.theme.datamodels.ConfiguredElement in project android by JetBrains.

the class ConfiguredThemeEditorStyleTest method testGetParentNames.

public void testGetParentNames() {
    myFixture.copyFileToProject("themeEditor/attributeResolution/styles_base.xml", "res/values/styles.xml");
    myFixture.copyFileToProject("themeEditor/attributeResolution/styles-v17.xml", "res/values-v17/styles.xml");
    myFixture.copyFileToProject("themeEditor/attributeResolution/styles-v19.xml", "res/values-v19/styles.xml");
    VirtualFile file = myFixture.copyFileToProject("themeEditor/attributeResolution/styles-v20.xml", "res/values-v20/styles.xml");
    ConfigurationManager configurationManager = myFacet.getConfigurationManager();
    Configuration configuration = configurationManager.getConfiguration(file);
    ThemeResolver resolver = new ThemeResolver(configuration);
    ConfiguredThemeEditorStyle theme = resolver.getTheme("AppTheme");
    assertNotNull(theme);
    Collection<ConfiguredElement<String>> parents = theme.getParentNames();
    assertSize(2, parents);
    ImmutableList<String> parentNames = ImmutableList.of(Iterables.get(parents, 0).getElement(), Iterables.get(parents, 1).getElement());
    assertContainsElements(parentNames, "Base.V20", "Base.V17");
    // Set API 17 and try the same resolution
    //noinspection ConstantConditions
    configuration.setTarget(new CompatibilityRenderTarget(configurationManager.getHighestApiTarget(), 17, configurationManager.getHighestApiTarget()));
    parents = theme.getParentNames();
    assertSize(2, parents);
    parentNames = ImmutableList.of(Iterables.get(parents, 0).getElement(), Iterables.get(parents, 1).getElement());
    assertContainsElements(parentNames, "Base.V20", "Base.V17");
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ConfiguredElement(com.android.tools.idea.editors.theme.datamodels.ConfiguredElement) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configuration(com.android.tools.idea.configurations.Configuration) CompatibilityRenderTarget(com.android.tools.idea.rendering.multi.CompatibilityRenderTarget) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)

Example 3 with ConfiguredElement

use of com.android.tools.idea.editors.theme.datamodels.ConfiguredElement in project android by JetBrains.

the class RestrictedConfiguration method restrict.

/**
   * Returns a restricted version of the passed configuration. The value returned will be incompatible with any other configuration in the
   * item. This configuration can be used when we want to make sure that the configuration selected will be displayed.
   * Note: allItems should contain compatible
   */
@Nullable
public static </*if there is no configuration that matches the constraints*/
T> FolderConfiguration restrict(@NotNull ConfiguredElement<T> compatible, Collection<ConfiguredElement<T>> allItems) {
    ArrayList<FolderConfiguration> incompatibleConfigurations = Lists.newArrayListWithCapacity(allItems.size());
    boolean found = false;
    for (ConfiguredElement configuredItem : allItems) {
        FolderConfiguration configuration = configuredItem.getConfiguration();
        if (configuredItem.equals(compatible)) {
            found = true;
            continue;
        }
        incompatibleConfigurations.add(configuration);
    }
    assert found;
    RestrictedConfiguration restricted = restrict(compatible.getConfiguration(), incompatibleConfigurations);
    return (restricted != null) ? restricted.getAny() : null;
}
Also used : ConfiguredElement(com.android.tools.idea.editors.theme.datamodels.ConfiguredElement) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with ConfiguredElement

use of com.android.tools.idea.editors.theme.datamodels.ConfiguredElement in project android by JetBrains.

the class ThemeAttributeResolver method resolveAll.

@NotNull
private List<EditedStyleItem> resolveAll() {
    ThemeEditorStyle theme = new ThemeEditorStyle(myManager, myStyle.getQualifiedName());
    for (FolderConfiguration folder : theme.getFolders()) {
        resolveFromInheritance(myStyle, folder, new RestrictedConfiguration(), new HashSet<String>());
    }
    List<EditedStyleItem> result = Lists.newArrayList();
    FolderConfiguration configuration = myStyle.getConfiguration().getFullConfig();
    for (String key : myItemValueMap.keySet()) {
        Collection<ConfiguredElement<ItemResourceValue>> itemValues = myItemValueMap.get(key);
        final ConfiguredElement<ItemResourceValue> selectedValue = (ConfiguredElement<ItemResourceValue>) configuration.findMatchingConfigurable(Lists.<Configurable>newArrayList(itemValues));
        if (selectedValue == null) {
            // TODO: there is NO value for this attribute in the current config,so instead we need to show "no value for current device"
            result.add(new EditedStyleItem(itemValues.iterator().next(), itemValues, myStyle));
        } else {
            itemValues.remove(selectedValue);
            assert !itemValues.contains(selectedValue);
            result.add(new EditedStyleItem(selectedValue, itemValues, myStyle));
        }
    }
    return result;
}
Also used : ConfiguredElement(com.android.tools.idea.editors.theme.datamodels.ConfiguredElement) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ThemeEditorStyle) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configurable(com.android.ide.common.resources.configuration.Configurable) RestrictedConfiguration(com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration) EditedStyleItem(com.android.tools.idea.editors.theme.datamodels.EditedStyleItem) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ConfiguredElement (com.android.tools.idea.editors.theme.datamodels.ConfiguredElement)4 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)3 ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)3 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)1 Configurable (com.android.ide.common.resources.configuration.Configurable)1 Configuration (com.android.tools.idea.configurations.Configuration)1 ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)1 VariantsComboItem (com.android.tools.idea.editors.theme.attributes.variants.VariantsComboItem)1 EditedStyleItem (com.android.tools.idea.editors.theme.datamodels.EditedStyleItem)1 ThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ThemeEditorStyle)1 RestrictedConfiguration (com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration)1 CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1