001/*
002 * File   : $Source$
003 * Date   : $Date$
004 * Version: $Revision$
005 *
006 * This library is part of OpenCms -
007 * the Open Source Content Management System
008 *
009 * Copyright (C) 2002 - 2009 Alkacon Software (https://www.alkacon.com)
010 *
011 * This library is free software; you can redistribute it and/or
012 * modify it under the terms of the GNU Lesser General Public
013 * License as published by the Free Software Foundation; either
014 * version 2.1 of the License, or (at our option) any later version.
015 *
016 * This library is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 * Lesser General Public License for more details.
020 *
021 * For further information about Alkacon Software, please see the
022 * company website: https://www.alkacon.com
023 *
024 * For further information about OpenCms, please see the
025 * project website: https://www.opencms.org
026 *
027 * You should have received a copy of the GNU Lesser General Public
028 * License along with this library; if not, write to the Free Software
029 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
030 */
031
032package org.opencms.ade.postupload.client.ui;
033
034import org.opencms.ade.postupload.client.Messages;
035import org.opencms.ade.postupload.shared.CmsPostUploadDialogBean;
036import org.opencms.ade.postupload.shared.CmsPostUploadDialogPanelBean;
037import org.opencms.gwt.client.property.CmsPropertySubmitHandler;
038import org.opencms.gwt.client.property.CmsSimplePropertyEditor;
039import org.opencms.gwt.client.property.I_CmsPropertyEditorHandler;
040import org.opencms.gwt.client.rpc.CmsRpcAction;
041import org.opencms.gwt.client.ui.CmsAlertDialog;
042import org.opencms.gwt.client.ui.I_CmsButton.ButtonColor;
043import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
044import org.opencms.gwt.client.ui.input.I_CmsFormField;
045import org.opencms.gwt.client.ui.input.form.A_CmsFormFieldPanel;
046import org.opencms.gwt.client.ui.input.form.CmsForm;
047import org.opencms.gwt.client.ui.input.form.I_CmsFormHandler;
048import org.opencms.gwt.shared.property.CmsPropertyModification;
049import org.opencms.util.CmsStringUtil;
050import org.opencms.util.CmsUUID;
051import org.opencms.xml.content.CmsXmlContentProperty;
052
053import java.util.ArrayList;
054import java.util.List;
055import java.util.Map;
056import java.util.function.Function;
057
058import com.google.gwt.core.client.Scheduler;
059import com.google.gwt.core.client.Scheduler.ScheduledCommand;
060import com.google.gwt.dom.client.Style.Unit;
061import com.google.gwt.event.dom.client.ClickEvent;
062import com.google.gwt.event.dom.client.ClickHandler;
063import com.google.gwt.user.client.ui.FlowPanel;
064
065import elemental2.core.JsString;
066
067/**
068 * Panel for the property dialog.<p>
069 */
070public class CmsUploadPropertyPanel extends FlowPanel implements I_CmsFormHandler {
071
072    /** The upload property dialog containing this panel. */
073    CmsUploadPropertyDialog m_dialog;
074
075    /** The property editor handler instance. */
076    I_CmsPropertyEditorHandler m_propertyEditorHandler;
077
078    /** The original file extension. */
079    private String m_originalExtension;
080
081    /** The property editor instance. */
082    private CmsSimplePropertyEditor m_propertyEditor;
083
084    /** The path relative resource path. */
085    private String m_resourcePath;
086
087    /** The handler for transferring property values to other uploads. */
088    private I_CmsUpdateAllUploadsHandler m_updateAllHandler;
089
090    /** The values. */
091    private CmsPostUploadDialogPanelBean m_values;
092
093    /**
094     * Public constructor.<p>
095     *
096     * @param dialog the dialog which this panel is added to
097     * @param options the data to fill UI component options
098     * @param values the bean with the current values
099     */
100    public CmsUploadPropertyPanel(
101        CmsUploadPropertyDialog dialog,
102        CmsPostUploadDialogBean options,
103        CmsPostUploadDialogPanelBean values) {
104
105        m_values = values;
106        m_dialog = dialog;
107        m_resourcePath = values.getInfoBean().getSubTitle();
108        m_updateAllHandler = new I_CmsUpdateAllUploadsHandler() {
109
110            @Override
111            public void updateAll(String propName, String value) {
112
113                if (CmsStringUtil.isEmptyOrWhitespaceOnly(value)) {
114                    CmsAlertDialog alert = new CmsAlertDialog(
115                        CmsConfirmTransferWidget.MessageStrings.caption(),
116                        CmsConfirmTransferWidget.MessageStrings.emptyStringsNotAllowed());
117                    alert.center();
118
119                } else {
120                    CmsConfirmTransferWidget.showDialog(overwriteMode -> {
121                        List<CmsUUID> fileIds = new ArrayList<>(options.getResources().keySet());
122                        CmsRpcAction<Void> action = new CmsRpcAction<Void>() {
123
124                            @Override
125                            public void execute() {
126
127                                start(0, true);
128                                final boolean withBasicProperties = dialog.m_dialogData.isAddBasicProperties();
129                                dialog.getDialogService().updateAllFiles(
130                                    fileIds,
131                                    propName,
132                                    value,
133                                    overwriteMode,
134                                    withBasicProperties,
135                                    this);
136                            }
137
138                            @Override
139                            protected void onResponse(Void result) {
140
141                                stop(false);
142
143                            }
144
145                        };
146                        action.execute();
147                    });
148                }
149
150            }
151        };
152        initializePropertyEditor();
153        I_CmsFormField filenameField = getFilenameField();
154        m_originalExtension = getExtension(filenameField.getModelValue());
155        if (options.hasImage()) {
156            CmsImagePreview preview = new CmsImagePreview();
157            preview.getElement().getStyle().setMarginTop(5, Unit.PX);
158            add(preview);
159            if (values.getPreviewLink() != null) {
160                preview.setFullPreview(values.getPermalink());
161                preview.setImageUrl(values.getPreviewLink());
162                preview.setHighResImageUrl(values.getHighResPreviewLink());
163                preview.setInfo1(values.getPreviewInfo1());
164                preview.setInfo2(values.getPreviewInfo2());
165            } else {
166                preview.hide();
167            }
168
169        }
170
171        // height may change on click
172        addDomHandler(new ClickHandler() {
173
174            public void onClick(ClickEvent event) {
175
176                m_dialog.updateHeight();
177            }
178        }, ClickEvent.getType());
179    }
180
181    /**
182     * Gets the property editor instance.<p>
183     *
184     * @return the property editor instance
185     */
186    public CmsSimplePropertyEditor getPropertyEditor() {
187
188        return m_propertyEditor;
189    }
190
191    /**
192     * Returns the resourcePath.<p>
193     *
194     * @return the resourcePath
195     */
196    public String getResourcePath() {
197
198        return m_resourcePath;
199    }
200
201    /**
202     * Returns the content bean (values) of the current dialog.<p>
203     *
204     * @return the content bean (values) of the current dialog
205     */
206    public CmsPostUploadDialogPanelBean getUpdatedValues() {
207
208        CmsPostUploadDialogPanelBean bean = new CmsPostUploadDialogPanelBean(
209            m_values.getStructureId(),
210            m_values.getInfoBean());
211
212        if (!m_values.equals(bean)) {
213            m_values = bean;
214        }
215        return m_values;
216    }
217
218    /**
219     * @see org.opencms.gwt.client.ui.input.form.I_CmsFormHandler#isSubmitting()
220     */
221    public boolean isSubmitting() {
222
223        // TODO Auto-generated method stub
224        return false;
225    }
226
227    /**
228     * @see org.opencms.gwt.client.ui.input.form.I_CmsFormHandler#onSubmitValidationResult(org.opencms.gwt.client.ui.input.form.CmsForm, boolean)
229     */
230    public void onSubmitValidationResult(CmsForm form, boolean ok) {
231
232        if (ok) {
233            I_CmsFormField filenameField = getFilenameField();
234
235            String value = filenameField.getWidget().getFormValueAsString();
236            String extension = getExtension(value);
237
238            if (!extension.equalsIgnoreCase(m_originalExtension)) {
239
240                String titleKey = Messages.GUI_DIALOG_CHANGE_FILE_EXTENSION_WARNING_TITLE_2;
241                String textKey = Messages.GUI_DIALOG_CHANGE_FILE_EXTENSION_WARNING_TEXT_0;
242                String yesKey = Messages.GUI_DIALOG_BUTTON_KEEP_1;
243                String noKey = Messages.GUI_DIALOG_BUTTON_CHANGE_1;
244                if ("".equals(extension)) {
245                    titleKey = Messages.GUI_DIALOG_CHANGE_FILE_EXTENSION_TO_EMPTY_WARNING_TITLE_2;
246                    noKey = Messages.GUI_DIALOG_BUTTON_CHANGE_TO_EMPTY_1;
247                }
248
249                CmsYesNoDialog confirmation = new CmsYesNoDialog(
250                    Messages.get().key(titleKey, m_originalExtension, extension),
251                    Messages.get().key(textKey),
252                    (dialog, useOriginalExtension) -> {
253                        dialog.hide();
254                        if (useOriginalExtension) {
255                            String correctedValue = value.substring(0, value.length() - extension.length())
256                                + m_originalExtension;
257                            filenameField.getModel().setValue(correctedValue, false);
258                        }
259                        form.handleSubmit(new CmsPropertySubmitHandler(m_propertyEditorHandler));
260                    });
261                Function<String, String> format = s -> {
262                    if ("".equals(s)) {
263                        return "\"\"";
264                    }
265                    if (s.length() > 10) {
266                        return s.substring(0, 10) + JsString.fromCodePoint(8230 /* ellipsis */);
267                    }
268                    return s;
269                };
270                confirmation.getNoButton().setText(Messages.get().key(noKey, format.apply(extension)));
271                confirmation.getNoButton().setButtonStyle(ButtonStyle.TEXT, ButtonColor.BLUE);
272                confirmation.getYesButton().setText(Messages.get().key(yesKey, format.apply(m_originalExtension)));
273                confirmation.getYesButton().setButtonStyle(ButtonStyle.TEXT, ButtonColor.GREEN);
274                confirmation.center();
275            } else {
276                form.handleSubmit(new CmsPropertySubmitHandler(m_propertyEditorHandler));
277            }
278        }
279    }
280
281    /**
282     * @see org.opencms.gwt.client.ui.input.form.I_CmsFormHandler#onValidationResult(org.opencms.gwt.client.ui.input.form.CmsForm, boolean)
283     */
284    public void onValidationResult(CmsForm form, boolean ok) {
285
286        // do nothing for now
287    }
288
289    /**
290     * Sets up the property editor.<p>
291     */
292    protected void initializePropertyEditor() {
293
294        Map<String, CmsXmlContentProperty> propertyConfig = m_values.getPropertyDefinitions();
295        m_propertyEditorHandler = new CmsUploadPropertyEditorHandler(m_dialog, m_values);
296        CmsSimplePropertyEditor propertyEditor = new CmsUploadPropertyEditor(
297            m_values.getStructureId(),
298            propertyConfig,
299            m_propertyEditorHandler,
300            m_updateAllHandler);
301        propertyEditor.getForm().setFormHandler(this);
302        m_propertyEditor = propertyEditor;
303        m_propertyEditor.initializeWidgets(null);
304        A_CmsFormFieldPanel propertiesPanel = m_propertyEditor.getForm().getWidget();
305        add(propertiesPanel);
306    }
307
308    /**
309     * @see com.google.gwt.user.client.ui.Widget#onLoad()
310     */
311    @Override
312    protected void onLoad() {
313
314        super.onLoad();
315        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
316
317            public void execute() {
318
319                if (m_dialog != null) {
320                    m_dialog.updateHeight();
321                }
322            }
323        });
324    }
325
326    private String getExtension(String filename) {
327
328        int dotPos = filename.lastIndexOf('.');
329        if (dotPos == -1) {
330            return "";
331        } else {
332            return filename.substring(dotPos);
333        }
334    }
335
336    private I_CmsFormField getFilenameField() {
337
338        return m_propertyEditor.getForm().getFields().values().stream().filter(
339            field -> field.getId().contains(CmsPropertyModification.FILE_NAME_PROPERTY)).findFirst().orElse(null);
340    }
341
342}