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
071        this(targetFolderRootPath);
072        setIcon(icon);
073    }
074
075    /**
076     * Constructor.<p>
077     *
078     * @param targetFolderRootPath the upload target folder root path
079     */
080    public CmsUploadButton(String targetFolderRootPath) {
081
082        super();
083        registerRpc(this);
084        m_uploadListener = new ArrayList<I_UploadListener>();
085        getState().setTargetFolderRootPath(targetFolderRootPath);
086    }
087
088    /**
089     * Adds an upload listener.<p>
090     *
091     * @param listener the listener instance
092     */
093    public void addUploadListener(I_UploadListener listener) {
094
095        m_uploadListener.add(listener);
096    }
097
098    /**
099     * @see org.opencms.ui.shared.rpc.I_CmsUploadRpc#onUploadFinished(java.util.List)
100     */
101    public void onUploadFinished(List<String> uploadedFiles) {
102
103        for (I_UploadListener listener : m_uploadListener) {
104            listener.onUploadFinished(uploadedFiles);
105        }
106    }
107
108    /**
109     * Removes the given upload listener.<p>
110     *
111     * @param listener the listener to remove
112     */
113    public void removeUploadListener(I_UploadListener listener) {
114
115        m_uploadListener.remove(listener);
116    }
117
118    @Override
119    public void setEnabled(boolean enabled) {
120
121        super.setEnabled(enabled);
122    }
123
124    /**
125     * Sets the upload target folder.<p>
126     *
127     * @param targetFolder the upload target
128     */
129    public void setTargetFolder(String targetFolder) {
130
131        getState().setTargetFolderRootPath(targetFolder);
132    }
133
134    /**
135     * @see com.vaadin.ui.AbstractComponent#getState()
136     */
137    @Override
138    protected CmsUploadState getState() {
139
140        return (CmsUploadState)super.getState();
141    }
142}