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());
}
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);
}
}
}
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);
}
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;
}
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));
}
Aggregations