001/*
002 *  Copyright 2011 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.skinfactory.parameters;
017
018import java.io.File;
019import java.io.IOException;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.cocoon.xml.AttributesImpl;
025import org.apache.cocoon.xml.XMLUtils;
026import org.apache.commons.io.FileUtils;
027import org.xml.sax.ContentHandler;
028import org.xml.sax.SAXException;
029
030import org.ametys.runtime.i18n.I18nizableText;
031import org.ametys.skinfactory.filefilter.DirectoryFilter;
032import org.ametys.skinfactory.filefilter.VariantFileFilter;
033import org.ametys.web.skin.SkinModel;
034
035/**
036 * Implementation of {@link AbstractSkinParameter} for a variant
037 */
038public class VariantParameter extends AbstractSkinParameter
039{
040    private String _iconGlyph;
041    private String _iconSmall;
042    private String _iconLarge;
043    private List<Variant> _variants;
044    
045    
046    /**
047     * Constructor
048     * @param id the unique id
049     * @param label the label
050     * @param description the description
051     * @param variants the list of values for this variant
052     */
053    public VariantParameter(String id, I18nizableText label, I18nizableText description, List<Variant> variants)
054    {
055        super(id, label, description);
056        _variants = variants;
057    }
058    
059    /**
060     * Constructor
061     * @param id the unique id
062     * @param label the label
063     * @param description the description
064     * @param iconGlyph The CSS classe for icon, to use instead of small and large icon
065     * @param iconSmall The small icon
066     * @param iconLarge The large icon
067     * @param variants the list of values for this variant
068     */
069    public VariantParameter(String id, I18nizableText label, I18nizableText description, String iconGlyph, String iconSmall, String iconLarge, List<Variant> variants)
070    {
071        super(id, label, description);
072        _iconGlyph = iconGlyph;
073        _iconSmall = iconSmall;
074        _iconLarge = iconLarge;
075        _variants = variants;
076    }
077    
078    @Override
079    public SkinParameterType getType()
080    {
081        return SkinParameterType.VARIANT;
082    }
083    /**
084     * Get the variants
085     * @return The variants
086     */
087    public List<Variant> getVariants()
088    {
089        return _variants;
090    }
091    
092    /**
093     * Set the CSS icon
094     * @param iconGlyph the CSS icon
095     */
096    public void setIconGlyph(String iconGlyph)
097    {
098        _iconGlyph = iconGlyph;
099    }
100    
101    /**
102     * Get the CSS icon
103     * @return The CSS icon
104     */
105    public String getIconGlyph ()
106    {
107        return _iconGlyph;
108    }
109    
110    /**
111     * Set the small icon relative path
112     * @param iconSmall the small icon
113     */
114    public void setIconSmall(String iconSmall)
115    {
116        _iconSmall = iconSmall;
117    }
118    
119    /**
120     * Get the small icon
121     * @return The small icon
122     */
123    public String getIconSmall ()
124    {
125        return _iconSmall;
126    }
127    
128    /**
129     * Set the large icon relative path
130     * @param iconLarge the large icon
131     */
132    public void setIconLarge(String iconLarge)
133    {
134        _iconLarge = iconLarge;
135    }
136    
137    /**
138     * Get the large icon
139     * @return The large icon
140     */
141    public String getIconLarge ()
142    {
143        return _iconLarge;
144    }
145    
146    @Override
147    public void apply(File tempDir, File modelDir, Object value, String lang) throws SkinParameterException
148    {
149        File variantFile = new File(modelDir, "model/variants/" + this._id);
150        File srcFile = new File (variantFile, (String) value);
151        
152        try
153        {
154            // Copy variant files recursively excluding CVS and .svn directories
155            FileUtils.copyDirectory(srcFile, tempDir, new VariantFileFilter((String) value, srcFile), false);
156        }
157        catch (IOException e)
158        {
159            throw new SkinParameterException ("Unable to apply variant parameter '" + getId() + "'", e);
160        }
161    }
162
163    @Override
164    public void toSAX(ContentHandler contentHandler, String modelName) throws SAXException
165    {
166        AttributesImpl attrs = new AttributesImpl();
167        attrs.addCDATAAttribute("id", getId());
168        attrs.addCDATAAttribute("type", SkinParameterType.VARIANT.name().toLowerCase());
169        
170        XMLUtils.startElement(contentHandler, "parameter", attrs);
171        
172        getLabel().toSAX(contentHandler, "label");
173        getDescription().toSAX(contentHandler, "description");
174        
175        if (getIconGlyph() != null)
176        {
177            XMLUtils.createElement(contentHandler, "iconGlyph", getIconGlyph());
178        }
179        
180        if (getIconSmall() != null)
181        {
182            XMLUtils.createElement(contentHandler, "iconSmall", "/plugins/skinfactory/" + modelName + "/_thumbnail/16/16/model/variants/" + this._id + "/" + getIconSmall());
183        }
184        else
185        {
186            XMLUtils.createElement(contentHandler, "iconSmall", "/plugins/skinfactory/resources/img/variant_default_16.png");
187        }
188        
189        if (getIconLarge() != null)
190        {
191            XMLUtils.createElement(contentHandler, "iconLarge", "/plugins/skinfactory/" + modelName + "/_thumbnail/32/32/model/variants/" + this._id + "/" + getIconLarge());
192        }
193        else
194        {
195            XMLUtils.createElement(contentHandler, "iconLarge", "/plugins/skinfactory/resources/img/variant_default_32.png");
196        }
197        
198        XMLUtils.endElement(contentHandler, "parameter");
199        
200    }
201
202    @Override
203    public Map<String, Object> toJson(String modelName)
204    {
205        Map<String, Object> jsonObject = new HashMap<>();
206        
207        jsonObject.put("id", getId());
208        jsonObject.put("type", SkinParameterType.VARIANT.name().toLowerCase());
209        
210        jsonObject.put("label", getLabel());
211        jsonObject.put("description", getDescription());
212        
213        if (getIconGlyph() != null)
214        {
215            jsonObject.put("iconGlyph", getIconGlyph());
216        }
217        
218        if (getIconSmall() != null)
219        {
220            jsonObject.put("iconSmall", "/plugins/skinfactory/" + modelName + "/_thumbnail/16/16/model/variants/" + this._id + "/" + getIconSmall());
221        }
222        else
223        {
224            jsonObject.put("iconSmall", "/plugins/skinfactory/resources/img/variant_default_16.png");
225        }
226        
227        if (getIconLarge() != null)
228        {
229            jsonObject.put("iconLarge", "/plugins/skinfactory/" + modelName + "/_thumbnail/32/32/model/variants/" + this._id + "/" + getIconLarge());
230        }
231        else
232        {
233            jsonObject.put("iconLarge", "/plugins/skinfactory/resources/img/variant_default_32.png");
234        }
235        
236        return jsonObject;
237    }
238    
239    @Override
240    public String getDefaultValue(SkinModel model)
241    {
242        File variantFile = new File(model.getFile(), "model/variants/" + this._id);
243        File[] listFiles = variantFile.listFiles(new DirectoryFilter());
244        for (File child : listFiles)
245        {
246            return child.getName();
247        }
248        return null;
249    }
250    
251    @Override
252    public String getDefaultValue(SkinModel model, String lang)
253    {
254        return getDefaultValue(model);
255    }
256}