001/**
002 *  Copyright 2016 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.core.ui.ribbonconfiguration;
017
018import java.util.ArrayList;
019import java.util.HashSet;
020import java.util.List;
021import java.util.Set;
022
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.cocoon.xml.XMLUtils;
026import org.slf4j.Logger;
027import org.xml.sax.ContentHandler;
028import org.xml.sax.SAXException;
029
030import org.ametys.core.ui.RibbonManager;
031
032/**
033 * A group in a defined size
034 */
035public class GroupSize
036{
037    /** The list of elements in the group (controls or layouts) */
038    protected List<Element> _elements = new ArrayList<>();
039    
040    /** The logger */
041    protected Logger _groupSizeLogger;
042    
043    /**
044     * Creates a group in a defined size
045     * @param groupSizeConfiguration The configuration for the size
046     * @param ribbonManager The ribbon manager
047     * @param logger The logger
048     * @throws ConfigurationException if an error occurred
049     */
050    public GroupSize(Configuration groupSizeConfiguration, RibbonManager ribbonManager, Logger logger) throws ConfigurationException
051    {
052        this._groupSizeLogger = logger;
053        
054        Configuration[] elementsConfigurations = groupSizeConfiguration.getChildren();
055        for (Configuration elementConfiguration : elementsConfigurations)
056        {
057            if ("control".equals(elementConfiguration.getName()))
058            {
059                ControlRef control = new ControlRef(elementConfiguration, ribbonManager, _groupSizeLogger);
060                _elements.add(control);
061            }
062            else if ("layout".equals(elementConfiguration.getName()))
063            {
064                Layout layout = new Layout(elementConfiguration, ribbonManager, _groupSizeLogger);
065                _elements.add(layout);
066            }
067            else if (_groupSizeLogger.isWarnEnabled())
068            {
069                _groupSizeLogger.warn("During configuration of the ribbon, the group use an unknow tag '" + elementConfiguration.getName() + "'");
070            }
071        }
072    }
073    
074    /**
075     * Creates an empty group in a defined size
076     * @param logger The logger
077     */
078    public GroupSize(Logger logger)
079    {
080        this._groupSizeLogger = logger;
081    }
082    
083    /**
084     * Get a set of all referenced ids
085     * @return A non null set of control ids
086     */
087    public Set<String> getControlIds()
088    {
089        return _getControlIds(_elements);
090    }
091    
092    private Set<String> _getControlIds(List<Element> elements)
093    {
094        Set<String> ids = new HashSet<>();
095        
096        for (Element element : elements)
097        {
098            if (element instanceof ControlRef)
099            {
100                ControlRef controlRef = (ControlRef) element;
101                ids.add(controlRef._id);
102            }
103            else if (element instanceof Layout)
104            {
105                Layout layout = (Layout) element;
106        
107                ids.addAll(_getControlIds(layout._elements));
108            }
109            else
110            {
111                Toolbar toolbar = (Toolbar) element;
112                
113                ids.addAll(_getControlIds(toolbar._elements));
114            }
115        }
116        
117        return ids;
118    }
119    
120    /**
121     * Retrieve the list of children elements in this element.
122     * @return The list of elements.
123     */
124    public List<Element> getChildren()
125    {
126        return _elements;
127    }
128    
129    /**
130     * Sax the the configuration of the group size.
131     * @param elementName The name of the surrounding element to use
132     * @param handler The content handler where to sax
133     * @throws SAXException if an error occurs
134     */
135    public void toSAX(String elementName, ContentHandler handler) throws SAXException
136    {
137        XMLUtils.startElement(handler, elementName);
138
139        for (Element element : _elements)
140        {
141            element.toSAX(handler);
142        }
143
144        XMLUtils.endElement(handler, elementName);
145    }
146    
147    /**
148     * Test if this GroupSize contains the same elements as another GroupSize
149     * @param obj The other GroupSize
150     * @return True if they are equals
151     */
152    public boolean isSame(GroupSize obj)
153    {
154        List<Element> objElements = obj.getChildren();
155        if (objElements.size() != _elements.size())
156        {
157            return false;
158        }
159        for (int i = 0; i < _elements.size(); i++)
160        {
161            Element element = _elements.get(i);
162            Element objElement = objElements.get(i);
163            if (element == null || objElement == null || !element.isSame(objElement))
164            {
165                return false;
166            }
167        }
168        return true;
169    }
170}