Author: nbelaevski
Date: 2009-03-12 16:22:50 -0400 (Thu, 12 Mar 2009)
New Revision: 12947
Added:
trunk/ui/fileUpload/src/main/java/org/richfaces/event/
trunk/ui/fileUpload/src/main/java/org/richfaces/event/UploadAjaxActionEvent.java
Modified:
trunk/ui/fileUpload/src/main/java/org/richfaces/component/UIFileUpload.java
trunk/ui/fileUpload/src/main/java/org/richfaces/renderkit/FileUploadRendererBase.java
Log:
rich:fileUpload refactored
Modified: trunk/ui/fileUpload/src/main/java/org/richfaces/component/UIFileUpload.java
===================================================================
--- trunk/ui/fileUpload/src/main/java/org/richfaces/component/UIFileUpload.java 2009-03-12
19:57:38 UTC (rev 12946)
+++ trunk/ui/fileUpload/src/main/java/org/richfaces/component/UIFileUpload.java 2009-03-12
20:22:50 UTC (rev 12947)
@@ -41,6 +41,7 @@
import org.ajax4jsf.context.AjaxContextImpl;
import org.ajax4jsf.request.MultipartRequest;
import org.richfaces.event.FileUploadListener;
+import org.richfaces.event.UploadAjaxActionEvent;
import org.richfaces.event.UploadEvent;
import org.richfaces.renderkit.FileUploadRendererBase;
@@ -52,232 +53,229 @@
*/
public abstract class UIFileUpload extends UIInput {
- /**
- * <p>
- * The standard component type for this component.
- * </p>
- */
- public static final String COMPONENT_TYPE =
"org.richfaces.component.FileUpload";
+ /**
+ * <p>
+ * The standard component type for this component.
+ * </p>
+ */
+ public static final String COMPONENT_TYPE =
"org.richfaces.component.FileUpload";
- /**
- * <p>
- * The standard component family for this component.
- * </p>
- */
- public static final String COMPONENT_FAMILY =
"org.richfaces.component.FileUpload";
+ /**
+ * <p>
+ * The standard component family for this component.
+ * </p>
+ */
+ public static final String COMPONENT_FAMILY =
"org.richfaces.component.FileUpload";
- private String localContentType;
+ private String localContentType;
- private String localFileName;
+ private String localFileName;
- private Integer localFileSize;
+ private Integer localFileSize;
- private InputStream localInputStream;
-
- private void setupProgressBarValueExpression(FacesContext context, String uid) {
- FileUploadRendererBase renderer =
(FileUploadRendererBase)this.getRenderer(context);
- UIComponent progressBar = renderer.getProgressBar(context, this);
- String percentExpression = FileUploadConstants.PERCENT_BEAN_NAME +
"['"+uid+"']";
+ private InputStream localInputStream;
+
+ private void setupProgressBarValueExpression(FacesContext context, String uid) {
+ FileUploadRendererBase renderer = (FileUploadRendererBase)this.getRenderer(context);
+ UIComponent progressBar = renderer.getProgressBar(context, this);
+ String percentExpression = FileUploadConstants.PERCENT_BEAN_NAME +
"['"+uid+"']";
ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
ELContext elContext = context.getELContext();
-
+
ValueExpression value = expressionFactory
- .createValueExpression(elContext, "#{" + percentExpression + "}",
+ .createValueExpression(elContext, "#{" + percentExpression + "}",
Integer.class);
progressBar.setValueExpression("value", value);
-
+
ValueExpression enabled = expressionFactory
- .createValueExpression(elContext,
+ .createValueExpression(elContext,
"#{" + percentExpression + " < 100}", //100 - disable progress
when upload reaches 100%
Boolean.class);
progressBar.setValueExpression("enabled", enabled);
- }
+ }
-
- public String getLocalContentType() {
- return localContentType;
- }
- public void setLocalContentType(String localContentType) {
- this.localContentType = localContentType;
- }
+ public String getLocalContentType() {
+ return localContentType;
+ }
- public String getLocalFileName() {
- return localFileName;
- }
+ public void setLocalContentType(String localContentType) {
+ this.localContentType = localContentType;
+ }
+ public String getLocalFileName() {
+ return localFileName;
+ }
+
public void setLocalFileName(String localFileName) {
- this.localFileName = localFileName;
- }
+ this.localFileName = localFileName;
+ }
- public Integer getLocalFileSize() {
- return localFileSize;
- }
+ public Integer getLocalFileSize() {
+ return localFileSize;
+ }
- public void setLocalFileSize(Integer localFileSize) {
- this.localFileSize = localFileSize;
- }
+ public void setLocalFileSize(Integer localFileSize) {
+ this.localFileSize = localFileSize;
+ }
- public InputStream getLocalInputStream() {
- return localInputStream;
- }
+ public InputStream getLocalInputStream() {
+ return localInputStream;
+ }
- public void setLocalInputStream(InputStream localInputStream) {
- this.localInputStream = localInputStream;
- }
+ public void setLocalInputStream(InputStream localInputStream) {
+ this.localInputStream = localInputStream;
+ }
- public abstract void setAcceptedTypes(String acceptedTypes);
+ public abstract void setAcceptedTypes(String acceptedTypes);
- public abstract String getAcceptedTypes();
+ public abstract String getAcceptedTypes();
- public abstract Integer getMaxFilesQuantity();
+ public abstract Integer getMaxFilesQuantity();
- public abstract void setMaxFilesQuantity(Integer maxFilesQuantity);
+ public abstract void setMaxFilesQuantity(Integer maxFilesQuantity);
- public abstract String getListHeight();
+ public abstract String getListHeight();
- public abstract void setListHeight(String listHeight);
+ public abstract void setListHeight(String listHeight);
- public abstract String getListWidth();
+ public abstract String getListWidth();
- public abstract void setListWidth(String listWidth);
+ public abstract void setListWidth(String listWidth);
- public abstract String getStyleClass();
+ public abstract String getStyleClass();
- public abstract String getStyle();
+ public abstract String getStyle();
- public abstract void setStyleClass(String styleClass);
+ public abstract void setStyleClass(String styleClass);
- public abstract void setStyle(String style);
-
- public abstract Object getLocale();
+ public abstract void setStyle(String style);
- public abstract void setLocale(Object locale);
+ public abstract Object getLocale();
- public abstract MethodBinding getFileUploadListener();
+ public abstract void setLocale(Object locale);
- public abstract void setFileUploadListener(MethodBinding scrollerListener);
+ public abstract MethodBinding getFileUploadListener();
- public void addFileUploadListener(FileUploadListener listener) {
- addFacesListener(listener);
- }
+ public abstract void setFileUploadListener(MethodBinding scrollerListener);
- public FileUploadListener[] getFileUploadListeners() {
- return (FileUploadListener[]) getFacesListeners(FileUploadListener.class);
- }
+ public void addFileUploadListener(FileUploadListener listener) {
+ addFacesListener(listener);
+ }
- public void removeFileUploadListener(FileUploadListener listener) {
- removeFacesListener(listener);
- }
-
- public void reset () {
- this.localContentType = null;
- this.localContentType = null;
- this.localFileName = null;
- this.localFileSize = null;
- this.localInputStream = null;
- }
+ public FileUploadListener[] getFileUploadListeners() {
+ return (FileUploadListener[]) getFacesListeners(FileUploadListener.class);
+ }
- public void broadcast(FacesEvent e) {
+ public void removeFileUploadListener(FileUploadListener listener) {
+ removeFacesListener(listener);
+ }
- if (e instanceof UploadEvent) {
- FacesContext facesContext = FacesContext.getCurrentInstance();
- MethodBinding binding = getFileUploadListener();
- if (binding != null) {
- binding.invoke(facesContext, new Object[] { e });
- }
+ public void reset () {
+ this.localContentType = null;
+ this.localContentType = null;
+ this.localFileName = null;
+ this.localFileSize = null;
+ this.localInputStream = null;
+ }
- } else {
- FacesContext facesContext = FacesContext.getCurrentInstance();
- ExternalContext externalContext = facesContext.getExternalContext();
- Map<String, String> requestParameterMap =
externalContext.getRequestParameterMap();
-
- String actionString = requestParameterMap.get("action");
-
- if (actionString != null &&
requestParameterMap.containsKey(this.getClientId(facesContext))) {
- AjaxContext ajaxContext =
AjaxContextImpl.getCurrentInstance(facesContext);
- String uid =
requestParameterMap.get(FileUploadConstants.UPLOAD_FILES_ID);
+ public void broadcast(FacesEvent e) {
- Map<String, Object> sessionMap = externalContext.getSessionMap();
+ if (e instanceof UploadEvent) {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ MethodBinding binding = getFileUploadListener();
+ if (binding != null) {
+ binding.invoke(facesContext, new Object[] { e });
+ }
- if ("progress".equals(actionString)) {
- setupProgressBarValueExpression(facesContext, uid);
- Map<String, Integer> requestsSizeMap = (Map<String,
Integer>) sessionMap.get(FileUploadConstants.REQUEST_SIZE_BEAN_NAME);
-
- if (requestsSizeMap != null) {
- ajaxContext.setResponseData(requestsSizeMap.get(uid));
- } else {
- ajaxContext.setResponseData(null);
- }
- } else if (FileUploadConstants.FILE_UPLOAD_ACTION_STOP.equals(actionString))
{
- MultipartRequest multipartRequest =
MultipartRequest.lookupRequest(facesContext, uid);
- if (multipartRequest != null) {
- multipartRequest.stop();
- }
- }
- }
- }
+ } else if (e instanceof UploadAjaxActionEvent) {
+ UploadAjaxActionEvent uploadActionEvent = (UploadAjaxActionEvent) e;
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ ExternalContext externalContext = facesContext.getExternalContext();
+
+ AjaxContext ajaxContext = AjaxContextImpl.getCurrentInstance(facesContext);
+ String uid = uploadActionEvent.getUploadId();
+ String actionString = uploadActionEvent.getAction();
+
+ if ("progress".equals(actionString)) {
+ setupProgressBarValueExpression(facesContext, uid);
+ Map<String, Object> sessionMap = externalContext.getSessionMap();
+ Map<String, Integer> requestsSizeMap = (Map<String, Integer>)
sessionMap.get(FileUploadConstants.REQUEST_SIZE_BEAN_NAME);
- }
-
- /**
- *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();
+ if (requestsSizeMap != null) {
+ ajaxContext.setResponseData(requestsSizeMap.get(uid));
+ } else {
+ ajaxContext.setResponseData(null);
+ }
+ } else if (FileUploadConstants.FILE_UPLOAD_ACTION_STOP.equals(actionString)) {
+ MultipartRequest multipartRequest = MultipartRequest.lookupRequest(facesContext,
uid);
+ if (multipartRequest != null) {
+ multipartRequest.stop();
+ }
+ }
}
- //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);
- }
+ super.broadcast(e);
}
-
- public Locale getAsLocale(Object locale) {
- if (locale instanceof Locale) {
+/**
+ *Parse Locale from String.
+ *String must be represented as Locale.toString(); xx_XX_XXXX
+ */
- return (Locale) locale;
+public Locale parseLocale(String localeStr){
- } else if (locale instanceof String) {
+ int length = localeStr.length();
+ if(null==localeStr||length<2){
+ return Locale.getDefault();
+ }
- return parseLocale((String) locale);
+ //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);
+ }
+}
- } else {
+public Locale getAsLocale(Object locale) {
- 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");
- }
+ 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");
}
}
}
+}
Added: trunk/ui/fileUpload/src/main/java/org/richfaces/event/UploadAjaxActionEvent.java
===================================================================
--- trunk/ui/fileUpload/src/main/java/org/richfaces/event/UploadAjaxActionEvent.java
(rev 0)
+++
trunk/ui/fileUpload/src/main/java/org/richfaces/event/UploadAjaxActionEvent.java 2009-03-12
20:22:50 UTC (rev 12947)
@@ -0,0 +1,40 @@
+/**
+ *
+ */
+package org.richfaces.event;
+
+import javax.faces.component.UIComponent;
+
+import org.ajax4jsf.event.AjaxEvent;
+
+/**
+ * @author Nick Belaevski
+ * @since 3.3.1
+ */
+public class UploadAjaxActionEvent extends AjaxEvent {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = -9018152705741192920L;
+
+ private String action;
+
+ private String uploadId;
+
+ public UploadAjaxActionEvent(UIComponent component, String action, String uploadId) {
+ super(component);
+
+ this.action = action;
+ this.uploadId = uploadId;
+ }
+
+ public String getAction() {
+ return action;
+ }
+
+ public String getUploadId() {
+ return uploadId;
+ }
+
+}
Modified:
trunk/ui/fileUpload/src/main/java/org/richfaces/renderkit/FileUploadRendererBase.java
===================================================================
---
trunk/ui/fileUpload/src/main/java/org/richfaces/renderkit/FileUploadRendererBase.java 2009-03-12
19:57:38 UTC (rev 12946)
+++
trunk/ui/fileUpload/src/main/java/org/richfaces/renderkit/FileUploadRendererBase.java 2009-03-12
20:22:50 UTC (rev 12947)
@@ -48,7 +48,6 @@
import javax.servlet.http.HttpSession;
import org.ajax4jsf.context.AjaxContext;
-import org.ajax4jsf.event.AjaxEvent;
import org.ajax4jsf.javascript.JSFunction;
import org.ajax4jsf.javascript.JSFunctionDefinition;
import org.ajax4jsf.javascript.JSLiteral;
@@ -64,6 +63,7 @@
import org.richfaces.component.UIFileUpload;
import org.richfaces.component.UIProgressBar;
import org.richfaces.component.util.HtmlUtil;
+import org.richfaces.event.UploadAjaxActionEvent;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;
@@ -115,52 +115,53 @@
@Override
@SuppressWarnings("unchecked")
protected void doDecode(FacesContext context, UIComponent component) {
-
- new AjaxEvent(component).queue();
-
+ UIFileUpload fileUpload = (UIFileUpload) component;
+ String clientId = component.getClientId(context);
ExternalContext externalContext = context.getExternalContext();
Map<String, String> requestParameterMap =
externalContext.getRequestParameterMap();
- String fileUploadIndicator =
requestParameterMap.get(FileUploadConstants.FILE_UPLOAD_INDICATOR);
- if (fileUploadIndicator != null &&
Boolean.TRUE.toString().equals(fileUploadIndicator)) {
- UIFileUpload fileUpload = (UIFileUpload) component;
- String clientId = component.getClientId(context);
- String id = requestParameterMap.get(clientId);
+ if (requestParameterMap.get(clientId) != null) {
+ String actionString =
requestParameterMap.get(FileUploadConstants.FILE_UPLOAD_ACTION);
+
+ String uid = requestParameterMap.get(FileUploadConstants.UPLOAD_FILES_ID);
+
+ if (actionString != null) {
+ new UploadAjaxActionEvent(component, actionString, uid).queue();
+ }
- if (id == null) {
- return; // Avoid listener calling for another fileUpload
- }
-
- String uid = requestParameterMap.get(FileUploadConstants.UPLOAD_FILES_ID);
- decreaseFileCounter(context, id);
-
- MultipartRequest multipartRequest = MultipartRequest.lookupRequest(context, uid);
-
- boolean isFlash = (requestParameterMap.get("_richfaces_send_http_error") !=
null);
-
- List<UploadItem> fileList = multipartRequest.getUploadItems();
-
- if (fileList == null || fileList.size() == 0) {
- return;
- }
-
- boolean formUpload = multipartRequest.isFormUpload();
-
- if (isFlash && !formUpload && fileList.size() > 0) {
- try {
- UploadItem item = fileList.get(0);
- int actualSize = item.getFileSize();
- int clientSize =
Integer.parseInt(requestParameterMap.get("_richfaces_size"));
-
- if (actualSize != clientSize) {
- return; // File uploading has been stopped on the client side
- }
- }catch (Exception e) {
+ String fileUploadIndicator =
requestParameterMap.get(FileUploadConstants.FILE_UPLOAD_INDICATOR);
+ if (fileUploadIndicator != null &&
Boolean.TRUE.toString().equals(fileUploadIndicator)) {
+ decreaseFileCounter(context, clientId);
+
+ MultipartRequest multipartRequest = MultipartRequest.lookupRequest(context, uid);
+
+ boolean isFlash = (requestParameterMap.get("_richfaces_send_http_error") !=
null);
+
+ List<UploadItem> fileList = multipartRequest.getUploadItems();
+
+ if (fileList == null || fileList.size() == 0) {
return;
}
+
+ boolean formUpload = multipartRequest.isFormUpload();
+
+ if (isFlash && !formUpload && fileList.size() > 0) {
+ try {
+ UploadItem item = fileList.get(0);
+ int actualSize = item.getFileSize();
+ int clientSize =
Integer.parseInt(requestParameterMap.get("_richfaces_size"));
+
+ if (actualSize != clientSize) {
+ return; // File uploading has been stopped on the client side
+ }
+ }catch (Exception e) {
+ return;
+ }
+ }
+ onUploadComplete(context, fileList, fileUpload, formUpload);
}
- onUploadComplete(context, fileList, fileUpload, formUpload);
}
+
}
@@ -451,7 +452,7 @@
Map options = AjaxRendererUtils.buildEventOptions(context, component);
Map parameters = (Map) options.get("parameters");
- parameters.put("action", action);
+ parameters.put(FileUploadConstants.FILE_UPLOAD_ACTION, action);
parameters.put(FileUploadConstants.UPLOAD_FILES_ID, new JSReference("uid"));
parameters.put(clientId, clientId);
parameters.put(AjaxRendererUtils.AJAX_SINGLE_ATTR, clientId);