use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.
the class AndroidResourceUtil method addValueResource.
private static boolean addValueResource(@NotNull Project project, @NotNull VirtualFile resDir, @NotNull final String resourceName, @NotNull final ResourceType resourceType, @NotNull String fileName, @NotNull List<String> dirNames, @Nullable final String resourceValue, @NotNull final Processor<ResourceElement> afterAddedProcessor) throws Exception {
if (dirNames.size() == 0) {
return false;
}
final VirtualFile[] resFiles = new VirtualFile[dirNames.size()];
for (int i = 0, n = dirNames.size(); i < n; i++) {
String dirName = dirNames.get(i);
resFiles[i] = WriteAction.compute(() -> findOrCreateResourceFile(project, resDir, fileName, dirName));
if (resFiles[i] == null) {
return false;
}
}
if (!ReadonlyStatusHandler.ensureFilesWritable(project, resFiles)) {
return false;
}
final Resources[] resourcesElements = new Resources[resFiles.length];
for (int i = 0; i < resFiles.length; i++) {
final Resources resources = AndroidUtils.loadDomElement(project, resFiles[i], Resources.class);
if (resources == null) {
AndroidUtils.reportError(project, AndroidBundle.message("not.resource.file.error", fileName));
return false;
}
resourcesElements[i] = resources;
}
List<PsiFile> psiFiles = Lists.newArrayListWithExpectedSize(resFiles.length);
PsiManager manager = PsiManager.getInstance(project);
for (VirtualFile file : resFiles) {
PsiFile psiFile = manager.findFile(file);
if (psiFile != null) {
psiFiles.add(psiFile);
}
}
PsiFile[] files = psiFiles.toArray(new PsiFile[psiFiles.size()]);
WriteCommandAction<Void> action = new WriteCommandAction<Void>(project, "Add Resource", files) {
@Override
protected void run(@NotNull Result<Void> result) {
for (Resources resources : resourcesElements) {
final ResourceElement element = addValueResource(resourceType, resources, resourceValue);
element.getName().setValue(resourceName);
afterAddedProcessor.process(element);
}
}
};
action.execute();
return true;
}
use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.
the class ResourceManager method findValueResources.
// not recommended to use, because it is too slow
@NotNull
public List<ResourceElement> findValueResources(@NotNull String resourceType, @NotNull String resourceName, boolean distinguishDelimitersInName) {
final List<ValueResourceInfoImpl> resources = findValueResourceInfos(resourceType, resourceName, distinguishDelimitersInName, false);
final List<ResourceElement> result = new ArrayList<ResourceElement>();
for (ValueResourceInfoImpl resource : resources) {
final ResourceElement domElement = resource.computeDomElement();
if (domElement != null) {
result.add(domElement);
}
}
return result;
}
use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.
the class ResourceManager method getValueResources.
protected List<ResourceElement> getValueResources(@NotNull final ResourceType resourceType, @Nullable Set<VirtualFile> files) {
final List<ResourceElement> result = new ArrayList<ResourceElement>();
List<Pair<Resources, VirtualFile>> resourceFiles = getResourceElements(files);
for (final Pair<Resources, VirtualFile> pair : resourceFiles) {
final Resources resources = pair.getFirst();
ApplicationManager.getApplication().runReadAction(new Runnable() {
@Override
public void run() {
if (!resources.isValid() || myProject.isDisposed()) {
return;
}
final List<ResourceElement> valueResources = AndroidResourceUtil.getValueResourcesFromElement(resourceType, resources);
for (ResourceElement valueResource : valueResources) {
final String resName = valueResource.getName().getValue();
if (resName != null && isResourcePublic(resourceType.getName(), resName)) {
result.add(valueResource);
}
}
}
});
}
return result;
}
use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.
the class AndroidColorAnnotator method annotate.
@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
if (element instanceof XmlTag) {
XmlTag tag = (XmlTag) element;
String tagName = tag.getName();
if ((ResourceType.COLOR.getName().equals(tagName) || ResourceType.DRAWABLE.getName().equals(tagName) || ResourceType.MIPMAP.getName().equals(tagName))) {
DomElement domElement = DomManager.getDomManager(element.getProject()).getDomElement(tag);
if (domElement instanceof ResourceElement || ApplicationManager.getApplication().isUnitTestMode()) {
String value = tag.getValue().getText().trim();
annotateXml(element, holder, value);
}
} else if (TAG_ITEM.equals(tagName)) {
XmlTagValue value = tag.getValue();
String text = value.getText();
annotateXml(element, holder, text);
}
} else if (element instanceof XmlAttributeValue) {
XmlAttributeValue v = (XmlAttributeValue) element;
String value = v.getValue();
if (value == null || value.isEmpty()) {
return;
}
annotateXml(element, holder, value);
} else if (element instanceof PsiReferenceExpression) {
ResourceReferenceType referenceType = AndroidPsiUtils.getResourceReferenceType(element);
if (referenceType != ResourceReferenceType.NONE) {
// (isResourceReference will return true for both "R.drawable.foo" and the foo literal leaf in the
// same expression, which would result in both elements getting annotated and the icon showing up
// in the gutter twice. Instead we only count the outer one.
ResourceType type = AndroidPsiUtils.getResourceType(element);
if (type == ResourceType.COLOR || type == ResourceType.DRAWABLE || type == ResourceType.MIPMAP) {
String name = AndroidPsiUtils.getResourceName(element);
annotateResourceReference(type, holder, element, name, referenceType == ResourceReferenceType.FRAMEWORK);
}
}
}
}
use of org.jetbrains.android.dom.resources.ResourceElement in project android by JetBrains.
the class CreateXmlResourceDialog method checkIfResourceAlreadyExists.
@Nullable
public static ValidationInfo checkIfResourceAlreadyExists(@NotNull Project project, @NotNull VirtualFile resourceDir, @NotNull String resourceName, @NotNull ResourceType resourceType, @NotNull List<String> dirNames, @NotNull String fileName) {
if (resourceName.length() == 0 || dirNames.size() == 0 || fileName.length() == 0) {
return null;
}
for (String directoryName : dirNames) {
final VirtualFile resourceSubdir = resourceDir.findChild(directoryName);
if (resourceSubdir == null) {
continue;
}
final VirtualFile resFile = resourceSubdir.findChild(fileName);
if (resFile == null) {
continue;
}
if (resFile.getFileType() != StdFileTypes.XML) {
return new ValidationInfo("File " + FileUtil.toSystemDependentName(resFile.getPath()) + " is not XML file");
}
final Resources resources = AndroidUtils.loadDomElement(project, resFile, Resources.class);
if (resources == null) {
return new ValidationInfo(AndroidBundle.message("not.resource.file.error", FileUtil.toSystemDependentName(resFile.getPath())));
}
for (ResourceElement element : AndroidResourceUtil.getValueResourcesFromElement(resourceType, resources)) {
if (resourceName.equals(element.getName().getValue())) {
return new ValidationInfo("resource '" + resourceName + "' already exists in " + FileUtil.toSystemDependentName(resFile.getPath()));
}
}
}
return null;
}
Aggregations