1 5 package org.exoplatform.faces.core.renderer.html; 6 7 8 import javax.faces.component.UIComponent; 9 import javax.faces.context.ExternalContext; 10 import javax.faces.context.FacesContext; 11 import javax.faces.context.ResponseWriter; 12 import org.exoplatform.faces.FacesConstants; 13 import org.exoplatform.faces.core.component.UIFileUpload; 14 import java.io.IOException ; 15 import java.util.ResourceBundle ; 16 20 public class FileUploadRenderer extends HtmlBasicRenderer { 21 final public void encodeBegin( FacesContext context, UIComponent component ) throws IOException { 22 UIFileUpload uicomp = (UIFileUpload) component; 23 ExternalContext econtext = context.getExternalContext() ; 24 ResourceBundle res = getApplicationResourceBundle(econtext) ; 25 String uploadHeader = "Upload File" ; 26 String fileLabel = "File" ; 27 String nameLabel = "Name" ; 28 try { 29 uploadHeader = res.getString("UIFileUpload.header.upload-file-header") ; 30 fileLabel = res.getString("UIFileUpload.label.file") ; 31 nameLabel = res.getString("UIFileUpload.label.name") ; 32 } catch (Exception ex) {} 33 String baseUrl = uicomp.getBaseURL(context); 34 ResponseWriter w = context.getResponseWriter() ; 35 String clazz = uicomp.getClazz() ; 36 if(clazz == null) clazz = "" ; 37 w.write("<form name='uploadForm' action='"+ baseUrl +"' method='post' enctype='multipart/form-data'") ; 38 w.write(" class='UISimpleForm"); 39 if(clazz != null) { 40 w.write(' ') ; w.write(clazz) ; 41 } 42 w.write("'>\n"); 43 w. write("<input type='hidden' name='"+ FacesConstants.ACTION +"' value=''/>\n") ; 44 w. write("<table>") ; 45 if(uicomp.isShowHeader()) { 46 w. write("<tr>\n") ; 47 w. write("<th colspan='2'>\n") ; w.write(uploadHeader) ;w.write("</th>\n") ; 48 w. write("</tr>\n") ; 49 } 50 int numOfField = uicomp.getNumberOfField() ; 51 for (int i = 1 ; i <= numOfField; i++) { 52 w. write("<tr>\n") ; 53 w. write("<td>\n") ; w.write(fileLabel + " " + i) ; w.write("</td>\n") ; 54 w. write("<td>\n") ; w.write("<input type='file' name='file-" + i + "'/>") ;w.write("</td>\n") ; 55 w. write("</tr>\n") ; 56 w. write("<tr>\n") ; 57 w. write("<td>\n") ; w.write(nameLabel + " " + i) ; w.write("</td>\n") ; 58 w. write("<td>\n") ; w.write("<input name='name-" + i + "' value=''/>") ; w.write("</td>\n") ; 59 w. write("</tr>\n") ; 60 } 61 w. write("<tr>\n") ; 62 w. write(getScript()) ; 63 w. write("<td colspan='2' align='center' style='padding: 10px'>") ; 64 w. write("<input style='width: auto' type='submit' value='Upload' onclick=\"setAction('upload')\"/>") ; 65 if(uicomp.isShowCancel()) { 66 w. write("<input style='width: auto' type='submit' value='Cancel' onclick=\"setAction('cancel')\"/>") ; 67 } 68 w. write("</td>") ; 69 w. write("</tr>\n") ; 70 w. write("</table>\n") ; 71 w.write("</form>\n"); 72 } 73 74 private String getScript() { 75 String script = 76 "<script type=\"text/javascript\">\n" + 77 " function setAction(action) {\n" + 78 " document.uploadForm.elements['"+ ACTION +"'].value = action ;\n" + 79 " }\n" + 80 "</script>\n" ; 81 return script ; 82 } 83 } | Popular Tags |