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.ade.containerpage.client;
029
030import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
031import org.opencms.gwt.shared.CmsClientVariantInfo;
032import org.opencms.gwt.shared.CmsGwtConstants;
033
034import com.google.gwt.dom.client.Element;
035import com.google.gwt.event.logical.shared.CloseEvent;
036import com.google.gwt.event.logical.shared.CloseHandler;
037import com.google.gwt.user.client.Window;
038import com.google.gwt.user.client.ui.PopupPanel;
039
040/**
041 * Class used to display a client variant of a template context.<p>
042 */
043public class CmsClientVariantDisplay {
044
045    /**
046     * Popup subclass which exposes the getGlassElement method.<p>
047     */
048    class VariantPopup extends PopupPanel {
049
050        /**
051         * Creates a new instance.<p>
052         */
053        public VariantPopup() {
054
055            super(true, true);
056            addStyleName(I_CmsLayoutBundle.INSTANCE.dialogCss().popup());
057            addStyleName(I_CmsLayoutBundle.INSTANCE.generalCss().opencms());
058        }
059
060        /**
061         * @see com.google.gwt.user.client.ui.PopupPanel#getGlassElement()
062         */
063        @Override
064        public Element getGlassElement() {
065
066            return super.getGlassElement();
067        }
068    }
069
070    /** the container page handler. */
071    CmsContainerpageHandler m_containerpageHandler;
072
073    /** The popup. */
074    private VariantPopup m_popup;
075
076    /**
077     * Creates a new instance.<p>
078     *
079     * @param handler the container page handler
080     */
081    public CmsClientVariantDisplay(CmsContainerpageHandler handler) {
082
083        m_containerpageHandler = handler;
084    }
085
086    /**
087     * Clears a currently displayed popup.<p>
088     *
089     */
090    public void clear() {
091
092        if (m_popup != null) {
093            m_popup.hide();
094        }
095        m_popup = null;
096
097    }
098
099    /**
100     * Shows the given context/variant combination.<p>
101     *
102     * @param context the template context
103     * @param info the client variant
104     */
105    public void show(String context, CmsClientVariantInfo info) {
106
107        clear();
108        String url = buildClientVariantUrl(context, info);
109        CmsClientVariantFrame container = new CmsClientVariantFrame(
110            url,
111            info.getScreenWidth(),
112            info.getScreenHeight(),
113            m_containerpageHandler);
114        VariantPopup popup = new VariantPopup();
115        popup.setGlassEnabled(true);
116        popup.add(container);
117        m_popup = popup;
118        m_popup.getGlassElement().addClassName(I_CmsLayoutBundle.INSTANCE.dialogCss().popupOverlay());
119        m_popup.getGlassElement().addClassName(I_CmsLayoutBundle.INSTANCE.generalCss().opencms());
120        m_popup.getGlassElement().getStyle().setBackgroundColor("white");
121        m_popup.getGlassElement().getStyle().setOpacity(0.8);
122        m_popup.getElement().getStyle().setZIndex(200000);
123        popup.center();
124
125        popup.addCloseHandler(new CloseHandler<PopupPanel>() {
126
127            public void onClose(CloseEvent<PopupPanel> event) {
128
129                m_containerpageHandler.activateSelection();
130            }
131        });
132    }
133
134    /**
135     * Builds the URL for the client variant.<p>
136     *
137     * @param context the template context name
138     * @param info the client variant info
139     *
140     * @return the URL for the variant
141     */
142    private String buildClientVariantUrl(String context, CmsClientVariantInfo info) {
143
144        String currentUrl = Window.Location.getHref();
145        // remove fragment
146        currentUrl = currentUrl.replaceFirst("#.*$", "");
147        String connector = "?";
148        if (currentUrl.indexOf('?') >= 0) {
149            connector = "&";
150        }
151        String targetUrl = currentUrl + connector + CmsGwtConstants.PARAM_TEMPLATE_CONTEXT + "=" + context;
152        return targetUrl;
153    }
154}