001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.containerpage.client.ui;
029
030import org.opencms.ade.containerpage.client.Messages;
031import org.opencms.gwt.client.ui.CmsListItem;
032import org.opencms.gwt.client.ui.CmsListItemWidget;
033import org.opencms.gwt.client.ui.CmsPopup;
034import org.opencms.gwt.client.ui.CmsPushButton;
035import org.opencms.gwt.client.ui.I_CmsButton.ButtonColor;
036import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
037import org.opencms.gwt.client.ui.input.CmsCheckBox;
038import org.opencms.gwt.shared.CmsListInfoBean;
039
040import java.util.Arrays;
041import java.util.List;
042
043import com.google.gwt.core.client.GWT;
044import com.google.gwt.event.dom.client.ClickEvent;
045import com.google.gwt.event.logical.shared.ValueChangeEvent;
046import com.google.gwt.uibinder.client.UiBinder;
047import com.google.gwt.uibinder.client.UiField;
048import com.google.gwt.uibinder.client.UiHandler;
049import com.google.gwt.user.client.rpc.AsyncCallback;
050import com.google.gwt.user.client.ui.Composite;
051import com.google.gwt.user.client.ui.Label;
052import com.google.gwt.user.client.ui.Panel;
053
054/**
055 * Widget for asking the user for confirmation whether a container page element should be removed.<p>
056 */
057public class CmsConfirmRemoveWidget 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_CONFIRM_REMOVAL_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_CONFIRM_REMOVAL_DELETE_CHECKBOX_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_CONFIRM_REMOVAL_TEXT_0);
102        }
103
104        /**
105         * Message string provider.<p>
106         *
107         * @return a message string
108         */
109        public static String noReferenceText() {
110
111            return Messages.get().key(Messages.GUI_CONFIRM_REMOVAL_CAN_DELETE_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, CmsConfirmRemoveWidget> {
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 the resource should be deleted. */
140    @UiField
141    protected CmsCheckBox m_checkBox;
142
143    /** True if this dialog has a checkbox for deleting the resource. */
144    protected boolean m_hasDeleteCheckbox;
145
146    /** The widget containing the element info box. */
147    @UiField
148    protected Panel m_infoBoxContainer;
149
150    /** The label with the dialog text. */
151    @UiField
152    protected Label m_mainLabel;
153
154    /** The label which appears when the user is given the option to delete the resource. */
155    @UiField
156    protected Label m_noReferenceLabel;
157
158    /** The OK button. */
159    @UiField
160    protected CmsPushButton m_okButton;
161
162    /**
163     * The callback which will be called when the user has confirmed or cancelled the element removal.<p>
164     */
165    protected AsyncCallback<Boolean> m_removeCallback;
166
167    /** True if the resource should be deleted. */
168    boolean m_deleteContent;
169
170    /** The dialog instance which this widget appears in. */
171    private CmsPopup m_popup;
172
173    /**
174     * Creates a new instance.<p>
175     *
176     * @param elementInfo the element info bean
177     * @param canDelete true if the user should be given the option to delete the resource
178     * @param removeCallback the callback to execute after the dialog closes
179     */
180    public CmsConfirmRemoveWidget(
181        CmsListInfoBean elementInfo,
182        boolean canDelete,
183        AsyncCallback<Boolean> removeCallback) {
184
185        m_removeCallback = removeCallback;
186        initWidget(uibinder.createAndBindUi(this));
187        CmsListItemWidget itemWidget = new CmsListItemWidget(elementInfo);
188        CmsListItem item = new CmsListItem(itemWidget);
189        m_infoBoxContainer.add(item);
190        m_okButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.RED);
191        if (canDelete) {
192            m_checkBox.setVisible(true);
193            m_checkBox.setChecked(true);
194            m_deleteContent = true;
195            m_noReferenceLabel.setVisible(true);
196        }
197    }
198
199    /**
200     * Gets the buttons for the dialog.<p>
201     *
202     * @return the buttons for the dialog
203     */
204    public List<CmsPushButton> getButtons() {
205
206        return Arrays.asList(m_cancelButton, m_okButton);
207    }
208
209    /**
210     * The click handler for the cancel button.<p>
211     *
212     * @param event the click event
213     */
214    @UiHandler("m_cancelButton")
215    public void onClickCancel(ClickEvent event) {
216
217        m_popup.hide();
218        m_removeCallback.onFailure(null);
219    }
220
221    /**
222     * The click handler for the OK button.<p>
223     *
224     * @param event the click event
225     */
226    @UiHandler("m_okButton")
227    public void onClickOk(ClickEvent event) {
228
229        m_removeCallback.onSuccess(Boolean.valueOf(m_deleteContent));
230        m_popup.hide();
231    }
232
233    /**
234     * Value change handler for the 'delete' checkbox.<p>
235     *
236     * @param event the value change event
237     */
238    @UiHandler("m_checkBox")
239    public void onValueChange(ValueChangeEvent<Boolean> event) {
240
241        m_deleteContent = event.getValue().booleanValue();
242    }
243
244    /**
245     * Sets the popup which this widget is used in.<p>
246     *
247     * @param popup the popup
248     */
249    public void setPopup(CmsPopup popup) {
250
251        m_popup = popup;
252    }
253
254}