Search in sources :

Example 1 with VariantsComboItem

use of com.android.tools.idea.editors.theme.attributes.variants.VariantsComboItem 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 VariantsComboItem

use of com.android.tools.idea.editors.theme.attributes.variants.VariantsComboItem 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)

Aggregations

FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)2 VariantsComboItem (com.android.tools.idea.editors.theme.attributes.variants.VariantsComboItem)2 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)1 ConfiguredElement (com.android.tools.idea.editors.theme.datamodels.ConfiguredElement)1 ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)1