use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.
the class AndroidIconProvider method getIcon.
@Nullable
@Override
public Icon getIcon(@NotNull PsiElement element, @Iconable.IconFlags int flags) {
if (element instanceof XmlFile) {
final VirtualFile file = ((XmlFile) element).getVirtualFile();
if (file == null) {
return null;
}
if (FN_ANDROID_MANIFEST_XML.equals(file.getName())) {
return AndroidIcons.ManifestFile;
}
VirtualFile parent = file.getParent();
if (parent != null) {
String parentName = parent.getName();
// Check whether resource folder is (potentially) a resource folder with qualifiers
int index = parentName.indexOf('-');
if (index != -1) {
FolderConfiguration config = FolderConfiguration.getConfigForFolder(parentName);
if (config != null && config.getLocaleQualifier() != null && ResourceFolderType.getFolderType(parentName) != null) {
// If resource folder qualifier specifies locale, show a flag icon
return FlagManager.get().getFlag(config);
}
}
}
}
return null;
}
use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.
the class PsiResourceItem method getSource.
@Nullable
@Override
public ResourceFile getSource() {
ResourceFile source = super.getSource();
// Temporary safety workaround
if (source == null && myFile != null && myFile.getParent() != null) {
PsiDirectory parent = myFile.getParent();
if (parent != null) {
String name = parent.getName();
ResourceFolderType folderType = ResourceFolderType.getFolderType(name);
FolderConfiguration configuration = FolderConfiguration.getConfigForFolder(name);
int index = name.indexOf('-');
String qualifiers = index == -1 ? "" : name.substring(index + 1);
source = new PsiResourceFile(myFile, Collections.<ResourceItem>singletonList(this), qualifiers, folderType, configuration);
setSource(source);
}
}
return source;
}
use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.
the class ThemeEditorUtils method showCreateNewStyleDialog.
/**
* Creates a new style by displaying the dialog of the {@link NewStyleDialog}.
* @param defaultParentStyle is used in NewStyleDialog, will be preselected in the parent text field and name will be suggested based on it
* @param themeEditorContext current theme editor context
* @param isTheme whether theme or style will be created
* @param message is used in NewStyleDialog to display message to user
* @return the new style name or null if the style wasn't created
*/
@Nullable
public static String showCreateNewStyleDialog(@Nullable ConfiguredThemeEditorStyle defaultParentStyle, @NotNull final ThemeEditorContext themeEditorContext, boolean isTheme, boolean enableParentChoice, @Nullable final String message, @Nullable ThemeSelectionPanel.ThemeChangedListener themeChangedListener) {
// if isTheme is true, defaultParentStyle shouldn't be null
String defaultParentStyleName = null;
if (isTheme && defaultParentStyle == null) {
ImmutableList<String> defaultThemes = getDefaultThemeNames(themeEditorContext.getThemeResolver());
defaultParentStyleName = !defaultThemes.isEmpty() ? defaultThemes.get(0) : null;
} else if (defaultParentStyle != null) {
defaultParentStyleName = defaultParentStyle.getQualifiedName();
}
final NewStyleDialog dialog = new NewStyleDialog(isTheme, themeEditorContext, defaultParentStyleName, (defaultParentStyle == null) ? null : defaultParentStyle.getName(), message);
dialog.enableParentChoice(enableParentChoice);
if (themeChangedListener != null) {
dialog.setThemeChangedListener(themeChangedListener);
}
boolean createStyle = dialog.showAndGet();
if (!createStyle) {
return null;
}
int minModuleApi = getMinApiLevel(themeEditorContext.getCurrentContextModule());
int minAcceptableApi = ResolutionUtils.getOriginalApiLevel(ResolutionUtils.getStyleResourceUrl(dialog.getStyleParentName()), themeEditorContext.getProject());
final String fileName = AndroidResourceUtil.getDefaultResourceFileName(ResourceType.STYLE);
FolderConfiguration config = new FolderConfiguration();
if (minModuleApi < minAcceptableApi) {
VersionQualifier qualifier = new VersionQualifier(minAcceptableApi);
config.setVersionQualifier(qualifier);
}
if (fileName == null) {
LOG.error("Couldn't find a default filename for ResourceType.STYLE");
return null;
}
final List<String> dirNames = Collections.singletonList(config.getFolderName(ResourceFolderType.VALUES));
String parentStyleName = dialog.getStyleParentName();
Module module = themeEditorContext.getCurrentContextModule();
AndroidFacet facet = AndroidFacet.getInstance(module);
if (facet == null) {
LOG.error("Create new style for non-Android module " + module.getName());
return null;
}
Project project = module.getProject();
VirtualFile resourceDir = facet.getPrimaryResourceDir();
if (resourceDir == null) {
AndroidUtils.reportError(project, AndroidBundle.message("check.resource.dir.error", module.getName()));
return null;
}
boolean isCreated = createNewStyle(project, resourceDir, dialog.getStyleName(), parentStyleName, fileName, dirNames);
return isCreated ? dialog.getStyleName() : null;
}
use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.
the class ThemeEditorUtils method copyTheme.
/**
* Copies a theme to a values folder with api version apiLevel,
* potentially creating the necessary folder or file.
* @param apiLevel api level of the folder the theme is copied to
* @param toBeCopied theme to be copied
*/
public static void copyTheme(int apiLevel, @NotNull final XmlTag toBeCopied) {
ApplicationManager.getApplication().assertWriteAccessAllowed();
PsiFile file = toBeCopied.getContainingFile();
assert file instanceof XmlFile : file;
ResourceFolderType folderType = ResourceHelper.getFolderType(file);
assert folderType != null : file;
FolderConfiguration config = ResourceHelper.getFolderConfiguration(file);
assert config != null : file;
VersionQualifier qualifier = new VersionQualifier(apiLevel);
config.setVersionQualifier(qualifier);
String folder = config.getFolderName(folderType);
if (folderType != ResourceFolderType.VALUES) {
OverrideResourceAction.forkResourceFile((XmlFile) file, folder, false);
} else {
XmlTag tag = OverrideResourceAction.getValueTag(PsiTreeUtil.getParentOfType(toBeCopied, XmlTag.class, false));
if (tag != null) {
PsiDirectory dir = null;
PsiDirectory resFolder = file.getParent();
if (resFolder != null) {
resFolder = resFolder.getParent();
}
if (resFolder != null) {
dir = resFolder.findSubdirectory(folder);
if (dir == null) {
dir = resFolder.createSubdirectory(folder);
}
}
OverrideResourceAction.forkResourceValue(toBeCopied.getProject(), tag, file, dir, false);
}
}
}
use of com.android.ide.common.resources.configuration.FolderConfiguration in project android by JetBrains.
the class ParentRendererEditor method updateVariantsCombo.
private void updateVariantsCombo() {
if (myItem == null) {
myVariantsComboBox.setVisible(false);
return;
}
myVariantsComboBox.setVisible(true);
Collection<ConfiguredElement<String>> allParents = myItem.getParentNames();
final String currentVariantColor = ColorUtil.toHex(ThemeEditorConstants.CURRENT_VARIANT_COLOR);
final String notSelectedVariantColor = ColorUtil.toHex(ThemeEditorConstants.NOT_SELECTED_VARIANT_COLOR);
final ArrayList<VariantsComboItem> variants = Lists.newArrayListWithCapacity(allParents.size());
ConfiguredThemeEditorStyle currentParent = myItem.getParent(myContext.getThemeResolver());
ConfiguredElement<String> selectedElement = null;
if (currentParent != null) {
//noinspection unchecked
selectedElement = (ConfiguredElement<String>) currentParent.getConfiguration().getFullConfig().findMatchingConfigurable(ImmutableList.copyOf(allParents));
}
if (selectedElement == null) {
selectedElement = allParents.iterator().next();
}
for (ConfiguredElement<String> configuredParent : allParents) {
FolderConfiguration restrictedConfig = RestrictedConfiguration.restrict(configuredParent, allParents);
String parentName = configuredParent.getElement();
if (restrictedConfig == null) {
// This type is not visible
LOG.warn(String.format("For style '%1$s': Folder configuration '%2$s' can never be selected. There are no qualifiers combination that would allow selecting it.", parentName, configuredParent.getConfiguration()));
continue;
}
if (configuredParent.getConfiguration().equals(selectedElement.getConfiguration())) {
// This is the selected parent
variants.add(0, new VariantsComboItem(String.format(ThemeEditorConstants.CURRENT_VARIANT_TEMPLATE, currentVariantColor, configuredParent.getConfiguration().toShortDisplayString()), restrictedConfig, configuredParent.getConfiguration()));
} else {
variants.add(new VariantsComboItem(String.format(ThemeEditorConstants.NOT_SELECTED_VARIANT_TEMPLATE, notSelectedVariantColor, configuredParent.getConfiguration().toShortDisplayString(), " - " + parentName), restrictedConfig, configuredParent.getConfiguration()));
}
}
myVariantsComboBox.setModel(new CollectionComboBoxModel<VariantsComboItem>(variants, variants.get(0)));
}
Aggregations