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.ui.shared.components.CmsUploadState;
031import org.opencms.ui.shared.rpc.I_CmsUploadRpc;
032
033import java.util.ArrayList;
034import java.util.List;
035
036import com.vaadin.server.Resource;
037import com.vaadin.ui.Button;
038
039/**
040 * The upload button.<p>
041 */
042public class CmsUploadButton extends Button implements I_CmsUploadRpc {
043
044    /**
045     * Upload listener interface.<p>
046     */
047    public interface I_UploadListener {
048
049        /**
050         * Called once the upload is finished.<p>
051         *
052         * @param uploadedFiles the uploaded files root paths
053         */
054        void onUploadFinished(List<String> uploadedFiles);
055    }
056
057    /** Serial version id. */
058    private static final long serialVersionUID = -8591991683786743571L;
059
060    /** The upoad listeners. */
061    private List<I_UploadListener> m_uploadListener;
062
063    /**
064     * Constructor.<p>
065     *
066     * @param icon the button icon
067     * @param targetFolderRootPath the target folder path
068     */
069    public CmsUploadButton(Resource icon, String targetFolderRootPath) {
070        this(targetFolderRootPath);
071        setIcon(icon);
072    }
073
074    /**
075     * Constructor.<p>
076     *
077     * @param targetFolderRootPath the upload target folder root path
078     */
079    public CmsUploadButton(String targetFolderRootPath) {
080        super();
081        registerRpc(this);
082        m_uploadListener = new ArrayList<I_UploadListener>();
083        getState().setTargetFolderRootPath(targetFolderRootPath);
084    }
085
086    /**
087     * Adds an upload listener.<p>
088     *
089     * @param listener the listener instance
090     */
091    public void addUploadListener(I_UploadListener listener) {
092
093        m_uploadListener.add(listener);
094    }
095
096    /**
097     * @see org.opencms.ui.shared.rpc.I_CmsUploadRpc#onUploadFinished(java.util.List)
098     */
099    public void onUploadFinished(List<String> uploadedFiles) {
100
101        for (I_UploadListener listener : m_uploadListener) {
102            listener.onUploadFinished(uploadedFiles);
103        }
104    }
105
106    /**
107     * Removes the given upload listener.<p>
108     *
109     * @param listener the listener to remove
110     */
111    public void removeUploadListener(I_UploadListener listener) {
112
113        m_uploadListener.remove(listener);
114    }
115
116    /**
117     * Sets the upload target folder.<p>
118     *
119     * @param targetFolder the upload target
120     */
121    public void setTargetFolder(String targetFolder) {
122
123        getState().setTargetFolderRootPath(targetFolder);
124    }
125
126    /**
127     * @see com.vaadin.ui.AbstractComponent#getState()
128     */
129    @Override
130    protected CmsUploadState getState() {
131
132        return (CmsUploadState)super.getState();
133    }
134}