You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by sv...@apache.org on 2011/07/04 20:11:47 UTC

svn commit: r1142754 - in /wicket/trunk: wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/ wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/

Author: svenmeier
Date: Mon Jul  4 18:11:46 2011
New Revision: 1142754

URL: http://svn.apache.org/viewvc?rev=1142754&view=rev
Log:
WICKET-3310: use i18n messages for progress, simplified javascript

Added:
    wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.properties   (with props)
    wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource_de.properties   (with props)
    wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource_fr.properties   (with props)
Modified:
    wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/UploadInfo.java
    wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java
    wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.java
    wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/progressbar.js

Modified: wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/UploadInfo.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/UploadInfo.java?rev=1142754&r1=1142753&r2=1142754&view=diff
==============================================================================
--- wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/UploadInfo.java (original)
+++ wicket/trunk/wicket-core/src/main/java/org/apache/wicket/protocol/http/servlet/UploadInfo.java Mon Jul  4 18:11:46 2011
@@ -16,6 +16,8 @@
  */
 package org.apache.wicket.protocol.http.servlet;
 
+import java.util.Locale;
+
 import org.apache.wicket.IClusterable;
 import org.apache.wicket.util.lang.Bytes;
 import org.apache.wicket.util.time.Duration;
@@ -63,19 +65,23 @@ public class UploadInfo implements IClus
 	}
 
 	/**
+	 * @param locale
+	 *            locale for formatting
 	 * @return human readable string of bytes uploaded so far
 	 */
-	public String getBytesUploadedString()
+	public String getBytesUploadedString(Locale locale)
 	{
-		return Bytes.bytes(bytesUploaded).toString();
+		return Bytes.bytes(bytesUploaded).toString(locale);
 	}
 
 	/**
+	 * @param locale
+	 *            locale for formatting
 	 * @return human readable string of total number of bytes
 	 */
-	public String getTotalBytesString()
+	public String getTotalBytesString(Locale locale)
 	{
-		return Bytes.bytes(totalBytes).toString();
+		return Bytes.bytes(totalBytes).toString(locale);
 	}
 
 	/**
@@ -112,11 +118,13 @@ public class UploadInfo implements IClus
 	}
 
 	/**
+	 * @param locale
+	 *            locale for formatting
 	 * @return transfer rate in a human readable string
 	 */
-	public String getTransferRateString()
+	public String getTransferRateString(Locale locale)
 	{
-		return Bytes.bytes(getTransferRateBPS()).toString() + "/s";
+		return Bytes.bytes(getTransferRateBPS()).toString(locale) + "/s";
 	}
 
 	/**
@@ -147,11 +155,13 @@ public class UploadInfo implements IClus
 	}
 
 	/**
+	 * @param locale
+	 *            locale for formatting
 	 * @return estimate of the remaining time in a human readable string
 	 */
-	public String getRemainingTimeString()
+	public String getRemainingTimeString(Locale locale)
 	{
-		return Duration.milliseconds(getRemainingMilliseconds()).toString();
+		return Duration.milliseconds(getRemainingMilliseconds()).toString(locale);
 	}
 
 

Modified: wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java?rev=1142754&r1=1142753&r2=1142754&view=diff
==============================================================================
--- wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java (original)
+++ wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadProgressBar.java Mon Jul  4 18:11:46 2011
@@ -16,8 +16,7 @@
  */
 package org.apache.wicket.extensions.ajax.markup.html.form.upload;
 
-import java.io.ByteArrayOutputStream;
-import java.io.PrintStream;
+import java.util.Formatter;
 
 import org.apache.wicket.Application;
 import org.apache.wicket.Component;
@@ -58,6 +57,10 @@ import org.slf4j.LoggerFactory;
  * </code>
  * </pre>
  * 
+ * Implementation detail: Despite being located in an Ajax package, the progress communication is
+ * not done via Ajax but with an IFrame instead due to a bug in Webkit based browsers, see
+ * WICKET-3202.
+ * 
  * @author Andrew Lombardi
  */
 public class UploadProgressBar extends Panel
@@ -84,7 +87,7 @@ public class UploadProgressBar extends P
 		@Override
 		public String toString()
 		{
-			return "Ajax UploadProgressBar initializer";
+			return "UploadProgressBar initializer";
 		}
 
 		/** {@inheritDoc} */
@@ -133,21 +136,23 @@ public class UploadProgressBar extends P
 	 *            component id (not null)
 	 * @param form
 	 *            form that is submitted (not null)
-	 * @param fileUploadField
+	 * @param uploadField
 	 *            the file upload field to check for a file upload, or null to display the upload
 	 *            field for every submit of the given form
 	 */
-	public UploadProgressBar(final String id, final Form<?> form,
-		final FileUploadField fileUploadField)
+	public UploadProgressBar(final String id, final Form<?> form, final FileUploadField uploadField)
 	{
 		super(id);
-		uploadField = fileUploadField;
-		this.form = form;
-		form.setOutputMarkupId(true);
-		if (fileUploadField != null)
+
+		this.uploadField = uploadField;
+		if (uploadField != null)
 		{
-			fileUploadField.setOutputMarkupId(true);
+			uploadField.setOutputMarkupId(true);
 		}
+
+		this.form = form;
+		form.setOutputMarkupId(true);
+
 		setRenderBodyOnly(true);
 
 		barDiv = new WebMarkupContainer("bar");
@@ -199,14 +204,14 @@ public class UploadProgressBar extends P
 
 		final String uploadFieldId = (uploadField == null) ? "" : uploadField.getMarkupId();
 
-		ByteArrayOutputStream out = new ByteArrayOutputStream();
-		PrintStream js = new PrintStream(out);
-
-		js.printf("  var def=new Wicket.WUPB.Def('%s', '%s', '%s', '%s','%s');", getMarkupId(),
-			statusDiv.getMarkupId(), barDiv.getMarkupId(), urlFor(ref, null), uploadFieldId);
-		js.printf(" new Wicket.WUPB(def).bind('%s');", getCallbackForm().getMarkupId());
+		final String status = UploadStatusResource.getStatus("statusStarting", getLocale());
 
-		response.renderOnDomReadyJavaScript(new String(out.toByteArray()));
+		StringBuilder builder = new StringBuilder(128);
+		Formatter formatter = new Formatter(builder);
+		formatter.format("new Wicket.WUPB('%s', '%s', '%s', '%s', '%s', '%s').bind('%s')",
+			getMarkupId(), statusDiv.getMarkupId(), barDiv.getMarkupId(), urlFor(ref, null),
+			uploadFieldId, status, getCallbackForm().getMarkupId());
+		response.renderOnDomReadyJavaScript(builder.toString());
 	}
 
 	/**

Modified: wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.java?rev=1142754&r1=1142753&r2=1142754&view=diff
==============================================================================
--- wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.java (original)
+++ wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.java Mon Jul  4 18:11:46 2011
@@ -16,13 +16,18 @@
  */
 package org.apache.wicket.extensions.ajax.markup.html.form.upload;
 
+import java.text.MessageFormat;
+import java.util.Locale;
+import java.util.ResourceBundle;
+
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.wicket.Application;
+import org.apache.wicket.Session;
 import org.apache.wicket.protocol.http.servlet.MultipartServletWebRequestImpl;
 import org.apache.wicket.protocol.http.servlet.UploadInfo;
 import org.apache.wicket.request.resource.AbstractResource;
 
-
 /**
  * A resource that prints out basic statistics about the current upload. This resource is used to
  * feed the progress bar information by the progress bar javascript which requests this resource
@@ -39,24 +44,27 @@ class UploadStatusResource extends Abstr
 	@Override
 	protected ResourceResponse newResourceResponse(final Attributes attributes)
 	{
+		// Determine encoding
+		final String encoding = Application.get()
+			.getRequestCycleSettings()
+			.getResponseRequestEncoding();
+
 		ResourceResponse response = new ResourceResponse();
-		response.setContentType("text/html");
+		response.setContentType("text/html; charset=" + encoding);
 
-		final String content = getStatus(attributes);
+		final String status = getStatus(attributes);
 		response.setWriteCallback(new WriteCallback()
 		{
 			@Override
 			public void writeData(final Attributes attributes)
 			{
-				attributes.getResponse().write(content);
+				attributes.getResponse().write("<html><body>|");
+				attributes.getResponse().write(status);
+				attributes.getResponse().write("|</body></html>");
 			}
 		});
 
-		response.setContentLength(content.getBytes().length);
-
 		return response;
-
-
 	}
 
 	/**
@@ -72,17 +80,35 @@ class UploadStatusResource extends Abstr
 		String status = null;
 		if ((info == null) || (info.getTotalBytes() < 1))
 		{
-			status = "0|0|0|0|0";
+			status = "100|";
 		}
 		else
 		{
-			status = "" + info.getPercentageComplete() + "|" + info.getBytesUploadedString() + "|" +
-				info.getTotalBytesString() + "|" + info.getTransferRateString() + "|" +
-				info.getRemainingTimeString();
+			Locale locale = Session.get().getLocale();
+
+			String pattern = getStatus("statusUpdate", locale);
+
+			status = info.getPercentageComplete() +
+				"|" +
+				MessageFormat.format(pattern, info.getPercentageComplete(),
+					info.getBytesUploadedString(locale), info.getTotalBytesString(locale),
+					info.getTransferRateString(locale), info.getRemainingTimeString(locale));
 		}
-		status = "<html><body>|" + status + "|</body></html>";
 		return status;
 	}
 
-
+	/**
+	 * Get a status message for the given key.
+	 * 
+	 * @param key
+	 *            message key
+	 * @param locale
+	 *            locale for message
+	 * @return status message
+	 */
+	static String getStatus(String key, Locale locale)
+	{
+		return ResourceBundle.getBundle(UploadStatusResource.class.getName(), locale)
+			.getString(key);
+	}
 }

Added: wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.properties
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.properties?rev=1142754&view=auto
==============================================================================
--- wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.properties (added)
+++ wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.properties Mon Jul  4 18:11:46 2011
@@ -0,0 +1,16 @@
+#  Licensed to the Apache Software Foundation (ASF) under one or more
+#  contributor license agreements.  See the NOTICE file distributed with
+#  this work for additional information regarding copyright ownership.
+#  The ASF licenses this file to You 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.
+statusStarting=Upload starting...
+statusUpdate={0}% finished, {1} of {2} at {3}; {4}
\ No newline at end of file

Propchange: wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource_de.properties
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource_de.properties?rev=1142754&view=auto
==============================================================================
--- wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource_de.properties (added)
+++ wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource_de.properties Mon Jul  4 18:11:46 2011
@@ -0,0 +1,16 @@
+#  Licensed to the Apache Software Foundation (ASF) under one or more
+#  contributor license agreements.  See the NOTICE file distributed with
+#  this work for additional information regarding copyright ownership.
+#  The ASF licenses this file to You 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.
+statusStarting=Hochladen startet...
+statusUpdate={0}% abgeschlossen, {1} von {2} mit {3}; {4}
\ No newline at end of file

Propchange: wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource_de.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource_fr.properties
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource_fr.properties?rev=1142754&view=auto
==============================================================================
--- wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource_fr.properties (added)
+++ wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource_fr.properties Mon Jul  4 18:11:46 2011
@@ -0,0 +1,16 @@
+#  Licensed to the Apache Software Foundation (ASF) under one or more
+#  contributor license agreements.  See the NOTICE file distributed with
+#  this work for additional information regarding copyright ownership.
+#  The ASF licenses this file to You 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.
+statusStarting=t\u00e9l\u00e9charger \u00e0 partir...
+statusUpdate={0}% termin\u00e9e, {1} de {2} \u00e0 {3}; {4}
\ No newline at end of file

Propchange: wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/UploadStatusResource_fr.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/progressbar.js
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/progressbar.js?rev=1142754&r1=1142753&r2=1142754&view=diff
==============================================================================
--- wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/progressbar.js (original)
+++ wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/form/upload/progressbar.js Mon Jul  4 18:11:46 2011
@@ -17,11 +17,15 @@
 if (typeof(Wicket) == "undefined") Wicket = { };
 
 Wicket.WUPB = Wicket.Class.create();
-
 Wicket.WUPB.prototype = {
 		
-	initialize : function(def) {
-		this.def = def;
+	initialize : function(formid, statusid, barid, url, fileid, initialStatus) {
+		this.formid = formid;
+		this.statusid = statusid;
+		this.barid = barid;
+		this.url = url;
+		this.fileid = fileid;
+		this.initialStatus = initialStatus;
 	},
 
 	bind : function(formid) {
@@ -41,16 +45,15 @@ Wicket.WUPB.prototype = {
 	
 	start : function(){
 		this.displayprogress = true;
-		if (this.def.fileid) {
-			var fileupload = Wicket.$(this.def.fileid);
+		if (this.fileid) {
+			var fileupload = Wicket.$(this.fileid);
 			this.displayprogress = fileupload && fileupload.value && fileupload.value != '';
 		}
-		if(this.displayprogress) {
-			this.setStatus('Upload starting...');
-			Wicket.$(this.def.barid).firstChild.firstChild.style.width='0%';
-
-			Wicket.$(this.def.statusid).style.display='block';
-			Wicket.$(this.def.barid).style.display='block';
+		if (this.displayprogress) {
+			this.setPercent(0);
+			this.setStatus(this.initialStatus);
+			Wicket.$(this.statusid).style.display='block';
+			Wicket.$(this.barid).style.display='block';
 			this.scheduleUpdate();
 		}
 	},
@@ -58,19 +61,23 @@ Wicket.WUPB.prototype = {
 	setStatus : function(status){
 		var label = document.createElement("label");
 		label.innerHTML = status;
-		var oldLabel = Wicket.$(this.def.statusid).firstChild;
+		var oldLabel = Wicket.$(this.statusid).firstChild;
 		if( oldLabel != null){
-			Wicket.$(this.def.statusid).removeChild(oldLabel);
+			Wicket.$(this.statusid).removeChild(oldLabel);
 		}
-		Wicket.$(this.def.statusid).appendChild(label);		
+		Wicket.$(this.statusid).appendChild(label);		
+	},
+	
+	setPercent : function(progressPercent){
+		Wicket.$(this.barid).firstChild.firstChild.style.width = progressPercent + '%';
 	},
 	
 	scheduleUpdate : function(){
-		window.setTimeout(this.ajax.bind(this), 1000);
+		window.setTimeout(this.load.bind(this), 1000);
 	},
 
-	ajax : function() {
-		var URL = this.def.url + '?anticache=' + Math.random();
+	load : function() {
+		var URL = this.url + '?anticache=' + Math.random();
 		
     	this.iframe = Wicket._createIFrame(""+Math.random());
 		
@@ -78,11 +85,9 @@ Wicket.WUPB.prototype = {
 		
 		Wicket.Event.add(this.iframe, "load", this.update.bind(this));
 		this.iframe.src = URL; 
-		
 	},
 
 	update : function() {
-		
 		if(this.iframe.contentDocument){
 			var responseAsText = this.iframe.contentDocument.body.innerHTML;
 		}else{
@@ -92,45 +97,20 @@ Wicket.WUPB.prototype = {
 		
 		var update = responseAsText.split('|');
 
-		var completed_upload_size = update[2];
-		var total_upload_size = update[3];
 		var progressPercent = update[1];
-		var transferRate = update[4];
-		var timeRemaining = update[5];
+		var status = update[2];
 
-		if ((timeRemaining != "") && (completed_upload_size != 0)) {
+		this.setPercent(progressPercent);
+		this.setStatus( status );
 
-			Wicket.$(this.def.barid).firstChild.firstChild.style.width = progressPercent + '%';
-			this.setStatus( progressPercent
-				+ '% finished, ' + completed_upload_size + ' of '
-				+ total_upload_size + ' at ' + transferRate + "; "
-				+ timeRemaining );
-
-		}
-		
 		this.iframe.parentNode.removeChild(this.iframe);
 		this.iframe = null;
 		
-		if (progressPercent == 100 || timeRemaining == 0) {
-			if (progressPercent == 100) {
-				Wicket.$(this.def.barid).firstChild.firstChild.style.width = '100%';
-			}
-			wicketHide(this.def.statusid);
-			wicketHide(this.def.barid);
+		if (progressPercent == 100) {
+			wicketHide(this.statusid);
+			wicketHide(this.barid);
 		} else {
 			this.scheduleUpdate();
 		}
 	}
-};
-
-Wicket.WUPB.Def = Wicket.Class.create();
-
-Wicket.WUPB.Def.prototype = {
-	initialize : function(formid, statusid, barid, url, fileid) {
-		this.formid = formid;
-		this.statusid = statusid;
-		this.barid = barid;
-		this.url = url;
-		this.fileid = fileid;
-	}
 };
\ No newline at end of file