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.client;
029
030import org.opencms.ade.upload.client.I_CmsUploadContext;
031import org.opencms.ade.upload.client.ui.CmsDialogUploadButtonHandler;
032import org.opencms.gwt.client.ui.input.upload.I_CmsUploadButtonHandler;
033import org.opencms.ui.shared.components.CmsUploadState;
034import org.opencms.ui.shared.rpc.I_CmsUploadRpc;
035
036import java.util.List;
037
038import com.google.common.base.Supplier;
039import com.google.gwt.user.client.ui.Widget;
040import com.vaadin.client.communication.StateChangeEvent;
041import com.vaadin.client.ui.button.ButtonConnector;
042import com.vaadin.shared.ui.Connect;
043
044/**
045 * The upload button connector.<p>
046 */
047@Connect(org.opencms.ui.components.CmsUploadButton.class)
048public class CmsUploadButtonConnector extends ButtonConnector {
049
050    /**
051     * Button context supplier.<p>
052     */
053    protected class ButtonContextSupplier implements Supplier<I_CmsUploadContext> {
054
055        /**
056         * @see com.google.common.base.Supplier#get()
057         */
058        public I_CmsUploadContext get() {
059
060            return new I_CmsUploadContext() {
061
062                public void onUploadFinished(List<String> uploadedFiles) {
063
064                    CmsUploadButtonConnector.this.onUploadFinished(uploadedFiles);
065                }
066
067            };
068        }
069    }
070
071    /** serial version id. */
072    private static final long serialVersionUID = -746097406461678513L;
073
074    /** The RPC proxy. */
075    private I_CmsUploadRpc m_rpc;
076
077    /**
078     * Constructor.<p>
079     */
080    public CmsUploadButtonConnector() {
081
082        m_rpc = getRpcProxy(I_CmsUploadRpc.class);
083    }
084
085    /**
086     * @see com.vaadin.client.ui.AbstractComponentConnector#getState()
087     */
088    @Override
089    public CmsUploadState getState() {
090
091        return (CmsUploadState)super.getState();
092    }
093
094    /**
095     * @see com.vaadin.client.ui.AbstractComponentConnector#getWidget()
096     */
097    @Override
098    public CmsUploadButton getWidget() {
099
100        return (CmsUploadButton)super.getWidget();
101    }
102
103    /**
104     * @see com.vaadin.client.ui.AbstractComponentConnector#onStateChanged(com.vaadin.client.communication.StateChangeEvent)
105     */
106    @Override
107    public void onStateChanged(StateChangeEvent stateChangeEvent) {
108
109        super.onStateChanged(stateChangeEvent);
110        getWidget().reinitButton(createButtonHandler());
111        getWidget().setUploadEnabled(isEnabled());
112    }
113
114    /**
115     * @see com.vaadin.client.ui.AbstractComponentConnector#createWidget()
116     */
117    @Override
118    protected Widget createWidget() {
119
120        CmsUploadButton uploadButton = new CmsUploadButton(createButtonHandler());
121        uploadButton.setUploadEnabled(isEnabled());
122        return uploadButton;
123    }
124
125    /**
126     * Called once the upload is finished.<p>
127     *
128     * @param uploadedFiles the uploaded files
129     */
130    void onUploadFinished(List<String> uploadedFiles) {
131
132        m_rpc.onUploadFinished(uploadedFiles);
133        // create a new button handler instance, as the old one is in a messed up state
134        getWidget().reinitButton(createButtonHandler());
135    }
136
137    /**
138     * Creates a button handler according to the component state.<p>
139     *
140     * @return the button handler
141     */
142    private I_CmsUploadButtonHandler createButtonHandler() {
143
144        I_CmsUploadButtonHandler buttonHandler = null;
145        switch (getState().getUploadType()) {
146            case singlefile:
147                buttonHandler = new CmsSingleFileUploadHandler(
148                    new ButtonContextSupplier(),
149                    getState().getDialogTitle());
150                ((CmsSingleFileUploadHandler)buttonHandler).setTargetFolderPath(getState().getTargetFolderRootPath());
151                ((CmsSingleFileUploadHandler)buttonHandler).setTargetFileName(getState().getTargetFileName());
152                ((CmsSingleFileUploadHandler)buttonHandler).setTargetFileNamePrefix(
153                    getState().getTargetFileNamePrefix());
154                break;
155            case multifile:
156            default:
157                buttonHandler = new CmsDialogUploadButtonHandler(new ButtonContextSupplier());
158                ((CmsDialogUploadButtonHandler)buttonHandler).setIsTargetRootPath(true);
159                ((CmsDialogUploadButtonHandler)buttonHandler).setTargetFolder(getState().getTargetFolderRootPath());
160                break;
161        }
162        return buttonHandler;
163    }
164}