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