001/*
002 *  Copyright 2010 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.cms.clientsideelement.styles;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.configuration.Configurable;
024import org.apache.avalon.framework.configuration.Configuration;
025import org.apache.avalon.framework.configuration.ConfigurationException;
026import org.apache.avalon.framework.logger.AbstractLogEnabled;
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.avalon.framework.service.Serviceable;
030import org.apache.commons.lang3.StringUtils;
031import org.apache.excalibur.source.SourceResolver;
032
033import org.ametys.runtime.i18n.I18nizableText;
034import org.ametys.runtime.plugin.component.PluginAware;
035
036/**
037 * This implementation is based upon a static configuration.
038 */
039public class StaticHTMLEditorStyleExtension extends AbstractLogEnabled implements HTMLEditorStyleExtensionPoint, Configurable, Serviceable, PluginAware
040{
041    /** Default icons style for table */
042    protected static final String[] TABLE_DEFAULT_ICONS = new String[] {"/plugins/cms/resources/img/content/edition/table/Table_Border_16.png", "/plugins/cms/resources/img/content/edition/table/Table_Border_32.png", "/plugins/cms/resources/img/content/edition/table/Table_Border_32.png"};
043    /** Default icons style for link */
044    protected static final String[] LINK_DEFAULT_ICONS = new String[] {"/plugins/cms/resources/img/content/edition/link/Style_Normal_16.png", "/plugins/cms/resources/img/content/edition/link/Style_Normal_32.png", "/plugins/cms/resources/img/content/edition/link/Style_Normal_32.png"};
045    /** Default icons style for image */
046    protected static final String[] IMAGE_DEFAULT_ICONS = new String[] {"/plugins/cms/resources/img/content/edition/image/Image_Style_Border_16.png", "/plugins/cms/resources/img/content/edition/link/Style_Normal_32.png", "/plugins/cms/resources/img/content/edition/link/Style_Normal_32.png"};
047    /** Default icons style for unordered list */
048    protected static final String[] UL_DEFAULT_ICONS = new String[] {"/plugins/cms/resources/img/content/edition/list/disc_16.png", "/plugins/cms/resources/img/content/edition/link/Style_Normal_32.png", "/plugins/cms/resources/img/content/edition/link/Style_Normal_32.png"};
049    /** Default icons style for ordered list */
050    protected static final String[] OL_DEFAULT_ICONS = new String[] {"/plugins/cms/resources/img/content/edition/list/decimal_16.png", "/plugins/cms/resources/img/content/edition/link/Style_Normal_32.png", "/plugins/cms/resources/img/content/edition/link/Style_Normal_32.png"};
051    
052    /** Excalibur source resolver */
053    protected SourceResolver _resolver;
054    /** The values of the file */
055    protected StyleCategory _paraStyleCategory;
056    /** The values of the file */
057    protected StyleCategory _tableStyleCategory;
058    /** The values of the file */
059    protected StyleCategory _linkStyleCategory;
060    /** The values of the file */
061    protected StyleCategory _imageStyleCategory;
062    /** The values of the file */
063    protected StyleCategory _ulStyleCategory;
064    /** The values of the file */
065    protected StyleCategory _olStyleCategory;
066    /** The plugin that declared the instance */
067    protected String _pluginName;
068    /** The feature that declared the instance */
069    protected String _featureName;
070    
071    @Override
072    public void service(ServiceManager manager) throws ServiceException
073    {
074        _resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
075    }
076    
077    @Override
078    public void setPluginInfo(String pluginName, String featureName, String id)
079    {
080        _pluginName = pluginName;
081        _featureName = featureName;
082    }
083    
084    @Override
085    public void configure(Configuration configuration) throws ConfigurationException
086    {
087        _configure(configuration, "plugin." + _pluginName, new HashMap<String, Object>());
088    }
089    
090    /**
091     * Configure this instance upon a configuration
092     * @param configuration The configuration
093     * @param defaultCatalogue The default i18n catalogue to use
094     * @param contextParameters Contextuals parameters transmitted by the environment.
095     * @throws ConfigurationException If the configuration is incorrect
096     */
097    protected void _configure(Configuration configuration, String defaultCatalogue, Map<String, Object> contextParameters) throws ConfigurationException
098    {
099        _paraStyleCategory = _configureStyleCategory(configuration.getChild("para"), defaultCatalogue, null, contextParameters);
100        _tableStyleCategory = _configureStyleCategory(configuration.getChild("table"), defaultCatalogue, TABLE_DEFAULT_ICONS, contextParameters);
101        _linkStyleCategory = _configureStyleCategory(configuration.getChild("link"), defaultCatalogue, LINK_DEFAULT_ICONS, contextParameters);
102        _imageStyleCategory = _configureStyleCategory(configuration.getChild("image"), defaultCatalogue, IMAGE_DEFAULT_ICONS, contextParameters);
103        _ulStyleCategory = _configureStyleCategory(configuration.getChild("unorderedlist"), defaultCatalogue, UL_DEFAULT_ICONS, contextParameters);
104        _olStyleCategory = _configureStyleCategory(configuration.getChild("orderedlist"), defaultCatalogue, OL_DEFAULT_ICONS, contextParameters);
105    }
106    
107    /**
108     * Configure this instance upon the style part of a configuration
109     * @param styleConfiguration The style part of the configuration
110     * @param defaultCatalogue The default i18n catalogue to use
111     * @param defaultIcons the path to the button's default icons in small, medium and large size
112     * @param contextParameters Contextuals parameters transmitted by the environment.
113     * @return The style that represents the values read
114     * @throws ConfigurationException If the configuration is incorrect
115     */
116    protected StyleCategory _configureStyleCategory(Configuration styleConfiguration, String defaultCatalogue, String[] defaultIcons, Map<String, Object> contextParameters) throws ConfigurationException
117    {
118        String boCSSFile = _getImport(styleConfiguration.getChild("import").getChild("button"), contextParameters);
119        String inlineEditorCSSFile = _getImport(styleConfiguration.getChild("import").getChild("inline-editor"), contextParameters);
120        
121        List<Style> styleList = new ArrayList<>();
122        for (Configuration styleConf : styleConfiguration.getChildren("style"))
123        {
124            Style style = _configureStyle(styleConf, defaultCatalogue, defaultIcons, contextParameters);
125            styleList.add(style);
126        }
127        
128        return new StyleCategory(boCSSFile, inlineEditorCSSFile, styleList);
129    }
130    
131    /**
132     * Determine an url from a configuration node.
133     * @param configuration Read the optional plugin attribute and concatenate it to the value of the node. Can be null
134     * @param contextParameters Contextuals parameters transmitted by the environment.
135     * @return The non-null path to the plugin resource
136     */
137    protected String _getImport(Configuration configuration, Map<String, Object> contextParameters)
138    {
139        String boPlugin = configuration.getAttribute("plugin", _pluginName);
140        String boFileLocation = "/plugins/" + boPlugin + "/resources/";
141        
142        String uri = configuration.getValue(null);
143        return StringUtils.isBlank(uri) ? null : boFileLocation + uri;
144    }
145    
146    /**
147     * Configure a style
148     * @param configuration The style configuration
149     * @param defaultCatalogue The default i18n catalogue to use
150     * @param defaultIcons the path to the button's default icons in small, medium and large size
151     * @param contextParameters Contextual parameters transmitted by the environment.
152     * @return The style configured
153     * @throws ConfigurationException If the configuration is incorrect
154     */
155    protected Style _configureStyle(Configuration configuration, String defaultCatalogue, String[] defaultIcons, Map<String, Object> contextParameters) throws ConfigurationException
156    {
157        String buttonSmallIcon = defaultIcons != null ? defaultIcons[0] : null;
158        String buttonMediumIcon = defaultIcons != null ? defaultIcons[1] : null;
159        String buttonLargeIcon = defaultIcons != null ? defaultIcons[2] : null;
160        I18nizableText buttonLabel;
161        I18nizableText buttonDescription;
162        String buttonCSSClass;
163
164        Configuration iconSmallConf = configuration.getChild("button").getChild("icon-small", false);
165        if (iconSmallConf != null)
166        {
167            buttonSmallIcon = _getImport(iconSmallConf, contextParameters);
168        }
169        
170        Configuration iconMediumConf = configuration.getChild("button").getChild("icon-medium", false);
171        if (iconMediumConf == null)
172        {
173            // Try to look for "icon" if no "icon-medium".
174            iconMediumConf = configuration.getChild("button").getChild("icon", false);
175        }
176        
177        if (iconMediumConf != null)
178        {
179            buttonMediumIcon = _getImport(iconMediumConf, contextParameters);
180        }
181        
182        Configuration iconLargeConf = configuration.getChild("button").getChild("icon-large", false);
183        if (iconLargeConf == null)
184        {
185            // Try to look for "icon" if no "icon-large".
186            iconLargeConf = configuration.getChild("button").getChild("icon", false);
187        }
188        
189        if (iconLargeConf != null)
190        {
191            buttonLargeIcon = _getImport(iconLargeConf, contextParameters);
192        }
193        
194        Configuration labelConf = configuration.getChild("button").getChild("label");
195        if (labelConf.getAttributeAsBoolean("i18n", false))
196        {
197            buttonLabel = new I18nizableText(defaultCatalogue, labelConf.getValue());
198        }
199        else 
200        {
201            buttonLabel = new I18nizableText(labelConf.getValue());
202        }
203
204        Configuration descriptionConf = configuration.getChild("button").getChild("description");
205        if (descriptionConf.getAttributeAsBoolean("i18n", false))
206        {
207            buttonDescription = new I18nizableText(defaultCatalogue, descriptionConf.getValue());
208        }
209        else 
210        {
211            buttonDescription = new I18nizableText(descriptionConf.getValue());
212        }
213        
214        buttonCSSClass = configuration.getChild("button").getChild("cssclass").getValue("");
215        
216        String inlineEditorRender = configuration.getChild("inline-editor").getValue();
217        
218        return new Style(buttonLabel, buttonDescription, buttonSmallIcon, buttonMediumIcon, buttonLargeIcon, buttonCSSClass, inlineEditorRender);
219    }
220
221    @Override
222    public StyleCategory getPara(Map<String, Object> contextualParameters)
223    {
224        return _paraStyleCategory;
225    }
226    
227    @Override
228    public StyleCategory getTable(Map<String, Object> contextualParameters)
229    {
230        return _tableStyleCategory;
231    }
232    
233    @Override
234    public StyleCategory getLink(Map<String, Object> contextualParameters)
235    {
236        return _linkStyleCategory;
237    }
238    
239    @Override
240    public StyleCategory getImage(Map<String, Object> contextualParameters)
241    {
242        return _imageStyleCategory;
243    }
244    
245    @Override
246    public StyleCategory getUnorderedList(Map<String, Object> contextualParameters)
247    {
248        return _ulStyleCategory;
249    }
250    
251    @Override
252    public StyleCategory getOrderedList(Map<String, Object> contextualParameters)
253    {
254        return _olStyleCategory;
255    }
256}