001/*
002 *  Copyright 2021 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 */
016
017package org.ametys.cms.color;
018
019import java.util.HashMap;
020import java.util.LinkedHashMap;
021import java.util.Map;
022
023import org.apache.avalon.framework.component.Component;
024import org.apache.avalon.framework.configuration.Configurable;
025import org.apache.avalon.framework.configuration.Configuration;
026import org.apache.avalon.framework.configuration.ConfigurationException;
027import org.apache.commons.lang3.StringUtils;
028
029import org.ametys.core.ui.Callable;
030import org.ametys.runtime.plugin.component.AbstractLogEnabled;
031
032/**
033 * Component listing the available colors
034 */
035public abstract class AbstractColorsComponent extends AbstractLogEnabled implements Component, Configurable
036{
037    private Map<String, Map<String, String>> _colors;
038    private String _defaultKey;
039    private String _colorCSSClass;
040    
041    public void configure(Configuration configuration) throws ConfigurationException
042    {
043        Configuration newConfiguration = configuration;
044        if (!"colors".equals(configuration.getName()))
045        {
046            newConfiguration = configuration.getChild("colors");
047        }
048        
049        ColorsDefinition colorsConf = parseColors(newConfiguration);
050        
051        _defaultKey = colorsConf.defaultKey();
052        _colors = colorsConf.colors();
053        _colorCSSClass = colorsConf.colorClass();
054    }
055    
056    /**
057     * Get the key to use as default in the colors
058     * @return A non null key.
059     */
060    public String getDefaultKey()
061    {
062        return _defaultKey;
063    }
064    
065    /**
066     * Get the colors by index
067     * A 'color' is an association key-hexacolor. Where key can be 'main', 'text'... 
068     * @return The map
069     */
070    @Callable
071    public Map<String, Map<String, String>> getColors()
072    {
073        return _colors;
074    }
075    
076    /**
077     * Get the color class
078     * @return the color class
079     */
080    public String getColorCSSClassPrefix()
081    {
082        return _colorCSSClass;
083    }
084    
085    /**
086     * Parse colors from configuration
087     * @param configuration the configuration
088     * @return the colors configuration
089     * @throws ConfigurationException if a configuration error occurred
090     */
091    public ColorsDefinition parseColors(Configuration configuration) throws ConfigurationException
092    {
093        Map<String, Map<String, String>> colors = new LinkedHashMap<>();
094        String defaultKey = configuration.getAttribute("default", "");
095        for (Configuration colorConfiguration : configuration.getChildren("color"))
096        {
097            Map<String, String> color = new HashMap<>();
098            
099            for (Configuration child : colorConfiguration.getChildren())
100            {
101                String name = child.getName();
102                String value = child.getValue();
103                
104                color.put(name, value);
105            }
106
107            String id = colorConfiguration.getAttribute("id");
108            colors.put(id, color);
109            
110            if (StringUtils.isEmpty(defaultKey))
111            {
112                defaultKey = id;
113            }
114        }
115        
116        String colorClass = configuration.getAttribute("css-class", "color");
117        return new ColorsDefinition(defaultKey, colors, colorClass);
118    }
119    
120    /**
121     * Class representing a colors definition
122     * @param defaultKey the default color key
123     * @param colors the colors
124     * @param colorClass the color class
125     */
126    protected record ColorsDefinition(String defaultKey, Map<String, Map<String, String>> colors, String colorClass) { /* empty */ }
127}