Search in sources :

Example 31 with ResourceFolderType

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());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Condition(com.intellij.openapi.util.Condition) ResourceType(com.android.resources.ResourceType) ResourceFolderType(com.android.resources.ResourceFolderType) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) VisibleForTesting(com.android.annotations.VisibleForTesting)

Example 32 with ResourceFolderType

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;
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) Document(org.w3c.dom.Document) XmlDocument(com.android.manifmerger.XmlDocument) Nullable(org.jetbrains.annotations.Nullable)

Example 33 with ResourceFolderType

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();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceFolderType(com.android.resources.ResourceFolderType) NotNull(org.jetbrains.annotations.NotNull)

Example 34 with ResourceFolderType

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);
        }
    };
}
Also used : ResourceFolderType(com.android.resources.ResourceFolderType) HtmlBuilder(com.android.utils.HtmlBuilder) InvalidOptionValueException(org.jetbrains.android.uipreview.InvalidOptionValueException) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) DeviceConfiguratorPanel(org.jetbrains.android.uipreview.DeviceConfiguratorPanel)

Example 35 with ResourceFolderType

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();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ResourceFolderType(com.android.resources.ResourceFolderType) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) 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