Search in sources :

Example 16 with ResourceFolderType

use of com.android.resources.ResourceFolderType in project android by JetBrains.

the class XmlWrongFileTypeInspection method checkFile.

@Nullable
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
    if (!(file instanceof XmlFile)) {
        return null;
    }
    final XmlFile xmlFile = (XmlFile) file;
    final XmlTag rootTag = xmlFile.getRootTag();
    if (rootTag == null) {
        return null;
    }
    final AndroidFacet facet = AndroidFacet.getInstance(file);
    if (facet == null) {
        return null;
    }
    final DomFileDescription<?> description = DomManager.getDomManager(file.getProject()).getDomFileDescription(xmlFile);
    // File is recognized correctly, there are no need to show an error
    if (description != null) {
        return null;
    }
    final PsiDirectory directory = file.getContainingDirectory();
    if (directory == null) {
        return null;
    }
    if (!AndroidResourceUtil.isResourceSubdirectory(directory, null)) {
        return null;
    }
    final String name = rootTag.getName();
    final ImmutableCollection<ResourceFolderType> folderTypes = determineResourceFolderTypeByRootTag(facet, name);
    if (folderTypes.isEmpty()) {
        return null;
    }
    final String directoryName = directory.getName();
    final String resourceType;
    final String resourceQualifier;
    // If current resource directory has a qualifier we want to preserve it while moving
    final int dashIndex = directoryName.indexOf('-');
    if (dashIndex != -1) {
        resourceType = directoryName.substring(0, dashIndex);
        resourceQualifier = directoryName.substring(dashIndex + 1);
    } else {
        resourceType = directoryName;
        resourceQualifier = null;
    }
    String folderEnumeration = AndroidTextUtils.generateCommaSeparatedList(Collections2.transform(folderTypes, TYPE_NAME_FUNCTION), "or");
    if (folderTypes.size() > 1) {
        folderEnumeration = "either " + folderEnumeration;
    }
    final String message = String.format("<%1$s> XML file should be in %2$s, not \"%3$s\"", name, folderEnumeration, resourceType);
    final ASTNode node = XmlChildRole.START_TAG_NAME_FINDER.findChild(rootTag.getNode());
    final LocalQuickFix[] quickFixes = new LocalQuickFix[folderTypes.size()];
    int i = 0;
    for (ResourceFolderType type : folderTypes) {
        final String resultFolder = resourceQualifier == null ? type.getName() : type.getName() + '-' + resourceQualifier;
        quickFixes[i++] = new MoveFileQuickFix(resultFolder, xmlFile);
    }
    final ProblemDescriptor descriptor = manager.createProblemDescriptor(node == null ? rootTag : node.getPsi(), message, isOnTheFly, quickFixes, ProblemHighlightType.GENERIC_ERROR);
    return new ProblemDescriptor[] { descriptor };
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) ResourceFolderType(com.android.resources.ResourceFolderType) PsiDirectory(com.intellij.psi.PsiDirectory) ASTNode(com.intellij.lang.ASTNode) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with ResourceFolderType

use of com.android.resources.ResourceFolderType in project android by JetBrains.

the class OverrideResourceAction method forkResourceFile.

/**
   * Create a variation (copy) of a given resource file
   *
   * @param surface   the design surface for the resource file to fork
   * @param newFolder the resource folder to create, or null to ask the user
   * @param open      if true, open the file after creating it
   */
public static void forkResourceFile(@NotNull EditorDesignSurface surface, @Nullable String newFolder, boolean open) {
    Configuration configuration = surface.getConfiguration();
    if (configuration == null) {
        assert false;
        // Should not happen
        return;
    }
    final VirtualFile file = configuration.getFile();
    if (file == null) {
        assert false;
        // Should not happen
        return;
    }
    Module module = configuration.getModule();
    if (module == null) {
        assert false;
        // Should not happen
        return;
    }
    XmlFile xmlFile = (XmlFile) configuration.getPsiFile();
    ResourceFolderType folderType = ResourceHelper.getFolderType(xmlFile);
    if (folderType == null) {
        folderType = ResourceFolderType.LAYOUT;
    }
    forkResourceFile(module.getProject(), folderType, file, xmlFile, newFolder, configuration, open);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configuration(com.android.tools.idea.configurations.Configuration) XmlFile(com.intellij.psi.xml.XmlFile) ResourceFolderType(com.android.resources.ResourceFolderType) Module(com.intellij.openapi.module.Module)

Example 18 with ResourceFolderType

use of com.android.resources.ResourceFolderType in project android by JetBrains.

the class OverrideResourceAction method forkResourceFile.

/**
   * Create a variation (copy) of a given resource file (of a given type).
   *
   * @param xmlFile     the XML resource file to fork
   * @param myNewFolder the resource folder to create, or null to ask the user
   * @param open        if true, open the file after creating it
   */
public static void forkResourceFile(@NotNull final XmlFile xmlFile, @Nullable String myNewFolder, boolean open) {
    VirtualFile file = xmlFile.getVirtualFile();
    if (file == null) {
        return;
    }
    Module module = AndroidPsiUtils.getModuleSafely(xmlFile);
    if (module == null) {
        return;
    }
    ResourceFolderType folderType = ResourceHelper.getFolderType(xmlFile);
    if (folderType == null || folderType == ResourceFolderType.VALUES) {
        return;
    }
    Configuration configuration = null;
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet != null) {
        configuration = facet.getConfigurationManager().getConfiguration(file);
    }
    forkResourceFile(module.getProject(), folderType, file, xmlFile, myNewFolder, configuration, open);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configuration(com.android.tools.idea.configurations.Configuration) ResourceFolderType(com.android.resources.ResourceFolderType) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 19 with ResourceFolderType

use of com.android.resources.ResourceFolderType in project android by JetBrains.

the class AndroidResFolderNode method getChildren.

/**
   * Returns the children of the res folder. Rather than showing the existing directory hierarchy, this merges together
   * all the folders by their {@link com.android.resources.ResourceFolderType}.
   */
@NotNull
@Override
public Collection<? extends AbstractTreeNode> getChildren() {
    // collect all res folders from all source providers
    List<PsiDirectory> resFolders = Lists.newArrayList();
    for (PsiDirectory directory : getSourceDirectories()) {
        resFolders.addAll(Lists.newArrayList(directory.getSubdirectories()));
    }
    // group all the res folders by their folder type
    HashMultimap<ResourceFolderType, PsiDirectory> foldersByResourceType = HashMultimap.create();
    for (PsiDirectory resFolder : resFolders) {
        ResourceFolderType type = ResourceFolderType.getFolderType(resFolder.getName());
        if (type == null) {
            // skip unknown folder types inside res
            continue;
        }
        foldersByResourceType.put(type, resFolder);
    }
    // create a node for each res folder type that actually has some resources
    AndroidProjectTreeBuilder treeBuilder = (AndroidProjectTreeBuilder) myProjectViewPane.getTreeBuilder();
    List<AbstractTreeNode> children = Lists.newArrayListWithExpectedSize(foldersByResourceType.size());
    for (ResourceFolderType type : foldersByResourceType.keySet()) {
        Set<PsiDirectory> folders = foldersByResourceType.get(type);
        final AndroidResFolderTypeNode androidResFolderTypeNode = new AndroidResFolderTypeNode(myProject, getValue(), Lists.newArrayList(folders), getSettings(), type, myProjectViewPane);
        children.add(androidResFolderTypeNode);
        // Inform the tree builder of the node that this particular virtual file maps to
        for (PsiDirectory folder : folders) {
            treeBuilder.createMapping(folder.getVirtualFile(), androidResFolderTypeNode);
        }
    }
    return children;
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) PsiDirectory(com.intellij.psi.PsiDirectory) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) AndroidProjectTreeBuilder(com.android.tools.idea.navigator.AndroidProjectTreeBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with ResourceFolderType

use of com.android.resources.ResourceFolderType in project android by JetBrains.

the class AndroidXmlSchemaProvider method getAvailableNamespaces.

@NotNull
@Override
public Set<String> getAvailableNamespaces(@NotNull XmlFile file, @Nullable String tagName) {
    Set<String> result = new HashSet<String>();
    AndroidFacet facet = AndroidFacet.getInstance(file);
    if (facet != null) {
        result.add(TOOLS_URI);
        ResourceFolderType type = ResourceHelper.getFolderType(file.getOriginalFile());
        if (type == ResourceFolderType.VALUES) {
            result.add(XLIFF_URI);
        } else if (type != ResourceFolderType.MIPMAP && type != ResourceFolderType.RAW) {
            result.add(NS_RESOURCES);
            if (type == ResourceFolderType.DRAWABLE) {
                result.add(AAPT_URI);
            }
            String localNs = getLocalXmlNamespace(facet);
            if (localNs != null) {
                result.add(localNs);
            }
            // Some xml files may contain xliff.
            if (type == ResourceFolderType.XML) {
                result.add(XLIFF_URI);
            }
        }
    }
    return result;
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ResourceFolderType (com.android.resources.ResourceFolderType)46 VirtualFile (com.intellij.openapi.vfs.VirtualFile)12 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)10 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)10 NotNull (org.jetbrains.annotations.NotNull)10 ResourceType (com.android.resources.ResourceType)8 XmlFile (com.intellij.psi.xml.XmlFile)8 File (java.io.File)8 PsiFile (com.intellij.psi.PsiFile)7 Nullable (org.jetbrains.annotations.Nullable)7 PsiDirectory (com.intellij.psi.PsiDirectory)6 Module (com.intellij.openapi.module.Module)5 XmlTag (com.intellij.psi.xml.XmlTag)5 Configuration (com.android.tools.idea.configurations.Configuration)3 PsiElement (com.intellij.psi.PsiElement)3 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)2 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)2 HtmlBuilder (com.android.utils.HtmlBuilder)2 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)2 Project (com.intellij.openapi.project.Project)2