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;
020import java.util.UUID;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.configuration.DefaultConfiguration;
025import org.apache.cocoon.xml.AttributesImpl;
026import org.apache.cocoon.xml.XMLUtils;
027import org.slf4j.Logger;
028import org.xml.sax.ContentHandler;
029import org.xml.sax.SAXException;
030
031import org.ametys.core.ui.RibbonControlsManager;
032import org.ametys.core.ui.RibbonManager;
033
034/**
035 * A control
036 */
037public class ControlRef implements Element
038{
039    /** The id of the control */
040    protected String _id;
041
042    /** Number of columns used by the control upon some layouts. 1 is the common and defaul value */
043    protected int _colspan;
044    
045    /** Logger */
046    protected Logger _controlLogger;
047    
048    /**
049     * Creates a control reference
050     * @param controlConfiguration The configuration for the control
051     * @param ribbonManager The ribbon manager
052     * @param logger The logger
053     * @throws ConfigurationException if an error occurred
054     */
055    public ControlRef(Configuration controlConfiguration, RibbonManager ribbonManager, Logger logger) throws ConfigurationException
056    {
057        String refId = controlConfiguration.getAttribute("ref-id", null);
058        if (refId == null && controlConfiguration.getChildren().length == 0)
059        {
060            _initialize(controlConfiguration.getAttribute("id"), controlConfiguration.getAttributeAsInteger("colspan", 1), logger);
061        }
062        else
063        {
064            String id = controlConfiguration.getAttribute("id", UUID.randomUUID().toString());
065            DefaultConfiguration defaultConfig = new DefaultConfiguration(controlConfiguration);
066            
067            String classname = controlConfiguration.getAttribute("class", null);
068            if (classname == null)
069            {
070                if (refId != null)
071                {
072                    defaultConfig.setAttribute("point", controlConfiguration.getAttribute("point", RibbonControlsManager.ROLE));
073                }
074                else
075                {
076                    classname = org.ametys.core.ui.SimpleMenu.class.getName();
077                    defaultConfig.setAttribute("class", classname);
078                }
079            }
080            
081            ribbonManager.addExtension(id, "core-ui", null, defaultConfig);
082
083            _initialize(id, controlConfiguration.getAttributeAsInteger("colspan", 1), logger);
084        }
085    }
086
087    /**
088     * Creates a control reference
089     * @param id The id referenced by this control
090     * @param colspan The colspan of this control
091     * @param logger The logger
092     */
093    public ControlRef(String id, int colspan, Logger logger)
094    {
095        _initialize(id, colspan, logger);
096    }
097
098    private void _initialize(String id, int colspan, Logger logger)
099    {
100        this._controlLogger = logger;
101        
102        this._id = id;
103        if (_controlLogger.isDebugEnabled())
104        {
105            _controlLogger.debug("Control id is " + this._id);
106        }
107        
108        this._colspan = colspan;
109        if (_controlLogger.isDebugEnabled())
110        {
111            _controlLogger.debug("Control colspan is " + this._colspan);
112        }
113    }
114    
115    /**
116     * Get the id of the ControlRef
117     * @return The id
118     */
119    public String getId()
120    {
121        return _id;
122    }
123
124    @Override
125    public int getColumns()
126    {
127        return _colspan;
128    }
129    
130    @Override
131    public void setColumns(int size)
132    {
133        _colspan = size;
134    }
135    
136    @Override
137    public List<Element> getChildren()
138    {
139        return new ArrayList<>();
140    }
141    
142    @Override
143    public void toSAX(ContentHandler handler) throws SAXException
144    {
145        AttributesImpl attrs = new AttributesImpl();
146        attrs.addCDATAAttribute("id", _id);
147        attrs.addCDATAAttribute("colspan", Integer.toString(_colspan));
148        XMLUtils.createElement(handler, "control", attrs);
149    }
150
151    public boolean isSame(Element element)
152    {
153        if (!(element instanceof ControlRef))
154        {
155            return false;
156        }
157        
158        ControlRef controlRef = (ControlRef) element;
159        return controlRef._id == _id && controlRef._colspan == _colspan;
160    }
161    
162    @Override
163    public String toString()
164    {
165        return super.toString() + "[" + _id + "]";
166    }
167}