use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class EditResourcePanel method setVariant.
public void setVariant(@NotNull List<ResourceItem> resources, @Nullable ResourceItem defaultValue) {
if (resources.size() > 1) {
resources = Lists.newArrayList(resources);
Collections.sort(resources, (element1, element2) -> {
File directory1 = element1.getFile().getParentFile();
File directory2 = element2.getFile().getParentFile();
return directory1.getName().compareTo(directory2.getName());
});
DefaultComboBoxModel model = new DefaultComboBoxModel();
String defaultSelection = null;
for (ResourceItem resource : resources) {
String name = resource.getFile().getParentFile().getName();
model.addElement(name);
if (defaultSelection == null && resource == defaultValue) {
defaultSelection = name;
}
}
model.setSelectedItem(defaultSelection);
myVariantComboBox.setModel(model);
}
myVariants = resources;
myVariantComboBox.setVisible(resources.size() > 1);
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ResourceFolderRepositoryTest method testInitFromHelperThread.
public void testInitFromHelperThread() throws Exception {
// By default, unit tests run from the EDT thread, which automatically have read access. Try loading a repository from a
// helper thread that doesn't have read access to make sure we grab the appropriate read locks.
// Use a data binding file, which we currently know uses a PsiDataBindingResourceItem.
VirtualFile file1 = myFixture.copyFileToProject(LAYOUT_WITH_DATA_BINDING, "res/layout-land/layout_with_data_binding.xml");
final PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
VirtualFile file2 = myFixture.copyFileToProject(VALUES_WITH_DUPES, "res/values-en/values_with_dupes.xml");
ExecutorService executorService = new SequentialTaskExecutor("", PooledThreadExecutor.INSTANCE);
Future<ResourceFolderRepository> loadJob = executorService.submit(new Callable<ResourceFolderRepository>() {
@Override
public ResourceFolderRepository call() throws Exception {
return createRepository();
}
});
final ResourceFolderRepository resources = loadJob.get();
assertNotNull(resources);
AndroidFacet facet = resources.getFacet();
assertEquals(1, resources.getDataBindingResourceFiles().size());
assertEquals("land", getOnlyItem(resources, ResourceType.LAYOUT, "layout_with_data_binding").getQualifiers());
ResourceItem dupedStringItem = resources.getResourceItem(ResourceType.STRING, "app_name").get(0);
assertNotNull(dupedStringItem);
assertEquals("en", dupedStringItem.getQualifiers());
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ResourceFolderRepositoryTest method testRemoveStringArrayItemElements.
public void testRemoveStringArrayItemElements() throws Exception {
resetScanCounter();
VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "security_questions"));
ResourceItem array = getOnlyItem(resources, ResourceType.ARRAY, "security_questions");
ResourceValue resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("Question 4", resourceValue.getValue());
assertTrue(resourceValue instanceof ArrayResourceValue);
ArrayResourceValue arv = (ArrayResourceValue) resourceValue;
assertEquals(5, arv.getElementCount());
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
String elementString = "<item>Question 3</item>";
final int offset = document.getText().indexOf(elementString);
document.deleteString(offset, offset + elementString.length());
document.insertString(offset, "<item>Question X</item>");
documentManager.commitDocument(document);
}
});
// First edit won't be incremental (file -> Psi).
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "security_questions"));
array = getOnlyItem(resources, ResourceType.ARRAY, "security_questions");
resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("Question 4", resourceValue.getValue());
assertTrue(resourceValue instanceof ArrayResourceValue);
arv = (ArrayResourceValue) resourceValue;
assertEquals(5, arv.getElementCount());
resetScanCounter();
// Now try another edit that is also a delete item, where things should be incremental now.
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
String elementString = "<item>Question X</item>";
final int offset = document.getText().indexOf(elementString);
document.deleteString(offset, offset + elementString.length());
documentManager.commitDocument(document);
}
});
assertTrue(resources.hasResourceItem(ResourceType.ARRAY, "security_questions"));
array = getOnlyItem(resources, ResourceType.ARRAY, "security_questions");
resourceValue = array.getResourceValue(false);
assertNotNull(resourceValue);
assertEquals("Question 5", resourceValue.getValue());
assertTrue(resourceValue instanceof ArrayResourceValue);
arv = (ArrayResourceValue) resourceValue;
assertEquals(4, arv.getElementCount());
// Shouldn't have done any full file rescans during the above edits
ensureIncremental();
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ResourceFolderRepositoryTest method testGradualEdits.
public void testGradualEdits() throws Exception {
resetScanCounter();
// Gradually type in the contents of a value file and make sure we end up with a valid view of the world
VirtualFile file1 = myFixture.copyFileToProject(VALUES_EMPTY, "res/values/myvalues.xml");
PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
final ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
final long generation = resources.getModificationCount();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
document.deleteString(0, document.getTextLength());
documentManager.commitDocument(document);
}
});
final String contents = "<!--\n" + " -->\n" + "\n" + "<resources xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">\n" + "\n" + " <!-- Titles -->\n" + " <string name=\"app_name\">Animations Demo</string>\n" + " <string name=\"title_zoom\">Zoom</string>\n" + " <string name=\"title_layout_changes\">Layout Changes</string>\n" + " <string name=\"title_template_step\">Step <xliff:g id=\"step_number\">%1$d</xliff:g>: Lorem\n" + " Ipsum</string>\n" + " <string name=\"ellipsis\">Here it is: \\u2026!</string>\n" + "\n" + " <item type=\"id\" name=\"action_next\" />\n" + "\n" + " <style name=\"DarkActionBar\" parent=\"android:Widget.Holo.ActionBar\">\n" + " <item name=\"android:background\">@android:color/transparent</item>\n" + " </style>\n" + "\n" + " <integer name=\"card_flip_time_half\">150</integer>\n" + "\n" + " <declare-styleable name=\"MyCustomView\">\n" + " <attr name=\"watchType\" format=\"enum\">\n" + " <enum name=\"type_countdown\" value=\"0\"/>\n" + " </attr>\n" + " <attr name=\"crash\" format=\"boolean\" />\n" + " </declare-styleable>\n" + "</resources>\n";
for (int i = 0; i < contents.length(); i++) {
final int offset = i;
final char character = contents.charAt(i);
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
document.insertString(offset, String.valueOf(character));
documentManager.commitDocument(document);
}
});
}
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
ensureSingleScan();
assertTrue(generation < resources.getModificationCount());
assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkActionBar"));
assertTrue(resources.hasResourceItem(ResourceType.STYLE, "DarkActionBar"));
ResourceItem style = getOnlyItem(resources, ResourceType.STYLE, "DarkActionBar");
StyleResourceValue srv = (StyleResourceValue) style.getResourceValue(false);
assertNotNull(srv);
ResourceValue actionBarStyle = srv.getItem("background", true);
assertNotNull(actionBarStyle);
assertEquals("@android:color/transparent", actionBarStyle.getValue());
//noinspection ConstantConditions
assertEquals("Zoom", getOnlyItem(resources, ResourceType.STRING, "title_zoom").getResourceValue(false).getValue());
}
});
}
use of com.android.ide.common.res2.ResourceItem in project android by JetBrains.
the class ResourceFolderRepositoryTest method testEditDeclareStyleableAttr.
public void testEditDeclareStyleableAttr() throws Exception {
// Check edits of the name in a <declare-styleable> element.
resetScanCounter();
VirtualFile file1 = myFixture.copyFileToProject(VALUES1, "res/values/myvalues.xml");
PsiFile psiFile1 = PsiManager.getInstance(getProject()).findFile(file1);
assertNotNull(psiFile1);
ResourceFolderRepository resources = createRepository();
assertNotNull(resources);
assertTrue(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomView"));
assertTrue(resources.hasResourceItem(ResourceType.ATTR, "watchType"));
ResourceItem style = getOnlyItem(resources, ResourceType.DECLARE_STYLEABLE, "MyCustomView");
DeclareStyleableResourceValue srv = (DeclareStyleableResourceValue) style.getResourceValue(false);
assertNotNull(srv);
assertEquals(5, srv.getAllAttributes().size());
AttrResourceValue watchType = findAttr(srv.getAllAttributes(), "watchType");
assertNotNull(watchType);
assertEquals(2, watchType.getAttributeValues().size());
assertEquals(Integer.valueOf(1), watchType.getAttributeValues().get("type_stopwatch"));
assertEquals(Integer.valueOf(0), watchType.getAttributeValues().get("type_countdown"));
AttrResourceValue crash = findAttr(srv.getAllAttributes(), "crash");
assertNotNull(crash);
assertNull(crash.getAttributeValues());
AttrResourceValue minWidth = findAttr(srv.getAllAttributes(), "minWidth");
assertNotNull(minWidth);
assertFalse(resources.hasResourceItem(ResourceType.ATTR, "minWidth"));
AttrResourceValue ignoredNoFormat = findAttr(srv.getAllAttributes(), "ignore_no_format");
assertNotNull(ignoredNoFormat);
assertFalse(resources.hasResourceItem(ResourceType.ATTR, "ignore_no_format"));
long generation = resources.getModificationCount();
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile1);
assertNotNull(document);
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int offset = document.getText().indexOf("MyCustomView");
document.insertString(offset + 8, "r");
documentManager.commitDocument(document);
}
});
// First edit won't be incremental (file -> Psi).
assertTrue(resources.isScanPending(psiFile1));
UIUtil.dispatchAllInvocationEvents();
assertTrue(generation < resources.getModificationCount());
assertTrue(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomrView"));
assertTrue(resources.hasResourceItem(ResourceType.ATTR, "watchType"));
assertFalse(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomView"));
resetScanCounter();
// Now try another edit, where things should be incremental now.
final long generation2 = resources.getModificationCount();
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
int offset = document.getText().indexOf("MyCustomrView");
document.insertString(offset + 8, "e");
documentManager.commitDocument(document);
}
});
assertTrue(generation2 < resources.getModificationCount());
assertTrue(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomerView"));
assertTrue(resources.hasResourceItem(ResourceType.ATTR, "watchType"));
assertFalse(resources.hasResourceItem(ResourceType.DECLARE_STYLEABLE, "MyCustomView"));
style = getOnlyItem(resources, ResourceType.DECLARE_STYLEABLE, "MyCustomerView");
srv = (DeclareStyleableResourceValue) style.getResourceValue(false);
assertNotNull(srv);
assertEquals(5, srv.getAllAttributes().size());
watchType = findAttr(srv.getAllAttributes(), "watchType");
assertNotNull(watchType);
assertEquals(2, watchType.getAttributeValues().size());
assertEquals(Integer.valueOf(1), watchType.getAttributeValues().get("type_stopwatch"));
// Shouldn't have done any full file rescans during the above edits
ensureIncremental();
}
Aggregations