Search in sources :

Example 6 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class AndroidIconProvider method getIcon.

@Nullable
@Override
public Icon getIcon(@NotNull PsiElement element, @Iconable.IconFlags int flags) {
    if (element instanceof XmlFile) {
        final VirtualFile file = ((XmlFile) element).getVirtualFile();
        if (file == null) {
            return null;
        }
        if (FN_ANDROID_MANIFEST_XML.equals(file.getName())) {
            return AndroidIcons.ManifestFile;
        }
        VirtualFile parent = file.getParent();
        if (parent != null) {
            String parentName = parent.getName();
            // Check whether resource folder is (potentially) a resource folder with qualifiers
            int index = parentName.indexOf('-');
            if (index != -1) {
                FolderConfiguration config = FolderConfiguration.getConfigForFolder(parentName);
                if (config != null && config.getLocaleQualifier() != null && ResourceFolderType.getFolderType(parentName) != null) {
                    // If resource folder qualifier specifies locale, show a flag icon
                    return FlagManager.get().getFlag(config);
                }
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class PsiResourceItem method getSource.

@Nullable
@Override
public ResourceFile getSource() {
    ResourceFile source = super.getSource();
    // Temporary safety workaround
    if (source == null && myFile != null && myFile.getParent() != null) {
        PsiDirectory parent = myFile.getParent();
        if (parent != null) {
            String name = parent.getName();
            ResourceFolderType folderType = ResourceFolderType.getFolderType(name);
            FolderConfiguration configuration = FolderConfiguration.getConfigForFolder(name);
            int index = name.indexOf('-');
            String qualifiers = index == -1 ? "" : name.substring(index + 1);
            source = new PsiResourceFile(myFile, Collections.<ResourceItem>singletonList(this), qualifiers, folderType, configuration);
            setSource(source);
        }
    }
    return source;
}
Also used : ResourceFile(com.android.ide.common.res2.ResourceFile) ResourceFolderType(com.android.resources.ResourceFolderType) PsiDirectory(com.intellij.psi.PsiDirectory) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class ThemeEditorUtils method showCreateNewStyleDialog.

/**
   * Creates a new style by displaying the dialog of the {@link NewStyleDialog}.
   * @param defaultParentStyle is used in NewStyleDialog, will be preselected in the parent text field and name will be suggested based on it
   * @param themeEditorContext  current theme editor context
   * @param isTheme whether theme or style will be created
   * @param message is used in NewStyleDialog to display message to user
   * @return the new style name or null if the style wasn't created
   */
@Nullable
public static String showCreateNewStyleDialog(@Nullable ConfiguredThemeEditorStyle defaultParentStyle, @NotNull final ThemeEditorContext themeEditorContext, boolean isTheme, boolean enableParentChoice, @Nullable final String message, @Nullable ThemeSelectionPanel.ThemeChangedListener themeChangedListener) {
    // if isTheme is true, defaultParentStyle shouldn't be null
    String defaultParentStyleName = null;
    if (isTheme && defaultParentStyle == null) {
        ImmutableList<String> defaultThemes = getDefaultThemeNames(themeEditorContext.getThemeResolver());
        defaultParentStyleName = !defaultThemes.isEmpty() ? defaultThemes.get(0) : null;
    } else if (defaultParentStyle != null) {
        defaultParentStyleName = defaultParentStyle.getQualifiedName();
    }
    final NewStyleDialog dialog = new NewStyleDialog(isTheme, themeEditorContext, defaultParentStyleName, (defaultParentStyle == null) ? null : defaultParentStyle.getName(), message);
    dialog.enableParentChoice(enableParentChoice);
    if (themeChangedListener != null) {
        dialog.setThemeChangedListener(themeChangedListener);
    }
    boolean createStyle = dialog.showAndGet();
    if (!createStyle) {
        return null;
    }
    int minModuleApi = getMinApiLevel(themeEditorContext.getCurrentContextModule());
    int minAcceptableApi = ResolutionUtils.getOriginalApiLevel(ResolutionUtils.getStyleResourceUrl(dialog.getStyleParentName()), themeEditorContext.getProject());
    final String fileName = AndroidResourceUtil.getDefaultResourceFileName(ResourceType.STYLE);
    FolderConfiguration config = new FolderConfiguration();
    if (minModuleApi < minAcceptableApi) {
        VersionQualifier qualifier = new VersionQualifier(minAcceptableApi);
        config.setVersionQualifier(qualifier);
    }
    if (fileName == null) {
        LOG.error("Couldn't find a default filename for ResourceType.STYLE");
        return null;
    }
    final List<String> dirNames = Collections.singletonList(config.getFolderName(ResourceFolderType.VALUES));
    String parentStyleName = dialog.getStyleParentName();
    Module module = themeEditorContext.getCurrentContextModule();
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet == null) {
        LOG.error("Create new style for non-Android module " + module.getName());
        return null;
    }
    Project project = module.getProject();
    VirtualFile resourceDir = facet.getPrimaryResourceDir();
    if (resourceDir == null) {
        AndroidUtils.reportError(project, AndroidBundle.message("check.resource.dir.error", module.getName()));
        return null;
    }
    boolean isCreated = createNewStyle(project, resourceDir, dialog.getStyleName(), parentStyleName, fileName, dirNames);
    return isCreated ? dialog.getStyleName() : null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class ThemeEditorUtils method copyTheme.

/**
   * Copies a theme to a values folder with api version apiLevel,
   * potentially creating the necessary folder or file.
   * @param apiLevel api level of the folder the theme is copied to
   * @param toBeCopied theme to be copied
   */
public static void copyTheme(int apiLevel, @NotNull final XmlTag toBeCopied) {
    ApplicationManager.getApplication().assertWriteAccessAllowed();
    PsiFile file = toBeCopied.getContainingFile();
    assert file instanceof XmlFile : file;
    ResourceFolderType folderType = ResourceHelper.getFolderType(file);
    assert folderType != null : file;
    FolderConfiguration config = ResourceHelper.getFolderConfiguration(file);
    assert config != null : file;
    VersionQualifier qualifier = new VersionQualifier(apiLevel);
    config.setVersionQualifier(qualifier);
    String folder = config.getFolderName(folderType);
    if (folderType != ResourceFolderType.VALUES) {
        OverrideResourceAction.forkResourceFile((XmlFile) file, folder, false);
    } else {
        XmlTag tag = OverrideResourceAction.getValueTag(PsiTreeUtil.getParentOfType(toBeCopied, XmlTag.class, false));
        if (tag != null) {
            PsiDirectory dir = null;
            PsiDirectory resFolder = file.getParent();
            if (resFolder != null) {
                resFolder = resFolder.getParent();
            }
            if (resFolder != null) {
                dir = resFolder.findSubdirectory(folder);
                if (dir == null) {
                    dir = resFolder.createSubdirectory(folder);
                }
            }
            OverrideResourceAction.forkResourceValue(toBeCopied.getProject(), tag, file, dir, false);
        }
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) VersionQualifier(com.android.ide.common.resources.configuration.VersionQualifier) ResourceFolderType(com.android.resources.ResourceFolderType) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) XmlTag(com.intellij.psi.xml.XmlTag)

Example 10 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration 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)

Aggregations

FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)69 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 NotNull (org.jetbrains.annotations.NotNull)14 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)12 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)12 Nullable (org.jetbrains.annotations.Nullable)12 ResourceItem (com.android.ide.common.res2.ResourceItem)10 LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)10 DensityQualifier (com.android.ide.common.resources.configuration.DensityQualifier)9 ResourceFolderType (com.android.resources.ResourceFolderType)9 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)7 IAndroidTarget (com.android.sdklib.IAndroidTarget)7 Project (com.intellij.openapi.project.Project)7 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)6 ResourceResolver (com.android.ide.common.resources.ResourceResolver)6 LayoutDirectionQualifier (com.android.ide.common.resources.configuration.LayoutDirectionQualifier)6 ScreenOrientationQualifier (com.android.ide.common.resources.configuration.ScreenOrientationQualifier)6 ScreenSizeQualifier (com.android.ide.common.resources.configuration.ScreenSizeQualifier)6 SessionParams (com.android.ide.common.rendering.api.SessionParams)5 CountryCodeQualifier (com.android.ide.common.resources.configuration.CountryCodeQualifier)5