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.List;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.apache.cocoon.xml.AttributesImpl;
024import org.apache.cocoon.xml.XMLUtils;
025import org.slf4j.Logger;
026import org.xml.sax.ContentHandler;
027import org.xml.sax.SAXException;
028
029import org.ametys.core.ui.RibbonManager;
030
031/**
032 * A toolbar of controls
033 */
034public class Toolbar implements Element
035{
036    /** Number of columns used by the control upon some layouts. 1 is the common and default value */
037    protected int _colspan;
038    
039    /** The elements in the layout. Can be controls */
040    protected List<Element> _elements = new ArrayList<>();
041
042    /** Logger */
043    protected Logger _toolbarLogger;
044    
045    /**
046     * Creates a toolbar of controls
047     * @param toolbarConfiguration The configuration for the layout
048     * @param ribbonManager The ribbon manager
049     * @param logger The logger
050     * @throws ConfigurationException if an error occurred
051     */
052    public Toolbar(Configuration toolbarConfiguration, RibbonManager ribbonManager, Logger logger) throws ConfigurationException
053    {
054        this(logger, toolbarConfiguration.getAttributeAsInteger("colspan", 1));
055        
056        Configuration[] elementsConfigurations = toolbarConfiguration.getChildren();
057        for (Configuration elementConfiguration : elementsConfigurations)
058        {
059            if ("control".equals(elementConfiguration.getName()))
060            {
061                ControlRef control = new ControlRef(elementConfiguration, ribbonManager, _toolbarLogger);
062                _elements.add(control);
063            }
064            else if (_toolbarLogger.isWarnEnabled())
065            {
066                _toolbarLogger.warn("During configuration of the ribbon, the toolbar use an unknow tag '" + elementConfiguration.getName() + "'");
067            }
068        }
069    }
070
071    /**
072     * Creates an empty toolbar of controls
073     * @param logger The logger
074     * @param colspan The toolbar colspan
075     */
076    public Toolbar(Logger logger, int colspan)
077    {
078        this._toolbarLogger = logger;
079        
080        this._colspan = colspan;
081        if (_toolbarLogger.isDebugEnabled())
082        {
083            _toolbarLogger.debug("Control colspan is " + this._colspan);
084        }
085    }
086    
087    @Override
088    public int getColumns()
089    {
090        return _colspan;
091    }
092    
093    @Override
094    public void setColumns(int size)
095    {
096        _colspan = size;
097    }
098    
099    @Override
100    public List<Element> getChildren()
101    {
102        return _elements;
103    }
104    
105    @Override
106    public void toSAX(ContentHandler handler) throws SAXException
107    {
108        AttributesImpl attrs = new AttributesImpl();
109        attrs.addCDATAAttribute("colspan", Integer.toString(_colspan));
110        
111        XMLUtils.startElement(handler, "toolbar", attrs);
112        
113        for (Element element : _elements)
114        {
115            element.toSAX(handler);
116        }
117        
118        XMLUtils.endElement(handler, "toolbar");
119    }
120
121    public boolean isSame(Element element)
122    {
123        if (!(element instanceof Toolbar))
124        {
125            return false;
126        }
127        
128        Toolbar toolbar = (Toolbar) element;
129        if (toolbar._colspan != _colspan)
130        {
131            return false;
132        }
133        
134        if (toolbar.getChildren().size() != _elements.size())
135        {
136            return false;
137        }
138        for (int i = 0; i < _elements.size(); i++)
139        {
140            Element child = _elements.get(i);
141            Element objElement = toolbar.getChildren().get(i);
142            if (child == null || objElement == null || !child.isSame(objElement))
143            {
144                return false;
145            }
146        }
147        
148        return true;
149    }
150}