Author: andrei_exadel
Date: 2008-02-20 11:44:12 -0500 (Wed, 20 Feb 2008)
New Revision: 6216
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/web/MultipartRequest.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:
label attribute customization
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-20
16:26:10 UTC (rev 6215)
+++
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/renderkit/FileUploadRendererBase.java 2008-02-20
16:44:12 UTC (rev 6216)
@@ -7,19 +7,25 @@
import java.util.Map;
import javax.el.ValueExpression;
+import javax.faces.FactoryFinder;
import javax.faces.component.UIComponent;
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 javax.servlet.http.HttpSession;
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.renderkit.AjaxRendererUtils;
import org.ajax4jsf.renderkit.RendererUtils;
+import org.ajax4jsf.resource.CountingOutputWriter;
import org.richfaces.component.UIProgressBar;
import org.richfaces.event.UploadEvent;
import org.richfaces.org.jboss.seam.ui.component.UIFileUpload;
@@ -63,14 +69,23 @@
Map<String, String[]> params = context.getExternalContext()
.getRequestParameterValuesMap();
- if (params.containsKey("percent")) {
+ if ("progress".equals(request.getParameter("action"))) {
AjaxContext ajaxContext = AjaxContextImpl.getCurrentInstance(context);
- ajaxContext.setResponseData("safasfas");
+ String uid = request.getParameter("uid");
+ HttpSession session = (HttpSession)context.getExternalContext().getSession(false);
+ Map<String, MultipartRequest> sessions = (Map<String, MultipartRequest>)
session
+ .getAttribute("_richfaces_upload_sessions");
+
+ if (sessions != null) {
+ MultipartRequest multipartRequest = sessions.get(uid);
+ if (multipartRequest != null) {
+ ajaxContext.setResponseData(multipartRequest.getSize());
+ }
+ }
+
+ }else {
+ new AjaxEvent(component).queue();
}
-//
-// AjaxContext ajaxContext = AjaxContextImpl.getCurrentInstance(context);
-
- new AjaxEvent(component).queue();
if (!(request instanceof MultipartRequest)) {
request = unwrapMultipartRequest(request);
}
@@ -123,15 +138,17 @@
// TODO Auto-generated method stub
return null;
}
-
- public String getStopScript(FacesContext context, UIComponent component)
- throws IOException {
-
+
+ private String getActionScript(FacesContext context, UIComponent component,
+ String action, Object oncomplete) throws IOException {
JSFunction ajaxFunction = AjaxRendererUtils.buildAjaxFunction(
component, context);
Map options = AjaxRendererUtils.buildEventOptions(context, component);
- ((Map) options.get("parameters")).put("action", "stop");
+ ((Map) options.get("parameters")).put("action", action);
((Map) options.get("parameters")).put("uid", new
JSReference("uid"));
+ if (oncomplete != null) {
+ options.put("onbeforedomupdate", oncomplete);
+ }
ajaxFunction.addParameter(options);
JSFunctionDefinition function = new JSFunctionDefinition("uid");
@@ -141,6 +158,63 @@
return function.toScript();
}
+ public String getStopScript(FacesContext context, UIComponent component)
+ throws IOException {
+ return getActionScript(context, component, "stop", null);
+ }
+
+ // TODO : complete this one
+ public String getFileSizeScript(FacesContext context, UIComponent component)
+ throws IOException {
+ JSFunctionDefinition oncomplete = new JSFunctionDefinition();
+ oncomplete.addParameter("request");
+ oncomplete.addParameter("event");
+ oncomplete.addParameter("data");
+ StringBuffer body = new StringBuffer("$('");
+ body.append(component.getClientId(context));
+ body.append("').component.getFileSize(data);");
+ oncomplete.addToBody(body);
+ return getActionScript(context, component, "progress", oncomplete);
+
+ }
+
+ public Object getLabelMarkup(FacesContext context, UIComponent component)
+ throws IOException {
+ CountingOutputWriter customWriter = new CountingOutputWriter();
+ StringBuffer result = null;
+ UIComponent label = component.getFacet("label");
+ try {
+ if (label != null) {
+
+ ResponseWriter writer = context.getResponseWriter();
+
+ String defaultRenderKitId = context.getApplication()
+ .getDefaultRenderKitId();
+ if (null == defaultRenderKitId) {
+ defaultRenderKitId = RenderKitFactory.HTML_BASIC_RENDER_KIT;
+ }
+ RenderKitFactory renderKitFactory = (RenderKitFactory) FactoryFinder
+ .getFactory(FactoryFinder.RENDER_KIT_FACTORY);
+ RenderKit renderKit = renderKitFactory.getRenderKit(context,
+ defaultRenderKitId);
+
+ ResponseWriter responseWriter = renderKit.createResponseWriter(
+ customWriter, null, "UTF-8");
+ context.setResponseWriter(responseWriter);
+ //renderChild(context, label);
+ writeScriptBody(context, label, false);
+ if (writer != null) {
+ context.setResponseWriter(writer);
+ }
+ result = customWriter.getContent();
+ }
+
+ } catch (Exception e) {
+ e.getMessage();
+ }
+ return (result != null) ? new JSLiteral(result.toString()) : null;
+ }
+
/**
* Finds an instance of MultipartRequest wrapped within a request or its
* (recursively) wrapped requests.
Modified:
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartRequest.java
===================================================================
---
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartRequest.java 2008-02-20
16:26:10 UTC (rev 6215)
+++
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartRequest.java 2008-02-20
16:44:12 UTC (rev 6216)
@@ -244,7 +244,7 @@
private long time;
- private ProgressData progressData = null;
+ //private ProgressData progressData = null;
private boolean shouldStop = false;
public MultipartRequest(HttpServletRequest request,
@@ -255,7 +255,7 @@
String contentLength = request.getHeader("Content-Length");
this.contentLength = Integer.parseInt(contentLength);
- initProgressInfo(this.contentLength);
+ //initProgressInfo(this.contentLength);
if (contentLength != null && maxRequestSize > 0
&& this.contentLength > maxRequestSize) {
throw new FileUploadException(
@@ -442,14 +442,14 @@
private void fillProgressInfo () {
Double percent = (Double) (100.0 * this.read / this.contentLength);
getSession().setAttribute(FileUploadRendererBase._percentBeanName,percent);
- if (this.progressData != null) {
+ /*if (this.progressData != null) {
progressData.setPercent(percent);
progressData.setUploaded((long)this.read);
progressData.setTime((new Date().getTime() / 1000)- this.time);
- }
+ }*/
}
- private void initProgressInfo(int size) {
+ /* private void initProgressInfo(int size) {
time = new Date().getTime()/1000;
progressData = getProgressData();
if (progressData != null) {
@@ -458,6 +458,7 @@
}
}
+ */
private byte[] getBoundaryMarker(String contentType) {
Map<String, Object> params = parseParams(contentType, ";");
@@ -546,6 +547,11 @@
parseRequest();
return parameters.get(name);
}
+
+
+ public Integer getSize() {
+ return contentLength;
+ }
@Override
public Enumeration getParameterNames() {
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-20
16:26:10 UTC (rev 6215)
+++
trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js 2008-02-20
16:44:12 UTC (rev 6216)
@@ -138,6 +138,23 @@
this.uploadObject.prepareProgressBar();
},
+ setupLabelUpdate: function () {
+ this.labelUpdateInterval = setInterval(function () { this.updateLabel(); }.bind(this),
this.uploadObject.progressBar.options['pollinterval']);
+ },
+
+ updateLabel: function () {
+ if (this.state != FileUploadEntry.UPLOAD_IN_PROGRESS) {
+ clearInterval(this.labelUpdateInterval);
+ }else {
+ var p = this.uploadObject.progressBar.getValue();
+ if (p) {
+ var content = this.uploadObject.labelMarkup.invoke('getContent',
this.uploadObject.progressData.getContext(p)).join('');
+ this.statusLabel.innerHTML = content;
+
+ }
+ }
+ },
+
finishProgressBar: function () {
this.uploadObject.finishProgressBar();
},
@@ -195,6 +212,61 @@
});
+ProgressData = Class.create();
+Object.extend(ProgressData.prototype, {
+ size: null,
+
+ startTime: null,
+
+ initialize: function(size) {
+ this.size = size;
+ this.startTime = parseInt((new Date().getTime())/1000);
+ },
+
+ ss: function () {
+ return parseInt((this.time - this.startTime) % 60) + "";
+ },
+
+ mm: function () {
+ return parseInt((this.time - this.startTime)/60)+ "";
+ },
+
+ hh: function () {
+ return parseInt((this.time - this.startTime)/3600) + "";
+ },
+
+ B: function () {
+ return this.size;
+ },
+
+ KB: function () {
+ return parseInt(this.size/1024);
+ },
+
+ MB: function () {
+ return parseInt(this.size/(1024*1024));
+ },
+
+ getContext: function (p) {
+ var context = {};
+ this.time = parseInt((new Date().getTime())/1000);
+ context['B'] = this.B();
+ context['KB'] = this.KB();
+ context['MB'] = this.MB();
+ context['ss'] = this.ss();
+ context['mm'] = this.mm();
+ context['hh'] = this.hh();
+ var s = this.size;
+ this.size = (this.size * p)/100;
+ context['_B'] = this.B();
+ context['_KB'] = this.KB();
+ context['_MB'] = this.MB();
+ this.size = s;
+ return context;
+ }
+
+});
+
LoadWatcher = Class.create();
Object.extend(LoadWatcher.prototype, {
initialize: function(iframe, callback) {
@@ -290,16 +362,19 @@
maxFileBatchSize: 5,
- initialize: function(id, stopScript, progressBarId, classes, options) {
+ initialize: function(id, stopScript, getFileSizeScript, progressBarId, classes, label,
options) {
this.id = id;
this.element = $(this.id);
//this.progressBarId = progressBarId;
this._progressBar = $(progressBarId);
this.progressBar = this._progressBar.component;
+ this.labelMarkup = label;
+
this.element.component = this;
this.stopScript = stopScript;
+ this.getFileSizeScript = getFileSizeScript;
this.iframe = $(this.id + "_iframe");
this.items = $(this.id + ":fileItems");
@@ -309,6 +384,19 @@
this.options = options || {};
this.setupAutoUpload();
},
+
+ getFileSize: function (data) {
+ if (data) {
+ var progressData = new ProgressData(data);
+ this.progressData = progressData;
+ if (this.activeEntry) {
+ this.activeEntry.setupLabelUpdate();
+ }
+ }else {
+ if (this.activeEntry)
+ this.getFileSizeScript(this.activeEntry.uid);
+ }
+ },
prepareProgressBar: function () {
this.progressBar.setValue(0);
@@ -637,6 +725,8 @@
parentForm.target = oldTarget;
parentForm.encoding = oldEncoding;
parentForm.enctype = oldEnctype;
+ if (this.labelMarkup)
+ this.getFileSizeScript(entry.uid);
}
}
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-20
16:26:10 UTC (rev 6215)
+++
trunk/sandbox/ui/fileUpload/src/main/templates/org/richfaces/fileUpload.jspx 2008-02-20
16:44:12 UTC (rev 6216)
@@ -123,7 +123,7 @@
}
};
- new FileUpload('#{clientId}', #{this:getStopScript(context, component)},
'#{this:getProgressBarId(context, component)}', FileUpload.CLASSES);
+ new FileUpload('#{clientId}', #{this:getStopScript(context, component)},
#{this:getFileSizeScript(context, component)} ,'#{this:getProgressBarId(context,
component)}', FileUpload.CLASSES, #{this:getLabelMarkup(context, component)});
</script>
</span>