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)));
}
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");
}
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;
}
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;
}
Aggregations