Author: andrei_exadel
Date: 2008-03-18 11:11:24 -0400 (Tue, 18 Mar 2008)
New Revision: 6910
Modified:
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
Log:
RF-2507
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-18
15:11:16 UTC (rev 6909)
+++
trunk/ui/fileUpload/src/main/java/org/richfaces/renderkit/FileUploadRendererBase.java 2008-03-18
15:11:24 UTC (rev 6910)
@@ -3,6 +3,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
@@ -48,581 +49,624 @@
*
*/
public abstract class FileUploadRendererBase extends
- TemplateEncoderRendererBase {
+ TemplateEncoderRendererBase {
- /** Attribute name where collection of files uploaded will be stored */
- private static final String _FILES_UPLOADED_ATTRIBUTE_NAME = "uploadData";
+ /** 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();
+ /** Multipart request class name */
+ private static final String _MULTIPART_REQUEST_CLASS_NAME = MultipartRequest.class
+ .getName();
- /** File upload bundle name */
- private static final String FILE_UPLOAD_BUNDLE =
"org.richfaces.renderkit.fileUpload";
+ /** 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 prefix */
+ private static final String bundlePrefix = "RICH_FILE_UPLOAD_";
- /** Bundle postfix */
- private static final String bundlePostfix = "_LABEL";
+ /** 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" };
+ /** 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" };
+ /** 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 };
+ /** 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()
- .getRequest();
+ /**
+ * 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()
+ .getRequest();
- String clientId = component.getClientId(context);
+ String clientId = component.getClientId(context);
- new AjaxEvent(component).queue();
+ new AjaxEvent(component).queue();
- Class requestClazz = request.getClass();
+ Class requestClazz = request.getClass();
- if (_MULTIPART_REQUEST_CLASS_NAME.equals(requestClazz.getName())
- || "org.jboss.seam.web.MultipartRequest".equals(requestClazz
- .getName())) {
+ 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);
+ HttpSession session = (HttpSession) context.getExternalContext()
+ .getSession(false);
+ String uid = request.getParameter(Filter.UPLOAD_FILES_ID);
+ String id = request.getParameter("id");
+ decreaseFileCounter(session, id);
+ Map<String, MultipartRequest> map = (Map<String, MultipartRequest>)
session
+ .getAttribute(Filter.REQUESTS_SESSIONS_BEAN_NAME);
- MultipartRequest multipartRequest = map.get(uid);
+ MultipartRequest multipartRequest = map.get(uid);
- clientId = clientId + ":file";
- String fileName = multipartRequest.getFileName(clientId);
- Object file = multipartRequest.getFile(clientId);
- fileUpload.setLocalFileName(fileName);
- onUploadComplete(context, file, fileUpload);
+ clientId = clientId + ":file";
+ String fileName = multipartRequest.getFileName(clientId);
+ Object file = multipartRequest.getFile(clientId);
+ fileUpload.setLocalFileName(fileName);
+ onUploadComplete(context, file, fileUpload);
+ }
+
}
- }
-
- /**
- * Gets form id
- * @param context - faces context
- * @param component - component
- * @return String form id
- */
- public String getFormId(FacesContext context, UIComponent component) {
- UIComponent form = AjaxRendererUtils.getNestingForm(component);
- if (form != null) {
- return form.getClientId(context);
+ /**
+ * Put max file count into session scope
+ *
+ * @param context
+ * @param component
+ * @param clientId
+ * @return
+ */
+ @SuppressWarnings("unchecked")
+ public Integer initMaxFilesCount(FacesContext context,
+ UIComponent component, String clientId) {
+ Integer max = (Integer) component.getAttributes().get(
+ "maxFilesQuantity");
+ HttpSession session = (HttpSession) context.getExternalContext()
+ .getSession(false);
+ synchronized (session) {
+ Map<String, Integer> map = (Map<String, Integer>) session
+ .getAttribute(Filter.UPLOADED_COUNTER);
+ if (map == null) {
+ map = Collections
+ .synchronizedMap(new HashMap<String, Integer>());
+ }
+ map.put(clientId, max);
+ }
+ return max;
}
- return "";
- }
-
-
- /**
- * Gets container id
- * @param context - faces context
- * @param component - component
- * @return String container id
- */
- public String getContainerId(FacesContext context, UIComponent component) {
- UIComponent container = (UIComponent)AjaxRendererUtils.findAjaxContainer(context,
component);
- if (container != null) {
- return container.getClientId(context);
+
+ /**
+ * Gets form id
+ *
+ * @param context -
+ * faces context
+ * @param component -
+ * component
+ * @return String form id
+ */
+ public String getFormId(FacesContext context, UIComponent component) {
+ UIComponent form = AjaxRendererUtils.getNestingForm(component);
+ if (form != null) {
+ return form.getClientId(context);
+ }
+ return "";
}
- return null;
- }
- /**
- * 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();
+ /**
+ * Gets container id
+ *
+ * @param context -
+ * faces context
+ * @param component -
+ * component
+ * @return String container id
+ */
+ public String getContainerId(FacesContext context, UIComponent component) {
+ UIComponent container = (UIComponent) AjaxRendererUtils
+ .findAjaxContainer(context, component);
+ if (container != null) {
+ return container.getClientId(context);
+ }
+ return null;
}
- 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.
+ /**
+ * 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);
}
- 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;
+ /**
+ * 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 (!found && (bundle1 != null || bundle2 != null)) {
- String label = getFromBundle(name, bundle1, bundle2);
- if (label != null) {
- map.put(name, getFromBundle(name, bundle1, bundle2));
- found = true;
+ if (null != messageBundle) {
+ bundle1 = ResourceBundle.getBundle(messageBundle, fileUpload
+ .getAsLocale(locale), loader);
}
- }
- if (!found) {
- map.put(name, defaultLables[i]);
- }
- i++;
+ 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;
}
- }
- /**
- * 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
- }
+ /**
+ * 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
- }
+ }
+
+ } 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;
}
- return v;
- }
- /**
- * Method checks if uploaded files count overflowed
- *
- * @param fileUpload
- * @param map
- * @return
- */
- private boolean checkFileCount(UIFileUpload fileUpload,
- Collection filesUploaded) {
- Integer max = fileUpload.getMaxFilesQuantity();
- if (filesUploaded != null && filesUploaded.size() >= max) {
- fileUpload.reset();
- return false;
+ /**
+ * Method decrease file counter
+ *
+ * @param session
+ * @param id
+ */
+ @SuppressWarnings("unchecked")
+ private void decreaseFileCounter(HttpSession session, String id) {
+ Map<String, Integer> map = (Map<String, Integer>) session
+ .getAttribute(Filter.UPLOADED_COUNTER);
+ if (map != null) {
+ Integer i = map.get(id);
+ if (i == null) {
+ i = 0;
+ } else {
+ i--;
+ }
+ map.put(id, i);
+ }
}
- return true;
- }
- /**
- * Put uploaded file into data attribute defined
- *
- * @param context
- * @param fileUpload
- * @param file
- */
- @SuppressWarnings("unchecked")
- private void storeData(FacesContext context, UIFileUpload fileUpload,
- Object file) {
- ValueExpression data = fileUpload
- .getValueExpression(_FILES_UPLOADED_ATTRIBUTE_NAME);
- UploadItem item = new UploadItem(fileUpload.getLocalFileName(), file);
- if (data != null) {
- if (data.getValue(context.getELContext()) instanceof Collection) {
- Collection collection = (Collection) data.getValue(context
- .getELContext());
- if (checkFileCount(fileUpload, collection)) {
- collection.add(item);
- } else {
- throw new FileUploadException("Files count overflow");
+ /**
+ * Put uploaded file into data attribute defined
+ *
+ * @param context
+ * @param fileUpload
+ * @param file
+ */
+ @SuppressWarnings("unchecked")
+ private void storeData(FacesContext context, UIFileUpload fileUpload,
+ Object file) {
+ ValueExpression data = fileUpload
+ .getValueExpression(_FILES_UPLOADED_ATTRIBUTE_NAME);
+ UploadItem item = new UploadItem(fileUpload.getLocalFileName(), file);
+ if (data != null) {
+ if (data.getValue(context.getELContext()) instanceof Collection) {
+ Collection collection = (Collection) data.getValue(context
+ .getELContext());
+ collection.add(item);
+ }
}
- }
+ new UploadEvent(fileUpload, item).queue();
}
- new UploadEvent(fileUpload, item).queue();
- }
- /**
- * Call with method after uploading completed
- *
- * @param context
- * @param file
- * @param fileUpload
- */
- private void onUploadComplete(FacesContext context, Object file,
- UIFileUpload fileUpload) {
- storeData(context, fileUpload, file);
- try {
- AjaxContext ajaxContext = AjaxContextImpl
- .getCurrentInstance(context);
- ajaxContext.setAjaxRequest(true);
- } catch (Exception e) {
- e.getMessage();
+ /**
+ * Call with method after uploading completed
+ *
+ * @param context
+ * @param file
+ * @param fileUpload
+ */
+ private void onUploadComplete(FacesContext context, Object file,
+ UIFileUpload fileUpload) {
+ storeData(context, fileUpload, file);
+ try {
+ AjaxContext ajaxContext = AjaxContextImpl
+ .getCurrentInstance(context);
+ ajaxContext.setAjaxRequest(true);
+ } catch (Exception e) {
+ e.getMessage();
+ }
}
- }
- /*
- * (non-Javadoc)
- *
- * @see org.ajax4jsf.renderkit.RendererBase#getComponentClass()
- */
- @Override
- protected Class<? extends UIComponent> getComponentClass() {
- return UIFileUpload.class;
- }
-
- /**
- * Generates common JS script by action value
- *
- * @param context
- * @param component
- * @param action
- * @param oncomplete
- * @return
- * @throws IOException
- */
- @SuppressWarnings("unchecked")
- private String getActionScript(FacesContext context, UIComponent component,
- String action, Object oncomplete) throws IOException {
- ComponentVariables variables = ComponentsVariableResolver.getVariables(this,
component);
- String clientId = component.getClientId(context);
- String containerId = (String)variables.getVariable("containerId");
- JSFunction ajaxFunction = new JSFunction(AjaxRendererUtils.AJAX_FUNCTION_NAME);
- ajaxFunction.addParameter(containerId);
- ajaxFunction.addParameter(new JSReference("formId"));
- ajaxFunction.addParameter(new JSReference("event"));
-// AjaxRendererUtils.buildAjaxFunction(
-// component, context);
-
- Map options = AjaxRendererUtils.buildEventOptions(context, component);
- Map parameters = (Map) options.get("parameters");
- parameters.put("action", action);
- parameters.put(Filter.UPLOAD_FILES_ID, new JSReference("uid"));
- parameters.put(clientId, clientId);
- if (oncomplete != null) {
- options.put("onbeforedomupdate", oncomplete);
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.ajax4jsf.renderkit.RendererBase#getComponentClass()
+ */
+ @Override
+ protected Class<? extends UIComponent> getComponentClass() {
+ return UIFileUpload.class;
}
- ajaxFunction.addParameter(options);
- JSFunctionDefinition function = new JSFunctionDefinition("uid");
- function.addParameter("formId");
- function.addParameter("event");
- function.addToBody(ajaxFunction.toScript());
+ /**
+ * Generates common JS script by action value
+ *
+ * @param context
+ * @param component
+ * @param action
+ * @param oncomplete
+ * @return
+ * @throws IOException
+ */
+ @SuppressWarnings("unchecked")
+ private String getActionScript(FacesContext context, UIComponent component,
+ String action, Object oncomplete) throws IOException {
+ ComponentVariables variables = ComponentsVariableResolver.getVariables(
+ this, component);
+ String clientId = component.getClientId(context);
+ String containerId = (String) variables.getVariable("containerId");
+ JSFunction ajaxFunction = new JSFunction(
+ AjaxRendererUtils.AJAX_FUNCTION_NAME);
+ ajaxFunction.addParameter(containerId);
+ ajaxFunction.addParameter(new JSReference("formId"));
+ ajaxFunction.addParameter(new JSReference("event"));
+ // AjaxRendererUtils.buildAjaxFunction(
+ // component, context);
- return function.toScript();
- }
+ Map options = AjaxRendererUtils.buildEventOptions(context, component);
+ Map parameters = (Map) options.get("parameters");
+ parameters.put("action", action);
+ parameters.put(Filter.UPLOAD_FILES_ID, new JSReference("uid"));
+ parameters.put(clientId, clientId);
+ if (oncomplete != null) {
+ options.put("onbeforedomupdate", oncomplete);
+ }
+ ajaxFunction.addParameter(options);
- /**
- * 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);
+ JSFunctionDefinition function = new JSFunctionDefinition("uid");
+ function.addParameter("formId");
+ function.addParameter("event");
+ function.addToBody(ajaxFunction.toScript());
+
+ 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 ScriptUtils.toScript(accepted);
- }
+ return JSReference.NULL;
}
- return JSReference.NULL;
- }
- /**
- * Generates JS script for stopping uploading process
- *
- * @param context
- * @param component
- * @return
- * @throws IOException
- */
- public String getStopScript(FacesContext context, UIComponent component)
- throws IOException {
- return getActionScript(context, component, "stop", null);
- }
+ /**
+ * Generates JS script for stopping uploading process
+ *
+ * @param context
+ * @param component
+ * @return
+ * @throws IOException
+ */
+ public String getStopScript(FacesContext context, UIComponent component)
+ throws IOException {
+ return getActionScript(context, component, "stop", null);
+ }
- /**
- * Generates JS script for getting file size from server
- *
- * @param context
- * @param component
- * @return
- * @throws IOException
- */
- 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);
+ /**
+ * Generates JS script for getting file size from server
+ *
+ * @param context
+ * @param component
+ * @return
+ * @throws IOException
+ */
+ 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);
- }
+ }
- /**
- * Generates progress label markup
- *
- * @param context
- * @param component
- * @return
- * @throws IOException
- */
- 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) {
+ /**
+ * Generates progress label markup
+ *
+ * @param context
+ * @param component
+ * @return
+ * @throws IOException
+ */
+ 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();
+ 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);
+ 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);
- writeScriptBody(context, label, false);
- if (writer != null) {
- context.setResponseWriter(writer);
+ ResponseWriter responseWriter = renderKit.createResponseWriter(
+ customWriter, null, "UTF-8");
+ context.setResponseWriter(responseWriter);
+ writeScriptBody(context, label, false);
+ if (writer != null) {
+ context.setResponseWriter(writer);
+ }
+ result = customWriter.getContent();
+ }
+
+ } catch (Exception e) {
+ e.getMessage();
}
- result = customWriter.getContent();
- }
+ return (result != null) ? new JSLiteral(result.toString())
+ : JSReference.NULL;
+ }
- } catch (Exception e) {
- e.getMessage();
+ /**
+ * Gets a string representing css specific height of downloaded file list
+ * panel.
+ *
+ * @param component
+ * file upload component
+ * @return a string representing css specific height of downloaded file list
+ * panel
+ */
+ public String getFileListHeight(UIFileUpload component) {
+ return HtmlUtil.qualifySize(component.getListHeight());
}
- return (result != null) ? new JSLiteral(result.toString())
- : JSReference.NULL;
- }
- /**
- * Gets a string representing css specific height of downloaded file list panel.
- *
- * @param component file upload component
- * @return a string representing css specific height of downloaded file list panel
- */
- public String getFileListHeight(UIFileUpload component) {
- return HtmlUtil.qualifySize(component.getListHeight());
- }
+ /**
+ * Gets a string representing css specific width of downloaded file list
+ * panel.
+ *
+ * @param component
+ * file upload component
+ * @return a string representing css specific width of downloaded file list
+ * panel
+ */
+ public String getFileListWidth(UIFileUpload component) {
+ return HtmlUtil.qualifySize(component.getListWidth());
+ }
- /**
- * Gets a string representing css specific width of downloaded file list panel.
- *
- * @param component file upload component
- * @return a string representing css specific width of downloaded file list panel
- */
- public String getFileListWidth(UIFileUpload component) {
- return HtmlUtil.qualifySize(component.getListWidth());
- }
+ /**
+ * Generate component custom events functions
+ *
+ * @param context
+ * @param component
+ * @param attributeName
+ * @return
+ */
+ public String getAsEventHandler(FacesContext context,
+ UIComponent component, String attributeName) {
+ Object eventHandler = RendererUtils.getInstance().getAsEventHandler(
+ context, component, attributeName, "");
+ if (eventHandler != null) {
+ return eventHandler.toString();
+ }
+ return JSReference.NULL.toScript();
+ }
- /**
- * Generate component custom events functions
- *
- * @param context
- * @param component
- * @param attributeName
- * @return
- */
- public String getAsEventHandler(FacesContext context,
- UIComponent component, String attributeName) {
- Object eventHandler = RendererUtils.getInstance().getAsEventHandler(
- context, component, attributeName, "");
- if (eventHandler != null) {
- return eventHandler.toString();
+ /**
+ * Gets progress bar Id
+ *
+ * @param context
+ * @param component
+ * @return
+ * @throws IOException
+ */
+ public String getProgressBarId(FacesContext context, UIComponent component)
+ throws IOException {
+ return getProgressBar(context, component).getClientId(context);
}
- return JSReference.NULL.toScript();
- }
- /**
- * Gets progress bar Id
- *
- * @param context
- * @param component
- * @return
- * @throws IOException
- */
- public String getProgressBarId(FacesContext context, UIComponent component)
- throws IOException {
- return getProgressBar(context, component).getClientId(context);
- }
+ /**
+ * Renders progress bar
+ *
+ * @param context
+ * @param component
+ * @throws IOException
+ */
+ public void renderProgress(FacesContext context, UIComponent component)
+ throws IOException {
+ UIComponent progressBar = getProgressBar(context, component);
+ renderChild(context, progressBar);
+ }
- /**
- * Renders progress bar
- *
- * @param context
- * @param component
- * @throws IOException
- */
- public void renderProgress(FacesContext context, UIComponent component)
- throws IOException {
- UIComponent progressBar = getProgressBar(context, component);
- renderChild(context, progressBar);
- }
+ /**
+ * Creates progress bar component
+ *
+ * @param context
+ * @param fileUpload
+ * @return
+ */
+ private UIComponent createProgressBar(FacesContext context,
+ UIComponent fileUpload) {
+ UIComponent progressBar = fileUpload.getFacet("progress");
+ if (null == progressBar) {
+ progressBar = context.getApplication().createComponent(
+ UIProgressBar.COMPONENT_TYPE);
+ }
+ fileUpload.getFacets().put("progress", progressBar);
+ return progressBar;
+ }
- /**
- * Creates progress bar component
- *
- * @param context
- * @param fileUpload
- * @return
- */
- private UIComponent createProgressBar(FacesContext context,
- UIComponent fileUpload) {
- UIComponent progressBar = fileUpload.getFacet("progress");
- if (null == progressBar) {
- progressBar = context.getApplication().createComponent(
- UIProgressBar.COMPONENT_TYPE);
+ /**
+ * Returns progress bar
+ *
+ * @param context
+ * @param component
+ * @return
+ */
+ public UIComponent getProgressBar(FacesContext context,
+ UIComponent component) {
+ UIComponent progressBar = component.getFacet("progress");
+ if (null == progressBar) {
+ progressBar = createProgressBar(context, component);
+ }
+ progressBar.getAttributes().put("minValue", -1);
+ progressBar.getAttributes().put("enabled", false);
+ progressBar.setTransient(false);
+ return progressBar;
}
- fileUpload.getFacets().put("progress", progressBar);
- return progressBar;
- }
- /**
- * Returns progress bar
- *
- * @param context
- * @param component
- * @return
- */
- public UIComponent getProgressBar(FacesContext context,
- UIComponent component) {
- UIComponent progressBar = component.getFacet("progress");
- if (null == progressBar) {
- progressBar = createProgressBar(context, component);
+ /*
+ * (non-Javadoc)
+ *
+ * @see
org.richfaces.renderkit.TemplateEncoderRendererBase#encodeChildren(javax.faces.context.FacesContext,
+ * javax.faces.component.UIComponent)
+ */
+ @Override
+ public void encodeChildren(FacesContext context, UIComponent component)
+ throws IOException {
+ ; // We should not render children
}
- progressBar.getAttributes().put("minValue", -1);
- progressBar.getAttributes().put("enabled", false);
- progressBar.setTransient(false);
- return progressBar;
- }
- /*
- * (non-Javadoc)
- *
- * @see
org.richfaces.renderkit.TemplateEncoderRendererBase#encodeChildren(javax.faces.context.FacesContext,
- * javax.faces.component.UIComponent)
- */
- @Override
- public void encodeChildren(FacesContext context, UIComponent component)
- throws IOException {
- ; // We should not render children
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see
org.ajax4jsf.renderkit.RendererBase#doEncodeChildren(javax.faces.context.ResponseWriter,
+ * javax.faces.context.FacesContext, javax.faces.component.UIComponent)
+ */
+ @Override
+ public void doEncodeChildren(ResponseWriter writer, FacesContext context,
+ UIComponent component) throws IOException {
+ ; // We should not render children
+ }
- /*
- * (non-Javadoc)
- *
- * @see
org.ajax4jsf.renderkit.RendererBase#doEncodeChildren(javax.faces.context.ResponseWriter,
- * javax.faces.context.FacesContext, javax.faces.component.UIComponent)
- */
- @Override
- public void doEncodeChildren(ResponseWriter writer, FacesContext context,
- UIComponent component) throws IOException {
- ; // We should not render children
- }
-
}
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-18
15:11:16 UTC (rev 6909)
+++
trunk/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js 2008-03-18
15:11:24 UTC (rev 6910)
@@ -11,14 +11,15 @@
FileUploadEntry.UPLOAD_TRANSFER_ERROR = "transfer_error";
FileUploadEntry.UPLOAD_SERVER_ERROR = "server_error";
FileUploadEntry.UPLOAD_SIZE_ERROR = "size_error";
+FileUploadEntry.UPLOAD_FORBIDDEN = "forbidden";
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_FORBIDDEN] = 'Uploading
forbidden';
-
FileUploadEntry.clearControlTemplate =
[
new E('a',
@@ -315,10 +316,13 @@
var iframeDocument = this.iframe.contentWindow.document;
var elt = iframeDocument.getElementById('_richfaces_file_upload_stopped');
var restr =
iframeDocument.getElementById('_richfaces_file_upload_size_restricted');
+ var forb = iframeDocument.getElementById('_richfaces_file_upload_forbidden');
if (elt) {
this.callback(FileUploadEntry.UPLOAD_CANCELED);
} else if (restr) {
this.callback(FileUploadEntry.UPLOAD_SIZE_ERROR);
+ } else if (forb) {
+ this.callback(FileUploadEntry.UPLOAD_SIZE_ERROR);
} else {
var state = iframeDocument.getElementById('javax.faces.ViewState');
if (state) {
@@ -891,7 +895,7 @@
parentForm.encoding = "multipart/form-data";
parentForm.enctype = "multipart/form-data";
parentForm.target = this.id + "_iframe";//this.id;
- parentForm.action = oldAction + (/\?/.test(oldAction) ?
'&_richfaces_upload_uid' : '?_richfaces_upload_uid') + '=' +
encodeURI(entry.uid);
+ parentForm.action = oldAction + (/\?/.test(oldAction) ?
'&_richfaces_upload_uid' : '?_richfaces_upload_uid') + '=' +
encodeURI(entry.uid) + "&id=" + this.id;
try {
var inputs = parentForm.elements;
Modified: trunk/ui/fileUpload/src/main/templates/org/richfaces/fileUpload.jspx
===================================================================
--- trunk/ui/fileUpload/src/main/templates/org/richfaces/fileUpload.jspx 2008-03-18
15:11:16 UTC (rev 6909)
+++ trunk/ui/fileUpload/src/main/templates/org/richfaces/fileUpload.jspx 2008-03-18
15:11:24 UTC (rev 6910)
@@ -46,6 +46,14 @@
variables.setVariable("listWidth", getFileListWidth(component));
variables.setVariable("listHeight", getFileListHeight(component));
+
+ Integer max = initMaxFilesCount(context, component, clientId);
+ String addStyle = "";
+ if (max == 0) {
+ addStyle = "; display: none;";
+ }
+ variables.setVariable("addStyle", addStyle);
+ variables.setVariable("maxCount", max);
]]>
</jsp:scriptlet>
@@ -65,7 +73,7 @@
<table class="upload_toolbar_decor">
<tr>
<td>
- <div class="upload_button_border" style=" float:left;">
+ <div class="upload_button_border" style=" float:left;
#{addStyle}">
<div class="#{addButtonClass}"
onmouseover="this.className='upload_button_light upload_font'"
onmousedown="this.className='upload_button_press upload_font'"
onmouseup="this.className='upload_button upload_font'"
onmouseout="this.className='upload_button upload_font'"
style="position: relative;"
id="#{clientId}:add1">
@@ -175,7 +183,7 @@
onerror : #{this:getAsEventHandler(context, component, "onerror")}
};
- new FileUpload('#{clientId}','#{formId}',#{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"]},'autoUpload':#{component.attributes["immediateUpload"]}},#{this:_getLabels(labels)});
+ new FileUpload('#{clientId}','#{formId}',#{this:getStopScript(context,
component)}, #{this:getFileSizeScript(context, component)}
,'#{this:getProgressBarId(context, component)}', FileUpload.CLASSES,
#{this:getLabelMarkup(context, component)}, #{maxCount}, events,
#{component.attributes["disabled"]}, #{this:getAcceptedTypes(context,
component)},
{'autoclear':#{component.attributes["autoclear"]},'autoUpload':#{component.attributes["immediateUpload"]}},#{this:_getLabels(labels)});
</script>
</span>
<f:call name="utils.encodeEndFormIfNessesary" />