001/*
002 *  Copyright 2010 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.cms.clientsideelement.styles;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.cocoon.xml.XMLUtils;
022import org.xml.sax.ContentHandler;
023import org.xml.sax.SAXException;
024
025import org.ametys.runtime.i18n.I18nizableText;
026
027/**
028 * This class handle style for paragraphs 
029 */
030public class Style
031{
032    /** See constructor */
033    protected String _buttonSmallIcon;
034    /** See constructor */
035    protected String _buttonMediumIcon;
036    /** See constructor */
037    protected String _buttonLargeIcon;
038    /** See constructor */
039    protected I18nizableText _buttonLabel;
040    /** See constructor */
041    protected I18nizableText _buttonDescription;
042    /** See constructor */
043    protected String _buttonCSSClass;
044    /** See constructor */
045    protected String _inlineEditorRender;
046    
047    /**
048     * Create a style for paragraphs
049     * @param buttonLabel Label of the backoffice button to apply this style
050     * @param buttonDescription Description of the backoffice button to apply this style
051     * @param buttonSmallIcon Image in 16x16 pixels (can be null) of the backoffice button to apply this style
052     * @param buttonMediumIcon Image in 32x32 pixels (can be null) of the backoffice button to apply this style
053     * @param buttonLargeIcon Image in 48x48 pixels (can be null) of the backoffice button to apply this style
054     * @param buttonCSSClass CSS class of the backoffice button to apply this style (used to give a preview appearance to the button) The CSS class has to be defined in the imported files (See HTMLEditorStyle)
055     * @param inlineEditorRender Information necessary in the inline editor to "apply" the style. Can be a "tagname.classname" or just a "classname" depending on the element to style
056     */
057    public Style (I18nizableText buttonLabel, I18nizableText buttonDescription, String buttonSmallIcon, String buttonMediumIcon, String buttonLargeIcon, String buttonCSSClass, String inlineEditorRender)
058    {
059        _buttonLabel = buttonLabel;
060        _buttonDescription = buttonDescription;
061        _buttonCSSClass = buttonCSSClass;
062        _buttonSmallIcon = buttonSmallIcon;
063        _buttonMediumIcon = buttonMediumIcon;
064        _buttonLargeIcon = buttonLargeIcon;
065        _inlineEditorRender = inlineEditorRender;
066    }
067    
068    /**
069     * Transform the current para style as a map of parameters. Parameters are :
070     * <ul>
071     * <li>icon-small (optional)</li>
072     * <li>icon-medium (optional)</li>
073     * <li>icon-large (optional)</li>
074     * <li>label</li>
075     * <li>description</li>
076     * <li>cssclass (optional)</li>
077     * <li>inline-editor-render</li>
078     * </ul>
079     * @return The non null map with parameters.
080     */
081    public Map<String, Object> getAsParameters()
082    {
083        Map<String, Object> params = new HashMap<>();
084        
085        params.put("label", _buttonLabel);
086        params.put("description", _buttonDescription);
087        if (_buttonCSSClass != null)
088        {
089            params.put("cls", _buttonCSSClass);
090        }
091        
092        if (_buttonSmallIcon != null)
093        {
094            params.put("icon-small", _buttonSmallIcon);
095        }
096        if (_buttonMediumIcon != null)
097        {
098            params.put("icon-medium", _buttonMediumIcon);
099        }
100        if (_buttonLargeIcon != null)
101        {
102            params.put("icon-large", _buttonLargeIcon);
103        }
104        
105        String inlineEditorTagName = null;
106        String inlineEditorCSSClass = null;
107        
108        int index = _inlineEditorRender.indexOf(".");
109        if (index == -1)
110        {
111            inlineEditorTagName = _inlineEditorRender;
112        }
113        else
114        {
115            inlineEditorTagName = _inlineEditorRender.substring(0, index);
116            inlineEditorCSSClass = _inlineEditorRender.substring(index + 1);
117        }
118        
119        params.put("inlineEditorRender", _inlineEditorRender);
120        params.put("tagName", inlineEditorTagName);
121        if (inlineEditorCSSClass != null)
122        {
123            params.put("style", inlineEditorCSSClass);
124        }
125        
126        return params;
127    }
128    
129    /**
130     * SAX the current para style
131     * @param contentHandler Where to SAX
132     * @throws SAXException If an error occured
133     */
134    public void toSAX(ContentHandler contentHandler) throws SAXException
135    {
136        XMLUtils.startElement(contentHandler, "style");
137        
138        XMLUtils.startElement(contentHandler, "button");
139        
140        _buttonLabel.toSAX(contentHandler, "label");
141        _buttonDescription.toSAX(contentHandler, "description");
142        
143        if (_buttonSmallIcon != null)
144        {
145            XMLUtils.createElement(contentHandler, "icon-small", _buttonSmallIcon);
146        }
147        if (_buttonMediumIcon != null)
148        {
149            XMLUtils.createElement(contentHandler, "icon-medium", _buttonMediumIcon);
150        }
151        if (_buttonLargeIcon != null)
152        {
153            XMLUtils.createElement(contentHandler, "icon-large", _buttonLargeIcon);
154        }
155       
156        if (_buttonCSSClass != null)
157        {
158            XMLUtils.createElement(contentHandler, "cssclass", _buttonCSSClass);
159        }
160        
161        XMLUtils.endElement(contentHandler, "button");
162        
163        XMLUtils.createElement(contentHandler, "inline-editor", _inlineEditorRender);
164        
165        XMLUtils.endElement(contentHandler, "style");
166    }
167    
168    /**
169     * See constructor
170     * @return the buttonCssClass.
171     */
172    public String getButtonCSSClass()
173    {
174        return _buttonCSSClass;
175    }
176    
177    /**
178     * See constructor
179     * @return the buttonDescription
180     */
181    public I18nizableText getButtonDescription()
182    {
183        return _buttonDescription;
184    }
185    
186    /**
187     * See constructor
188     * @return the buttonSmallIcon
189     */
190    public String getButtonSmallIcon()
191    {
192        return _buttonSmallIcon;
193    }
194    
195    /**
196     * See constructor
197     * @return the buttonSmallIcon
198     */
199    public String getButtonMediumIcon()
200    {
201        return _buttonMediumIcon;
202    }
203    
204    /**
205     * See constructor
206     * @return the buttonSmallIcon
207     */
208    public String getButtonLargeIcon()
209    {
210        return _buttonLargeIcon;
211    }
212    
213    /**
214     * See constructor
215     * @return the buttonLabel
216     */
217    public I18nizableText getButtonLabel()
218    {
219        return _buttonLabel;
220    }
221    
222    /**
223     * See constructor
224     * @return the inlineEditorRender
225     */
226    public String getInlineEditorRender()
227    {
228        return _inlineEditorRender;
229    }
230}