com.google.gwt.user.client.ui.FileUpload Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.servicemix.bundles.gwt-servlet
Show all versions of org.apache.servicemix.bundles.gwt-servlet
This OSGi bundle wraps ${pkgArtifactId} ${pkgVersion} jar file.
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.user.client.ui;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
/**
* A widget that wraps the HTML <input type='file'> element. This widget
* must be used with {@link com.google.gwt.user.client.ui.FormPanel} if it is to
* be submitted to a server.
*
*
*
Example
* {@example com.google.gwt.examples.FormPanelExample}
*
*
* CSS Style Rules
*
* - .gwt-FileUpload {}
*
* NOTICE about styling
*
* The developer should be aware that most browsers do not allow styling
* many properties of the rendered input-file element because of security restrictions.
* You can style certain properties like position, visibility, opacity, etc. But size,
* color, backgrounds etc. will not work either using css or calling widget methods like setSize().
*
*
*/
public class FileUpload extends FocusWidget implements HasName, HasChangeHandlers, HasEnabled {
/**
* Creates a FileUpload widget that wraps an existing <input
* type='file'> element.
*
* This element must already be attached to the document. If the element is
* removed from the document, you must call
* {@link RootPanel#detachNow(Widget)}.
*
* @param element the element to be wrapped
*/
public static FileUpload wrap(Element element) {
// Assert that the element is attached.
assert Document.get().getBody().isOrHasChild(element);
FileUpload fileUpload = new FileUpload(element);
// Mark it attached and remember it for cleanup.
fileUpload.onAttach();
RootPanel.detachOnWindowClose(fileUpload);
return fileUpload;
}
/**
* Constructs a new file upload widget.
*/
public FileUpload() {
this(Document.get().createFileInputElement());
setStyleName("gwt-FileUpload");
}
/**
* This constructor may be used by subclasses to explicitly use an existing
* element. This element must be an <input> element whose type is
* 'file'.
*
* @param element the element to be used
*/
protected FileUpload(Element element) {
assert InputElement.as(element).getType().equalsIgnoreCase("file");
setElement(element);
}
public HandlerRegistration addChangeHandler(ChangeHandler handler) {
return addDomHandler(handler, ChangeEvent.getType());
}
/**
* Gets the filename selected by the user. This property has no mutator, as
* browser security restrictions preclude setting it.
*
* @return the widget's filename
*/
public String getFilename() {
return getInputElement().getValue();
}
public String getName() {
return getInputElement().getName();
}
/**
* Gets whether this widget is enabled.
*
* @return true
if the widget is enabled
*/
public boolean isEnabled() {
return !getElement().getPropertyBoolean("disabled");
}
/**
* Sets whether this widget is enabled.
*
* @param enabled true
to enable the widget, false
* to disable it
*/
public void setEnabled(boolean enabled) {
getElement().setPropertyBoolean("disabled", !enabled);
}
public void setName(String name) {
getInputElement().setName(name);
}
private InputElement getInputElement() {
return getElement().cast();
}
/**
* Programmatic equivalent of the user clicking the button, opening
* the file selection browser.
*
*
* NOTE: in certain browsers programmatic click is disabled if the
* element display is none, for instance in webkit you have to move
* the element off screen.
*
*
*/
public void click() {
getInputElement().click();
}
}