001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.postupload.client.ui;
029
030import org.opencms.ade.postupload.client.Messages;
031import org.opencms.gwt.client.ui.CmsPopup;
032import org.opencms.gwt.client.ui.CmsPushButton;
033import org.opencms.gwt.client.ui.I_CmsButton.ButtonColor;
034import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
035import org.opencms.gwt.client.ui.input.CmsCheckBox;
036import org.opencms.gwt.client.util.CmsDomUtil;
037
038import java.util.Arrays;
039import java.util.List;
040import java.util.function.Consumer;
041
042import com.google.gwt.core.client.GWT;
043import com.google.gwt.event.dom.client.ClickEvent;
044import com.google.gwt.uibinder.client.UiBinder;
045import com.google.gwt.uibinder.client.UiField;
046import com.google.gwt.uibinder.client.UiHandler;
047import com.google.gwt.user.client.ui.Composite;
048import com.google.gwt.user.client.ui.HTML;
049import com.google.gwt.user.client.ui.Panel;
050
051/**
052 * Widget for asking the user for confirmation whether a property value should be transferred to all uploaded files.
053 *
054 * <p>Has an additional checkbox for enabling/disabling overwrite mode, which controls whether properties should also be copied to
055 * files on which the corresponding property is already set.
056 */
057public class CmsConfirmTransferWidget extends Composite {
058
059    /**
060     * Interface with message string constants.<p>
061     */
062    public static class MessageStrings {
063
064        /**
065         * Message string provider.<p>
066         *
067         * @return a message string
068         */
069        public static String cancel() {
070
071            return org.opencms.gwt.client.Messages.get().key(org.opencms.gwt.client.Messages.GUI_CANCEL_0);
072        }
073
074        /**
075         * Message string provider.<p>
076         *
077         * @return a message string
078         */
079        public static String caption() {
080
081            return Messages.get().key(Messages.GUI_APPLY_PROPERTY_TO_ALL_FILES_CAPTION_0);
082        }
083
084        /**
085         * Message string provider.<p>
086         *
087         * @return a message string
088         */
089        public static String checkboxText() {
090
091            return Messages.get().key(Messages.GUI_OVERWRITE_EXISTING_VALUES_0);
092        }
093
094        /**
095         * Message string provider.<p>
096         *
097         * @return a message string
098         */
099        public static String confirmText() {
100
101            return Messages.get().key(Messages.GUI_APPLY_PROPERTY_TO_ALL_FILES_0);
102        }
103
104        /**
105         * Message string provider.<p>
106         *
107         * @return a message string
108         */
109        public static String emptyStringsNotAllowed() {
110
111            return Messages.get().key(Messages.GUI_EMPTY_VALUES_CAN_NOT_BE_TRANSFERRED_0);
112        }
113
114        /**
115         * Message string provider.<p>
116         *
117         * @return a message string
118         */
119        public static String ok() {
120
121            return org.opencms.gwt.client.Messages.get().key(org.opencms.gwt.client.Messages.GUI_OK_0);
122        }
123    }
124
125    /**
126     * UiBinder interface for this dialog.<p>
127     */
128    interface I_UiBinder extends UiBinder<Panel, CmsConfirmTransferWidget> {
129        // empty uibinder interface
130    }
131
132    /** UiBinder instance for this dialog. */
133    private static I_UiBinder uibinder = GWT.create(I_UiBinder.class);
134
135    /** The Cancel button. */
136    @UiField
137    protected CmsPushButton m_cancelButton;
138
139    /** Checkbox for selecting whether existing property values should be overwritten. */
140    @UiField
141    protected CmsCheckBox m_checkBox;
142
143    /** The label with the dialog text. */
144    @UiField
145    protected HTML m_mainLabel;
146
147    /** The OK button. */
148    @UiField
149    protected CmsPushButton m_okButton;
150
151    /** Callback to call with the value of the 'overwrite' option. */
152    private Consumer<Boolean> m_callback;
153
154    /** The dialog instance which this widget appears in. */
155    private CmsPopup m_popup;
156
157    /**
158     * Creates a new instance.<p>
159     *
160     * @param callback the callback to call with the value of the 'overwrite' option (only called if the user clicks OK)
161     */
162    public CmsConfirmTransferWidget(Consumer<Boolean> callback) {
163
164        initWidget(uibinder.createAndBindUi(this));
165        m_okButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.RED);
166        m_mainLabel.setHTML(CmsDomUtil.escapeXml(MessageStrings.confirmText()).replaceAll("\n", "<br>"));
167        m_checkBox.setVisible(true);
168        m_checkBox.setChecked(false);
169        m_callback = callback;
170    }
171
172    /**
173     * Opens the dialog for confirming a property transfer.
174     *
175     * @param callback the callback to call with the value of the 'overwrite' option
176     */
177    public static void showDialog(Consumer<Boolean> callback) {
178
179        CmsPopup popup = new CmsPopup();
180        popup.setModal(true);
181        popup.setGlassEnabled(true);
182        popup.setCaption(MessageStrings.caption());
183        CmsConfirmTransferWidget widget = new CmsConfirmTransferWidget(callback);
184        widget.setPopup(popup);
185        popup.setMainContent(widget);
186        widget.getButtons().forEach(button -> popup.addButton(button));
187        popup.center();
188
189    }
190
191    /**
192     * Gets the buttons for the dialog.<p>
193     *
194     * @return the buttons for the dialog
195     */
196    public List<CmsPushButton> getButtons() {
197
198        return Arrays.asList(m_cancelButton, m_okButton);
199    }
200
201    /**
202     * The click handler for the cancel button.<p>
203     *
204     * @param event the click event
205     */
206    @UiHandler("m_cancelButton")
207    public void onClickCancel(ClickEvent event) {
208
209        m_popup.hide();
210    }
211
212    /**
213     * The click handler for the OK button.<p>
214     *
215     * @param event the click event
216     */
217    @UiHandler("m_okButton")
218    public void onClickOk(ClickEvent event) {
219
220        m_popup.hide();
221        boolean overwrite = m_checkBox.isChecked();
222        m_callback.accept(Boolean.valueOf(overwrite));
223
224    }
225
226    /**
227     * Sets the popup which this widget is used in.<p>
228     *
229     * @param popup the popup
230     */
231    public void setPopup(CmsPopup popup) {
232
233        m_popup = popup;
234    }
235
236}