Author: andrei_exadel
Date: 2008-02-22 08:09:42 -0500 (Fri, 22 Feb 2008)
New Revision: 6285
Removed:
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/ProgressData.java
Modified:
trunk/sandbox/ui/fileUpload/src/main/config/component/fileUpload.xml
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/UIFileUpload.java
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/renderkit/FileUploadRendererBase.java
trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js
trunk/sandbox/ui/fileUpload/src/main/templates/org/richfaces/fileUpload.jspx
Log:
accepted file types added & refactoring
Modified: trunk/sandbox/ui/fileUpload/src/main/config/component/fileUpload.xml
===================================================================
--- trunk/sandbox/ui/fileUpload/src/main/config/component/fileUpload.xml 2008-02-22
13:03:36 UTC (rev 6284)
+++ trunk/sandbox/ui/fileUpload/src/main/config/component/fileUpload.xml 2008-02-22
13:09:42 UTC (rev 6285)
@@ -204,6 +204,13 @@
<defaultvalue>false</defaultvalue>
</property>
<property>
+ <name>acceptedTypes</name>
+ <classname>java.lang.String</classname>
+ <description>
+ Files types allowed to upload
+ </description>
+ </property>
+ <property>
<name>onupload</name>
<classname>java.lang.String</classname>
</property>
Deleted:
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/ProgressData.java
===================================================================
---
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/ProgressData.java 2008-02-22
13:03:36 UTC (rev 6284)
+++
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/ProgressData.java 2008-02-22
13:09:42 UTC (rev 6285)
@@ -1,228 +0,0 @@
-/*
- * ProgressData.java Date created: 09.02.2008
- * Last modified by: $Author$
- * $Revision$ $Date$
- */
-
-package org.richfaces.org.jboss.seam.ui.component;
-
-import java.math.BigDecimal;
-
-/**
- * Provides progress data during file uploading progress
- *
- * @author "Andrey Markavtsov"
- *
- */
-public class ProgressData {
-
- private Integer SCALE = 2;
-
- /** Actual file size */
- private Long size = 0L;
-
- /** Seconds after process started */
- private Long time = 0L;
-
- /** Uploaded bytes count */
- private Long uploaded = 0L;
-
- /** Percent completed */
- private Double percent = 0.0;
-
- private Object scaleNumber(Double d) {
- if (d != null) {
- BigDecimal decimal = new BigDecimal(d);
- decimal = decimal.setScale(SCALE, 0);
- return decimal;
- }
- return null;
- }
-
- /**
- * Gets minutes of process
- *
- * @return
- */
- public String getMm() {
- Long mm = time / 60;
- if (mm != null)
- return String.valueOf(mm.intValue());
- return null;
- }
-
- /**
- * Gets seconds of process
- *
- * @return
- */
- public String getSs() {
- Long ss = time % 60;
- if (ss != null)
- return String.valueOf(ss.intValue());
- return null;
- }
-
- /**
- * Gets hours of process
- *
- * @return
- */
- public String getHh() {
- Long ss = time / 3600;
- if (ss != null)
- return String.valueOf(ss.intValue());
- return null;
- }
-
- /**
- * Gets days of process
- *
- * @return
- */
- public String getDd() {
- Long ss = time / 3600 * 24;
- if (ss != null)
- return String.valueOf(ss.intValue());
- return null;
- }
-
- /**
- * Gets uploaded bytes count
- *
- * @return
- */
- public Object getUploadedB() {
- return getUploaded();
- }
-
- /**
- * Gets uploaded kilo bytes count
- *
- * @return
- */
- public Object getUploadedKB() {
- Double kb = getUploaded() / 1024.0;
- return scaleNumber(kb);
- }
-
- /**
- * Gets uploaded mega bytes count
- *
- * @return
- */
- public Object getUploadedMB() {
- Double mb = getUploaded() / (1024.0 * 1024.0);
- return scaleNumber(mb);
- }
-
- /**
- * Gets uploaded giga bytes count
- *
- * @return
- */
- public Object getUploadedGB() {
- Double gb = getUploaded() / (1024.0 * 1024.0 * 1024.0);
- return scaleNumber(gb);
- }
-
- /**
- * Gets file size in bytes
- *
- * @return
- */
- public Object getB() {
- return size;
- }
-
- /**
- * Gets file size in kilo bytes
- *
- * @return
- */
- public Object getKB() {
- Double kb = size / 1024.0;
- return scaleNumber(kb);
- }
-
- /**
- * Gets file size in mega bytes
- *
- * @return
- */
- public Object getMB() {
- Double mb = size / (1024.0 * 1024.0);
- return scaleNumber(mb);
- }
-
- /**
- * Gets file size in giga bytes
- *
- * @return
- */
- public Object getGB() {
- Double gb = size / (1024.0 * 1024.0 * 1024.0);
- return scaleNumber(gb);
- }
-
- /**
- * @return the size
- */
- public Long getSize() {
- return size;
- }
-
- /**
- * @param size
- * the size to set
- */
- public void setSize(Long size) {
- this.size = size;
- }
-
- /**
- * @return the time
- */
- public Long getTime() {
- return time;
- }
-
- /**
- * @param time
- * the time to set
- */
- public void setTime(Long time) {
- this.time = time;
- }
-
- /**
- * @return the uploaded
- */
- public Long getUploaded() {
- return uploaded;
- }
-
- /**
- * @param uploaded
- * the uploaded to set
- */
- public void setUploaded(Long uploaded) {
- this.uploaded = uploaded;
- }
-
- /**
- * @return the percent
- */
- public Object getPercent() {
- return scaleNumber(percent);
- }
-
- /**
- * @param percent
- * the percent to set
- */
- public void setPercent(Double percent) {
- this.percent = percent;
- }
-
-}
Modified:
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/UIFileUpload.java
===================================================================
---
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/UIFileUpload.java 2008-02-22
13:03:36 UTC (rev 6284)
+++
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/UIFileUpload.java 2008-02-22
13:09:42 UTC (rev 6285)
@@ -1,16 +1,23 @@
package org.richfaces.org.jboss.seam.ui.component;
import java.io.InputStream;
+import java.util.Map;
import javax.el.MethodExpression;
import javax.faces.component.UIInput;
+import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.faces.event.FacesEvent;
+import javax.servlet.http.HttpServletRequest;
+import org.ajax4jsf.context.AjaxContext;
+import org.ajax4jsf.context.AjaxContextImpl;
import org.richfaces.event.FileUploadListener;
import org.richfaces.event.UploadEvent;
+import org.richfaces.org.jboss.seam.web.MultipartFilter;
+import org.richfaces.org.jboss.seam.web.MultipartRequest;
/**
* JSF component class
@@ -152,7 +159,27 @@
binding.invoke(facesContext.getELContext(), new Object[] { e });
}
- }
+ }else {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ ExternalContext externalContext = facesContext.getExternalContext();
+ HttpServletRequest request = (HttpServletRequest)externalContext.getRequest();
+ if ("progress".equals(request.getParameter("action"))) {
+ AjaxContext ajaxContext = AjaxContextImpl
+ .getCurrentInstance(facesContext);
+ String uid = request.getParameter("uid");
+ Map<String, Object> sessionMap = externalContext.getSessionMap();
+
+ Map<String, MultipartRequest> sessions = (Map<String,
MultipartRequest>) sessionMap
+ .get(MultipartFilter.REQUESTS_SESSIONS_BEAN_NAME);
+ if (sessions != null) {
+ MultipartRequest multipartRequest = sessions.get(uid);
+ if (multipartRequest != null) {
+ ajaxContext.setResponseData(multipartRequest.getSize());
+ }
+ }
+ }
+ }
+
}
}
Modified:
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/renderkit/FileUploadRendererBase.java
===================================================================
---
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/renderkit/FileUploadRendererBase.java 2008-02-22
13:03:36 UTC (rev 6284)
+++
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/renderkit/FileUploadRendererBase.java 2008-02-22
13:09:42 UTC (rev 6285)
@@ -4,25 +4,24 @@
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.util.HashMap;
import java.util.Map;
import javax.el.ValueExpression;
import javax.faces.FactoryFinder;
import javax.faces.component.UIComponent;
-import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.RenderKit;
import javax.faces.render.RenderKitFactory;
import javax.servlet.ServletRequest;
-import org.ajax4jsf.context.AjaxContext;
-import org.ajax4jsf.context.AjaxContextImpl;
import org.ajax4jsf.event.AjaxEvent;
import org.ajax4jsf.javascript.JSFunction;
import org.ajax4jsf.javascript.JSFunctionDefinition;
import org.ajax4jsf.javascript.JSLiteral;
import org.ajax4jsf.javascript.JSReference;
+import org.ajax4jsf.javascript.ScriptUtils;
import org.ajax4jsf.renderkit.AjaxRendererUtils;
import org.ajax4jsf.renderkit.RendererUtils;
import org.ajax4jsf.resource.CountingOutputWriter;
@@ -54,33 +53,9 @@
ServletRequest request = (ServletRequest) context.getExternalContext()
.getRequest();
-
String clientId = component.getClientId(context);
- Map<String, String[]> params = context.getExternalContext()
- .getRequestParameterValuesMap();
-
- if ("progress".equals(request.getParameter("action"))) {
- AjaxContext ajaxContext = AjaxContextImpl
- .getCurrentInstance(context);
- String uid = request.getParameter("uid");
- ExternalContext externalContext = context.getExternalContext();
- Map<String, Object> sessionMap = externalContext.getSessionMap();
-
- Map<String, MultipartRequest> sessions = (Map<String, MultipartRequest>)
sessionMap
- .get(MultipartFilter.REQUESTS_SESSIONS_BEAN_NAME);
- if (sessions != null) {
- MultipartRequest multipartRequest = sessions.get(uid);
- if (multipartRequest != null) {
- ajaxContext.setResponseData(multipartRequest.getSize());
- }
- }
- } else {
- new AjaxEvent(component).queue();
- }
- if (!(request instanceof MultipartRequest)) {
- request = unwrapMultipartRequest(request);
- }
+ new AjaxEvent(component).queue();
if (request instanceof MultipartRequest) {
MultipartRequest multipartRequest = (MultipartRequest) request;
@@ -91,7 +66,7 @@
.getFileContentType(clientId));
fileUpload.setLocalFileName(multipartRequest.getFileName(clientId));
fileUpload.setLocalFileSize(multipartRequest.getFileSize(clientId));
- new UploadEvent(component, multipartRequest.getFile(clientId)).queue();
+ new UploadEvent(component, multipartRequest.getFile(clientId),
multipartRequest.getFileName(clientId)).queue();
onUploadComplete(context, multipartRequest.getFile(clientId),
fileUpload);
}
@@ -169,6 +144,27 @@
return function.toScript();
}
+
+ /**
+ * Return accepted types map
+ * @param context
+ * @param component
+ * @return
+ */
+ public Object getAcceptedTypes(FacesContext context, UIComponent component) {
+ String acceptedTypes =
(String)component.getAttributes().get("acceptedTypes");
+ if (acceptedTypes != null) {
+ Map<String, Boolean> accepted = new HashMap<String, Boolean>();
+ String [] types = acceptedTypes.split("[,;]");
+ if (types != null) {
+ for (String type : types) {
+ accepted.put(type.toLowerCase(), true);
+ }
+ return ScriptUtils.toScript(accepted);
+ }
+ }
+ return JSReference.NULL;
+ }
/**
* Generates JS script for stopping uploading process
Modified:
trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js
===================================================================
---
trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js 2008-02-22
13:03:36 UTC (rev 6284)
+++
trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js 2008-02-22
13:09:42 UTC (rev 6285)
@@ -301,10 +301,10 @@
if (error) {
this.stop();
- this.onload();
+ this.onerror();
} else if (loaded) {
this.stop();
- this.onerror();
+ this.onload();
}
}
}.bind(this), 200);
@@ -371,7 +371,7 @@
maxFileBatchSize: null,
- initialize: function(id, stopScript, getFileSizeScript, progressBarId, classes, label,
maxFiles, events, disabled, options) {
+ initialize: function(id, stopScript, getFileSizeScript, progressBarId, classes, label,
maxFiles, events, disabled, acceptedTypes, options) {
this.id = id;
this.element = $(this.id);
this._progressBar = $(progressBarId);
@@ -381,6 +381,7 @@
this.disabled = disabled;
this.element.component = this;
+ this.acceptedTypes = acceptedTypes;
this.stopScript = stopScript;
this.getFileSizeScript = getFileSizeScript;
@@ -434,9 +435,22 @@
setupAutoUpload: function() {
this.runUpload = this.options.autoUpload;
},
+
+ checkFileType: function (fileName) {
+ if (!this.acceptedTypes || this.acceptedTypes['*']) { return true; }
+ if (/(?:\S+)\.(\S+)$/.test(fileName)) {
+ var type = RegExp.$1;
+ type = type.toLowerCase();
+ if (this.acceptedTypes[type]) {
+ return true;
+ }
+ }
+ return false;
+ },
add: function(elt) {
if (this.disabled) return;
+ if (!this.checkFileType(elt.value)) return;
var newEntry = new FileUploadEntry(elt, this);
this.entries.push(newEntry);
Modified: trunk/sandbox/ui/fileUpload/src/main/templates/org/richfaces/fileUpload.jspx
===================================================================
---
trunk/sandbox/ui/fileUpload/src/main/templates/org/richfaces/fileUpload.jspx 2008-02-22
13:03:36 UTC (rev 6284)
+++
trunk/sandbox/ui/fileUpload/src/main/templates/org/richfaces/fileUpload.jspx 2008-02-22
13:09:42 UTC (rev 6285)
@@ -158,7 +158,7 @@
onerror : #{this:getAsEventHandler(context, component, "onerror")}
};
- new FileUpload('#{clientId}', #{this:getStopScript(context, component)},
#{this:getFileSizeScript(context, component)} ,'#{this:getProgressBarId(context,
component)}', FileUpload.CLASSES, #{this:getLabelMarkup(context, component)},
'#{component.attributes["maxFilesQuantity"]}', events,
#{component.attributes["disabled"]});
+ new FileUpload('#{clientId}', #{this:getStopScript(context, component)},
#{this:getFileSizeScript(context, component)} ,'#{this:getProgressBarId(context,
component)}', FileUpload.CLASSES, #{this:getLabelMarkup(context, component)},
'#{component.attributes["maxFilesQuantity"]}', events,
#{component.attributes["disabled"]}, #{this:getAcceptedTypes(context,
component)});
</script>
</span>