/*
* Copyright 2009 Prime Technology.
*
* 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 org.primefaces.component.fileupload;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.el.ValueExpression;
import javax.el.MethodExpression;
import javax.faces.render.Renderer;
import java.io.IOException;
import org.primefaces.renderkit.PartialRenderer;
import org.primefaces.component.resource.Resource;
import javax.faces.component.UIComponent;
import javax.faces.event.ListenerFor;
import javax.faces.event.PostAddToViewEvent;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.event.AbortProcessingException;
import java.util.List;
import java.util.ArrayList;
import javax.faces.component.UINamingContainer;
@ListenerFor(systemEventClass = PostAddToViewEvent.class)
public class FileUpload extends UIComponentBase {
public static final String COMPONENT_TYPE = "org.primefaces.component.FileUpload";
public static final String COMPONENT_FAMILY = "org.primefaces.component";
private static final String DEFAULT_RENDERER = "org.primefaces.component.FileUploadRenderer";
private static final String OPTIMIZED_PACKAGE = "org.primefaces.component.";
protected enum PropertyKeys {
widgetVar
,update
,fileUploadListener
,multiple
,auto
,label
,image
,cancelImage
,width
,height
,allowTypes
,description
,sizeLimit
,wmode
,customUI
,style
,styleClass;
String toString;
PropertyKeys(String toString) {
this.toString = toString;
}
PropertyKeys() {}
public String toString() {
return ((this.toString != null) ? this.toString : super.toString());
}
}
public FileUpload() {
setRendererType(DEFAULT_RENDERER);
}
public String getFamily() {
return COMPONENT_FAMILY;
}
public java.lang.String getWidgetVar() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.widgetVar, null);
}
public void setWidgetVar(java.lang.String _widgetVar) {
getStateHelper().put(PropertyKeys.widgetVar, _widgetVar);
handleAttribute("widgetVar", _widgetVar);
}
public java.lang.String getUpdate() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.update, null);
}
public void setUpdate(java.lang.String _update) {
getStateHelper().put(PropertyKeys.update, _update);
handleAttribute("update", _update);
}
public javax.el.MethodExpression getFileUploadListener() {
return (javax.el.MethodExpression) getStateHelper().eval(PropertyKeys.fileUploadListener, null);
}
public void setFileUploadListener(javax.el.MethodExpression _fileUploadListener) {
getStateHelper().put(PropertyKeys.fileUploadListener, _fileUploadListener);
handleAttribute("fileUploadListener", _fileUploadListener);
}
public boolean isMultiple() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.multiple, false);
}
public void setMultiple(boolean _multiple) {
getStateHelper().put(PropertyKeys.multiple, _multiple);
handleAttribute("multiple", _multiple);
}
public boolean isAuto() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.auto, false);
}
public void setAuto(boolean _auto) {
getStateHelper().put(PropertyKeys.auto, _auto);
handleAttribute("auto", _auto);
}
public java.lang.String getLabel() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.label, null);
}
public void setLabel(java.lang.String _label) {
getStateHelper().put(PropertyKeys.label, _label);
handleAttribute("label", _label);
}
public java.lang.String getImage() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.image, null);
}
public void setImage(java.lang.String _image) {
getStateHelper().put(PropertyKeys.image, _image);
handleAttribute("image", _image);
}
public java.lang.String getCancelImage() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.cancelImage, null);
}
public void setCancelImage(java.lang.String _cancelImage) {
getStateHelper().put(PropertyKeys.cancelImage, _cancelImage);
handleAttribute("cancelImage", _cancelImage);
}
public java.lang.String getWidth() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.width, null);
}
public void setWidth(java.lang.String _width) {
getStateHelper().put(PropertyKeys.width, _width);
handleAttribute("width", _width);
}
public java.lang.String getHeight() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.height, null);
}
public void setHeight(java.lang.String _height) {
getStateHelper().put(PropertyKeys.height, _height);
handleAttribute("height", _height);
}
public java.lang.String getAllowTypes() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.allowTypes, null);
}
public void setAllowTypes(java.lang.String _allowTypes) {
getStateHelper().put(PropertyKeys.allowTypes, _allowTypes);
handleAttribute("allowTypes", _allowTypes);
}
public java.lang.String getDescription() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.description, null);
}
public void setDescription(java.lang.String _description) {
getStateHelper().put(PropertyKeys.description, _description);
handleAttribute("description", _description);
}
public java.lang.Long getSizeLimit() {
return (java.lang.Long) getStateHelper().eval(PropertyKeys.sizeLimit, java.lang.Long.MAX_VALUE);
}
public void setSizeLimit(java.lang.Long _sizeLimit) {
getStateHelper().put(PropertyKeys.sizeLimit, _sizeLimit);
handleAttribute("sizeLimit", _sizeLimit);
}
public java.lang.String getWmode() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.wmode, null);
}
public void setWmode(java.lang.String _wmode) {
getStateHelper().put(PropertyKeys.wmode, _wmode);
handleAttribute("wmode", _wmode);
}
public boolean isCustomUI() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.customUI, false);
}
public void setCustomUI(boolean _customUI) {
getStateHelper().put(PropertyKeys.customUI, _customUI);
handleAttribute("customUI", _customUI);
}
public java.lang.String getStyle() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.style, null);
}
public void setStyle(java.lang.String _style) {
getStateHelper().put(PropertyKeys.style, _style);
handleAttribute("style", _style);
}
public java.lang.String getStyleClass() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.styleClass, null);
}
public void setStyleClass(java.lang.String _styleClass) {
getStateHelper().put(PropertyKeys.styleClass, _styleClass);
handleAttribute("styleClass", _styleClass);
}
private String inputFileId;
public void broadcast(javax.faces.event.FacesEvent event) throws javax.faces.event.AbortProcessingException {
super.broadcast(event);
FacesContext facesContext = FacesContext.getCurrentInstance();
MethodExpression me = getFileUploadListener();
if (me != null) {
me.invoke(facesContext.getELContext(), new Object[] {event});
}
}
String getInputFileId(FacesContext facesContext) {
if(inputFileId == null) {
inputFileId = this.getClientId(facesContext).replaceAll(String.valueOf(UINamingContainer.getSeparatorChar(facesContext)), "_") + "_file";
}
return inputFileId;
}
protected FacesContext getFacesContext() {
return FacesContext.getCurrentInstance();
}
public boolean resourceExists(FacesContext facesContext, String name) {
java.util.List<UIComponent> resources = facesContext.getViewRoot().getComponentResources(facesContext, "head");
for(UIComponent component : resources) {
String value = component.toString();
if(value != null && value.equals(name))
return true;
}
return false;
}
@Override
public void processEvent(ComponentSystemEvent event) throws AbortProcessingException {
FacesContext facesContext = getFacesContext();
javax.faces.component.UIViewRoot viewroot = facesContext.getViewRoot();
Resource resource = null;
if(!resourceExists(facesContext, "/jquery/plugins/uploadify/uploadify.css")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/jquery/plugins/uploadify/uploadify.css");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/yui/utilities/utilities.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/yui/utilities/utilities.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/yui/cookie/cookie-min.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/yui/cookie/cookie-min.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/jquery/jquery.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/jquery/jquery.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/jquery/plugins/uploadify/swfobject.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/jquery/plugins/uploadify/swfobject.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/jquery/plugins/uploadify/jquery.uploadify.v2.1.0.min.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/jquery/plugins/uploadify/jquery.uploadify.v2.1.0.min.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/primefaces/core/core.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/primefaces/core/core.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/primefaces/uploader/uploader.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/primefaces/uploader/uploader.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
}
public void handleAttribute(String name, Object value) {
List<String> setAttributes = (List<String>) this.getAttributes().get("javax.faces.component.UIComponentBase.attributesThatAreSet");
if(setAttributes == null) {
String cname = this.getClass().getName();
if(cname != null && cname.startsWith(OPTIMIZED_PACKAGE)) {
setAttributes = new ArrayList<String>(6);
this.getAttributes().put("javax.faces.component.UIComponentBase.attributesThatAreSet", setAttributes);
}
}
if(setAttributes != null) {
if(value == null) {
ValueExpression ve = getValueExpression(name);
if(ve == null) {
setAttributes.remove(name);
} else if(!setAttributes.contains(name)) {
setAttributes.add(name);
}
}
}
}
}