Search in sources :

Example 51 with Configuration

use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.

the class AndroidThemePreviewToolWindowManager method getBestConfiguration.

@Nullable
private static /*if there is no available configuration that would select the passed file*/
Configuration getBestConfiguration(@Nullable PsiFile psiFile) {
    Module module = psiFile != null ? ModuleUtilCore.findModuleForPsiElement(psiFile) : null;
    if (module == null) {
        return null;
    }
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet == null) {
        return null;
    }
    VirtualFile virtualFile = psiFile.getVirtualFile();
    ConfigurationManager manager = facet.getConfigurationManager();
    List<VirtualFile> variations = ResourceHelper.getResourceVariations(virtualFile, false);
    if (variations.isEmpty()) {
        return manager.getConfiguration(virtualFile);
    }
    // There is more than one resource folder available so make sure we select a configuration that only matches the current file.
    Collection<FolderConfiguration> incompatible = Collections2.transform(variations, new Function<VirtualFile, FolderConfiguration>() {

        @Nullable
        @Override
        public FolderConfiguration apply(VirtualFile input) {
            assert input != null;
            return ResourceHelper.getFolderConfiguration(input);
        }
    });
    FolderConfiguration selectedFileFolderConfiguration = ResourceHelper.getFolderConfiguration(psiFile);
    if (selectedFileFolderConfiguration == null) {
        // This folder probably has invalid qualifiers or they are in the wrong order
        return null;
    }
    RestrictedConfiguration restrictedConfiguration = RestrictedConfiguration.restrict(selectedFileFolderConfiguration, incompatible);
    if (restrictedConfiguration == null) {
        // Unable to create a configuration that only matches this file
        return null;
    }
    FolderConfiguration restricted = restrictedConfiguration.getAny();
    Configuration newConfiguration = Configuration.create(manager, virtualFile, null, restricted);
    VersionQualifier newVersionQualifier = restricted.getVersionQualifier();
    if (newVersionQualifier != null) {
        IAndroidTarget realTarget = manager.getHighestApiTarget() != null ? manager.getHighestApiTarget() : manager.getTarget();
        assert realTarget != null;
        newConfiguration.setTarget(new CompatibilityRenderTarget(realTarget, newVersionQualifier.getVersion(), null));
    }
    return newConfiguration;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Configuration(com.android.tools.idea.configurations.Configuration) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) RestrictedConfiguration(com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration) VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) IAndroidTarget(com.android.sdklib.IAndroidTarget) RestrictedConfiguration(com.android.tools.idea.editors.theme.qualifiers.RestrictedConfiguration) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) CompatibilityRenderTarget(com.android.tools.idea.rendering.multi.CompatibilityRenderTarget) Module(com.intellij.openapi.module.Module) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) Nullable(org.jetbrains.annotations.Nullable) Nullable(org.jetbrains.annotations.Nullable)

Example 52 with Configuration

use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.

the class ChooseResourceDialog method ensurePickersInitialized.

private void ensurePickersInitialized() {
    boolean allowDrawables = allowDrawables();
    boolean allowColors = allowColors();
    if (allowColors || allowDrawables) {
        if (myStateListPicker != null || myColorPicker != null) {
            return;
        }
        Configuration configuration = getConfiguration();
        ResourceResolver resolver = configuration.getResourceResolver();
        assert resolver != null;
        ResourceValue resValue = null;
        if (myInitialValue != null) {
            resValue = resolver.findResValue(myInitialValue, myInitialValueIsFramework);
        }
        final ResourceType stateListType;
        final ResourceFolderType stateListFolderType;
        if (allowDrawables) {
            stateListType = ResourceType.DRAWABLE;
            stateListFolderType = ResourceFolderType.DRAWABLE;
        } else {
            stateListType = ResourceType.COLOR;
            stateListFolderType = ResourceFolderType.COLOR;
        }
        initializeStateListPicker(configuration, resolver, resValue, stateListType, stateListFolderType);
        initializeColorPicker(myInitialValue, myResourceNameVisibility, resolver, resValue);
    }
}
Also used : Configuration(com.android.tools.idea.configurations.Configuration) ResourceFolderType(com.android.resources.ResourceFolderType) ResourceResolver(com.android.ide.common.resources.ResourceResolver) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceType(com.android.resources.ResourceType)

Example 53 with Configuration

use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.

the class ResourceDrawablePanel method updateResolutionChain.

private void updateResolutionChain(@NotNull ResourceChooserItem item) {
    // Resource resolver
    myResolvedPanel.removeAll();
    ResourceValue resourceValue = item.getResourceValue();
    Configuration configuration = myDialog.getConfiguration();
    ResourceRepository frameworkResources = configuration.getFrameworkResources();
    if (frameworkResources != null) {
        AppResourceRepository appResources = AppResourceRepository.getAppResources(myDialog.geFacet(), true);
        ResourceItemResolver resolver = new ResourceItemResolver(configuration.getFullConfig(), frameworkResources, appResources, null);
        List<ResourceValue> lookupChain = Lists.newArrayList();
        lookupChain.add(resourceValue);
        resolver.setLookupChainList(lookupChain);
        resolver.resolveResValue(resourceValue);
        String prev = null;
        int indent = 0;
        if (lookupChain.size() >= 2) {
            for (ResourceValue element : lookupChain) {
                if (element == null) {
                    continue;
                }
                String value = element.getValue();
                if (value == null) {
                    continue;
                }
                String text = value;
                if (text.equals(prev)) {
                    continue;
                }
                // Strip paths
                if (!(text.startsWith(PREFIX_THEME_REF) || text.startsWith(PREFIX_RESOURCE_REF))) {
                    if (indent == 0) {
                        break;
                    }
                    int end = Math.max(text.lastIndexOf('/'), text.lastIndexOf('\\'));
                    if (end != -1) {
                        text = text.substring(end + 1);
                    }
                }
                if (indent > 0) {
                    // 21D2: Rightwards arrow
                    text = "⇒ " + text;
                }
                JBLabel label = new JBLabel(text);
                label.setBorder(IdeBorderFactory.createEmptyBorder(0, JBUI.scale(indent * 12), 0, 0));
                myResolvedPanel.add(label);
                indent++;
                prev = value;
            }
        }
    }
}
Also used : FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configuration(com.android.tools.idea.configurations.Configuration) JBLabel(com.intellij.ui.components.JBLabel) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceItemResolver(com.android.ide.common.resources.ResourceItemResolver) AppResourceRepository(com.android.tools.idea.res.AppResourceRepository) ResourceRepository(com.android.ide.common.resources.ResourceRepository)

Example 54 with Configuration

use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.

the class ThemeAttributeResolverTest method testResolveAllEnum.

/**
   * Tests {@link ThemeAttributeResolver#resolveAll(ConfiguredThemeEditorStyle, ThemeResolver)}
   */
public void testResolveAllEnum() {
    VirtualFile myFile = myFixture.copyFileToProject("themeEditor/styles.xml", "res/values/styles.xml");
    VirtualFile resourceDir = myFile.getParent().getParent();
    Configuration configuration = myFacet.getConfigurationManager().getConfiguration(myFile);
    createNewStyle(resourceDir, "ThemeA", "android:Theme", "red", Lists.newArrayList("values-port", "values-square", "values-land"));
    createNewStyle(resourceDir, "ThemeB", "ThemeA", null, Lists.newArrayList("values", "values-port"));
    // ResourceFolderRepository needs to rescan the files to pick up the changes.
    UIUtil.dispatchAllInvocationEvents();
    ThemeResolver themeResolver = new ThemeResolver(configuration);
    ConfiguredThemeEditorStyle style = themeResolver.getTheme("ThemeB");
    assertNotNull(style);
    Set<String> answer = Sets.newHashSet("-port:red", "-land:red", "-square:red");
    List<EditedStyleItem> items = ThemeAttributeResolver.resolveAll(style, configuration.getConfigurationManager());
    boolean foundColorPrimary = false;
    for (EditedStyleItem item : items) {
        if (item.getName().equals("colorPrimary") && item.getAttrGroup().equals("Other non-theme attributes.")) {
            foundColorPrimary = true;
            assertEquals(answer.size(), item.getAllConfiguredItems().size());
            for (ConfiguredElement<ItemResourceValue> value : item.getAllConfiguredItems()) {
                assertTrue(answer.contains(value.getConfiguration().getUniqueKey() + ":" + value.getElement().getValue()));
            }
        }
    }
    assertTrue(foundColorPrimary);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Configuration(com.android.tools.idea.configurations.Configuration) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle) EditedStyleItem(com.android.tools.idea.editors.theme.datamodels.EditedStyleItem)

Example 55 with Configuration

use of com.android.tools.idea.configurations.Configuration in project android by JetBrains.

the class ConfiguredThemeEditorStyleTest method testGetParentNamesParenInName.

/**
   * Test {@link ConfiguredThemeEditorStyle#getParentNames()} for a style:
   * <style name="ATheme.Red"></style>, i.e parent defined in the name
   */
public void testGetParentNamesParenInName() {
    VirtualFile virtualFile = myFixture.copyFileToProject("themeEditor/themeEditorStyle/styles.xml", "res/values/styles.xml");
    myFixture.copyFileToProject("themeEditor/themeEditorStyle/styles_1.xml", "res/values-v19/styles.xml");
    ConfigurationManager configurationManager = myFacet.getConfigurationManager();
    Configuration configuration = configurationManager.getConfiguration(virtualFile);
    ThemeResolver resolver = new ThemeResolver(configuration);
    ConfiguredThemeEditorStyle theme = resolver.getTheme("ATheme.Red");
    assertNotNull(theme);
    HashSet<String> parents = Sets.newHashSet();
    for (ConfiguredElement<String> parent : theme.getParentNames()) {
        parents.add(parent.getElement());
    }
    assertEquals(2, parents.size());
    assertContainsElements(parents, "ATheme", "BTheme");
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configuration(com.android.tools.idea.configurations.Configuration) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) ConfiguredThemeEditorStyle(com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)

Aggregations

Configuration (com.android.tools.idea.configurations.Configuration)95 VirtualFile (com.intellij.openapi.vfs.VirtualFile)38 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)23 ConfiguredThemeEditorStyle (com.android.tools.idea.editors.theme.datamodels.ConfiguredThemeEditorStyle)21 ResourceResolver (com.android.ide.common.resources.ResourceResolver)16 ConfigurationManager (com.android.tools.idea.configurations.ConfigurationManager)14 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)10 Module (com.intellij.openapi.module.Module)9 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)8 IAndroidTarget (com.android.sdklib.IAndroidTarget)7 Device (com.android.sdklib.devices.Device)7 State (com.android.sdklib.devices.State)7 NotNull (org.jetbrains.annotations.NotNull)7 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)6 EditedStyleItem (com.android.tools.idea.editors.theme.datamodels.EditedStyleItem)5 CompatibilityRenderTarget (com.android.tools.idea.rendering.multi.CompatibilityRenderTarget)5 NlModel (com.android.tools.idea.uibuilder.model.NlModel)4 DesignSurface (com.android.tools.idea.uibuilder.surface.DesignSurface)4 PsiFile (com.intellij.psi.PsiFile)4 ResourceFolderType (com.android.resources.ResourceFolderType)3