001/*
002 *  Copyright 2017 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.web.site;
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;
030
031/**
032 * Component listing the available colors for a site
033 */
034public class SiteColorsComponent implements Component, Configurable
035{
036    /** The component role */
037    public static final String ROLE = SiteColorsComponent.class.getName();
038    
039    private Map<String, Map<String, String>> _colors;
040    private String _defaultKey;
041    
042    public void configure(Configuration configuration) throws ConfigurationException
043    {
044        Map<String, Map<String, String>> siteColors = new LinkedHashMap<>();
045        String defaultKey = configuration.getAttribute("default", "");
046        for (Configuration colorConfiguration : configuration.getChildren("color"))
047        {
048            Map<String, String> color = new HashMap<>();
049            
050            for (Configuration child : colorConfiguration.getChildren())
051            {
052                String name = child.getName();
053                String value = child.getValue();
054                
055                color.put(name, value);
056            }
057
058            String id = colorConfiguration.getAttribute("id");
059            siteColors.put(id, color);
060            
061            if (StringUtils.isEmpty(defaultKey))
062            {
063                defaultKey = id;
064            }
065        }
066        
067        _defaultKey = defaultKey;
068        _colors = siteColors;
069    }
070    
071    /**
072     * Get the key to use as default in the colors
073     * @return A non null key.
074     */
075    public String getDefaultKey()
076    {
077        return _defaultKey;
078    }
079    
080    /**
081     * Get the colors by index
082     * A 'color' is an association key-hexacolor. Where key can be 'main', 'text'... 
083     * @return The map
084     */
085    @Callable
086    public Map<String, Map<String, String>> getColors()
087    {
088        return _colors;
089    }
090}