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.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        super(ui);
063        registerRpc(new I_CmsWindowServerRpc() {
064
065            private static final long serialVersionUID = 1L;
066
067            @SuppressWarnings("synthetic-access")
068            public void handleOpenResult(String id, boolean ok) {
069
070                FutureCallback<Boolean> callback = m_callbackMap.get(id);
071                if (callback != null) {
072                    callback.onSuccess(Boolean.valueOf(ok));
073                }
074                m_callbackMap.remove(id);
075            }
076
077        }, I_CmsWindowServerRpc.class);
078    }
079
080    /**
081     * Tries to open a new browser window.<p>
082     *
083     * If openning the window fails, the given callback is called.<p>
084     *
085     * @param location the URL to open in the new window
086     * @param target the target window name
087     *
088     * @param onFailure the callback to call if opening the window fails
089     */
090    public void open(String location, String target, final Runnable onFailure) {
091
092        String id = RandomStringUtils.randomAlphanumeric(16);
093        m_callbackMap.put(id, new FutureCallback<Boolean>() {
094
095            public void onFailure(Throwable t) {
096                //
097            }
098
099            public void onSuccess(Boolean result) {
100
101                if (!result.booleanValue()) {
102                    onFailure.run();
103                }
104            }
105        });
106        getRpcProxy(I_CmsWindowClientRpc.class).open(location, target, id);
107    }
108
109}