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, 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.ade.galleries.client.ui;
029
030import org.opencms.ade.galleries.client.preview.CmsCroppingParamBean;
031import org.opencms.ade.galleries.shared.I_CmsGalleryConfiguration;
032import org.opencms.file.CmsResource;
033import org.opencms.gwt.client.CmsCoreProvider;
034import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
035import org.opencms.gwt.client.ui.input.CmsSelectBox;
036import org.opencms.gwt.client.ui.input.CmsTextArea;
037import org.opencms.gwt.client.util.CmsDomUtil;
038import org.opencms.util.CmsStringUtil;
039import org.opencms.util.CmsUUID;
040
041import java.util.LinkedHashMap;
042import java.util.Map;
043
044import com.google.gwt.event.dom.client.FocusEvent;
045import com.google.gwt.event.logical.shared.ResizeEvent;
046import com.google.gwt.event.logical.shared.ValueChangeEvent;
047import com.google.gwt.event.logical.shared.ValueChangeHandler;
048import com.google.gwt.http.client.URL;
049import com.google.gwt.uibinder.client.UiHandler;
050
051/**
052 * A widget for selecting a resource from an ADE gallery dialog.<p>
053 *
054 * @since 8.0.0
055 */
056public class CmsImageGalleryField extends CmsGalleryField {
057
058    /** Parameter to split or generate the value string. */
059    private static final String PARAMETER_DESC = "description=";
060
061    /** Parameter to split or generate the value string. */
062    private static final String PARAMETER_FORMAT = "format=";
063
064    /** Parameter to split or generate the value string. */
065    private static final String PARAMETER_SCALE = "scale=";
066
067    /** The text area. */
068    protected CmsTextArea m_descriptionArea;
069
070    /** The select box. */
071    protected CmsSelectBox m_formatSelection;
072
073    /** Map of values for the Formats selection box. */
074    Map<String, String> m_formats = new LinkedHashMap<String, String>();
075
076    /** The description value. */
077    private String m_description;
078
079    /** The scale values. */
080    private String m_scaleValue;
081
082    /** The selected format. */
083    private String m_selectedFormat;
084
085    /**
086     * Constructs a new gallery widget.<p>
087     *
088     * @param configuration the gallery configuration
089     * @param allowUploads states if the upload button should be enabled for this widget
090     */
091    public CmsImageGalleryField(I_CmsGalleryConfiguration configuration, boolean allowUploads) {
092
093        super(configuration, allowUploads);
094        setHasImage(true);
095        m_descriptionArea = new CmsTextArea();
096
097        m_descriptionArea.addStyleName(
098            org.opencms.ade.galleries.client.ui.css.I_CmsLayoutBundle.INSTANCE.galleryFieldCss().descriptionField());
099        m_descriptionArea.getTextArea().setStyleName(I_CmsLayoutBundle.INSTANCE.globalWidgetCss().textAreaBox());
100        m_descriptionArea.getTextAreaContainer().addStyleName(
101            I_CmsLayoutBundle.INSTANCE.globalWidgetCss().textAreaBoxPanel());
102        m_descriptionArea.setRows(3);
103        m_descriptionArea.getTextAreaContainer().onResizeDescendant();
104        m_formatSelection = new CmsSelectBox();
105        m_formatSelection.addStyleName(
106            org.opencms.ade.galleries.client.ui.css.I_CmsLayoutBundle.INSTANCE.galleryFieldCss().formats());
107        m_formatSelection.getOpener().addStyleName(I_CmsLayoutBundle.INSTANCE.globalWidgetCss().selectBoxSelected());
108        m_formatSelection.getSelectorPopup().addStyleName(
109            I_CmsLayoutBundle.INSTANCE.globalWidgetCss().selectBoxPopup());
110        ValueChangeHandler<String> changeHandler = new ValueChangeHandler<String>() {
111
112            public void onValueChange(ValueChangeEvent<String> event) {
113
114                fireChange(false);
115            }
116        };
117        addToMain(m_formatSelection);
118        addToMain(m_descriptionArea);
119        m_descriptionArea.addValueChangeHandler(changeHandler);
120        m_formatSelection.addValueChangeHandler(changeHandler);
121        generatesFormatSelection();
122        m_resourceInfoPanel.setVisible(false);
123        addStyleName(org.opencms.ade.galleries.client.ui.css.I_CmsLayoutBundle.INSTANCE.galleryFieldCss().hasImage());
124    }
125
126    /**
127     * Initializes this class.<p>
128     */
129    public static void initClass() {
130
131        // do nothing
132    }
133
134    /**
135     * @see org.opencms.gwt.client.ui.input.I_CmsFormWidget#getFormValueAsString()
136     */
137    @Override
138    public String getFormValueAsString() {
139
140        String result = m_textbox.getValue().trim();
141        // only append the other field values if a link is set
142        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(result)) {
143            result += "?__" + PARAMETER_SCALE + m_scaleValue;
144            if (m_configuration.isUseFormats()) {
145                result += "&" + PARAMETER_FORMAT + m_formatSelection.getFormValueAsString();
146                m_selectedFormat = m_formatSelection.getFormValueAsString();
147            }
148            result += "&" + PARAMETER_DESC + URL.encode(m_descriptionArea.getFormValueAsString());
149            m_description = m_descriptionArea.getFormValueAsString();
150        }
151        return result;
152    }
153
154    /**
155     * On select box value change.<p>
156     *
157     * @param event the event
158     */
159    @UiHandler("m_formatSelection")
160    public void onSelectBoxChange(ValueChangeEvent<String> event) {
161
162        fireChange(false);
163    }
164
165    /**
166     * On textarea box value change.<p>
167     *
168     * @param event the event
169     */
170    @UiHandler("m_descriptionArea")
171    public void onTextAreaBoxChange(ValueChangeEvent<String> event) {
172
173        fireChange(false);
174    }
175
176    /**
177     * On textarea box resize.<p>
178     *
179     * @param event the event
180     */
181    @UiHandler("m_descriptionArea")
182    public void onTextAreaBoxResize(ResizeEvent event) {
183
184        ResizeEvent.fire(this, event.getWidth(), event.getHeight());
185    }
186
187    /**
188     * Sets the name of the input field.<p>
189     *
190     * @param name of the input field
191     * */
192    @Override
193    public void setName(String name) {
194
195        m_textbox.setName(name);
196        m_descriptionArea.setName(name + "_TextArea");
197
198    }
199
200    /**
201     * Sets the widget value.<p>
202     *
203     * @param value the value to set
204     * @param fireEvent if the change event should be fired
205     */
206    @Override
207    public void setValue(String value, boolean fireEvent) {
208
209        value = parseValue(value);
210        m_textbox.setValue(value);
211        if (fireEvent) {
212            fireChange(true);
213        }
214    }
215
216    /**
217     * Handles the focus event on the opener.<p>
218     *
219     * @param event the focus event
220     */
221    @UiHandler("m_descriptionArea")
222    protected void onFocusDescription(FocusEvent event) {
223
224        CmsDomUtil.fireFocusEvent(this);
225    }
226
227    /**
228     * Handles the focus event on the opener.<p>
229     *
230     * @param event the focus event
231     */
232    @UiHandler("m_formatSelection")
233    protected void onFocusSelect(FocusEvent event) {
234
235        CmsDomUtil.fireFocusEvent(this);
236    }
237
238    /**
239     * @see org.opencms.ade.galleries.client.ui.CmsGalleryField#setValueFromGallery(java.lang.String, org.opencms.util.CmsUUID, org.opencms.ade.galleries.client.preview.CmsCroppingParamBean)
240     */
241    @Override
242    protected void setValueFromGallery(
243        String resourcePath,
244        CmsUUID structureId,
245        CmsCroppingParamBean croppingParameter) {
246
247        m_croppingParam = new CmsCroppingParamBean(croppingParameter);
248        String path = resourcePath + "?";
249        path += croppingParameter.toString();
250        path += "&" + PARAMETER_FORMAT + croppingParameter.getFormatName();
251        setValue(path, true);
252        m_popup.hide();
253    }
254
255    /**
256     * Generates the format select box.<p>
257     **/
258    private void generatesFormatSelection() {
259
260        if (m_configuration.isUseFormats()) {
261            String[] formats = m_configuration.getImageFormatNames().split(",");
262            for (int i = 0; i < formats.length; i++) {
263                m_formats.put(formats[i].split(":")[0], formats[i].split(":")[1]);
264            }
265            m_formatSelection.setItems(m_formats);
266        } else {
267            m_formatSelection.removeFromParent();
268            m_descriptionArea.setRowsGallery(4);
269        }
270    }
271
272    /**
273     * Parses the value and all its informations.<p>
274     * First part is the URL of the image. The second one describes the scale of this image.<p>
275     * The last one sets the selected format.<p>
276     *
277     * @param value that should be parsed
278     *
279     * @return the URL of the image without any parameters
280     */
281
282    private String parseValue(String value) {
283
284        m_croppingParam = CmsCroppingParamBean.parseImagePath(value);
285        String path = "";
286        String params = "";
287        if (value.indexOf("?") > -1) {
288            path = value.substring(0, value.indexOf("?"));
289            params = value.substring(value.indexOf("?"));
290        } else {
291            path = value;
292        }
293        int indexofscale = params.indexOf(PARAMETER_SCALE);
294        if (indexofscale > -1) {
295            String scal = "";
296            int hasmoreValues = params.lastIndexOf("&");
297            if (hasmoreValues > indexofscale) {
298                scal = params.substring(indexofscale, params.indexOf("&")).replace(PARAMETER_SCALE, "");
299            } else {
300                scal = params.substring(indexofscale).replace(PARAMETER_SCALE, "");
301            }
302            if (!scal.equals(m_scaleValue)) {
303                m_scaleValue = scal;
304            }
305            params = params.replace(PARAMETER_SCALE + m_scaleValue, "");
306
307        }
308        int indexofformat = params.indexOf(PARAMETER_FORMAT);
309        if (indexofformat > -1) {
310            int hasmoreValues = params.lastIndexOf("&");
311            if (hasmoreValues > indexofformat) {
312                m_selectedFormat = params.substring(indexofformat, hasmoreValues).replace(PARAMETER_FORMAT, "");
313            } else {
314                m_selectedFormat = params.substring(indexofformat).replace(PARAMETER_FORMAT, "");
315            }
316            params = params.replace(PARAMETER_FORMAT + m_selectedFormat, "");
317            m_formatSelection.selectValue(m_selectedFormat);
318        }
319        int indexofdescritption = params.indexOf(PARAMETER_DESC);
320        if (indexofdescritption > -1) {
321            int hasmoreValues = params.lastIndexOf("&");
322            if (hasmoreValues > indexofdescritption) {
323                m_description = params.substring(indexofdescritption, hasmoreValues).replace(PARAMETER_DESC, "");
324            } else {
325                m_description = params.substring(indexofdescritption).replace(PARAMETER_DESC, "");
326            }
327            params = params.replace(PARAMETER_DESC + m_description, "");
328            m_description = URL.decode(m_description);
329            m_descriptionArea.setFormValueAsString(m_description);
330        }
331        updateUploadTarget(CmsResource.getFolderPath(path));
332        if (!path.isEmpty()) {
333            String imageLink = CmsCoreProvider.get().link(path);
334            setImagePreview(path, imageLink);
335
336        } else {
337            m_imagePreview.setInnerHTML("");
338        }
339        return path;
340
341    }
342}