Author: andrei_exadel
Date: 2008-03-13 08:05:46 -0400 (Thu, 13 Mar 2008)
New Revision: 6775
Modified:
trunk/ui/fileUpload/src/main/config/component/fileUpload.xml
trunk/ui/fileUpload/src/main/java/org/richfaces/component/UIFileUpload.java
trunk/ui/fileUpload/src/main/java/org/richfaces/renderkit/FileUploadRendererBase.java
trunk/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js
trunk/ui/fileUpload/src/main/templates/org/richfaces/fileUpload.jspx
trunk/ui/fileUpload/src/test/java/org/richfaces/component/FileUploadComponentTest.java
Log:
RF-2468
Modified: trunk/ui/fileUpload/src/main/config/component/fileUpload.xml
===================================================================
--- trunk/ui/fileUpload/src/main/config/component/fileUpload.xml 2008-03-13 11:44:06 UTC
(rev 6774)
+++ trunk/ui/fileUpload/src/main/config/component/fileUpload.xml 2008-03-13 12:05:46 UTC
(rev 6775)
@@ -225,6 +225,34 @@
<name>onerror</name>
<classname>java.lang.String</classname>
</property>
+ <property>
+ <name>addControlLabel</name>
+ <classname>java.lang.String</classname>
+ </property>
+ <property>
+ <name>uploadControlLabel</name>
+ <classname>java.lang.String</classname>
+ </property>
+ <property>
+ <name>stopControlLabel</name>
+ <classname>java.lang.String</classname>
+ </property>
+ <property>
+ <name>cancelEntryControlLabel</name>
+ <classname>java.lang.String</classname>
+ </property>
+ <property>
+ <name>clearControlLabel</name>
+ <classname>java.lang.String</classname>
+ </property>
+ <property>
+ <name>clearAllControlLabel</name>
+ <classname>java.lang.String</classname>
+ </property>
+ <property>
+ <name>stopEntryControlLabel</name>
+ <classname>java.lang.String</classname>
+ </property>
</component>
&listeners;
</components>
Modified: trunk/ui/fileUpload/src/main/java/org/richfaces/component/UIFileUpload.java
===================================================================
--- trunk/ui/fileUpload/src/main/java/org/richfaces/component/UIFileUpload.java 2008-03-13
11:44:06 UTC (rev 6774)
+++ trunk/ui/fileUpload/src/main/java/org/richfaces/component/UIFileUpload.java 2008-03-13
12:05:46 UTC (rev 6775)
@@ -1,14 +1,18 @@
package org.richfaces.component;
import java.io.InputStream;
+import java.util.Locale;
import java.util.Map;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
+import javax.faces.FacesException;
+import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
import javax.faces.event.FacesEvent;
import javax.servlet.http.HttpServletRequest;
@@ -109,7 +113,11 @@
public abstract void setStyleClass(String styleClass);
public abstract void setStyle(String style);
+
+ public abstract Object getLocale();
+ public abstract void setLocale(Object locale);
+
public abstract MethodExpression getFileUploadListener();
public abstract void setFileUploadListener(MethodExpression scrollerListener);
@@ -172,4 +180,63 @@
}
}
+
+ /**
+ *Parse Locale from String.
+ *String must be represented as Locale.toString(); xx_XX_XXXX
+*/
+
+ public Locale parseLocale(String localeStr){
+
+ int length = localeStr.length();
+ if(null==localeStr||length<2){
+ return Locale.getDefault();
+ }
+
+ //Lookup index of first '_' in string locale representation.
+ int index1 = localeStr.indexOf("_");
+ //Get first charters (if exist) from string
+ String language = null;
+ if(index1!=-1){
+ language = localeStr.substring(0, index1);
+ }else{
+ return new Locale(localeStr);
+ }
+ //Lookup index of second '_' in string locale representation.
+ int index2 = localeStr.indexOf("_", index1+1);
+ String country = null;
+ if(index2!=-1){
+ country = localeStr.substring(index1+1, index2);
+ String variant = localeStr.substring(index2+1);
+ return new Locale(language, country, variant);
+ }else{
+ country = localeStr.substring(index1+1);
+ return new Locale(language, country);
+ }
+ }
+
+ public Locale getAsLocale(Object locale) {
+
+ if (locale instanceof Locale) {
+
+ return (Locale) locale;
+
+ } else if (locale instanceof String) {
+
+ return parseLocale((String) locale);
+
+ } else {
+
+ FacesContext context = FacesContext.getCurrentInstance();
+ Application application = context.getApplication();
+ Converter converter = application
+ .createConverter(locale.getClass());
+ if (null != converter) {
+ return parseLocale(converter.getAsString(context, this, locale));
+ } else {
+ throw new FacesException(
+ "Wrong locale attibute type or there is no converter for custom attibute
type");
+ }
+ }
+ }
}
Modified:
trunk/ui/fileUpload/src/main/java/org/richfaces/renderkit/FileUploadRendererBase.java
===================================================================
---
trunk/ui/fileUpload/src/main/java/org/richfaces/renderkit/FileUploadRendererBase.java 2008-03-13
11:44:06 UTC (rev 6774)
+++
trunk/ui/fileUpload/src/main/java/org/richfaces/renderkit/FileUploadRendererBase.java 2008-03-13
12:05:46 UTC (rev 6775)
@@ -1,9 +1,12 @@
package org.richfaces.renderkit;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
import javax.el.ValueExpression;
import javax.faces.FactoryFinder;
@@ -13,6 +16,7 @@
import javax.faces.render.RenderKit;
import javax.faces.render.RenderKitFactory;
import javax.servlet.ServletRequest;
+import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.ajax4jsf.Filter;
@@ -45,17 +49,44 @@
/** Attribute name where collection of files uploaded will be stored */
private static final String _FILES_UPLOADED_ATTRIBUTE_NAME = "uploadData";
-
+
/** Multipart request class name */
- private static final String _MULTIPART_REQUEST_CLASS_NAME =
MultipartRequest.class.getName();
+ private static final String _MULTIPART_REQUEST_CLASS_NAME = MultipartRequest.class
+ .getName();
- /*
- * (non-Javadoc)
- *
- * @see
org.ajax4jsf.renderkit.RendererBase#doDecode(javax.faces.context.FacesContext,
- * javax.faces.component.UIComponent)
+ /** File upload bundle name */
+ private static final String FILE_UPLOAD_BUNDLE =
"org.richfaces.renderkit.fileUpload";
+
+ /** Bundle prefix */
+ private static final String bundlePrefix = "RICH_FILE_UPLOAD_";
+
+ /** Bundle postfix */
+ private static final String bundlePostfix = "_LABEL";
+
+ /** Set of bundles that can be defined */
+ private static final String[] bundlesLables = { "add", "upload",
"stop",
+ "clear_all", "entry_cancel", "entry_clear",
"entry_stop", "done",
+ "size_error", "transfer_error" };
+
+ /** Default labels values */
+ private static final String[] defaultLables = { "Add...",
"<b>Upload</b>",
+ "<b>Stop</b>", "Clear All", "Cancel",
"Clear", "Stop",
+ "<b>Done</b>", "File size restricted",
"Transfer error occuried" };
+
+ /** Set of attributes that can define label's value */
+ private static final String[] labelAttribues = { "addControlLabel",
+ "uploadControlLabel", "stopControlLabel",
"clearAllControlLabel",
+ "cancelEntryControlLabel", "clearControlLabel",
+ "stopEntryControlLabel", null, null, null };
+
+
+ /**
+ * Overrides standard JSF component method.
+ * @param context faces context
+ * @param component file upload component
*/
@Override
+ @SuppressWarnings ("unchecked")
protected void doDecode(FacesContext context, UIComponent component) {
UIFileUpload fileUpload = (UIFileUpload) component;
ServletRequest request = (ServletRequest) context.getExternalContext()
@@ -64,19 +95,21 @@
String clientId = component.getClientId(context);
new AjaxEvent(component).queue();
-
+
Class requestClazz = request.getClass();
-
- if (_MULTIPART_REQUEST_CLASS_NAME
- .equals(requestClazz.getName())
- || "org.jboss.seam.web.MultipartRequest".equals(requestClazz.getName())) {
-
- HttpSession session = (HttpSession)context.getExternalContext().getSession(false);
+
+ if (_MULTIPART_REQUEST_CLASS_NAME.equals(requestClazz.getName())
+ || "org.jboss.seam.web.MultipartRequest".equals(requestClazz
+ .getName())) {
+
+ HttpSession session = (HttpSession) context.getExternalContext()
+ .getSession(false);
String uid = request.getParameter(Filter.UPLOAD_FILES_ID);
- Map<String, MultipartRequest> map = (Map<String,
MultipartRequest>)session.getAttribute(Filter.REQUESTS_SESSIONS_BEAN_NAME);
-
+ Map<String, MultipartRequest> map = (Map<String, MultipartRequest>)
session
+ .getAttribute(Filter.REQUESTS_SESSIONS_BEAN_NAME);
+
MultipartRequest multipartRequest = map.get(uid);
-
+
clientId = clientId + ":file";
String fileName = multipartRequest.getFileName(clientId);
Object file = multipartRequest.getFile(clientId);
@@ -85,7 +118,128 @@
}
}
+
+
+ /**
+ * Generates map with internalized labels to be put into JS
+ * @param o map of labels
+ * @return javascript hash map
+ */
+ public Object _getLabels(Object o) {
+ return ScriptUtils.toScript(o);
+ }
+
+
+ /**
+ * Gets internalized labels.
+ * At the first system is looking for them in appropriate attributes if they are
defined.
+ * Next search place is application and file upload bundles.
+ * If no result - default label value will be set up.
+ * @param context facesContext instance
+ * @param component UIComponent
+ * @return map of labels
+ */
+ public Map<String, String> getLabels(FacesContext context,
+ UIComponent component) {
+ Map<String, String> labelsMap = new HashMap<String, String>();
+ ResourceBundle bundle1 = null;
+ ResourceBundle bundle2 = null;
+ UIFileUpload fileUpload = (UIFileUpload) component;
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ String messageBundle = context.getApplication().getMessageBundle();
+ Object locale = fileUpload.getLocale();
+ if (locale == null) {
+ HttpServletRequest request = (HttpServletRequest) context
+ .getExternalContext().getRequest();
+ locale = request.getLocale();
+ }
+ if (null != messageBundle) {
+ bundle1 = ResourceBundle.getBundle(messageBundle, fileUpload
+ .getAsLocale(locale), loader);
+ }
+ try {
+ bundle2 = ResourceBundle.getBundle(FILE_UPLOAD_BUNDLE, fileUpload
+ .getAsLocale(locale), loader);
+ } catch (MissingResourceException e) {
+ // No external bundle was found, ignore this exception.
+ }
+ initLabels(labelsMap, bundle1, bundle2, fileUpload);
+ return labelsMap;
+ }
+
+
+ /**
+ * Sets values for labels.
+ * @param map map of labels
+ * @param bundle1 application bundle
+ * @param bundle2 file upload bundle
+ * @param fileUpload
+ */
+ private void initLabels(Map<String, String> map, ResourceBundle bundle1,
+ ResourceBundle bundle2, UIFileUpload fileUpload) {
+ int i = 0;
+ for (String name : bundlesLables) {
+ boolean found = false;
+ if (labelAttribues[i] != null) {
+ String attributeName = labelAttribues[i];
+ if (fileUpload.getAttributes().get(attributeName) != null) {
+ map.put(name, (String) fileUpload.getAttributes().get(
+ attributeName));
+ found = true;
+ }
+ }
+ if (!found && (bundle1 != null || bundle2 != null)) {
+ String label = getFromBundle(name, bundle1, bundle2);
+ if (label != null) {
+ map.put(name, getFromBundle(name, bundle1, bundle2));
+ found = true;
+ }
+ }
+ if (!found) {
+ map.put(name, defaultLables[i]);
+ }
+ i++;
+ }
+ }
+
+ /**
+ * Looking for labels name in bundles provided.
+ * @param bundle1 application bundle
+ * @param bundle2 file upload bundle
+ * @return String label value
+ */
+ private String getFromBundle(String name, ResourceBundle bundle1,
+ ResourceBundle bundle2) {
+ name = bundlePrefix + name.toUpperCase() + bundlePostfix;
+ String v = null;
+ if (bundle1 != null) {
+ try {
+ v = bundle1.getString(name);
+ } catch (Exception e) {
+ if (bundle2 != null) {
+ try {
+ v = bundle2.getString(name);
+ } catch (Exception ex) {
+ // no value has been found
+ }
+ }
+
+ }
+
+ } else if (bundle2 != null) {
+ try {
+ v = bundle2.getString(name);
+ ByteArrayOutputStream b = new ByteArrayOutputStream();
+ b.write(v.getBytes());
+ b.toString("UTF-8");
+ } catch (Exception e) {
+ // no value has been found
+ }
+ }
+ return v;
+ }
+
/**
* Method checks if uploaded files count overflowed
*
Modified:
trunk/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js
===================================================================
---
trunk/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js 2008-03-13
11:44:06 UTC (rev 6774)
+++
trunk/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js 2008-03-13
12:05:46 UTC (rev 6775)
@@ -1,6 +1,24 @@
FileUploadEntry = {};
FileUploadEntry = Class.create();
+
+FileUploadEntry.INITIALIZED = "initialized";
+FileUploadEntry.READY = "ready";
+FileUploadEntry.UPLOAD_IN_PROGRESS = "progress";
+
+FileUploadEntry.UPLOAD_CANCELED = "canceled";
+FileUploadEntry.UPLOAD_SUCCESS = "done";
+FileUploadEntry.UPLOAD_TRANSFER_ERROR = "transfer_error";
+FileUploadEntry.UPLOAD_SERVER_ERROR = "server_error";
+FileUploadEntry.UPLOAD_SIZE_ERROR = "size_error";
+
+FileUploadEntry.LABELS = {};
+FileUploadEntry.LABELS[FileUploadEntry.INITIALIZED] = '';
+FileUploadEntry.LABELS[FileUploadEntry.READY] = '';
+FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_IN_PROGRESS] = 'uploading';
+FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_CANCELED] = '';
+
+
FileUploadEntry.clearControlTemplate =
[
new E('a',
@@ -11,7 +29,7 @@
'href':'#'
},
[
- new T('Clear')
+ new T(function (context) {return Richfaces.evalMacro("controlLink",
context);} )
])
];
@@ -25,7 +43,7 @@
'href':'#'
},
[
- new T('Stop')
+ new T(function (context) {return Richfaces.evalMacro("controlLink",
context);} )
])
];
@@ -39,7 +57,7 @@
'href':'#'
},
[
- new T('Cancel')
+ new T(function (context) {return Richfaces.evalMacro("controlLink",
context);} )
])
];
@@ -81,26 +99,7 @@
];
-FileUploadEntry.INITIALIZED = "initialized";
-FileUploadEntry.READY = "ready";
-FileUploadEntry.UPLOAD_IN_PROGRESS = "progress";
-FileUploadEntry.UPLOAD_CANCELED = "canceled";
-FileUploadEntry.UPLOAD_SUCCESS = "success";
-FileUploadEntry.UPLOAD_TRANSFER_ERROR = "transfer_error";
-FileUploadEntry.UPLOAD_SERVER_ERROR = "server_error";
-FileUploadEntry.UPLOAD_SIZE_ERROR = "size_error";
-
-FileUploadEntry.LABELS = {};
-FileUploadEntry.LABELS[FileUploadEntry.INITIALIZED] = '';
-FileUploadEntry.LABELS[FileUploadEntry.READY] = '';
-FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_IN_PROGRESS] = 'uploading';
-FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_CANCELED] = '';
-FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_SUCCESS] =
'<b>Done</b>';
-FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_TRANSFER_ERROR] =
'transfer_error';
-FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_SERVER_ERROR] = 'server_error';
-FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_SIZE_ERROR] = 'File size
restricted';
-
FileUploadEntry.getComponent = function(elt) {
while (elt) {
var component = elt.component;
@@ -197,11 +196,11 @@
Element.insert(this.statusLabel, FileUploadEntry.LABELS[newState]);
if (newState == FileUploadEntry.UPLOAD_IN_PROGRESS) {
- Element.update(this.controlArea,
FileUploadEntry.stopControlTemplate.invoke('getContent',{'className':
this.uploadObject.classes.FILE_ENTRY_CONTROL.ENABLED}).join(''));
+ Element.update(this.controlArea,
FileUploadEntry.stopControlTemplate.invoke('getContent',{'controlLink':
FileUploadEntry.LABELS['entry_stop'],'className':
this.uploadObject.classes.FILE_ENTRY_CONTROL.ENABLED}).join(''));
} else if (newState == FileUploadEntry.UPLOAD_SUCCESS) {
- Element.update(this.controlArea,
FileUploadEntry.clearControlTemplate.invoke('getContent',{'className':
this.uploadObject.classes.FILE_ENTRY_CONTROL.ENABLED}).join(''));
+ Element.update(this.controlArea,
FileUploadEntry.clearControlTemplate.invoke('getContent',{'controlLink':
FileUploadEntry.LABELS['entry_clear'],'className':
this.uploadObject.classes.FILE_ENTRY_CONTROL.ENABLED}).join(''));
} else {
- Element.update(this.controlArea,
FileUploadEntry.cancelControlTemplate.invoke('getContent',{'className':
this.uploadObject.classes.FILE_ENTRY_CONTROL.ENABLED}).join(''));
+ Element.update(this.controlArea,
FileUploadEntry.cancelControlTemplate.invoke('getContent',{'controlLink':
FileUploadEntry.LABELS['entry_cancel'],'className':
this.uploadObject.classes.FILE_ENTRY_CONTROL.ENABLED}).join(''));
}
if (newState == FileUploadEntry.UPLOAD_SUCCESS) {
@@ -374,7 +373,7 @@
uploadedCount: 0,
- initialize: function(id, stopScript, getFileSizeScript, progressBarId, classes, label,
maxFiles, events, disabled, acceptedTypes, options) {
+ initialize: function(id, stopScript, getFileSizeScript, progressBarId, classes, label,
maxFiles, events, disabled, acceptedTypes, options, labels) {
this.id = id;
this.element = $(this.id);
this._progressBar = $(progressBarId);
@@ -402,8 +401,17 @@
this.setupAutoUpload();
this.checkFrame();
this.initFileEntryWidth();
+ this.initLabels(labels);
},
+ initLabels: function (labels) {
+ if (labels) {
+ for (var l in labels) {
+ FileUploadEntry.LABELS[l] = labels[l];
+ }
+ }
+ },
+
initFileEntryWidth: function () {
var w = this.element.clientWidth - 115;
this.fileEntryWidth = w;
@@ -640,13 +648,13 @@
switchUploadButton: function () {
if (this.runUpload) {
var d = $(this.id + ":upload2");
- d.innerHTML = "<b>Stop</b>";
+ d.innerHTML = FileUploadEntry.LABELS['stop'];
d.onclick = function () {
this.stop();
}.bind(this);
}else {
var d = $(this.id + ":upload2");
- d.innerHTML = "<b>Upload</b>";
+ d.innerHTML = FileUploadEntry.LABELS['upload'];;
d.onclick = function () {
this.upload();
}.bind(this);
Modified: trunk/ui/fileUpload/src/main/templates/org/richfaces/fileUpload.jspx
===================================================================
--- trunk/ui/fileUpload/src/main/templates/org/richfaces/fileUpload.jspx 2008-03-13
11:44:06 UTC (rev 6774)
+++ trunk/ui/fileUpload/src/main/templates/org/richfaces/fileUpload.jspx 2008-03-13
12:05:46 UTC (rev 6775)
@@ -34,6 +34,13 @@
variables.setVariable("addButtonClassContent",addButtonClassContent);
variables.setVariable("uploadListClass",uploadListClass);
+ Map<String, String> labels = getLabels(context, component);
+ variables.setVariable("labels",labels);
+ variables.setVariable("addLabel",labels.get("add"));
+ variables.setVariable("uploadLabel",labels.get("upload"));
+ variables.setVariable("clearAllLabel",labels.get("clear_all"));
+
+
]]>
</jsp:scriptlet>
@@ -57,7 +64,7 @@
style="position: relative; overflow: hidden; width:70px;"
id="#{clientId}:add1">
<div class="#{addButtonClassContent}"
- id="#{clientId}:add2" style="direction:
rtl;">Add...</div>
+ id="#{clientId}:add2" style="direction:
rtl;">#{addLabel}</div>
<input type="file" style="cursor: pointer; z-index: 3; right:
-10px; top: -10px; font-size: 10em; position: absolute"
class="hidden"
id="#{clientId}:file"
@@ -74,7 +81,7 @@
<a href="#" class="upload_button_selection">
<div class="upload_button_content upload_font upload_ico upload_ico_start
#{component.attributes['uploadButtonClass']}"
id="#{clientId}:upload2">
- <b>Upload</b>
+ #{uploadLabel}
</div>
</a>
</div>
@@ -91,7 +98,7 @@
id="#{clientId}:clean1">
<a href="#" class="upload_button_selection">
<div class="upload_button_content upload_font upload_ico
upload_ico_clear_dis #{component.attributes['cleanButtonClass']}"
- id="#{clientId}:clean2">Clear All</div>
+ id="#{clientId}:clean2">#{clearAllLabel}</div>
</a>
</div>
</div>
@@ -159,7 +166,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"]}, #{this:getAcceptedTypes(context,
component)}, {'autoclear':#{component.attributes["autoclear"]}});
+ 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)},
{'autoclear':#{component.attributes["autoclear"]}},#{this:_getLabels(labels)});
</script>
</span>
Modified:
trunk/ui/fileUpload/src/test/java/org/richfaces/component/FileUploadComponentTest.java
===================================================================
---
trunk/ui/fileUpload/src/test/java/org/richfaces/component/FileUploadComponentTest.java 2008-03-13
11:44:06 UTC (rev 6774)
+++
trunk/ui/fileUpload/src/test/java/org/richfaces/component/FileUploadComponentTest.java 2008-03-13
12:05:46 UTC (rev 6775)
@@ -65,6 +65,7 @@
fileUpload = (UIFileUpload)
application.createComponent(UIFileUpload.COMPONENT_TYPE);
fileUpload.setAcceptedTypes("application/zip,image/jpeg,video/mpeg");
fileUpload.setMaxFilesQuantity(5);
+ fileUpload.setLocale("en");
Map<String, UIComponent> facets = fileUpload.getFacets();
UIProgressBar progressBar = (UIProgressBar)
application.createComponent(UIProgressBar.COMPONENT_TYPE);
facets.put("progress", progressBar);