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.report;
029
030import org.opencms.main.CmsLog;
031import org.opencms.report.A_CmsReportThread;
032import org.opencms.ui.CmsVaadinUtils;
033import org.opencms.ui.components.CmsBasicDialog;
034
035import java.io.IOException;
036import java.io.InputStream;
037
038import org.apache.commons.lang3.RandomStringUtils;
039import org.apache.commons.logging.Log;
040import org.apache.commons.text.StringEscapeUtils;
041
042import com.google.gwt.event.shared.HandlerRegistration;
043import com.vaadin.server.Page;
044import com.vaadin.ui.CustomLayout;
045import com.vaadin.ui.JavaScript;
046
047/**
048 * Report overlay, shows the wait spinner first and the report window later in case off longer running threads.<p>
049 */
050public class CmsReportOverlay extends CustomLayout {
051
052    /** The serial version id. */
053    private static final long serialVersionUID = 12519444651216053L;
054
055    /** The logger instance for this class. */
056    private static final Log LOG = CmsLog.getLog(CmsReportOverlay.class);
057
058    /** The delay in ms before the report window is shown. */
059    private static final int REPORT_VIEW_DELAY = 5000;
060
061    /** The report widget. */
062    CmsReportWidget m_report;
063
064    /**
065     * Constructor.<p>
066     *
067     * @param thread the report thread
068     */
069    public CmsReportOverlay(A_CmsReportThread thread) {
070
071        setId(RandomStringUtils.randomAlphabetic(8));
072
073        try {
074            @SuppressWarnings("resource")
075            InputStream layoutStream = CmsVaadinUtils.readCustomLayout(getClass(), "reportoverlay.html");
076            initTemplateContentsFromInputStream(layoutStream);
077        } catch (IOException e) {
078            LOG.error(e.getLocalizedMessage(), e);
079        }
080
081        m_report = new CmsReportWidget(thread);
082        m_report.setWidth("100%");
083        m_report.setHeight((Page.getCurrent().getBrowserWindowHeight() - 200) + "px");
084        CmsBasicDialog dialogContent = new CmsBasicDialog();
085        dialogContent.setContent(m_report);
086        addComponent(dialogContent, "content");
087
088        m_report.addReportFinishedHandler(new Runnable() {
089
090            public void run() {
091
092                setVisible(false);
093            }
094        });
095        // add a timeout to show the report window
096        JavaScript.eval(
097            "setTimeout(function(){ "
098                + "var el= document.getElementById('"
099                + getId()
100                + "'); if (el!=null) el.classList.add('o-report-show'); },"
101                + REPORT_VIEW_DELAY
102                + ")");
103    }
104
105    /**
106     * Adds an action that should be executed if the report is finished.<p>
107     *
108     * Note that this action will only be called if the report is finished while the report widget is actually
109     * displayed. For example, if the user closes the browser window before the report is finished, this will not be executed.
110     *
111     * @param handler the handler
112     * @return the handler registration
113     */
114    public HandlerRegistration addReportFinishedHandler(final Runnable handler) {
115
116        return m_report.addReportFinishedHandler(handler);
117    }
118
119    /**
120     * Sets the window title.<p>
121     *
122     * @param title the new window title
123     */
124    public void setTitle(String title) {
125
126        /* HACK: Using a Label destroys the layout for some reason, so we resort to setting the caption directly in the
127         element via an explicit JavaScript call. */
128        JavaScript.eval(
129            "document.querySelector('#"
130                + getId()
131                + " .fakewindowheader').innerHTML = '"
132                + StringEscapeUtils.escapeEcmaScript(title)
133                + "'");
134    }
135}