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.CmsCoreProvider;
035import org.opencms.gwt.client.ui.I_CmsButton.Size;
036import org.opencms.gwt.client.ui.input.CmsSelectBox;
037import org.opencms.gwt.client.ui.input.CmsTextBox;
038import org.opencms.gwt.client.ui.input.CmsVfsLinkWidget;
039import org.opencms.gwt.client.ui.input.I_CmsFormWidget;
040import org.opencms.gwt.client.util.CmsJSONMap;
041import org.opencms.gwt.client.util.I_CmsSimpleCallback;
042import org.opencms.util.CmsStringUtil;
043
044import java.util.HashMap;
045import java.util.Map;
046import java.util.Map.Entry;
047
048import com.google.gwt.core.client.GWT;
049import com.google.gwt.uibinder.client.UiBinder;
050import com.google.gwt.uibinder.client.UiField;
051import com.google.gwt.user.client.ui.Composite;
052import com.google.gwt.user.client.ui.Label;
053import com.google.gwt.user.client.ui.Widget;
054
055/**
056 * Advanced image tag properties form, use in editor mode only.<p>
057 *
058 * @since 8.0.
059 */
060public class CmsImageAdvancedForm extends Composite {
061
062    /** The ui binder interface. */
063    interface I_CmsImageAdvancedFormUiBinder extends UiBinder<Widget, CmsImageAdvancedForm> {
064        // GWT interface, nothing to do
065    }
066
067    /** Ui binder instance. */
068    private static I_CmsImageAdvancedFormUiBinder m_uiBinder = GWT.create(I_CmsImageAdvancedFormUiBinder.class);
069
070    /** Description input field. */
071    @UiField
072    protected CmsTextBox m_inputDescription;
073
074    /** Id input field. */
075    @UiField
076    protected CmsTextBox m_inputId;
077
078    /** Language code input field. */
079    @UiField
080    protected CmsTextBox m_inputLanguageCode;
081
082    /** Style input field. */
083    @UiField
084    protected CmsTextBox m_inputStyle;
085
086    /** Style classes input field. */
087    @UiField
088    protected CmsTextBox m_inputStyleClasses;
089
090    /** URL input field. */
091    @UiField
092    protected CmsVfsLinkWidget m_inputUrl;
093
094    /** Description field label. */
095    @UiField
096    protected Label m_labelDescription;
097
098    /** Id field label. */
099    @UiField
100    protected Label m_labelId;
101
102    /** Language code field label. */
103    @UiField
104    protected Label m_labelLanguageCode;
105
106    /** Set link checkbox label. */
107    @UiField
108    protected Label m_labelSetLink;
109
110    /** Style field label. */
111    @UiField
112    protected Label m_labelStyle;
113
114    /** Style classes field label. */
115    @UiField
116    protected Label m_labelStyleClasses;
117
118    /** Additional attributes label. */
119    @UiField
120    protected Label m_labelTagAttributes;
121
122    /** Target field label. */
123    @UiField
124    protected Label m_labelTarget;
125
126    /** Language direction field label. */
127    @UiField
128    protected Label m_labelTextDirection;
129
130    /** URL field label. */
131    @UiField
132    protected Label m_labelUrl;
133
134    /** Target select box. */
135    @UiField
136    protected CmsSelectBox m_selectTarget;
137
138    /** Language direction select box. */
139    @UiField
140    protected CmsSelectBox m_selectTextDirection;
141
142    /** The form fields. */
143    private Map<Attribute, I_CmsFormWidget> m_fields;
144
145    /**
146     * The constructor.<p>
147     */
148    public CmsImageAdvancedForm() {
149
150        initWidget(m_uiBinder.createAndBindUi(this));
151
152        m_labelUrl.setText(Messages.get().key(Messages.GUI_ADVANCED_TAB_LABEL_URL_0));
153        m_labelTarget.setText(Messages.get().key(Messages.GUI_ADVANCED_TAB_LABEL_TARGET_0));
154        m_labelId.setText(Messages.get().key(Messages.GUI_ADVANCED_TAB_LABEL_ID_0));
155        m_labelStyleClasses.setText(Messages.get().key(Messages.GUI_ADVANCED_TAB_LABEL_STYLE_CLASSES_0));
156        m_labelStyle.setText(Messages.get().key(Messages.GUI_ADVANCED_TAB_LABEL_STYLE_RULES_0));
157        m_labelSetLink.setText(Messages.get().key(Messages.GUI_ADVANCED_TAB_LABEL_SET_LINK_0));
158        m_labelTagAttributes.setText(Messages.get().key(Messages.GUI_ADVANCED_TAB_LABEL_ATTRIBUTES_0));
159        m_labelDescription.setText(Messages.get().key(Messages.GUI_ADVANCED_TAB_LABEL_DESCRIPTION_0));
160        m_labelLanguageCode.setText(Messages.get().key(Messages.GUI_ADVANCED_TAB_LABEL_LANGUAGE_CODE_0));
161        m_labelTextDirection.setText(Messages.get().key(Messages.GUI_ADVANCED_TAB_LABEL_TEXT_DIRECTION_0));
162
163        m_selectTarget.addOption("", Messages.get().key(Messages.GUI_ADVANCED_TAB_VALUE_NOT_SET_0));
164        m_selectTarget.addOption("_blank", Messages.get().key(Messages.GUI_ADVANCED_TAB_VALUE_NEW_WINDOW_0));
165        m_selectTarget.addOption("_top", Messages.get().key(Messages.GUI_ADVANCED_TAB_VALUE_TOP_WINDOW_0));
166        m_selectTarget.addOption("_self", Messages.get().key(Messages.GUI_ADVANCED_TAB_VALUE_SELF_WINDOW_0));
167        m_selectTarget.addOption("_parent", Messages.get().key(Messages.GUI_ADVANCED_TAB_VALUE_PARENT_WINDOW_0));
168
169        m_selectTextDirection.addOption("ltr", Messages.get().key(Messages.GUI_ADVANCED_TAB_VALUE_LEFT_TO_RIGHT_0));
170        m_selectTextDirection.addOption("rtl", Messages.get().key(Messages.GUI_ADVANCED_TAB_VALUE_RIGHT_TO_LEFT_0));
171        m_inputUrl.setButtonSize(Size.small);
172        m_inputUrl.addInputStyleName(I_CmsLayoutBundle.INSTANCE.imageAdvancedFormCss().input());
173        m_inputUrl.addStyleName(I_CmsLayoutBundle.INSTANCE.imageAdvancedFormCss().linkWidget());
174        m_fields = new HashMap<Attribute, I_CmsFormWidget>();
175        m_fields.put(Attribute.linkTarget, m_selectTarget);
176        m_fields.put(Attribute.dir, m_selectTextDirection);
177        m_fields.put(Attribute.linkPath, m_inputUrl);
178        m_fields.put(Attribute.longDesc, m_inputDescription);
179        m_fields.put(Attribute.style, m_inputStyle);
180        m_fields.put(Attribute.clazz, m_inputStyleClasses);
181        m_fields.put(Attribute.id, m_inputId);
182        m_fields.put(Attribute.lang, m_inputLanguageCode);
183    }
184
185    /**
186     * Displays the provided image information.<p>
187     *
188     * @param imageInfo the image information
189     * @param imageAttributes the image attributes
190     * @param initialFill flag to indicate that a new image has been selected
191     */
192    public void fillContent(CmsImageInfoBean imageInfo, CmsJSONMap imageAttributes, boolean initialFill) {
193
194        for (Entry<Attribute, I_CmsFormWidget> entry : m_fields.entrySet()) {
195            String val = imageAttributes.getString(entry.getKey().name());
196            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(val)) {
197                if ((entry.getKey() == Attribute.linkPath) && val.startsWith(CmsCoreProvider.get().getVfsPrefix())) {
198                    entry.getValue().setFormValueAsString(val.substring(CmsCoreProvider.get().getVfsPrefix().length()));
199                } else {
200                    entry.getValue().setFormValueAsString(val);
201                }
202            }
203        }
204    }
205
206    /**
207     * Adds necessary attributes to the map.<p>
208     *
209     * @param attributes the attribute map
210     * @param callback the callback to execute
211     */
212    public void getImageAttributes(
213        final Map<String, String> attributes,
214        final I_CmsSimpleCallback<Map<String, String>> callback) {
215
216        boolean requiresLink = false;
217
218        for (Entry<Attribute, I_CmsFormWidget> entry : m_fields.entrySet()) {
219            String val = entry.getValue().getFormValueAsString();
220            if (CmsStringUtil.isEmptyOrWhitespaceOnly(val)) {
221                continue;
222            }
223            if (entry.getKey() == Attribute.linkPath) {
224                requiresLink = true;
225            } else {
226                attributes.put(entry.getKey().name(), val);
227            }
228        }
229        if (requiresLink) {
230            CmsCoreProvider.get().substituteLinkForRootPath(
231                m_fields.get(Attribute.linkPath).getFormValueAsString(),
232                new I_CmsSimpleCallback<String>() {
233
234                    public void execute(String arg) {
235
236                        attributes.put(Attribute.linkPath.name(), arg);
237                        callback.execute(attributes);
238                    }
239                });
240        } else {
241            callback.execute(attributes);
242        }
243    }
244
245}