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.main.CmsLog;
031import org.opencms.ui.CmsVaadinUtils;
032
033import java.io.InputStream;
034
035import org.apache.commons.lang3.RandomStringUtils;
036import org.apache.commons.logging.Log;
037import org.apache.commons.text.StringEscapeUtils;
038
039import com.vaadin.ui.CustomLayout;
040import com.vaadin.ui.JavaScript;
041
042/**
043 * Layout which looks like a Vaadin window without actually being a window.<p>
044 */
045public class CmsFakeWindow extends CustomLayout {
046
047    /** Serial version id. */
048    private static final long serialVersionUID = 1L;
049
050    /** The logger instance for this class. */
051    private static final Log LOG = CmsLog.getLog(CmsFakeWindow.class);
052
053    /** The content location. */
054    public static final String LOCATION_CONTENT = "content";
055
056    /** The window title. */
057    private String m_windowTitle;
058
059    /**
060     * Creates a new instance.<p>
061     */
062    public CmsFakeWindow() {
063
064        super();
065
066        // Need this for setWindowTitle, see below
067        setId(RandomStringUtils.randomAlphabetic(8));
068
069        try {
070            @SuppressWarnings("resource")
071            InputStream layoutStream = CmsVaadinUtils.readCustomLayout(getClass(), "CmsFakeWindow.html");
072            initTemplateContentsFromInputStream(layoutStream);
073        } catch (Exception e) {
074            LOG.error(e.getLocalizedMessage(), e);
075        }
076    }
077
078    /**
079     * Gets the window title.<p>
080     *
081     * @return the window title
082     */
083    public String getWindowTitle() {
084
085        return m_windowTitle;
086    }
087
088    /**
089     * Sets the window title.<p>
090     *
091     * @param title the new window title
092     */
093    public void setWindowTitle(String title) {
094
095        /* HACK: Using a Label destroys the layout for some reason, so we resort to setting the caption directly in the
096         element via an explicit JavaScript call. */
097        m_windowTitle = title;
098        JavaScript.eval(
099            "document.querySelector('#"
100                + getId()
101                + " .fakewindowheader').innerHTML = '"
102                + StringEscapeUtils.escapeEcmaScript(title)
103                + "'");
104    }
105
106}