001/*
002 *  Copyright 2020 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.plugins.workspaces.calendars;
017
018import java.util.LinkedHashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.component.Component;
022import org.apache.avalon.framework.configuration.Configurable;
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025
026/**
027 * Component listing the available colors for a {@link Calendar}
028 */
029public class CalendarColorsComponent implements Component, Configurable
030{
031    /** The component role */
032    public static final String ROLE = CalendarColorsComponent.class.getName();
033
034    private Map<String, CalendarColor> _colors;
035    private Map<String, CalendarColor> _allColors;
036    
037    public void configure(Configuration configuration) throws ConfigurationException
038    {
039        Map<String, CalendarColor> allColors = new LinkedHashMap<>();
040        Map<String, CalendarColor> calendarColors = new LinkedHashMap<>();
041        
042        for (Configuration colorConfiguration : configuration.getChildren("resourceColor"))
043        {
044            String id = colorConfiguration.getAttribute("id");
045            String label = colorConfiguration.getChild("label").getValue();
046            String bgColor = colorConfiguration.getChild("bg").getValue();
047            String fgColor = colorConfiguration.getChild("fg").getValue();
048            
049            CalendarColor color = new CalendarColor(id, label, bgColor, fgColor);
050            allColors.put(id, color);
051        }
052        
053        for (Configuration colorConfiguration : configuration.getChildren("color"))
054        {
055            String id = colorConfiguration.getAttribute("id");
056            String label = colorConfiguration.getChild("label").getValue();
057            String bgColor = colorConfiguration.getChild("bg").getValue();
058            String fgColor = colorConfiguration.getChild("fg").getValue();
059            
060            CalendarColor color = new CalendarColor(id, label, bgColor, fgColor);
061            calendarColors.put(id, color);
062            allColors.put(id, color);
063        }
064        
065        _colors = calendarColors;
066        _allColors = allColors;
067    }
068
069    /**
070     * Get the colors of calendars
071     * @return colors
072     */
073    public Map<String, CalendarColor> getColors()
074    {
075        return _colors;
076    }
077    
078    /**
079     * Get the colors of calendars including those restricted for resource calendars
080     * @return colors
081     */
082    public Map<String, CalendarColor> getAllColors()
083    {
084        return _allColors;
085    }
086    
087    /**
088     * Get the color by id
089     * @param id the id
090     * @return the color
091     */
092    public CalendarColor getColor(String id)
093    {
094        return _allColors.get(id);
095    }
096    
097    /**
098     * Class representing a calendar color
099     *
100     */
101    public class CalendarColor 
102    {
103        String _bgColor;
104        String _fgColor;
105        String _label;
106        String _id;
107        
108        /**
109         * Constructor
110         * @param id the id
111         * @param label the label
112         * @param bgColor the color
113         * @param fgCOlor the text color
114         */
115        public CalendarColor (String id, String label, String bgColor, String fgCOlor)
116        {
117            _id = id;
118            _label = label;
119            _bgColor = bgColor;
120            _fgColor = fgCOlor;
121        }
122        
123        /**
124         * Get the id
125         * @return the id
126         */
127        public String getId()
128        {
129            return _id;
130        }
131        
132        /**
133         * Get the label
134         * @return the label
135         */
136        public String getLabel()
137        {
138            return _label;
139        }
140        
141        /**
142         * Get the color
143         * @return the color
144         */
145        public String getColor()
146        {
147            return _bgColor;
148        }
149        
150        /**
151         * Get the text color
152         * @return the text color
153         */
154        public String getTextColor()
155        {
156            return _fgColor;
157        }
158    }
159}