Search in sources :

Example 21 with LocalResourceRepository

use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.

the class DataBindingXmlReferenceContributor method getDataBindingInfo.

@Nullable
public static DataBindingInfo getDataBindingInfo(PsiElement element) {
    DataBindingInfo dataBindingInfo = null;
    Module module = ModuleUtilCore.findModuleForPsiElement(element);
    if (module != null) {
        AndroidFacet facet = AndroidFacet.getInstance(module);
        if (facet != null && facet.isDataBindingEnabled()) {
            LocalResourceRepository moduleResources = facet.getModuleResources(true);
            PsiFile topLevelFile = InjectedLanguageUtil.getTopLevelFile(element);
            if (topLevelFile != null) {
                String name = topLevelFile.getName();
                name = name.substring(0, name.lastIndexOf('.'));
                dataBindingInfo = moduleResources.getDataBindingInfoForLayout(name);
            }
        }
    }
    return dataBindingInfo;
}
Also used : LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) Module(com.intellij.openapi.module.Module) DataBindingInfo(com.android.tools.idea.res.DataBindingInfo) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with LocalResourceRepository

use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.

the class ResourceResolverCacheTest method test.

public void test() throws Exception {
    VirtualFile file1 = myFixture.copyFileToProject("render/layout1.xml", "res/layout/layout1.xml");
    VirtualFile file2 = myFixture.copyFileToProject("render/layout2.xml", "res/layout/layout2.xml");
    VirtualFile file3 = myFixture.copyFileToProject("javadoc/strings/strings.xml", "res/values/strings.xml");
    assertNotNull(file1);
    assertNotNull(file2);
    assertNotNull(file3);
    AndroidFacet facet = AndroidFacet.getInstance(myModule);
    assertNotNull(facet);
    Project project = getProject();
    PsiFile psiFile1 = PsiManager.getInstance(project).findFile(file1);
    assertNotNull(psiFile1);
    PsiFile psiFile2 = PsiManager.getInstance(project).findFile(file2);
    assertNotNull(psiFile2);
    final PsiFile psiFile3 = PsiManager.getInstance(project).findFile(file3);
    assertNotNull(psiFile3);
    ConfigurationManager configurationManager = facet.getConfigurationManager();
    assertNotNull(configurationManager);
    final Configuration configuration1 = configurationManager.getConfiguration(file1);
    Configuration configuration2 = configurationManager.getConfiguration(file2);
    assertNotNull(configuration1.getTheme());
    assertEquals(configuration2.getTheme(), configuration1.getTheme());
    ResourceResolver resolver1 = configuration1.getResourceResolver();
    ResourceResolver resolver2 = configuration2.getResourceResolver();
    assertSame(resolver1, resolver2);
    assertSame(resolver1, configuration1.getResourceResolver());
    configuration1.setTheme("Theme.Light");
    final ResourceResolver resolver1b = configuration1.getResourceResolver();
    assertNotSame(resolver1b, resolver1);
    assertNotSame(resolver1b, resolver2);
    assertSame(resolver1b, configuration1.getResourceResolver());
    configuration2.setTheme("Theme.Light");
    assertSame(resolver1b, configuration2.getResourceResolver());
    // Test project resource changes, should invalidate
    final LocalResourceRepository resources = myFacet.getModuleResources(true);
    assertNotNull(resources);
    final long generation = resources.getModificationCount();
    assertEquals("Cancel", configuration1.getResourceResolver().findResValue("@string/cancel", false).getValue());
    WriteCommandAction.runWriteCommandAction(null, new Runnable() {

        @Override
        public void run() {
            //noinspection ConstantConditions
            XmlTagValue value = ((XmlFile) psiFile3).getRootTag().getSubTags()[1].getValue();
            assertEquals("Cancel", value.getTrimmedText());
            value.setText("\"FooBar\"");
        }
    });
    assertTrue(resources.isScanPending(psiFile3));
    ApplicationManager.getApplication().invokeLater(new Runnable() {

        @Override
        public void run() {
            assertTrue(generation < resources.getModificationCount());
            assertNotSame(resolver1b, configuration1.getResourceResolver());
            assertEquals("FooBar", configuration1.getResourceResolver().findResValue("@string/cancel", false).getValue());
        }
    });
    ResourceResolverCache cache = configuration1.getConfigurationManager().getResolverCache();
    assertSame(cache, configuration2.getConfigurationManager().getResolverCache());
    ResourceRepository frameworkResources = cache.getFrameworkResources(configuration1.getFullConfig(), configuration1.getTarget());
    assertTrue(frameworkResources instanceof FrameworkResourceLoader.IdeFrameworkResources);
    assertTrue(((FrameworkResourceLoader.IdeFrameworkResources) frameworkResources).getSkippedLocales());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlTagValue(com.intellij.psi.xml.XmlTagValue) XmlFile(com.intellij.psi.xml.XmlFile) FrameworkResourceLoader(org.jetbrains.android.sdk.FrameworkResourceLoader) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ResourceResolver(com.android.ide.common.resources.ResourceResolver) PsiFile(com.intellij.psi.PsiFile) ResourceRepository(com.android.ide.common.resources.ResourceRepository) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository)

Example 23 with LocalResourceRepository

use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.

the class ThemeEditorUtilsTest method testResourceResolverVisitor.

public void testResourceResolverVisitor() {
    myFixture.copyFileToProject("themeEditor/apiTestBefore/stylesApi-v14.xml", "res/values-v14/styles.xml");
    myFixture.copyFileToProject("themeEditor/apiTestBefore/stylesApi-v19.xml", "res/values-v19/styles.xml");
    myFixture.copyFileToProject("themeEditor/apiTestBefore/stylesApi-v21.xml", "res/values-v21/styles.xml");
    final AtomicInteger visitedRepos = new AtomicInteger(0);
    // With only one source set, this should be called just once.
    ThemeEditorUtils.acceptResourceResolverVisitor(myFacet, new ThemeEditorUtils.ResourceFolderVisitor() {

        @Override
        public void visitResourceFolder(@NotNull LocalResourceRepository resources, String moduleName, @NotNull String variantName, boolean isSelected) {
            assertEquals("main", variantName);
            visitedRepos.incrementAndGet();
        }
    });
    assertEquals(1, visitedRepos.get());
// TODO: Test variants
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository)

Example 24 with LocalResourceRepository

use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.

the class ThemeEditorUtilsTest method testCopyTheme.

public void testCopyTheme() {
    myFixture.copyFileToProject("themeEditor/styles_1.xml", "res/values/styles.xml");
    myFixture.copyFileToProject("themeEditor/apiTestBefore/stylesApi-v19.xml", "res/values-v19/styles.xml");
    LocalResourceRepository repository = AppResourceRepository.getAppResources(myModule, true);
    assertNotNull(repository);
    List<ResourceItem> resources = repository.getResourceItem(ResourceType.STYLE, "AppTheme");
    assertNotNull(resources);
    assertFalse(resources.isEmpty());
    final XmlTag sourceXml = LocalResourceRepository.getItemTag(getProject(), resources.get(0));
    assertNotNull(sourceXml);
    new WriteCommandAction.Simple(myModule.getProject(), "Copy a theme") {

        @Override
        protected void run() throws Throwable {
            ThemeEditorUtils.copyTheme(16, sourceXml);
            ThemeEditorUtils.copyTheme(19, sourceXml);
        }
    }.execute();
    myFixture.checkResultByFile("res/values-v16/styles.xml", "themeEditor/testCopyTheme/styles-v16.xml", true);
    myFixture.checkResultByFile("res/values-v19/styles.xml", "themeEditor/testCopyTheme/styles-v19.xml", true);
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ResourceItem(com.android.ide.common.res2.ResourceItem) XmlTag(com.intellij.psi.xml.XmlTag)

Example 25 with LocalResourceRepository

use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.

the class StringsCellEditorTest method testIsCellEditable.

public void testIsCellEditable() {
    Collection<VirtualFile> directories = Collections.singletonList(myFixture.copyDirectoryToProject("stringsEditor/base/res", "res"));
    LocalResourceRepository repository = ModuleResourceRepository.createForTest(myFacet, directories);
    JTable table = new JBTable(new StringResourceTableModel(StringResourceParser.parse(myFacet, repository)));
    TableUtils.selectCellAt(table, 0, StringResourceTableModel.DEFAULT_VALUE_COLUMN);
    assertTrue(new StringsCellEditor().isCellEditable(new ActionEvent(table, 0, null)));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ActionEvent(java.awt.event.ActionEvent) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) JBTable(com.intellij.ui.table.JBTable)

Aggregations

LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)25 NotNull (org.jetbrains.annotations.NotNull)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)7 Nullable (org.jetbrains.annotations.Nullable)6 ResourceItem (com.android.ide.common.res2.ResourceItem)5 Module (com.intellij.openapi.module.Module)5 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)5 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)4 DataBindingInfo (com.android.tools.idea.res.DataBindingInfo)4 ResourceRepository (com.android.ide.common.resources.ResourceRepository)3 LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)3 Locale (com.android.tools.idea.rendering.Locale)3 XmlTag (com.intellij.psi.xml.XmlTag)3 ResourceResolver (com.android.ide.common.resources.ResourceResolver)2 ResourceType (com.android.resources.ResourceType)2 State (com.android.sdklib.devices.State)2 AppResourceRepository (com.android.tools.idea.res.AppResourceRepository)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 PsiFile (com.intellij.psi.PsiFile)2 ArrayList (java.util.ArrayList)2