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.gwt.client.ui.input.upload.CmsFileInput;
031import org.opencms.gwt.client.ui.input.upload.I_CmsUploadButton;
032import org.opencms.gwt.client.ui.input.upload.I_CmsUploadButtonHandler;
033import org.opencms.gwt.client.util.CmsDomUtil;
034
035import java.util.ArrayList;
036import java.util.Collections;
037import java.util.Iterator;
038import java.util.List;
039
040import com.google.gwt.dom.client.Element;
041import com.google.gwt.dom.client.Style.Display;
042import com.google.gwt.event.dom.client.ChangeEvent;
043import com.google.gwt.event.dom.client.ChangeHandler;
044import com.google.gwt.event.shared.HandlerRegistration;
045import com.google.gwt.user.client.DOM;
046import com.google.gwt.user.client.Event;
047import com.google.gwt.user.client.ui.HasWidgets;
048import com.google.gwt.user.client.ui.Widget;
049import com.vaadin.client.ui.VButton;
050
051/**
052 * A Vaadin based upload button.<p>
053 */
054public class CmsUploadButton extends VButton implements I_CmsUploadButton, HasWidgets {
055
056    /** The button handler. */
057    private I_CmsUploadButtonHandler m_buttonHandler;
058
059    /** The current file input element. */
060    private CmsFileInput m_fileInput;
061
062    /** The input change handler registration. */
063    private HandlerRegistration m_inputChangeHandlerRegistration;
064
065    /** List of used input elements. */
066    private List<CmsFileInput> m_usedInputs;
067
068    /**
069     * Constructor.<p>
070     *
071     * @param buttonHandler the button handler
072     */
073    public CmsUploadButton(I_CmsUploadButtonHandler buttonHandler) {
074
075        super();
076        addStyleName("o-upload-button");
077        m_buttonHandler = buttonHandler;
078        m_buttonHandler.setButton(this);
079        m_usedInputs = new ArrayList<CmsFileInput>();
080        createFileInput();
081    }
082
083    /**
084     * @see com.google.gwt.user.client.ui.HasWidgets#add(com.google.gwt.user.client.ui.Widget)
085     */
086    public void add(Widget w) {
087
088        throw new UnsupportedOperationException("Adding widgets to the upload button is not allowed.");
089    }
090
091    /**
092     * @see com.google.gwt.user.client.ui.HasWidgets#clear()
093     */
094    public void clear() {
095
096        throw new UnsupportedOperationException("Clear is not supported by the upload button.");
097    }
098
099    /**
100     * @see org.opencms.gwt.client.ui.input.upload.I_CmsUploadButton#createFileInput()
101     */
102    public CmsFileInput createFileInput() {
103
104        // remove the current file input field and add a new one
105        CmsFileInput previous = m_fileInput;
106
107        if (m_fileInput != null) {
108            m_fileInput.getElement().getStyle().setDisplay(Display.NONE);
109        }
110        m_fileInput = new CmsFileInput();
111        m_usedInputs.add(m_fileInput);
112        if (m_inputChangeHandlerRegistration != null) {
113            m_inputChangeHandlerRegistration.removeHandler();
114        }
115
116        m_inputChangeHandlerRegistration = m_fileInput.addChangeHandler(new ChangeHandler() {
117
118            public void onChange(ChangeEvent event) {
119
120                onInputChange();
121            }
122        });
123        m_buttonHandler.initializeFileInput(m_fileInput);
124        getElement().appendChild(m_fileInput.getElement());
125        m_fileInput.sinkEvents(Event.ONCHANGE);
126        m_fileInput.setParent(this);
127        return previous;
128    }
129
130    /**
131     * @see org.opencms.gwt.client.ui.input.upload.I_CmsUploadButton#getButtonHandler()
132     */
133    public I_CmsUploadButtonHandler getButtonHandler() {
134
135        return m_buttonHandler;
136    }
137
138    /**
139     * @see com.google.gwt.user.client.ui.HasWidgets#iterator()
140     */
141    public Iterator<Widget> iterator() {
142
143        return m_fileInput != null
144        ? Collections.<Widget> singletonList(m_fileInput).iterator()
145        : Collections.<Widget> emptyIterator();
146    }
147
148    /**
149     * @see org.opencms.gwt.client.ui.input.upload.I_CmsUploadButton#reinitButton(org.opencms.gwt.client.ui.input.upload.I_CmsUploadButtonHandler)
150     */
151    public void reinitButton(I_CmsUploadButtonHandler buttonHandler) {
152
153        for (CmsFileInput input : m_usedInputs) {
154            input.onDetach();
155            input.getElement().removeFromParent();
156        }
157        m_usedInputs.clear();
158        m_buttonHandler = buttonHandler;
159        m_buttonHandler.setButton(this);
160        createFileInput();
161    }
162
163    /**
164     * @see com.google.gwt.user.client.ui.HasWidgets#remove(com.google.gwt.user.client.ui.Widget)
165     */
166    @Override
167    public boolean remove(Widget w) {
168
169        // Validate.
170        if (w != m_fileInput) {
171            return false;
172        }
173        // Orphan.
174        try {
175            m_fileInput.setParent(null);
176
177        } finally {
178            // Physical detach.
179            Element elem = w.getElement();
180            DOM.getParent(elem).removeChild(elem);
181
182            // Logical detach.
183            m_fileInput = null;
184        }
185        return true;
186    }
187
188    public void setEnabled(boolean enabled, String disabledMessage) {
189
190        setEnabled(enabled);
191        setUploadEnabled(enabled);
192    }
193
194    /**
195     * @see com.google.gwt.user.client.ui.Widget#doAttachChildren()
196     */
197    @Override
198    protected void doAttachChildren() {
199
200        if ((m_fileInput != null) && !m_fileInput.isAttached()) {
201            m_fileInput.onAttach();
202        }
203    }
204
205    /**
206     * Handles the input change.<p>
207     */
208    void onInputChange() {
209
210        // hack to reset the hover state
211        CmsDomUtil.clearHover(getElement());
212        m_buttonHandler.onChange(m_fileInput);
213    }
214
215    /**
216     * Sets the upload enabled.<p>
217     *
218     * @param enabled the enabled flag
219     */
220    void setUploadEnabled(boolean enabled) {
221
222        m_fileInput.setVisible(enabled);
223    }
224}