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.gwt.client.ui.input.upload;
029
030import com.google.gwt.core.client.GWT;
031import com.google.gwt.core.client.JsArray;
032import com.google.gwt.dom.client.Document;
033import com.google.gwt.dom.client.InputElement;
034import com.google.gwt.event.dom.client.ChangeEvent;
035import com.google.gwt.event.dom.client.ChangeHandler;
036import com.google.gwt.event.dom.client.HasChangeHandlers;
037import com.google.gwt.event.shared.HandlerRegistration;
038import com.google.gwt.user.client.ui.CmsWidget;
039import com.google.gwt.user.client.ui.HasName;
040import com.google.gwt.user.client.ui.Widget;
041
042/**
043 * A file input field.<p>
044 *
045 * @since 8.0.0
046 */
047public class CmsFileInput extends CmsWidget implements HasName, HasChangeHandlers {
048
049    /** The concrete file input implementation. */
050    private I_CmsFileInputService m_impl;
051
052    /** The input element. */
053    private InputElement m_inputElement;
054
055    /**
056     * The default constructor.<p>
057     */
058    public CmsFileInput() {
059
060        m_inputElement = Document.get().createFileInputElement();
061        setElement(m_inputElement);
062        setStyleName("gwt-FileUpload");
063        m_impl = GWT.create(CmsFileInputImpl.class);
064    }
065
066    /**
067     * @see com.google.gwt.event.dom.client.HasChangeHandlers#addChangeHandler(com.google.gwt.event.dom.client.ChangeHandler)
068     */
069    @Override
070    public HandlerRegistration addChangeHandler(ChangeHandler handler) {
071
072        return addDomHandler(handler, ChangeEvent.getType());
073    }
074
075    /**
076     * Returns an array of CmsFile objects.<p>
077     *
078     * @return an array of CmsFile objects
079     */
080    public CmsFileInfo[] getFiles() {
081
082        JsArray<CmsFileInfo> files = m_impl.getFiles(m_inputElement);
083        CmsFileInfo[] result = new CmsFileInfo[files.length()];
084        for (int i = 0; i < files.length(); ++i) {
085            result[i] = files.get(i);
086        }
087        return result;
088    }
089
090    /**
091     * @see com.google.gwt.user.client.ui.HasName#getName()
092     */
093    @Override
094    public String getName() {
095
096        return m_inputElement.getName();
097    }
098
099    /**
100     * Returns <code>true</code> if multiple file selection is allowed, <code>false</code> otherwise.<p>
101     *
102     * @return <code>true</code> if multiple file selection is allowed, <code>false</code> otherwise
103     */
104    public boolean isAllowedMultipleFiles() {
105
106        return m_impl.isAllowMultipleFiles(m_inputElement);
107    }
108
109    /**
110     * Returns <code>true</code> if the input field is disabled <code>false</code> otherwise.<p>
111     *
112     * @return <code>true</code> if the input field is disabled <code>false</code> otherwise
113     */
114    public boolean isDisabled() {
115
116        return m_inputElement.isDisabled();
117    }
118
119    /**
120     * @see com.google.gwt.user.client.ui.Widget#onAttach()
121     */
122    @Override
123    public void onAttach() {
124
125        super.onAttach();
126    }
127
128    /**
129     * @see com.google.gwt.user.client.ui.Widget#onDetach()
130     */
131    @Override
132    public void onDetach() {
133
134        super.onDetach();
135    }
136
137    /**
138     * Sets the 'accept' attribute.
139     *
140     * @param acceptAttribute the new value for the 'accept' attribute
141     */
142    public void setAccept(String acceptAttribute) {
143
144        m_inputElement.setAttribute("accept", acceptAttribute);
145    }
146
147    /**
148     * Sets the the flag for allowing multiple file selection.<p>
149     *
150     * @param allow <code>true</code> if the multiple file selection should be allowed
151     */
152    public void setAllowMultipleFiles(boolean allow) {
153
154        m_impl.setAllowMultipleFiles(m_inputElement, allow);
155    }
156
157    /**
158     * Sets the disabled flag.<p>
159     *
160     * @param disabled <code>true</code> if the input field should be disabled
161     */
162    public void setDisabled(boolean disabled) {
163
164        m_inputElement.setDisabled(disabled);
165    }
166
167    /**
168     * @see com.google.gwt.user.client.ui.HasName#setName(java.lang.String)
169     */
170    @Override
171    public void setName(String name) {
172
173        m_inputElement.setName(name);
174    }
175
176    /**
177     * @see com.google.gwt.user.client.ui.CmsWidget#setParent(com.google.gwt.user.client.ui.Widget)
178     */
179    @Override
180    public void setParent(Widget parent) {
181
182        super.setParent(parent);
183    }
184
185    /**
186     * Returns <code>true</code> if the control supports the HTML5 FileAPI and <code>false</code> otherwise.<p>
187     *
188     * @return <code>true</code> if the control supports the HTML5 FileAPI and <code>false</code> otherwise
189     */
190    public boolean supportsFileAPI() {
191
192        return m_impl.supportsFileAPI();
193    }
194
195}