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.ui.components;
029
030import org.opencms.ui.CmsVaadinUtils;
031import org.opencms.ui.FontOpenCms;
032
033import com.vaadin.ui.Button;
034import com.vaadin.ui.Button.ClickEvent;
035import com.vaadin.ui.Button.ClickListener;
036import com.vaadin.ui.UI;
037import com.vaadin.ui.Window;
038import com.vaadin.v7.shared.ui.label.ContentMode;
039import com.vaadin.v7.ui.Label;
040
041/**
042 * Basic confirmation dialog.<p>
043 */
044public class CmsConfirmationDialog extends CmsBasicDialog {
045
046    /** Serial version id. */
047    private static final long serialVersionUID = 1L;
048
049    /** OK button .*/
050    private Button m_okButton;
051
052    /** Cancel button. */
053    private Button m_cancelButton;
054
055    /** Label to display text. */
056    private Label m_label;
057
058    /** Warning icon. */
059    private Label m_icon;
060
061    /** Action to execute when confirmed. */
062    Runnable m_okAction;
063
064    /** Action to execute when cancelled. */
065    Runnable m_cancelAction;
066
067    /**
068     * Creates a new instance.<p>
069     *
070     * @param message the message
071     * @param okAction the action for the confirmation case
072     * @param cancelAction the action for the cancel case
073     */
074    public CmsConfirmationDialog(String message, Runnable okAction, Runnable cancelAction) {
075
076        m_okAction = okAction;
077
078        m_cancelAction = cancelAction;
079        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
080        m_label.setValue(message);
081        m_icon.setContentMode(ContentMode.HTML);
082        m_icon.setValue(FontOpenCms.WARNING.getHtml());
083        m_okButton.addClickListener(new ClickListener() {
084
085            private static final long serialVersionUID = 1L;
086
087            public void buttonClick(ClickEvent event) {
088
089                if (m_okAction != null) {
090                    m_okAction.run();
091                }
092            }
093        });
094        m_cancelButton.addClickListener(new ClickListener() {
095
096            private static final long serialVersionUID = 1L;
097
098            public void buttonClick(ClickEvent event) {
099
100                if (m_cancelAction != null) {
101                    m_cancelAction.run();
102                }
103
104            }
105        });
106    }
107
108    /**
109     * Shows the confirmation dialog in a window.<p>
110     *
111     * @param title the window title
112     * @param message the message to display in the dialog
113     * @param okAction the action to execute when the user clicks OK
114     *
115     * @return the dialog instance
116     */
117    public static CmsConfirmationDialog show(String title, String message, final Runnable okAction) {
118
119        return show(title, message, okAction, null);
120    }
121
122    /**
123     * Shows the confirmation dialog in a window.<p>
124     *
125     * @param title the window title
126     * @param message the message to display in the dialog
127     * @param okAction the action to execute when the user clicks OK
128     * @param cancelAction the action for the cancel case
129     *
130     * @return the dialog instance
131     */
132    public static CmsConfirmationDialog show(
133        String title,
134        String message,
135        final Runnable okAction,
136        final Runnable cancelAction) {
137
138        final Window window = CmsBasicDialog.prepareWindow();
139        window.setCaption(title);
140        CmsConfirmationDialog dialog = new CmsConfirmationDialog(message, new Runnable() {
141
142            public void run() {
143
144                window.close();
145                okAction.run();
146            }
147        }, new Runnable() {
148
149            public void run() {
150
151                if (cancelAction != null) {
152                    cancelAction.run();
153                }
154                window.close();
155            }
156        });
157        window.setContent(dialog);
158        UI.getCurrent().addWindow(window);
159        return dialog;
160    }
161
162    /**
163     * Shows the confirmation dialog in a window.<p>
164     *
165     * @param title the window title
166     * @param message the message to display in the dialog
167     * @param okAction the action to execute when the user clicks OK
168     * @param cancelAction the action for the cancel case
169     * @param hideUnused boolean
170     *
171     * @return the dialog instance
172     */
173    public static CmsConfirmationDialog show(
174        String title,
175        String message,
176        final Runnable okAction,
177        final Runnable cancelAction,
178        boolean hideUnused) {
179
180        final Window window = CmsBasicDialog.prepareWindow();
181        window.setCaption(title);
182        Runnable newOk = null;
183        Runnable newCancel = null;
184        if (okAction != null) {
185            newOk = new Runnable() {
186
187                public void run() {
188
189                    window.close();
190                    okAction.run();
191                }
192            };
193        }
194        if (cancelAction != null) {
195            newCancel = new Runnable() {
196
197                public void run() {
198
199                    window.close();
200                    cancelAction.run();
201                }
202            };
203        }
204        CmsConfirmationDialog dialog = new CmsConfirmationDialog(message, newOk, newCancel);
205        dialog.setHideUnusedButtons(hideUnused);
206        window.setContent(dialog);
207        UI.getCurrent().addWindow(window);
208        return dialog;
209    }
210
211    /**
212     * Gets the label.<p>
213     *
214     * @return the label
215     */
216    public Label getLabel() {
217
218        return m_label;
219    }
220
221    /**
222     * Hides button without a runnable.<p>
223     *
224     * @param hide boolean
225     */
226    public void setHideUnusedButtons(boolean hide) {
227
228        m_okButton.setVisible(!hide || (m_okAction != null));
229        m_cancelButton.setVisible(!hide || (m_cancelAction != null));
230    }
231
232    /**
233     * Sets the message.<p>
234     *
235     * @param message the message
236     */
237    public void setMessage(String message) {
238
239        m_label.setValue(message);
240    }
241
242}