Search in sources :

Example 11 with Locale

use of com.android.tools.idea.rendering.Locale in project android by JetBrains.

the class ConfigurationMatcher method adaptConfigSelection.

/**
   * Adapts the current device/config selection so that it's compatible with
   * the configuration.
   * <p/>
   * If the current selection is compatible, nothing is changed.
   * <p/>
   * If it's not compatible, configs from the current devices are tested.
   * <p/>
   * If none are compatible, it reverts to
   * {@link #findAndSetCompatibleConfig(boolean)}
   */
void adaptConfigSelection(boolean needBestMatch) {
    // check the device config (ie sans locale)
    // if still true, we need to find another config.
    boolean needConfigChange = true;
    boolean currentConfigIsCompatible = false;
    State selectedState = myConfiguration.getDeviceState();
    FolderConfiguration editedConfig = myConfiguration.getEditedConfig();
    Module module = myConfiguration.getModule();
    if (selectedState != null) {
        FolderConfiguration currentConfig = Configuration.getFolderConfig(module, selectedState, myConfiguration.getLocale(), myConfiguration.getTarget());
        if (currentConfig != null && editedConfig.isMatchFor(currentConfig)) {
            // current config is compatible
            currentConfigIsCompatible = true;
            if (!needBestMatch || isCurrentFileBestMatchFor(currentConfig)) {
                needConfigChange = false;
            }
        }
    }
    if (needConfigChange) {
        List<Locale> localeList = getPrioritizedLocales();
        // if the current state/locale isn't a correct match, then
        // look for another state/locale in the same device.
        FolderConfiguration testConfig = new FolderConfiguration();
        // first look in the current device.
        State matchState = null;
        int localeIndex = -1;
        Device device = myConfiguration.getDevice();
        IAndroidTarget target = myConfiguration.getTarget();
        if (device != null && target != null) {
            VersionQualifier versionQualifier = new VersionQualifier(target.getVersion().getFeatureLevel());
            mainloop: for (State state : device.getAllStates()) {
                testConfig.set(Configuration.getFolderConfig(module, state, myConfiguration.getLocale(), target));
                testConfig.setVersionQualifier(versionQualifier);
                // loop on the locales.
                for (int i = 0; i < localeList.size(); i++) {
                    Locale locale = localeList.get(i);
                    // update the test config with the locale qualifiers
                    testConfig.setLocaleQualifier(locale.qualifier);
                    if (editedConfig.isMatchFor(testConfig) && isCurrentFileBestMatchFor(testConfig)) {
                        matchState = state;
                        localeIndex = i;
                        break mainloop;
                    }
                }
            }
        }
        if (matchState != null) {
            myConfiguration.startBulkEditing();
            myConfiguration.setDeviceState(matchState);
            myConfiguration.setEffectiveDevice(device, matchState);
            myConfiguration.finishBulkEditing();
        } else {
            // no match in current device with any state/locale
            // attempt to find another device that can display this
            // particular state.
            findAndSetCompatibleConfig(currentConfigIsCompatible);
        }
    }
}
Also used : Locale(com.android.tools.idea.rendering.Locale) State(com.android.sdklib.devices.State) Device(com.android.sdklib.devices.Device) IAndroidTarget(com.android.sdklib.IAndroidTarget) Module(com.intellij.openapi.module.Module)

Example 12 with Locale

use of com.android.tools.idea.rendering.Locale in project android by JetBrains.

the class ConfigurationManagerTest method testGetLocales.

public void testGetLocales() {
    myFixture.copyFileToProject("xmlpull/layout.xml", "res/layout/layout1.xml");
    myFixture.copyFileToProject("xmlpull/layout.xml", "res/layout-no-rNO/layout1.xml");
    myFixture.copyFileToProject("xmlpull/layout.xml", "res/layout-no/layout1.xml");
    myFixture.copyFileToProject("xmlpull/layout.xml", "res/layout-se/layout2.xml");
    AndroidFacet facet = AndroidFacet.getInstance(myModule);
    assertNotNull(facet);
    ConfigurationManager manager = facet.getConfigurationManager();
    assertNotNull(manager);
    assertSame(manager, facet.getConfigurationManager());
    List<Locale> locales = manager.getLocales();
    assertEquals(Arrays.asList(Locale.create("no"), Locale.create("no-rNO"), Locale.create("se")), locales);
}
Also used : Locale(com.android.tools.idea.rendering.Locale) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 13 with Locale

use of com.android.tools.idea.rendering.Locale in project android by JetBrains.

the class LocaleMenuAction method getRelevantLocales.

/**
   * Like {@link ConfigurationManager#getLocales} but filters out locales not compatible
   * with language and region qualifiers in the current configuration's folder config
   *
   * @return the list of relevant locales in the project
   */
@NotNull
private List<Locale> getRelevantLocales() {
    List<Locale> locales = new ArrayList<Locale>();
    Configuration configuration = myRenderContext.getConfiguration();
    if (configuration == null) {
        return Collections.emptyList();
    }
    Module module = configuration.getConfigurationManager().getModule();
    LocaleQualifier specificLocale = configuration.getEditedConfig().getLocaleQualifier();
    // locale.
    if (specificLocale != null) {
        List<VirtualFile> variations = ResourceHelper.getResourceVariations(configuration.getFile(), false);
        for (VirtualFile variation : variations) {
            FolderConfiguration config = FolderConfiguration.getConfigForFolder(variation.getParent().getName());
            if (config != null && config.getLocaleQualifier() == null) {
                specificLocale = null;
                break;
            }
        }
    }
    LocalResourceRepository projectResources = ProjectResourceRepository.getProjectResources(module, true);
    Set<LocaleQualifier> languages = projectResources != null ? projectResources.getLocales() : Collections.<LocaleQualifier>emptySet();
    for (LocaleQualifier l : languages) {
        if (specificLocale != null && !specificLocale.isMatchFor(l)) {
            continue;
        }
        locales.add(Locale.create(l));
    }
    return locales;
}
Also used : Locale(com.android.tools.idea.rendering.Locale) VirtualFile(com.intellij.openapi.vfs.VirtualFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) LocalResourceRepository(com.android.tools.idea.res.LocalResourceRepository) ArrayList(java.util.ArrayList) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) Module(com.intellij.openapi.module.Module) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with Locale

use of com.android.tools.idea.rendering.Locale in project android by JetBrains.

the class LocaleMenuAction method getAllLocales.

@NotNull
public static List<Locale> getAllLocales() {
    List<String> sorted = LocaleManager.getLanguageCodes(true);
    List<Locale> locales = new ArrayList<Locale>(sorted.size());
    for (String language : sorted) {
        LocaleQualifier qualifier = new LocaleQualifier(language.length() == 2 ? language : BCP_47_PREFIX + language, language, null, null);
        Locale locale = Locale.create(qualifier);
        locales.add(locale);
    }
    return locales;
}
Also used : Locale(com.android.tools.idea.rendering.Locale) ArrayList(java.util.ArrayList) LocaleQualifier(com.android.ide.common.resources.configuration.LocaleQualifier) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with Locale

use of com.android.tools.idea.rendering.Locale in project android by JetBrains.

the class LocaleMenuAction method createPopupActionGroup.

@Override
@NotNull
protected DefaultActionGroup createPopupActionGroup() {
    DefaultActionGroup group = new DefaultActionGroup(null, true);
    // TODO: Offer submenus, lazily populated, which offer languages either by code or by name.
    // However, this doesn't currently work for the JBPopup dialog we're using as part
    // of the combo action (and using the JBPopup dialog rather than a Swing menu has some
    // other advantages: fitting in with the overall IDE look and feel (e.g. dark colors),
    // allowing typing to filter, etc.
    List<Locale> locales = getRelevantLocales();
    Configuration configuration = myRenderContext.getConfiguration();
    if (configuration != null && locales.size() > 0) {
        group.add(new SetLocaleAction(myRenderContext, getLocaleLabel(Locale.ANY, false, myClassicStyle), Locale.ANY));
        group.addSeparator();
        Collections.sort(locales, Locale.LANGUAGE_CODE_COMPARATOR);
        for (Locale locale : locales) {
            String title = getLocaleLabel(locale, false, myClassicStyle);
            VirtualFile better = ConfigurationMatcher.getBetterMatch(configuration, null, null, locale, null);
            if (better != null) {
                title = ConfigurationAction.getBetterMatchLabel(getLocaleLabel(locale, true, myClassicStyle), better, configuration.getFile());
            }
            group.add(new SetLocaleAction(myRenderContext, title, locale));
        }
        group.addSeparator();
    }
    group.add(new EditTranslationAction());
    return group;
}
Also used : Locale(com.android.tools.idea.rendering.Locale) VirtualFile(com.intellij.openapi.vfs.VirtualFile) FolderConfiguration(com.android.ide.common.resources.configuration.FolderConfiguration) DefaultActionGroup(com.intellij.openapi.actionSystem.DefaultActionGroup) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Locale (com.android.tools.idea.rendering.Locale)19 NotNull (org.jetbrains.annotations.NotNull)7 FolderConfiguration (com.android.ide.common.resources.configuration.FolderConfiguration)5 LocaleQualifier (com.android.ide.common.resources.configuration.LocaleQualifier)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 XmlTag (com.intellij.psi.xml.XmlTag)4 IAndroidTarget (com.android.sdklib.IAndroidTarget)3 Device (com.android.sdklib.devices.Device)3 State (com.android.sdklib.devices.State)3 LocalResourceRepository (com.android.tools.idea.res.LocalResourceRepository)3 Module (com.intellij.openapi.module.Module)3 ResourceItem (com.android.ide.common.res2.ResourceItem)2 ArrayList (java.util.ArrayList)2 LayoutLibrary (com.android.ide.common.rendering.LayoutLibrary)1 DefaultActionGroup (com.intellij.openapi.actionSystem.DefaultActionGroup)1 Project (com.intellij.openapi.project.Project)1 java.util (java.util)1 HashMap (java.util.HashMap)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1 Nullable (org.jetbrains.annotations.Nullable)1