use of com.android.resources.ResourceFolderType in project android by JetBrains.
the class ResourceFolderRepository method equalFilesItems.
// Not a super-robust comparator. Compares a subset of the repo state for testing.
// Returns false if a difference is found.
// Returns true if the repositories are roughly equivalent.
@VisibleForTesting
boolean equalFilesItems(ResourceFolderRepository other) {
File myResourceDirFile = VfsUtilCore.virtualToIoFile(myResourceDir);
File otherResourceDir = VfsUtilCore.virtualToIoFile(other.myResourceDir);
if (!FileUtil.filesEqual(myResourceDirFile, otherResourceDir)) {
return false;
}
if (myResourceFiles.size() != other.myResourceFiles.size()) {
return false;
}
for (Map.Entry<VirtualFile, ResourceFile> fileEntry : myResourceFiles.entrySet()) {
ResourceFile otherResFile = other.myResourceFiles.get(fileEntry.getKey());
if (otherResFile == null) {
return false;
}
if (!FileUtil.filesEqual(fileEntry.getValue().getFile(), otherResFile.getFile())) {
return false;
}
}
if (myItems.size() != other.myItems.size()) {
return false;
}
for (Map.Entry<ResourceType, ListMultimap<String, ResourceItem>> entry : myItems.entrySet()) {
ListMultimap<String, ResourceItem> ownEntries = entry.getValue();
ListMultimap<String, ResourceItem> otherEntries = other.myItems.get(entry.getKey());
if (otherEntries == null || otherEntries.size() != ownEntries.size()) {
return false;
}
for (Map.Entry<String, ResourceItem> itemEntry : ownEntries.entries()) {
List<ResourceItem> otherItemsList = otherEntries.get(itemEntry.getKey());
if (otherItemsList == null) {
return false;
}
final ResourceItem item = itemEntry.getValue();
if (!ContainerUtil.exists(otherItemsList, new Condition<ResourceItem>() {
@Override
public boolean value(ResourceItem resourceItem) {
// Use #compareTo instead of #equals because #equals compares pointers of mSource.
if (resourceItem.compareTo(item) != 0) {
return false;
}
// Skip ID type resources, where the ResourceValues are not important and where blob writing doesn't preserve the value.
if (item.getType() != ResourceType.ID) {
ResourceValue resValue = item.getResourceValue(false);
ResourceValue otherResValue = resourceItem.getResourceValue(false);
if (resValue == null || otherResValue == null) {
if (resValue != otherResValue) {
return false;
}
} else {
String resValueStr = resValue.getValue();
String otherResValueStr = otherResValue.getValue();
if (resValueStr == null || otherResValueStr == null) {
if (resValueStr != otherResValueStr) {
return false;
}
} else {
if (!resValueStr.equals(otherResValueStr)) {
return false;
}
}
}
}
// We can only compareValueWith (compare equivalence of XML nodes) for VALUE items.
// For others, the XML node may be different before and after serialization.
ResourceFile source = item.getSource();
ResourceFile otherSource = resourceItem.getSource();
if (source != null && otherSource != null) {
ResourceFolderType ownFolderType = ResourceHelper.getFolderType(source);
ResourceFolderType otherFolderType = ResourceHelper.getFolderType(otherSource);
if (otherFolderType != ownFolderType) {
return false;
}
if (otherFolderType == VALUES) {
return resourceItem.compareValueWith(item);
}
}
return true;
}
})) {
return false;
}
}
}
// Only compare the keys.
return myDataBindingResourceFiles.keySet().equals(other.myDataBindingResourceFiles.keySet());
}
use of com.android.resources.ResourceFolderType in project android by JetBrains.
the class RecipeMergeUtils method mergeXml.
/**
* Merges sourceXml into targetXml/targetFile (targetXml is the contents of targetFile).
* Returns the resulting xml if it still needs to be written to targetFile,
* or null if the file has already been/doesn't need to be updated.
*/
@Nullable
public static String mergeXml(@NotNull RenderingContext context, String sourceXml, String targetXml, File targetFile) {
boolean ok;
String fileName = targetFile.getName();
String contents;
if (fileName.equals(FN_ANDROID_MANIFEST_XML)) {
Document currentDocument = XmlUtils.parseDocumentSilently(targetXml, true);
assert currentDocument != null : targetXml + " failed to parse";
Document fragment = XmlUtils.parseDocumentSilently(sourceXml, true);
assert fragment != null : sourceXml + " failed to parse";
contents = mergeManifest(context.getModuleRoot(), targetFile, targetXml, sourceXml);
ok = contents != null;
} else {
// Merge plain XML files
String parentFolderName = targetFile.getParentFile().getName();
ResourceFolderType folderType = ResourceFolderType.getFolderType(parentFolderName);
// mergeResourceFile handles the file updates itself, so no content is returned in this case.
contents = mergeResourceFile(context, targetXml, sourceXml, fileName, folderType);
ok = contents != null;
}
// Finally write out the merged file
if (!ok) {
// Just insert into file along with comment, using the "standard" conflict
// syntax that many tools and editors recognize.
contents = wrapWithMergeConflict(targetXml, sourceXml);
// Report the conflict as a warning:
context.getWarnings().add(String.format("Merge conflict for: %1$s this file must be fixed by hand", targetFile.getName()));
}
return contents;
}
use of com.android.resources.ResourceFolderType in project android by JetBrains.
the class CreateResourceDirectoryAction method invokeDialog.
@NotNull
@Override
public PsiElement[] invokeDialog(@NotNull final Project project, @NotNull final DataContext dataContext) {
ResourceFolderType folderType = myResourceFolderType;
if (folderType == null) {
VirtualFile[] files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
folderType = CreateResourceFileAction.getUniqueFolderType(files);
}
NewResourceCreationHandler newResourceHandler = NewResourceCreationHandler.getInstance(project);
CreateResourceDirectoryDialogBase dialog = newResourceHandler.createNewResourceDirectoryDialog(project, LangDataKeys.MODULE.getData(dataContext), folderType, CreateResourceDialogUtils.findResourceDirectory(dataContext), dataContext, resDirectory -> new MyInputValidator(project, resDirectory));
dialog.setTitle(AndroidBundle.message("new.resource.dir.dialog.title"));
if (!dialog.showAndGet()) {
return PsiElement.EMPTY_ARRAY;
}
return dialog.getCreatedElements();
}
use of com.android.resources.ResourceFolderType in project android by JetBrains.
the class CreateResourceDirectoryDialogBase method setupDeviceConfigurationPanel.
protected DeviceConfiguratorPanel setupDeviceConfigurationPanel(final JComboBox resourceTypeComboBox, final JTextField directoryNameTextField, final JBLabel errorLabel) {
return new DeviceConfiguratorPanel() {
@Override
public void applyEditors() {
try {
doApplyEditors();
final FolderConfiguration config = this.getConfiguration();
final ResourceFolderType selectedResourceType = (ResourceFolderType) resourceTypeComboBox.getSelectedItem();
directoryNameTextField.setText(selectedResourceType != null ? config.getFolderName(selectedResourceType) : "");
errorLabel.setText("");
} catch (InvalidOptionValueException e) {
errorLabel.setText(new HtmlBuilder().openHtmlBody().coloredText(JBColor.RED, e.getMessage()).closeHtmlBody().getHtml());
directoryNameTextField.setText("");
}
setOKActionEnabled(directoryNameTextField.getText().length() > 0);
}
};
}
use of com.android.resources.ResourceFolderType in project android by JetBrains.
the class CreateResourceFileAction method invokeDialog.
@NotNull
@Override
public PsiElement[] invokeDialog(@NotNull final Project project, @NotNull final DataContext dataContext) {
Module module = LangDataKeys.MODULE.getData(dataContext);
if (module == null) {
return PsiElement.EMPTY_ARRAY;
}
final AndroidFacet facet = AndroidFacet.getInstance(module);
LOG.assertTrue(facet != null);
VirtualFile[] files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
ResourceFolderType folderType = getUniqueFolderType(files);
FolderConfiguration config = null;
if (files != null && files.length > 0) {
config = files.length == 1 ? FolderConfiguration.getConfigForFolder(files[0].getName()) : null;
}
myNavigate = true;
NewResourceCreationHandler newResourceHandler = NewResourceCreationHandler.getInstance(project);
final CreateResourceFileDialogBase dialog = newResourceHandler.createNewResourceFileDialog(facet, mySubactions.values(), folderType, null, null, config, true, false, CreateResourceDialogUtils.findResourceDirectory(dataContext), dataContext, createValidatorFactory(project));
if (!dialog.showAndGet()) {
return PsiElement.EMPTY_ARRAY;
}
return dialog.getCreatedElements();
}
Aggregations