001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.widgets;
029
030import org.opencms.file.CmsObject;
031import org.opencms.i18n.CmsMessages;
032import org.opencms.json.JSONArray;
033import org.opencms.json.JSONException;
034import org.opencms.json.JSONObject;
035import org.opencms.util.CmsMacroResolver;
036import org.opencms.util.CmsStringUtil;
037
038import java.util.ArrayList;
039import java.util.List;
040
041/**
042 * Configuration options for the VFS image widget.<p>
043 *
044 * The configuration options are read from the configuration String of the widget.<p>
045 *
046 * The configuration String has to be formatted as JSON object, with the following possible keys:<p>
047 * <ul>
048 * <li><code>class</code>: optional class implementing {@link I_CmsImageWidgetDynamicConfiguration} to dynamically
049 *            configure startup parameters and format values.</li>
050 * <li><code>formatnames</code>: list of format names to select, with pairs of selectable value and selectable text,
051 *            e.g. value1:optiontext1|value2:optiontext2</li>
052 * <li><code>formatvalues</code>: corresponding format values to the format names list,
053 *            can be dynamically generated by the dynamic configuration class.
054 *            The list of values should contain width and height information, with a '?' as sign for dynamic size
055 *            and with an 'x' as separator for width and height.
056 *            Example: ['200x?', '800x600']</li>
057 * <li><code>scaleparams</code>: default scale parameters (no width, height or crop information should be provided!)</li>
058 * <li><code>startup</code>: the startup folder, can be dynamically generated by the provided class,
059 *            in that case, use 'dynamic' as value.</li>
060 * <li><code>type</code>: the startup folder type, can be 'gallery' or 'category'. Can be dynamically generated
061 *            by the provided class, in that case, use 'dynamic' as value.</li>
062 * <li><code>usedescription</code>: indicates if the description input field for the image should be shown or not.</li>
063 * <li><code>useformat</code>: indicates if the format select box for the image should be shown or not.</li>
064 * </ul>
065 *
066 * An example configuration can look like this:<p>
067 * <code>{scaleparams: 'q:70,r:2,c:CCCC00', type: 'gallery', startup: '/demo_en/images/',
068 * usedescription: true, useformat: true, formatnames: 'imageleft:Image left|imageright:Image right|imagetop:Image top',
069 * formatvalues: ['150x?', '250x300', '?x250']}</code><p>
070 *
071 * @since 7.5.0
072 */
073public class CmsVfsImageWidgetConfiguration extends CmsGalleryWidgetConfiguration {
074
075    /** Configuration key name for the formatnames configuration. */
076    public static final String CONFIG_KEY_FORMATNAMES = "formatnames";
077
078    /** Configuration key name for the formatvalues configuration. */
079    public static final String CONFIG_KEY_FORMATVALUES = "formatvalues";
080
081    /** Configuration key name for the scaleparams configuration. */
082    public static final String CONFIG_KEY_SCALEPARAMS = "scaleparams";
083
084    /** Configuration key name for the usedescription configuration. */
085    public static final String CONFIG_KEY_USEDESCRIPTION = "usedescription";
086
087    /** Configuration key name for the useformat configuration. */
088    public static final String CONFIG_KEY_USEFORMAT = "useformat";
089
090    /** The type "category" for the initial image list to load. */
091    public static final String TYPE_CATEGORY = "category";
092
093    /** The type "gallery" for the initial image list to load. */
094    public static final String TYPE_GALLERY = "gallery";
095
096    /** The list of image format values matching the options for the format select box. */
097    private List<String> m_formatValues;
098
099    /** The scale parameters to apply to a scaled image (e.g. quality, type). */
100    private String m_scaleParams;
101
102    /** The list of select options for the format select box, must contain {@link CmsSelectWidgetOption} objects. */
103    private List<CmsSelectWidgetOption> m_selectFormat;
104
105    /** The select options for the format select box as String. */
106    private String m_selectFormatString;
107
108    /** The flag if the description field should be shown. */
109    private boolean m_showDescription;
110
111    /** The flag if the format select box should be shown. */
112    private boolean m_showFormat;
113
114    /**
115     * Generates an initialized configuration for the image widget using the given configuration string.<p>
116     *
117     * @param cms an initialized instance of a CmsObject
118     * @param widgetDialog the dialog where the widget is used on
119     * @param param the widget parameter to generate the widget for
120     * @param configuration the widget configuration string
121     */
122    public CmsVfsImageWidgetConfiguration(
123        CmsObject cms,
124        CmsMessages widgetDialog,
125        I_CmsWidgetParameter param,
126        String configuration) {
127
128        super();
129        init(cms, widgetDialog, param, configuration);
130    }
131
132    /**
133     * Returns the list of image format values matching the options for the format select box.<p>
134     *
135     * @return the list of image format values matching the options for the format select box
136     */
137    public List<String> getFormatValues() {
138
139        return m_formatValues;
140    }
141
142    /**
143     * Returns the scale parameters to apply to a scaled image (e.g. quality, type).<p>
144     *
145     * @return scale the parameters to apply to a scaled image
146     */
147    public String getScaleParams() {
148
149        return m_scaleParams;
150    }
151
152    /**
153     * Returns the list of select options for the format select box, must contain {@link CmsSelectWidgetOption} objects.<p>
154     *
155     * @return the list of select options for the format select box
156     */
157    public List<CmsSelectWidgetOption> getSelectFormat() {
158
159        return m_selectFormat;
160    }
161
162    /**
163     * Returns the select options for the format select box as String.<p>
164     *
165     * The String has the following structure <code>format name 1:localized name 1|format name 2:localized name 2|...</code>.<p>
166     *
167     * @return the select options for the format select box
168     */
169    public String getSelectFormatString() {
170
171        return m_selectFormatString;
172    }
173
174    /**
175     * Returns if the description field should be shown.<p>
176     *
177     * @return true if the description field should be shown, otherwise false
178     */
179    public boolean isShowDescription() {
180
181        return m_showDescription;
182    }
183
184    /**
185     * Returns if the format select box should be shown.<p>
186     *
187     * @return true if the format select box should be shown, otherwise false
188     */
189    public boolean isShowFormat() {
190
191        return m_showFormat;
192    }
193
194    /**
195     * Initializes the widget configuration using the given configuration string.<p>
196     *
197     * @param cms an initialized instance of a CmsObject
198     * @param widgetDialog the dialog where the widget is used on
199     * @param param the widget parameter to generate the widget for
200     * @param configuration the widget configuration string
201     */
202    @Override
203    protected void init(CmsObject cms, CmsMessages widgetDialog, I_CmsWidgetParameter param, String configuration) {
204
205        if (configuration == null) {
206            // no configuration String found, return
207            return;
208        }
209        configuration = CmsMacroResolver.resolveMacros(configuration, cms, widgetDialog);
210        JSONObject jsonObj = new JSONObject();
211        try {
212            jsonObj = new JSONObject(configuration);
213        } catch (JSONException e) {
214            // initialization failed
215            return;
216        }
217        // determine the class name that fills in values dynamically
218        setClassName(jsonObj.optString(CONFIG_KEY_CLASS, null));
219        I_CmsImageWidgetDynamicConfiguration dynConf = null;
220        if (getClassName() != null) {
221            try {
222                dynConf = (I_CmsImageWidgetDynamicConfiguration)Class.forName(getClassName()).newInstance();
223            } catch (Exception e) {
224                // class not found
225            }
226        }
227        // determine if the description field should be shown
228        setShowDescription(jsonObj.optBoolean(CONFIG_KEY_USEDESCRIPTION));
229        // determine if the format select box should be shown
230        setShowFormat(jsonObj.optBoolean(CONFIG_KEY_USEFORMAT));
231        if (isShowFormat()) {
232            // only parse options if the format select box should be shown
233            String optionsStr = (String)jsonObj.opt(CONFIG_KEY_FORMATNAMES);
234            setSelectFormatString(optionsStr);
235            setSelectFormat(CmsSelectWidgetOption.parseOptions(optionsStr));
236            // get the corresponding format values as well
237            JSONArray formatValues = jsonObj.optJSONArray(CONFIG_KEY_FORMATVALUES);
238            if (formatValues != null) {
239                List<String> formatValueList = new ArrayList<String>(formatValues.length());
240                for (int i = 0; i < formatValues.length(); i++) {
241                    formatValueList.add(formatValues.optString(i));
242                }
243                setFormatValues(formatValueList);
244            }
245            if (dynConf != null) {
246                setFormatValues(
247                    dynConf.getFormatValues(cms, widgetDialog, param, getSelectFormat(), getFormatValues()));
248            }
249        }
250        // determine the initial image list settings
251        setType(jsonObj.optString(CONFIG_KEY_TYPE));
252        if ((CONFIG_VALUE_DYNAMIC.equals(getType()) || CmsStringUtil.isEmpty(getType())) && (dynConf != null)) {
253            setType(dynConf.getType(cms, widgetDialog, param));
254        }
255        setStartup(jsonObj.optString(CONFIG_KEY_STARTUP));
256        if ((CONFIG_VALUE_DYNAMIC.equals(getStartup()) || CmsStringUtil.isEmpty(getStartup())) && (dynConf != null)) {
257            setStartup(dynConf.getStartup(cms, widgetDialog, param));
258        }
259        // determine the scale parameters
260        setScaleParams(jsonObj.optString(CONFIG_KEY_SCALEPARAMS));
261    }
262
263    /**
264     * Sets the list of image format values matching the options for the format select box.<p>
265     *
266     * @param formatValues the list of image format values matching the options for the format select box
267     */
268    private void setFormatValues(List<String> formatValues) {
269
270        m_formatValues = formatValues;
271    }
272
273    /**
274     * Sets the scale parameters to apply to a scaled image (e.g. quality, type).<p>
275     *
276     * @param scaleParams the scale parameters to apply to a scaled image
277     */
278    private void setScaleParams(String scaleParams) {
279
280        m_scaleParams = scaleParams;
281    }
282
283    /**
284     * Sets the list of select options for the format select box, must contain {@link CmsSelectWidgetOption} objects.<p>
285     *
286     * @param selectFormat the list of select options for the format select box
287     */
288    private void setSelectFormat(List<CmsSelectWidgetOption> selectFormat) {
289
290        m_selectFormat = selectFormat;
291    }
292
293    /**
294     * Sets the select options for the format select box as String.<p>
295     *
296     * @param formatString the select options for the format select box as String
297     */
298    private void setSelectFormatString(String formatString) {
299
300        m_selectFormatString = formatString;
301    }
302
303    /**
304     * Sets if the description field should be shown.<p>
305     *
306     * @param showDescription true if the description field should be shown, otherwise false
307     */
308    private void setShowDescription(boolean showDescription) {
309
310        m_showDescription = showDescription;
311    }
312
313    /**
314     * Sets if the format select box should be shown.<p>
315     *
316     * @param showFormat true if the format select box should be shown, otherwise false
317     */
318    private void setShowFormat(boolean showFormat) {
319
320        m_showFormat = showFormat;
321    }
322
323    /**
324     * Returns the values as a parameter string.<p>
325     *
326     * @return the values as a parameter string
327     * */
328    @Override
329    public String getConfigString() {
330
331        String result = "";
332        if (m_startup != null) {
333            result += "&startup=" + m_startup;
334        }
335        if (m_type != null) {
336            result += "&type=" + m_type;
337        }
338
339        return result;
340    }
341
342}