Search in sources :

Example 26 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class EditedStyleItemTest method testValueSetters.

public void testValueSetters() {
    // Do a simple instantiation and check that the setters update the right value
    VirtualFile myLayout = myFixture.copyFileToProject("xmlpull/layout.xml", "res/layout/layout1.xml");
    Configuration configuration = myFacet.getConfigurationManager().getConfiguration(myLayout);
    // We just get a theme that we use as fake source to pass to the ConfiguredItemResourceValue constructor
    ConfiguredThemeEditorStyle fakeTheme = ResolutionUtils.getStyle(configuration, "android:Theme", null);
    assertNotNull(fakeTheme);
    //noinspection ConstantConditions
    List<ConfiguredElement<ItemResourceValue>> items = ImmutableList.of(ConfiguredElement.create(FolderConfiguration.getConfigForFolder("values-v21"), new ItemResourceValue("attribute", false, "otherValue", false, null)));
    EditedStyleItem editedStyleItem = new EditedStyleItem(ConfiguredElement.create(new FolderConfiguration(), new ItemResourceValue("attribute", false, "selectedValue", false, null)), items, fakeTheme);
    assertEquals("selectedValue", editedStyleItem.getValue());
    assertEquals("selectedValue", editedStyleItem.getSelectedValue().getValue());
    assertEquals(1, editedStyleItem.getNonSelectedItemResourceValues().size());
    ConfiguredElement<ItemResourceValue> notSelectedItem = editedStyleItem.getNonSelectedItemResourceValues().iterator().next();
    assertEquals("otherValue", notSelectedItem.myValue.getValue());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Configuration(com.android.tools.idea.configurations.Configuration) ItemResourceValue(com.android.ide.common.rendering.api.ItemResourceValue) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration)

Example 27 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class OverrideResourceAction method forkResourceFile.

private static void forkResourceFile(@NotNull Project project, @NotNull final ResourceFolderType folderType, @NotNull final VirtualFile file, @Nullable final XmlFile xmlFile, @Nullable String myNewFolder, @Nullable Configuration configuration, boolean open) {
    final FolderConfiguration folderConfig;
    if (myNewFolder == null) {
        // Open a file chooser to select the configuration to be created
        VirtualFile parentFolder = file.getParent();
        assert parentFolder != null;
        VirtualFile res = parentFolder.getParent();
        folderConfig = selectFolderConfig(project, res, folderType);
    } else {
        folderConfig = FolderConfiguration.getConfigForFolder(myNewFolder);
    }
    if (folderConfig == null) {
        return;
    }
    final Computable<Pair<String, VirtualFile>> computable = new Computable<Pair<String, VirtualFile>>() {

        @Override
        public Pair<String, VirtualFile> compute() {
            String folderName = folderConfig.getFolderName(folderType);
            try {
                VirtualFile parentFolder = file.getParent();
                assert parentFolder != null;
                VirtualFile res = parentFolder.getParent();
                VirtualFile newParentFolder = res.findChild(folderName);
                if (newParentFolder == null) {
                    newParentFolder = res.createChildDirectory(this, folderName);
                    if (newParentFolder == null) {
                        String message = String.format("Could not create folder %1$s in %2$s", folderName, res.getPath());
                        return Pair.of(message, null);
                    }
                }
                final VirtualFile existing = newParentFolder.findChild(file.getName());
                if (existing != null && existing.exists()) {
                    String message = String.format("File 'res/%1$s/%2$s' already exists!", folderName, file.getName());
                    return Pair.of(message, null);
                }
                // Attempt to get the document from the PSI file rather than the file on disk: get edited contents too
                String text;
                if (xmlFile != null) {
                    text = xmlFile.getText();
                } else {
                    text = StreamUtil.readText(file.getInputStream(), "UTF-8");
                }
                VirtualFile newFile = newParentFolder.createChildData(this, file.getName());
                VfsUtil.saveText(newFile, text);
                return Pair.of(null, newFile);
            } catch (IOException e2) {
                String message = String.format("Failed to create File 'res/%1$s/%2$s' : %3$s", folderName, file.getName(), e2.getMessage());
                return Pair.of(message, null);
            }
        }
    };
    WriteCommandAction<Pair<String, VirtualFile>> action = new WriteCommandAction<Pair<String, VirtualFile>>(project, "Add Resource") {

        @Override
        protected void run(@NotNull Result<Pair<String, VirtualFile>> result) throws Throwable {
            result.setResult(computable.compute());
        }
    };
    Pair<String, VirtualFile> result = action.execute().getResultObject();
    String error = result.getFirst();
    VirtualFile newFile = result.getSecond();
    if (error != null) {
        Messages.showErrorDialog(project, error, "Create Resource");
    } else {
        // First create a compatible configuration based on the current configuration
        if (configuration != null) {
            ConfigurationManager configurationManager = configuration.getConfigurationManager();
            configurationManager.createSimilar(newFile, file);
        }
        if (open) {
            OpenFileDescriptor descriptor = new OpenFileDescriptor(project, newFile, -1);
            FileEditorManager.getInstance(project).openEditor(descriptor, true);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) ConfigurationManager(com.android.tools.idea.configurations.ConfigurationManager) Computable(com.intellij.openapi.util.Computable) Pair(com.android.utils.Pair)

Example 28 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class StringResourceParser method parseUnderReadLock.

@NotNull
private static StringResourceData parseUnderReadLock(AndroidFacet facet, LocalResourceRepository repository) {
    List<String> keys = Lists.newArrayList(repository.getItemsOfType(ResourceType.STRING));
    Collections.sort(keys);
    Map<String, StringResource> keyToResourceMap = new HashMap<>();
    Project project = facet.getModule().getProject();
    for (String key : keys) {
        List<ResourceItem> items = repository.getResourceItem(ResourceType.STRING, key);
        if (items == null) {
            continue;
        }
        StringResource stringResource = new StringResource(key);
        for (ResourceItem item : items) {
            XmlTag tag = LocalResourceRepository.getItemTag(project, item);
            if (tag != null && SdkConstants.VALUE_FALSE.equals(tag.getAttributeValue(SdkConstants.ATTR_TRANSLATABLE))) {
                stringResource.setTranslatable(false);
            }
            String itemStringRepresentation = resourceToString(project, item);
            FolderConfiguration config = item.getConfiguration();
            LocaleQualifier qualifier = config == null ? null : config.getLocaleQualifier();
            if (qualifier == null) {
                stringResource.setDefaultValue(item, itemStringRepresentation);
            } else {
                Locale locale = Locale.create(qualifier);
                stringResource.putTranslation(locale, item, itemStringRepresentation);
            }
        }
        keyToResourceMap.put(key, stringResource);
    }
    return new StringResourceData(facet, keyToResourceMap);
}
Also used : Locale(com.android.tools.idea.rendering.Locale) HashMap(java.util.HashMap) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Project(com.intellij.openapi.project.Project) ResourceItem(com.android.ide.common.res2.ResourceItem) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 29 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class StringsWriteUtils method getStringResourceItem.

@Nullable
private static ResourceItem getStringResourceItem(@NotNull AndroidFacet facet, @NotNull String key, @Nullable Locale locale) {
    LocalResourceRepository repository = facet.getModuleResources(true);
    // Ensure that items *just* created are processed by the resource repository
    repository.sync();
    List<ResourceItem> items = repository.getResourceItem(ResourceType.STRING, key);
    if (items == null) {
        return null;
    }
    for (ResourceItem item : items) {
        FolderConfiguration config = item.getConfiguration();
        LocaleQualifier qualifier = config == null ? null : config.getLocaleQualifier();
        if (qualifier == null) {
            if (locale == null) {
                return item;
            } else {
                continue;
            }
        }
        Locale l = Locale.create(qualifier);
        if (l.equals(locale)) {
            return item;
        }
    }
    return null;
}
Also used : Locale(com.android.tools.idea.rendering.Locale) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) ResourceItem(com.android.ide.common.res2.ResourceItem) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) Nullable(org.jetbrains.annotations.Nullable)

Example 30 with FolderConfiguration

use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.

the class AndroidExtractAsIncludeAction method doRefactorForPsiRange.

@Override
protected void doRefactorForPsiRange(@NotNull final Project project, @NotNull final PsiFile file, @NotNull final PsiElement from, @NotNull final PsiElement to) {
    final PsiDirectory dir = file.getContainingDirectory();
    if (dir == null) {
        return;
    }
    final AndroidFacet facet = AndroidFacet.getInstance(from);
    assert facet != null;
    final XmlTag parentTag = PsiTreeUtil.getParentOfType(from, XmlTag.class);
    assert parentTag != null;
    final List<XmlTag> tagsInRange = collectAllTags(from, to);
    assert tagsInRange.size() > 0 : "there is no tag inside the range";
    final String fileName = myTestConfig != null ? myTestConfig.myLayoutFileName : null;
    final String dirName = dir.getName();
    final FolderConfiguration config = dirName.length() > 0 ? FolderConfiguration.getConfig(dirName.split(SdkConstants.RES_QUALIFIER_SEP)) : null;
    final String title = "Extract Android Layout";
    AsyncResult<DataContext> dataContextAsyncResult = DataManager.getInstance().getDataContextFromFocus();
    dataContextAsyncResult.doWhenDone((Consumer<DataContext>) dataContext -> CommandProcessor.getInstance().executeCommand(project, new Runnable() {

        @Override
        public void run() {
            final XmlFile newFile = CreateResourceFileAction.createFileResource(facet, ResourceFolderType.LAYOUT, fileName, "temp_root", config, true, title, null, dataContext);
            if (newFile != null) {
                ApplicationManager.getApplication().runWriteAction(new Runnable() {

                    @Override
                    public void run() {
                        doRefactor(facet, file, newFile, from, to, parentTag, tagsInRange.size() > 1);
                    }
                });
            }
        }
    }, title, null, UndoConfirmationPolicy.REQUEST_CONFIRMATION));
}
Also used : Include(org.jetbrains.android.dom.layout.Include) DataContext(com.intellij.openapi.actionSystem.DataContext) SdkConstants(com.android.SdkConstants) XmlFile(com.intellij.psi.xml.XmlFile) ResourceFolderType(com.android.resources.ResourceFolderType) Document(com.intellij.openapi.editor.Document) HashSet(com.intellij.util.containers.HashSet) NonNls(org.jetbrains.annotations.NonNls) ResourceType(com.android.resources.ResourceType) DomElement(com.intellij.util.xml.DomElement) CreateResourceFileAction(org.jetbrains.android.actions.CreateResourceFileAction) ArrayList(java.util.ArrayList) IncludeReference(com.android.tools.idea.rendering.IncludeReference) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Project(com.intellij.openapi.project.Project) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) UndoConfirmationPolicy(com.intellij.openapi.command.UndoConfirmationPolicy) DataManager(com.intellij.ide.DataManager) AsyncResult(com.intellij.openapi.util.AsyncResult) XmlTag(com.intellij.psi.xml.XmlTag) ResourceHelper(com.android.tools.idea.res.ResourceHelper) LayoutViewElement(org.jetbrains.android.dom.layout.LayoutViewElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) Set(java.util.Set) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) TextRange(com.intellij.openapi.util.TextRange) AndroidCommonUtils(org.jetbrains.android.util.AndroidCommonUtils) TestOnly(org.jetbrains.annotations.TestOnly) CommandProcessor(com.intellij.openapi.command.CommandProcessor) Nullable(org.jetbrains.annotations.Nullable) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) List(java.util.List) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) DomManager(com.intellij.util.xml.DomManager) NotNull(org.jetbrains.annotations.NotNull) Consumer(com.intellij.util.Consumer) DataContext(com.intellij.openapi.actionSystem.DataContext) XmlFile(com.intellij.psi.xml.XmlFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)69 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 NotNull (org.jetbrains.annotations.NotNull)14 VersionQualifier (com.android.ide.common.resources.configuration.VersionQualifier)12 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)12 Nullable (org.jetbrains.annotations.Nullable)12 ResourceItem (com.android.ide.common.res2.ResourceItem)10 LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)10 DensityQualifier (com.android.ide.common.resources.configuration.DensityQualifier)9 ResourceFolderType (com.android.resources.ResourceFolderType)9 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)7 IAndroidTarget (com.android.sdklib.IAndroidTarget)7 Project (com.intellij.openapi.project.Project)7 ItemResourceValue (com.android.ide.common.rendering.api.ItemResourceValue)6 ResourceResolver (com.android.ide.common.resources.ResourceResolver)6 LayoutDirectionQualifier (com.android.ide.common.resources.configuration.LayoutDirectionQualifier)6 ScreenOrientationQualifier (com.android.ide.common.resources.configuration.ScreenOrientationQualifier)6 ScreenSizeQualifier (com.android.ide.common.resources.configuration.ScreenSizeQualifier)6 SessionParams (com.android.ide.common.rendering.api.SessionParams)5 CountryCodeQualifier (com.android.ide.common.resources.configuration.CountryCodeQualifier)5