Search in sources :

Example 6 with ResourceFolderType

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

the class AndroidResourceReference method handleElementRename.

@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
    if (newElementName.startsWith(SdkConstants.NEW_ID_PREFIX)) {
        newElementName = AndroidResourceUtil.getResourceNameByReferenceText(newElementName);
    }
    ResourceValue value = myValue.getValue();
    assert value != null;
    final ResourceType resType = value.getType();
    if (resType != null && newElementName != null) {
        // todo: do not allow new value resource name to contain dot, because it is impossible to check if it file or value otherwise
        final String newResName;
        // Does renamed resource point to a file?
        ResourceFolderType folderType = AndroidResourceUtil.XML_FILE_RESOURCE_TYPES.get(resType);
        if (folderType != null && newElementName.contains(".")) {
            // If it does, we need to chop off its extension when inserting the new value.
            newResName = AndroidCommonUtils.getResourceName(resType.getName(), newElementName);
        } else {
            newResName = newElementName;
        }
        // Note: We're using value.getResourceType(), not resType.getName() here, because we want the "+" in the new name
        myValue.setValue(ResourceValue.referenceTo(value.getPrefix(), value.getNamespace(), value.getResourceType(), newResName));
    }
    return myValue.getXmlTag();
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) ResourceValue(org.jetbrains.android.dom.resources.ResourceValue) ResourceType(com.android.resources.ResourceType)

Example 7 with ResourceFolderType

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

the class AndroidResourceUtil method findResourceFieldsForValueResource.

@NotNull
public static PsiField[] findResourceFieldsForValueResource(XmlTag tag, boolean onlyInOwnPackages) {
    final AndroidFacet facet = AndroidFacet.getInstance(tag);
    if (facet == null) {
        return PsiField.EMPTY_ARRAY;
    }
    ResourceFolderType fileResType = ResourceHelper.getFolderType(tag.getContainingFile());
    final String resourceType = fileResType == ResourceFolderType.VALUES ? getResourceTypeByValueResourceTag(tag) : null;
    if (resourceType == null) {
        return PsiField.EMPTY_ARRAY;
    }
    String name = tag.getAttributeValue(ATTR_NAME);
    if (name == null) {
        return PsiField.EMPTY_ARRAY;
    }
    return findResourceFields(facet, resourceType, name, onlyInOwnPackages);
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with ResourceFolderType

use of com.android.resources.ResourceFolderType 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 9 with ResourceFolderType

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

the class ResourceFolderRepository method findResourceFile.

@Nullable
private ResourceFile findResourceFile(String dirName, String fileName) {
    int index = dirName.indexOf('-');
    String qualifiers;
    String folderTypeName;
    if (index == -1) {
        qualifiers = "";
        folderTypeName = dirName;
    } else {
        qualifiers = dirName.substring(index + 1);
        folderTypeName = dirName.substring(0, index);
    }
    ResourceFolderType folderType = ResourceFolderType.getTypeByName(folderTypeName);
    for (ResourceFile resourceFile : myResourceFiles.values()) {
        String name = resourceFile.getFile().getName();
        ResourceFolderType resFolderType = ResourceHelper.getFolderType(resourceFile);
        if (folderType == resFolderType && fileName.equals(name) && qualifiers.equals(resourceFile.getQualifiers())) {
            return resourceFile;
        }
    }
    return null;
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with ResourceFolderType

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

the class ConfigurationMenuAction method createPopupActionGroup.

@Override
@NotNull
protected DefaultActionGroup createPopupActionGroup() {
    DefaultActionGroup group = new DefaultActionGroup("Configuration", true);
    Configuration configuration = mySurface.getConfiguration();
    if (configuration == null) {
        return group;
    }
    VirtualFile virtualFile = configuration.getFile();
    if (virtualFile != null) {
        Module module = configuration.getModule();
        if (module == null) {
            return group;
        }
        Project project = module.getProject();
        List<VirtualFile> variations = ResourceHelper.getResourceVariations(virtualFile, true);
        if (variations.size() > 1) {
            for (VirtualFile file : variations) {
                String title = String.format("Switch to %1$s", file.getParent().getName());
                group.add(new SwitchToVariationAction(title, project, file, virtualFile.equals(file)));
            }
            group.addSeparator();
        }
        ResourceFolderType folderType = ResourceHelper.getFolderType(configuration.getFile());
        if (folderType == ResourceFolderType.LAYOUT) {
            boolean haveLandscape = false;
            boolean haveLarge = false;
            for (VirtualFile file : variations) {
                String name = file.getParent().getName();
                if (name.startsWith(FD_RES_LAYOUT)) {
                    FolderConfiguration config = FolderConfiguration.getConfigForFolder(name);
                    if (config != null) {
                        ScreenOrientationQualifier orientation = config.getScreenOrientationQualifier();
                        if (orientation != null && orientation.getValue() == ScreenOrientation.LANDSCAPE) {
                            haveLandscape = true;
                            if (haveLarge) {
                                break;
                            }
                        }
                        ScreenSizeQualifier size = config.getScreenSizeQualifier();
                        if (size != null && size.getValue() == ScreenSize.XLARGE) {
                            haveLarge = true;
                            if (haveLandscape) {
                                break;
                            }
                        }
                    }
                }
            }
            // Do statistics on what is needed!
            if (!haveLandscape) {
                group.add(new CreateVariationAction(mySurface, "Create Landscape Variation", "layout-land"));
            }
            if (!haveLarge) {
                group.add(new CreateVariationAction(mySurface, "Create layout-xlarge Variation", "layout-xlarge"));
            //group.add(new CreateVariationAction(mySurface, "Create layout-sw600dp Variation...", "layout-sw600dp"));
            }
            group.add(new CreateVariationAction(mySurface, "Create Other...", null));
        } else {
            group.add(new CreateVariationAction(mySurface, "Create Alternative...", null));
        }
    /* TODO: Restore multi-configuration editing
      if (mySurface.supportsPreviews()) {
        addMultiConfigActions(group);
      }
      */
    }
    return group;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ScreenSizeQualifier(com.android.ide.common.resources.configuration.ScreenSizeQualifier) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) DefaultActionGroup(com.intellij.openapi.actionSystem.DefaultActionGroup) Project(com.intellij.openapi.project.Project) ScreenOrientationQualifier(com.android.ide.common.resources.configuration.ScreenOrientationQualifier) ResourceFolderType(com.android.resources.ResourceFolderType) Module(com.intellij.openapi.module.Module) 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