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