001/*
002 *  Copyright 2015 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.skinfactory.clientsidelement;
017
018import java.util.ArrayList;
019import java.util.LinkedHashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.avalon.framework.configuration.DefaultConfiguration;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028
029import org.ametys.cms.languages.Language;
030import org.ametys.cms.languages.LanguagesManager;
031import org.ametys.core.ui.ClientSideElement;
032import org.ametys.core.ui.SimpleMenu;
033import org.ametys.core.ui.StaticClientSideElement;
034import org.ametys.core.util.I18nUtils;
035import org.ametys.runtime.i18n.I18nizableText;
036
037/**
038 * {@link StaticClientSideElement} for selection of language for a skin
039 */
040public class SkinLanguageMenu extends SimpleMenu
041{
042    private LanguagesManager _languageManager;
043    private boolean _languagesInitialized;
044    private I18nUtils _i18nUtils;
045    
046    @Override
047    public void service(ServiceManager smanager) throws ServiceException
048    {
049        super.service(smanager);
050        _languageManager = (LanguagesManager) smanager.lookup(LanguagesManager.ROLE);
051        _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE);
052    }
053    
054    @Override
055    protected void _getGalleryItems(Map<String, Object> parameters, Map<String, Object> contextualParameters)
056    {
057        try
058        {
059            _lazyInitializeLanguageGallery();
060        }
061        catch (Exception e)
062        {
063            throw new IllegalStateException("Unable to lookup client side element local components", e);
064        }
065        
066        if (_galleryItems.size() > 0)
067        {
068            parameters.put("gallery-item", new LinkedHashMap<String, Object>());
069            
070            for (GalleryItem galleryItem : _galleryItems)
071            {
072                @SuppressWarnings("unchecked")
073                Map<String, Object> galleryItems = (Map<String, Object>) parameters.get("gallery-item");
074                galleryItems.put("gallery-groups", new ArrayList<>());
075                
076                for (GalleryGroup galleryGp : galleryItem.getGroups())
077                {
078                    @SuppressWarnings("unchecked")
079                    List<Object> galleryGroups = (List<Object>) galleryItems.get("gallery-groups");
080                    
081                    Map<String, Object> groupParams = new LinkedHashMap<>();
082                    
083                    I18nizableText label = galleryGp.getLabel();
084                    groupParams.put("label", label);
085                    
086                    // Group items
087                    List<String> gpItems = new ArrayList<>();
088                    for (ClientSideElement element : galleryGp.getItems())
089                    {
090                        gpItems.add(element.getId());
091                    }
092                    
093                    if (gpItems.size() > 0)
094                    {
095                        groupParams.put("items", gpItems);
096                        galleryGroups.add(groupParams);
097                    }
098                }
099            }
100        }
101    }
102    
103    /**
104     * Lazy initialization of the language gallery
105     * @throws ConfigurationException if the language configuration is wrong
106     */
107    private synchronized void _lazyInitializeLanguageGallery() throws ConfigurationException
108    {
109        if (!_languagesInitialized)
110        {
111            Map<String, Language> availableLanguages = _languageManager.getAvailableLanguages();
112            
113            if (availableLanguages.size() > 0)
114            {
115                GalleryItem galleryItem = new GalleryItem();
116                
117                GalleryGroup galleryGroup = new GalleryGroup(new I18nizableText("plugin." + _pluginName, "RIBBON_TABS_TAB_SKINFACTORY_GROUP_LANG_LABEL"));
118                galleryItem.addGroup(galleryGroup);
119                
120                for (String id : availableLanguages.keySet())
121                {
122                    Language language = availableLanguages.get(id);
123                    
124                    String itemId = this.getId() + "-" + id;
125                    
126                    Configuration conf = _getLanguageConfiguration(itemId, language);
127                    _getGalleryItemManager().addComponent(_pluginName, null, itemId, StaticClientSideElement.class, conf);
128                    galleryGroup.addItem(new UnresolvedItem(itemId, true));
129                }
130                
131                _galleryItems.add(galleryItem);
132            }
133            
134            if (_galleryItems.size() > 0)
135            {
136                try
137                {
138                    _getGalleryItemManager().initialize();
139                }
140                catch (Exception e)
141                {
142                    throw new ConfigurationException("Unable to lookup parameter local components", e);
143                }
144            }
145        }
146        
147        _languagesInitialized = true;
148    }
149    
150    /**
151     * Get the configuration of the language item
152     * @param id The id of item
153     * @param language The language
154     * @return The configuration
155     */
156    protected Configuration _getLanguageConfiguration (String id, Language language)
157    {
158        DefaultConfiguration conf = new DefaultConfiguration("extension");
159        conf.setAttribute("id", id);
160        
161        DefaultConfiguration classConf = new DefaultConfiguration("class");
162        classConf.setAttribute("name", "Ametys.ribbon.element.ui.ButtonController");
163        
164        // Label
165        DefaultConfiguration labelConf = new DefaultConfiguration("label");
166        labelConf.setValue(_i18nUtils.translate(language.getLabel()));
167        classConf.addChild(labelConf);
168        
169        // Icons
170        DefaultConfiguration iconSmallConf = new DefaultConfiguration("icon-small");
171        iconSmallConf.setValue(language.getSmallIcon());
172        classConf.addChild(iconSmallConf);
173        DefaultConfiguration iconMediumConf = new DefaultConfiguration("icon-medium");
174        iconMediumConf.setValue(language.getMediumIcon());
175        classConf.addChild(iconMediumConf);
176        DefaultConfiguration iconLargeConf = new DefaultConfiguration("icon-large");
177        iconLargeConf.setValue(language.getLargeIcon());
178        classConf.addChild(iconLargeConf);
179
180        // Language
181        DefaultConfiguration langConf = new DefaultConfiguration("lang");
182        langConf.setValue(language.getCode());
183        classConf.addChild(langConf);
184
185        // Common configuration
186        @SuppressWarnings("unchecked")
187        Map<String, Object> commonConfig = (Map<String, Object>) this._script.getParameters().get("items-config");
188        for (String tagName : commonConfig.keySet())
189        {
190            DefaultConfiguration c = new DefaultConfiguration(tagName);
191            c.setValue(String.valueOf(commonConfig.get(tagName)));
192            classConf.addChild(c);
193        }
194        
195        conf.addChild(classConf);
196        return conf;
197    }
198    
199}