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.gwt.client.ui;
029
030import org.opencms.gwt.client.Messages;
031import org.opencms.gwt.client.ui.I_CmsButton.ButtonColor;
032import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
033import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
034
035import com.google.gwt.core.client.Scheduler;
036import com.google.gwt.core.client.Scheduler.ScheduledCommand;
037import com.google.gwt.dom.client.Style.Display;
038import com.google.gwt.event.dom.client.ClickEvent;
039import com.google.gwt.event.dom.client.ClickHandler;
040import com.google.gwt.user.client.Command;
041import com.google.gwt.user.client.ui.FlowPanel;
042import com.google.gwt.user.client.ui.Widget;
043
044/**
045 * Provides an alert dialog with a button.<p>
046 *
047 * @since 8.0.0
048 */
049public class CmsAlertDialog extends CmsPopup implements I_CmsTruncable {
050
051    /** The panel for the bottom widgets. */
052    private FlowPanel m_bottomWidgets;
053
054    /** The 'close' button. */
055    private CmsPushButton m_closeButton;
056
057    /** The content text. */
058    private FlowPanel m_content;
059
060    /** The action handler. */
061    private I_CmsCloseDialogHandler m_handler;
062
063    /** The panel for the top widgets. */
064    private FlowPanel m_topWidgets;
065
066    /** The warning message. */
067    private CmsMessageWidget m_warningMessage;
068
069    /**
070     * Constructor.<p>
071     */
072    public CmsAlertDialog() {
073
074        this("", "");
075    }
076
077    /**
078     * Constructor.<p>
079     *
080     * @param title the title and heading of the dialog
081     * @param content the content text
082     */
083    public CmsAlertDialog(String title, String content) {
084
085        this(title, content, Messages.get().key(Messages.GUI_CLOSE_0));
086    }
087
088    /**
089     * Constructor.<p>
090     *
091     * @param title the title and heading of the dialog
092     * @param content the content text
093     * @param buttonText the button text
094     */
095    public CmsAlertDialog(String title, String content, String buttonText) {
096
097        this(title, content, buttonText, null);
098    }
099
100    /**
101     * Constructor.<p>
102     *
103     * @param title the title and heading of the dialog
104     * @param content the content text
105     * @param buttonText the button text
106     * @param buttonIconClass the button icon class
107     */
108    public CmsAlertDialog(String title, String content, String buttonText, String buttonIconClass) {
109
110        super(title);
111        setAutoHideEnabled(false);
112        setModal(true);
113        setGlassEnabled(true);
114
115        // create the dialogs content panel
116        m_content = new FlowPanel();
117        m_content.addStyleName(I_CmsLayoutBundle.INSTANCE.dialogCss().alertMainContent());
118
119        // create the top widget panel
120        m_topWidgets = new FlowPanel();
121        m_topWidgets.addStyleName(I_CmsLayoutBundle.INSTANCE.dialogCss().alertTopContent());
122        m_topWidgets.getElement().getStyle().setDisplay(Display.NONE);
123        m_content.add(m_topWidgets);
124
125        // create the warning message
126        m_warningMessage = new CmsMessageWidget();
127        m_warningMessage.addStyleName(I_CmsLayoutBundle.INSTANCE.generalCss().border());
128        m_warningMessage.setMessageHtml(content);
129
130        m_content.add(m_warningMessage);
131
132        // create the bottom widget panel
133        m_bottomWidgets = new FlowPanel();
134        m_bottomWidgets.addStyleName(I_CmsLayoutBundle.INSTANCE.dialogCss().alertBottomContent());
135        m_bottomWidgets.getElement().getStyle().setDisplay(Display.NONE);
136        m_content.add(m_bottomWidgets);
137
138        // set the content to the popup
139        setMainContent(m_content);
140
141        // add the close button
142        m_closeButton = new CmsPushButton();
143        m_closeButton.setText(buttonText);
144        m_closeButton.setImageClass(buttonIconClass);
145        m_closeButton.setUseMinWidth(true);
146        m_closeButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.BLUE);
147        m_closeButton.addClickHandler(new ClickHandler() {
148
149            /**
150             * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
151             */
152            public void onClick(ClickEvent event) {
153
154                onClose();
155            }
156        });
157        addButton(m_closeButton);
158        addDialogClose(new Command() {
159
160            public void execute() {
161
162                onClose();
163            }
164        });
165    }
166
167    /**
168     * Adds a widget to this dialogs bottom content.<p>
169     *
170     * @param w the widget to add
171     */
172    public void addBottomWidget(Widget w) {
173
174        m_content.removeStyleName(I_CmsLayoutBundle.INSTANCE.dialogCss().alertMainContent());
175        m_bottomWidgets.getElement().getStyle().clearDisplay();
176        m_bottomWidgets.add(w);
177
178    }
179
180    /**
181     * Adds a widget to this dialogs top content.<p>
182     *
183     * @param w the widget to add
184     */
185    public void addTopWidget(Widget w) {
186
187        m_content.removeStyleName(I_CmsLayoutBundle.INSTANCE.dialogCss().alertMainContent());
188        m_topWidgets.getElement().getStyle().clearDisplay();
189        m_topWidgets.add(w);
190    }
191
192    /**
193     * @see org.opencms.gwt.client.ui.CmsPopup#center()
194     */
195    @Override
196    public void center() {
197
198        super.center();
199        onShow();
200    }
201
202    /**
203     * Returns the button widget.<p>
204     *
205     * @return the button
206     */
207    public CmsPushButton getCloseButton() {
208
209        return m_closeButton;
210    }
211
212    /**
213     * Sets the cancel/close button icon class.<p>
214     *
215     * @param iconClass the icon class
216     */
217    public void setCloseIconClass(String iconClass) {
218
219        getCloseButton().setImageClass(iconClass);
220    }
221
222    /**
223     * Sets the close button text.<p>
224     *
225     * @param text the button text
226     */
227    public void setCloseText(String text) {
228
229        m_closeButton.setText(text);
230    }
231
232    /**
233     * Sets the dialog handler.<p>
234     *
235     * @param handler the handler to set
236     */
237    public void setHandler(I_CmsCloseDialogHandler handler) {
238
239        m_handler = handler;
240    }
241
242    /**
243     * Sets the warning text (HTML possible).<p>
244     *
245     * @param warningText the warning text to set
246     */
247    public void setWarningMessage(String warningText) {
248
249        m_warningMessage.setMessageHtml(warningText);
250    }
251
252    /**
253     * @see org.opencms.gwt.client.ui.CmsPopup#show()
254     */
255    @Override
256    public void show() {
257
258        super.show();
259        onShow();
260    }
261
262    /**
263     * @see org.opencms.gwt.client.ui.I_CmsTruncable#truncate(java.lang.String, int)
264     */
265    public void truncate(String textMetricsKey, int clientWidth) {
266
267        for (Widget w : m_topWidgets) {
268            if (w instanceof I_CmsTruncable) {
269                ((I_CmsTruncable)w).truncate(textMetricsKey, clientWidth);
270            }
271        }
272    }
273
274    /**
275     * Returns the dialog handler.<p>
276     *
277     * @return the dialog handler
278     */
279    protected I_CmsCloseDialogHandler getHandler() {
280
281        return m_handler;
282    }
283
284    /**
285     * Returns the top widgets panel.<p>
286     *
287     * @return the top widgets panel
288     */
289    protected FlowPanel getTopWidgets() {
290
291        return m_topWidgets;
292    }
293
294    /**
295     * Executed on 'close' click. <p>
296     */
297    protected void onClose() {
298
299        getCloseButton().setEnabled(false);
300        if (getHandler() != null) {
301            getHandler().onClose();
302        }
303        hide();
304    }
305
306    /**
307     * Executed when the dialog is shown.<p>
308     */
309    protected void onShow() {
310
311        getCloseButton().setEnabled(true);
312        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
313
314            public void execute() {
315
316                truncate(CmsAlertDialog.this.hashCode() + "", getTopWidgets().getElement().getClientWidth());
317
318            }
319        });
320    }
321}