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.preview.ui;
029
030import org.opencms.ade.galleries.client.Messages;
031import org.opencms.ade.galleries.client.preview.CmsImagePreviewHandler.Attribute;
032import org.opencms.ade.galleries.client.ui.css.I_CmsLayoutBundle;
033import org.opencms.ade.galleries.shared.CmsImageInfoBean;
034import org.opencms.gwt.client.ui.CmsPushButton;
035import org.opencms.gwt.client.ui.I_CmsButton.Size;
036import org.opencms.gwt.client.ui.input.CmsCheckBox;
037import org.opencms.gwt.client.ui.input.CmsSelectBox;
038import org.opencms.gwt.client.ui.input.CmsTextBox;
039import org.opencms.gwt.client.ui.input.I_CmsFormWidget;
040import org.opencms.gwt.client.util.CmsJSONMap;
041import org.opencms.gwt.shared.property.CmsClientProperty;
042import org.opencms.util.CmsStringUtil;
043
044import java.util.Arrays;
045import java.util.HashMap;
046import java.util.Map;
047import java.util.Map.Entry;
048
049import com.google.gwt.core.client.GWT;
050import com.google.gwt.dom.client.Style.VerticalAlign;
051import com.google.gwt.event.dom.client.ClickEvent;
052import com.google.gwt.event.logical.shared.ValueChangeEvent;
053import com.google.gwt.event.logical.shared.ValueChangeHandler;
054import com.google.gwt.uibinder.client.UiBinder;
055import com.google.gwt.uibinder.client.UiField;
056import com.google.gwt.uibinder.client.UiHandler;
057import com.google.gwt.user.client.ui.Composite;
058import com.google.gwt.user.client.ui.Label;
059import com.google.gwt.user.client.ui.Widget;
060
061/**
062 * Simple image tag properties form, use in editor mode only.<p>
063 *
064 * @since 8.0.
065 */
066public class CmsImageEditorForm extends Composite {
067
068    /** Ui binder interface. */
069    protected interface I_CmsImageEditorFormatsTabUiBinder extends UiBinder<Widget, CmsImageEditorForm> {
070        // GWT interface, nothing to do
071    }
072
073    /** Ui binder instance. */
074    private static I_CmsImageEditorFormatsTabUiBinder m_uiBinder = GWT.create(I_CmsImageEditorFormatsTabUiBinder.class);
075
076    /** The reset copyright button. */
077    @UiField
078    protected CmsPushButton m_buttonResetCopyright;
079
080    /** The reset alt/title button. */
081    @UiField
082    protected CmsPushButton m_buttonResetTitle;
083
084    /** The insert copyright check-box. */
085    @UiField
086    protected CmsCheckBox m_checkboxInsertCopyright;
087
088    /** The insert link to original check-box. */
089    @UiField
090    protected CmsCheckBox m_checkboxInsertLinkOrig;
091
092    /** The insert subtitle check-box. */
093    @UiField
094    protected CmsCheckBox m_checkboxInsertSubtitle;
095
096    /** The insert spacing check-box. */
097    @UiField
098    protected CmsCheckBox m_checkboxSpacing;
099
100    /** The alt/title input field. */
101    @UiField
102    protected CmsTextBox m_inputAltTitle;
103
104    /** The copyright input field. */
105    @UiField
106    protected CmsTextBox m_inputCopyright;
107
108    /** The hSpace input field. */
109    @UiField
110    protected CmsTextBox m_inputHSpace;
111
112    /** The vSpace input field. */
113    @UiField
114    protected CmsTextBox m_inputVSpace;
115
116    /** The alignment select-box label. */
117    @UiField
118    protected Label m_labelAlign;
119
120    /** The alt/title field label. */
121    @UiField
122    protected Label m_labelAltTitle;
123
124    /** The copyright field label. */
125    @UiField
126    protected Label m_labelCopyright;
127
128    /** The hSpace field label. */
129    @UiField
130    protected Label m_labelHSpace;
131
132    /** The image spacing check-box label. */
133    @UiField
134    protected Label m_labelImageSpacing;
135
136    /** The insert copyright check-box label. */
137    @UiField
138    protected Label m_labelInsertCopyright;
139
140    /** The insert link to original check-box label. */
141    @UiField
142    protected Label m_labelInsertLinkOrig;
143
144    /** The insert subtitles check-box label. */
145    @UiField
146    protected Label m_labelInsertSubtitle;
147
148    /** The vSpace field label. */
149    @UiField
150    protected Label m_labelVSpace;
151
152    /** The alignment select-box. */
153    @UiField
154    CmsSelectBox m_selectAlign;
155
156    /** The form fields. */
157    private Map<Attribute, I_CmsFormWidget> m_fields;
158
159    /** The initial image attribute values. */
160    private CmsJSONMap m_initialImageAttributes;
161
162    /**
163     * Constructor.<p>
164     */
165    public CmsImageEditorForm() {
166
167        initWidget(m_uiBinder.createAndBindUi(this));
168        m_labelAltTitle.setText(Messages.get().key(Messages.GUI_IMAGE_TITLE_ALT_0));
169        m_labelCopyright.setText(Messages.get().key(Messages.GUI_IMAGE_COPYRIGHT_0));
170        m_labelImageSpacing.setText(Messages.get().key(Messages.GUI_IMAGE_SPACING_0));
171        m_labelInsertSubtitle.setText(Messages.get().key(Messages.GUI_IMAGE_INSERT_SUBTITLE_0));
172        m_labelInsertCopyright.setText(Messages.get().key(Messages.GUI_IMAGE_INSERT_COPYRIGHT_0));
173        m_labelHSpace.setText(Messages.get().key(Messages.GUI_IMAGE_HSPACE_0));
174        m_labelVSpace.setText(Messages.get().key(Messages.GUI_IMAGE_VSPACE_0));
175        m_labelInsertLinkOrig.setText(Messages.get().key(Messages.GUI_IMAGE_INSERT_LINK_TO_ORG_0));
176        m_buttonResetTitle.setSize(Size.small);
177        m_buttonResetTitle.setText(Messages.get().key(Messages.GUI_IMAGE_RESET_TITLE_0));
178        m_buttonResetCopyright.setSize(Size.small);
179        m_buttonResetCopyright.setText(Messages.get().key(Messages.GUI_IMAGE_RESET_COPYRIGHT_0));
180        for (CmsCheckBox checkBox : Arrays.asList(
181            m_checkboxInsertCopyright,
182            m_checkboxInsertLinkOrig,
183            m_checkboxInsertSubtitle)) {
184            checkBox.getElement().getStyle().setVerticalAlign(VerticalAlign.MIDDLE);
185        }
186
187        m_labelAlign.setText(Messages.get().key(Messages.GUI_IMAGE_ALIGN_0));
188        m_selectAlign.addOption("", Messages.get().key(Messages.GUI_IMAGE_ALIGN_NOT_SET_0));
189        m_selectAlign.addOption("left", Messages.get().key(Messages.GUI_IMAGE_ALIGN_LEFT_0));
190        m_selectAlign.addOption("right", Messages.get().key(Messages.GUI_IMAGE_ALIGN_RIGHT_0));
191
192        m_checkboxSpacing.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
193
194            public void onValueChange(ValueChangeEvent<Boolean> event) {
195
196                // if spacing is activated and no previous values present, set '5' as default
197                if (event.getValue().booleanValue()
198                    && CmsStringUtil.isEmptyOrWhitespaceOnly(m_inputHSpace.getFormValueAsString())
199                    && CmsStringUtil.isEmptyOrWhitespaceOnly(m_inputVSpace.getFormValueAsString())) {
200                    m_inputHSpace.setFormValueAsString("5");
201                    m_inputVSpace.setFormValueAsString("5");
202                }
203            }
204        });
205
206        m_fields = new HashMap<Attribute, I_CmsFormWidget>();
207        m_fields.put(Attribute.alt, m_inputAltTitle);
208        m_fields.put(Attribute.hspace, m_inputHSpace);
209        m_fields.put(Attribute.vspace, m_inputVSpace);
210        m_fields.put(Attribute.align, m_selectAlign);
211        m_fields.put(Attribute.copyright, m_inputCopyright);
212        m_fields.put(Attribute.insertCopyright, m_checkboxInsertCopyright);
213        m_fields.put(Attribute.insertSubtitle, m_checkboxInsertSubtitle);
214        m_fields.put(Attribute.insertSpacing, m_checkboxSpacing);
215        m_fields.put(Attribute.insertLinkOrig, m_checkboxInsertLinkOrig);
216    }
217
218    /**
219     * Displays the provided image information.<p>
220     *
221     * @param imageInfo the image information
222     * @param imageAttributes the image attributes
223     * @param initialFill flag to indicate that a new image has been selected
224     */
225    public void fillContent(CmsImageInfoBean imageInfo, CmsJSONMap imageAttributes, boolean initialFill) {
226
227        m_initialImageAttributes = imageAttributes;
228        for (Entry<Attribute, I_CmsFormWidget> entry : m_fields.entrySet()) {
229            String val = imageAttributes.getString(entry.getKey().name());
230            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(val)) {
231                entry.getValue().setFormValueAsString(val);
232            } else {
233                if (entry.getKey() == Attribute.alt) {
234                    entry.getValue().setFormValueAsString(
235                        imageInfo.getProperties().get(CmsClientProperty.PROPERTY_TITLE));
236                }
237                if (entry.getKey() == Attribute.copyright) {
238                    entry.getValue().setFormValueAsString(imageInfo.getCopyright());
239                }
240                if (initialFill && (entry.getKey() == Attribute.align)) {
241                    entry.getValue().setFormValueAsString("left");
242                }
243            }
244        }
245    }
246
247    /**
248     * Adds necessary attributes to the map.<p>
249     *
250     * @param attributes the attribute map
251     * @return the attribute map
252     */
253    public Map<String, String> getImageAttributes(Map<String, String> attributes) {
254
255        for (Entry<Attribute, I_CmsFormWidget> entry : m_fields.entrySet()) {
256            String val = entry.getValue().getFormValueAsString();
257            if (CmsStringUtil.isEmptyOrWhitespaceOnly(val)) {
258                continue;
259            }
260            attributes.put(entry.getKey().name(), val);
261            // put the same value in 'alt' and 'title' attribute
262            if (entry.getKey() == Attribute.alt) {
263                attributes.put(Attribute.title.name(), val);
264            }
265        }
266        return attributes;
267    }
268
269    /**
270     * Hides the enhanced image options in this form.<p>
271     *
272     * @param hide if <code>true</code> the enhanced options will get hidden
273     */
274    public void hideEnhancedOptions(boolean hide) {
275
276        if (hide) {
277            addStyleName(I_CmsLayoutBundle.INSTANCE.previewDialogCss().hiding());
278        } else {
279            removeStyleName(I_CmsLayoutBundle.INSTANCE.previewDialogCss().hiding());
280        }
281    }
282
283    /**
284     * Handles the click on 'reset copyright' button.<p>
285     *
286     * @param event the click event
287     */
288    @UiHandler("m_buttonResetCopyright")
289    protected void onResetCopyrightClick(ClickEvent event) {
290
291        resetValue(Attribute.copyright);
292    }
293
294    /**
295     * Handles the click on 'reset title' button.<p>
296     *
297     * @param event the click event
298     */
299    @UiHandler("m_buttonResetTitle")
300    protected void onResetTitleClick(ClickEvent event) {
301
302        resetValue(Attribute.alt);
303    }
304
305    /**
306     * Resets the value for the given attribute to it's initial value.<p>
307     *
308     * @param attribute the attribute to reset
309     */
310    protected void resetValue(Attribute attribute) {
311
312        String initValue = "";
313        if (m_initialImageAttributes.containsKey(attribute.name())) {
314            initValue = m_initialImageAttributes.getString(attribute.name());
315        }
316        if (m_fields.containsKey(attribute)) {
317            m_fields.get(attribute).setFormValueAsString(initValue);
318        }
319    }
320
321}