001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.extensions;
029
030import org.opencms.ui.shared.rpc.I_CmsWindowClientRpc;
031import org.opencms.ui.shared.rpc.I_CmsWindowServerRpc;
032
033import java.util.Map;
034import java.util.concurrent.ConcurrentHashMap;
035
036import org.apache.commons.lang3.RandomStringUtils;
037
038import com.google.common.util.concurrent.FutureCallback;
039import com.vaadin.server.AbstractExtension;
040import com.vaadin.ui.UI;
041
042/**
043 * Extension used to open new browser windows.<p>
044 * While Page.open() can also be used to open new windows, it doesn't give you any feedback on whether
045 * opening the window was successful. Using this extension, it is possible to pass a callback which gets called
046 * with a parameter indicating whether opening the window succeeded or not.
047 */
048public class CmsWindowExtension extends AbstractExtension {
049
050    /** Serial version id. */
051    private static final long serialVersionUID = 1L;
052
053    /** Map of callbacks which have not yet been called. */
054    private Map<String, FutureCallback<Boolean>> m_callbackMap = new ConcurrentHashMap<String, FutureCallback<Boolean>>();
055
056    /**
057     * Creates a new instance and binds it to the given UI.<p>
058     *
059     * @param ui the UI
060     */
061    public CmsWindowExtension(UI ui) {
062
063        super(ui);
064        registerRpc(new I_CmsWindowServerRpc() {
065
066            private static final long serialVersionUID = 1L;
067
068            @SuppressWarnings("synthetic-access")
069            public void handleOpenResult(String id, boolean ok) {
070
071                FutureCallback<Boolean> callback = m_callbackMap.get(id);
072                if (callback != null) {
073                    callback.onSuccess(Boolean.valueOf(ok));
074                }
075                m_callbackMap.remove(id);
076            }
077
078        }, I_CmsWindowServerRpc.class);
079    }
080
081    /**
082     * Tries to open a new browser window.<p>
083     *
084     * If openning the window fails, the given callback is called.<p>
085     *
086     * @param location the URL to open in the new window
087     * @param target the target window name
088     *
089     * @param onFailure the callback to call if opening the window fails
090     */
091    public void open(String location, String target, final Runnable onFailure) {
092
093        String id = RandomStringUtils.randomAlphanumeric(16);
094        m_callbackMap.put(id, new FutureCallback<Boolean>() {
095
096            public void onFailure(Throwable t) {
097
098                //
099            }
100
101            public void onSuccess(Boolean result) {
102
103                if (!result.booleanValue()) {
104                    onFailure.run();
105                }
106            }
107        });
108        getRpcProxy(I_CmsWindowClientRpc.class).open(location, target, id);
109    }
110
111}