martin-g commented on a change in pull request #467:
URL: https://github.com/apache/wicket/pull/467#discussion_r610330790



##########
File path: 
wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/FileUploadField.java
##########
@@ -47,6 +59,146 @@
 {
        private static final long serialVersionUID = 1L;
 
+       /**
+        * Description of file properties as in browser client side.
+        */
+       public static class FileDescription implements Serializable  {
+
+               private static final long serialVersionUID = 1L;
+
+               private final String fileName;
+               private final long fileSize;
+               private final Date lastModified;
+               private final String mimeType;
+
+               public FileDescription(String fileName, long fileSize, long 
lastModified, String mimeType) {
+                       this.fileName = fileName;
+                       this.fileSize = fileSize;
+                       this.lastModified = new Date(lastModified);
+                       this.mimeType = mimeType;
+               }
+
+               public String getFileName() {
+                       return fileName;
+               }
+
+               public long getFileSize() {
+                       return fileSize;
+               }
+
+               public Date getLastModified() {
+                       return lastModified;
+               }
+
+               public String getMimeType() {
+                       return mimeType;
+               }
+
+               @Override
+               public boolean equals(Object o) {
+                       if (this == o) return true;
+                       if (o == null || getClass() != o.getClass()) return 
false;
+                       FileDescription that = (FileDescription) o;
+                       return fileName.equals(that.fileName);
+               }
+
+               @Override
+               public int hashCode() {
+                       return Objects.hash(fileName);
+               }
+       }
+
+       /**
+        *  {@link org.apache.wicket.ajax.form.OnChangeAjaxBehavior} that 
streams back to server properties
+        *  of the selected file (at client side), even when file has not yet 
being uploaded.
+s       *
+        * @author Ernesto Reinaldo Barreiro (reier...@gmail.com).
+        */
+       public static abstract class OnFileSelectedBehavior extends 
OnChangeAjaxBehavior
+       {
+               private static final long serialVersionUID = 1L;
+
+               @Override
+               protected void onBind() {
+                       super.onBind();
+                       Component component = getComponent();
+                       if (!(component instanceof FileUploadField))
+                       {
+                               throw new WicketRuntimeException("Behavior " + 
getClass().getName()
+                                               + " can only be added to an 
instance of a FileUploadField");
+                       }
+               }
+
+               @Override
+               protected void onUpdate(AjaxRequestTarget target)
+               {
+                       Request request = RequestCycle.get().getRequest();
+                       List<FileDescription> fileDescriptions = new 
ArrayList<>();
+                       IRequestParameters parameters = 
request.getRequestParameters();
+                       String[] fileNames = 
parse(parameters.getParameterValue("fileName"));
+                       String[] fileSizes = 
parse(parameters.getParameterValue("fileSize"));
+                       String[] lastModifiedDates = 
parse(parameters.getParameterValue("lastModified"));
+                       String[] mimeTypes = 
parse(parameters.getParameterValue("mimeType"));
+                       for (int i = 0; i < fileNames.length; i++) {
+                               FileDescription fileDescription = new 
FileDescription(
+                                               fileNames[i],
+                                               Long.parseLong(fileSizes[i]),

Review comment:
       Prefer `Long.parseLong(String s, int radix)` instead

##########
File path: 
wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/FileUploadField.java
##########
@@ -47,6 +59,146 @@
 {
        private static final long serialVersionUID = 1L;
 
+       /**
+        * Description of file properties as in browser client side.
+        */
+       public static class FileDescription implements Serializable  {
+
+               private static final long serialVersionUID = 1L;
+
+               private final String fileName;
+               private final long fileSize;
+               private final Date lastModified;
+               private final String mimeType;
+
+               public FileDescription(String fileName, long fileSize, long 
lastModified, String mimeType) {
+                       this.fileName = fileName;
+                       this.fileSize = fileSize;
+                       this.lastModified = new Date(lastModified);
+                       this.mimeType = mimeType;
+               }
+
+               public String getFileName() {
+                       return fileName;
+               }
+
+               public long getFileSize() {
+                       return fileSize;
+               }
+
+               public Date getLastModified() {
+                       return lastModified;
+               }
+
+               public String getMimeType() {
+                       return mimeType;
+               }
+
+               @Override
+               public boolean equals(Object o) {
+                       if (this == o) return true;
+                       if (o == null || getClass() != o.getClass()) return 
false;
+                       FileDescription that = (FileDescription) o;
+                       return fileName.equals(that.fileName);
+               }
+
+               @Override
+               public int hashCode() {
+                       return Objects.hash(fileName);
+               }
+       }
+
+       /**
+        *  {@link org.apache.wicket.ajax.form.OnChangeAjaxBehavior} that 
streams back to server properties
+        *  of the selected file (at client side), even when file has not yet 
being uploaded.
+s       *
+        * @author Ernesto Reinaldo Barreiro (reier...@gmail.com).
+        */
+       public static abstract class OnFileSelectedBehavior extends 
OnChangeAjaxBehavior
+       {
+               private static final long serialVersionUID = 1L;
+
+               @Override
+               protected void onBind() {
+                       super.onBind();
+                       Component component = getComponent();
+                       if (!(component instanceof FileUploadField))
+                       {
+                               throw new WicketRuntimeException("Behavior " + 
getClass().getName()
+                                               + " can only be added to an 
instance of a FileUploadField");
+                       }
+               }
+
+               @Override
+               protected void onUpdate(AjaxRequestTarget target)
+               {
+                       Request request = RequestCycle.get().getRequest();
+                       List<FileDescription> fileDescriptions = new 
ArrayList<>();
+                       IRequestParameters parameters = 
request.getRequestParameters();
+                       String[] fileNames = 
parse(parameters.getParameterValue("fileName"));
+                       String[] fileSizes = 
parse(parameters.getParameterValue("fileSize"));
+                       String[] lastModifiedDates = 
parse(parameters.getParameterValue("lastModified"));
+                       String[] mimeTypes = 
parse(parameters.getParameterValue("mimeType"));
+                       for (int i = 0; i < fileNames.length; i++) {
+                               FileDescription fileDescription = new 
FileDescription(
+                                               fileNames[i],
+                                               Long.parseLong(fileSizes[i]),
+                                               
Long.parseLong(lastModifiedDates[i]),
+                                               mimeTypes[i]);
+                               fileDescriptions.add(fileDescription);
+                       }
+                       onFileSelected(target, fileDescriptions);
+               }
+
+               private String[] parse(StringValue stringValue) {
+                       return stringValue.toString().split(",");
+               }
+
+               /**
+                * Called when a file, at client side is selected.
+                *
+                * @param target The {@link 
org.apache.wicket.ajax.AjaxRequestTarget}
+                * @param fileDescriptions A list of FileDescription
+                */
+               protected abstract void onFileSelected(AjaxRequestTarget 
target, List<FileDescription> fileDescriptions);

Review comment:
       s/onFileSelected/onSelect/ ? I think @svenmeier also suggested this name

##########
File path: 
wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/FileUploadField.java
##########
@@ -47,6 +59,146 @@
 {
        private static final long serialVersionUID = 1L;
 
+       /**
+        * Description of file properties as in browser client side.
+        */
+       public static class FileDescription implements Serializable  {
+
+               private static final long serialVersionUID = 1L;
+
+               private final String fileName;
+               private final long fileSize;
+               private final Date lastModified;
+               private final String mimeType;
+
+               public FileDescription(String fileName, long fileSize, long 
lastModified, String mimeType) {
+                       this.fileName = fileName;
+                       this.fileSize = fileSize;
+                       this.lastModified = new Date(lastModified);
+                       this.mimeType = mimeType;
+               }
+
+               public String getFileName() {
+                       return fileName;
+               }
+
+               public long getFileSize() {
+                       return fileSize;
+               }
+
+               public Date getLastModified() {
+                       return lastModified;
+               }
+
+               public String getMimeType() {
+                       return mimeType;
+               }
+
+               @Override
+               public boolean equals(Object o) {
+                       if (this == o) return true;
+                       if (o == null || getClass() != o.getClass()) return 
false;
+                       FileDescription that = (FileDescription) o;
+                       return fileName.equals(that.fileName);
+               }
+
+               @Override
+               public int hashCode() {
+                       return Objects.hash(fileName);
+               }
+       }
+
+       /**
+        *  {@link org.apache.wicket.ajax.form.OnChangeAjaxBehavior} that 
streams back to server properties
+        *  of the selected file (at client side), even when file has not yet 
being uploaded.
+s       *
+        * @author Ernesto Reinaldo Barreiro (reier...@gmail.com).
+        */
+       public static abstract class OnFileSelectedBehavior extends 
OnChangeAjaxBehavior
+       {
+               private static final long serialVersionUID = 1L;
+
+               @Override
+               protected void onBind() {
+                       super.onBind();
+                       Component component = getComponent();
+                       if (!(component instanceof FileUploadField))
+                       {
+                               throw new WicketRuntimeException("Behavior " + 
getClass().getName()
+                                               + " can only be added to an 
instance of a FileUploadField");
+                       }
+               }
+
+               @Override
+               protected void onUpdate(AjaxRequestTarget target)
+               {
+                       Request request = RequestCycle.get().getRequest();
+                       List<FileDescription> fileDescriptions = new 
ArrayList<>();
+                       IRequestParameters parameters = 
request.getRequestParameters();
+                       String[] fileNames = 
parse(parameters.getParameterValue("fileName"));
+                       String[] fileSizes = 
parse(parameters.getParameterValue("fileSize"));
+                       String[] lastModifiedDates = 
parse(parameters.getParameterValue("lastModified"));
+                       String[] mimeTypes = 
parse(parameters.getParameterValue("mimeType"));
+                       for (int i = 0; i < fileNames.length; i++) {
+                               FileDescription fileDescription = new 
FileDescription(
+                                               fileNames[i],
+                                               Long.parseLong(fileSizes[i]),
+                                               
Long.parseLong(lastModifiedDates[i]),
+                                               mimeTypes[i]);
+                               fileDescriptions.add(fileDescription);
+                       }
+                       onFileSelected(target, fileDescriptions);
+               }
+
+               private String[] parse(StringValue stringValue) {
+                       return stringValue.toString().split(",");
+               }
+
+               /**
+                * Called when a file, at client side is selected.
+                *
+                * @param target The {@link 
org.apache.wicket.ajax.AjaxRequestTarget}
+                * @param fileDescriptions A list of FileDescription
+                */
+               protected abstract void onFileSelected(AjaxRequestTarget 
target, List<FileDescription> fileDescriptions);
+
+               @Override
+               protected void updateAjaxAttributes(AjaxRequestAttributes 
attributes) {
+                       super.updateAjaxAttributes(attributes);
+                       attributes.getAjaxCallListeners().add(new 
IAjaxCallListener()
+                       {
+                               @Override
+                               public CharSequence getPrecondition(Component 
component)
+                               {
+                                       return "if (this.files) {  window." + 
component.getMarkupId() + "_files = this.files; return true; } " +
+                                                       "else return false;";
+                               }
+                       });
+
+                       String varName = 
"window."+getComponent().getMarkupId()+"_files";
+                       attributes.getDynamicExtraParameters().add("var files = 
{};" +
+                                       "var fileNames = [];" +
+                                       "var fileSizes = [];" +
+                                       "var lastModifiedDates = [];" +
+                                       "var mimeTypes = [];" +
+                                       "for (var i = 0; i < 
"+varName+".length; i++) {" +
+                                                       " f = " + varName + 
"[i];" +

Review comment:
       missing `var` for `f`

##########
File path: 
wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/FileUploadField.java
##########
@@ -47,6 +59,146 @@
 {
        private static final long serialVersionUID = 1L;
 
+       /**
+        * Description of file properties as in browser client side.
+        */
+       public static class FileDescription implements Serializable  {
+
+               private static final long serialVersionUID = 1L;
+
+               private final String fileName;
+               private final long fileSize;
+               private final Date lastModified;
+               private final String mimeType;
+
+               public FileDescription(String fileName, long fileSize, long 
lastModified, String mimeType) {
+                       this.fileName = fileName;
+                       this.fileSize = fileSize;
+                       this.lastModified = new Date(lastModified);
+                       this.mimeType = mimeType;
+               }
+
+               public String getFileName() {
+                       return fileName;
+               }
+
+               public long getFileSize() {
+                       return fileSize;
+               }
+
+               public Date getLastModified() {
+                       return lastModified;
+               }
+
+               public String getMimeType() {
+                       return mimeType;
+               }
+
+               @Override
+               public boolean equals(Object o) {
+                       if (this == o) return true;
+                       if (o == null || getClass() != o.getClass()) return 
false;
+                       FileDescription that = (FileDescription) o;
+                       return fileName.equals(that.fileName);
+               }
+
+               @Override
+               public int hashCode() {
+                       return Objects.hash(fileName);
+               }
+       }
+
+       /**
+        *  {@link org.apache.wicket.ajax.form.OnChangeAjaxBehavior} that 
streams back to server properties
+        *  of the selected file (at client side), even when file has not yet 
being uploaded.
+s       *
+        * @author Ernesto Reinaldo Barreiro (reier...@gmail.com).
+        */
+       public static abstract class OnFileSelectedBehavior extends 
OnChangeAjaxBehavior
+       {
+               private static final long serialVersionUID = 1L;
+
+               @Override
+               protected void onBind() {
+                       super.onBind();
+                       Component component = getComponent();
+                       if (!(component instanceof FileUploadField))
+                       {
+                               throw new WicketRuntimeException("Behavior " + 
getClass().getName()
+                                               + " can only be added to an 
instance of a FileUploadField");
+                       }
+               }
+
+               @Override
+               protected void onUpdate(AjaxRequestTarget target)
+               {
+                       Request request = RequestCycle.get().getRequest();
+                       List<FileDescription> fileDescriptions = new 
ArrayList<>();
+                       IRequestParameters parameters = 
request.getRequestParameters();
+                       String[] fileNames = 
parse(parameters.getParameterValue("fileName"));
+                       String[] fileSizes = 
parse(parameters.getParameterValue("fileSize"));
+                       String[] lastModifiedDates = 
parse(parameters.getParameterValue("lastModified"));
+                       String[] mimeTypes = 
parse(parameters.getParameterValue("mimeType"));
+                       for (int i = 0; i < fileNames.length; i++) {
+                               FileDescription fileDescription = new 
FileDescription(
+                                               fileNames[i],
+                                               Long.parseLong(fileSizes[i]),
+                                               
Long.parseLong(lastModifiedDates[i]),
+                                               mimeTypes[i]);
+                               fileDescriptions.add(fileDescription);
+                       }
+                       onFileSelected(target, fileDescriptions);
+               }
+
+               private String[] parse(StringValue stringValue) {
+                       return stringValue.toString().split(",");
+               }
+
+               /**
+                * Called when a file, at client side is selected.
+                *
+                * @param target The {@link 
org.apache.wicket.ajax.AjaxRequestTarget}
+                * @param fileDescriptions A list of FileDescription
+                */
+               protected abstract void onFileSelected(AjaxRequestTarget 
target, List<FileDescription> fileDescriptions);
+
+               @Override
+               protected void updateAjaxAttributes(AjaxRequestAttributes 
attributes) {
+                       super.updateAjaxAttributes(attributes);
+                       attributes.getAjaxCallListeners().add(new 
IAjaxCallListener()
+                       {
+                               @Override
+                               public CharSequence getPrecondition(Component 
component)
+                               {
+                                       return "if (this.files) {  window." + 
component.getMarkupId() + "_files = this.files; return true; } " +

Review comment:
       Let's put the new property to `window.Wicket` namespace instead of to 
the global `window` namespace.

##########
File path: 
wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/FileUploadField.java
##########
@@ -47,6 +59,146 @@
 {
        private static final long serialVersionUID = 1L;
 
+       /**
+        * Description of file properties as in browser client side.
+        */
+       public static class FileDescription implements Serializable  {
+
+               private static final long serialVersionUID = 1L;
+
+               private final String fileName;
+               private final long fileSize;
+               private final Date lastModified;
+               private final String mimeType;
+
+               public FileDescription(String fileName, long fileSize, long 
lastModified, String mimeType) {
+                       this.fileName = fileName;
+                       this.fileSize = fileSize;
+                       this.lastModified = new Date(lastModified);
+                       this.mimeType = mimeType;
+               }
+
+               public String getFileName() {
+                       return fileName;
+               }
+
+               public long getFileSize() {
+                       return fileSize;
+               }
+
+               public Date getLastModified() {
+                       return lastModified;
+               }
+
+               public String getMimeType() {
+                       return mimeType;
+               }
+
+               @Override
+               public boolean equals(Object o) {
+                       if (this == o) return true;
+                       if (o == null || getClass() != o.getClass()) return 
false;
+                       FileDescription that = (FileDescription) o;
+                       return fileName.equals(that.fileName);
+               }
+
+               @Override
+               public int hashCode() {
+                       return Objects.hash(fileName);
+               }
+       }
+
+       /**
+        *  {@link org.apache.wicket.ajax.form.OnChangeAjaxBehavior} that 
streams back to server properties
+        *  of the selected file (at client side), even when file has not yet 
being uploaded.
+s       *
+        * @author Ernesto Reinaldo Barreiro (reier...@gmail.com).
+        */
+       public static abstract class OnFileSelectedBehavior extends 
OnChangeAjaxBehavior
+       {
+               private static final long serialVersionUID = 1L;
+
+               @Override
+               protected void onBind() {
+                       super.onBind();
+                       Component component = getComponent();
+                       if (!(component instanceof FileUploadField))
+                       {
+                               throw new WicketRuntimeException("Behavior " + 
getClass().getName()
+                                               + " can only be added to an 
instance of a FileUploadField");
+                       }
+               }
+
+               @Override
+               protected void onUpdate(AjaxRequestTarget target)
+               {
+                       Request request = RequestCycle.get().getRequest();
+                       List<FileDescription> fileDescriptions = new 
ArrayList<>();
+                       IRequestParameters parameters = 
request.getRequestParameters();
+                       String[] fileNames = 
parse(parameters.getParameterValue("fileName"));
+                       String[] fileSizes = 
parse(parameters.getParameterValue("fileSize"));
+                       String[] lastModifiedDates = 
parse(parameters.getParameterValue("lastModified"));
+                       String[] mimeTypes = 
parse(parameters.getParameterValue("mimeType"));
+                       for (int i = 0; i < fileNames.length; i++) {
+                               FileDescription fileDescription = new 
FileDescription(
+                                               fileNames[i],
+                                               Long.parseLong(fileSizes[i]),
+                                               
Long.parseLong(lastModifiedDates[i]),
+                                               mimeTypes[i]);
+                               fileDescriptions.add(fileDescription);
+                       }
+                       onFileSelected(target, fileDescriptions);
+               }
+
+               private String[] parse(StringValue stringValue) {
+                       return stringValue.toString().split(",");
+               }
+
+               /**
+                * Called when a file, at client side is selected.
+                *
+                * @param target The {@link 
org.apache.wicket.ajax.AjaxRequestTarget}
+                * @param fileDescriptions A list of FileDescription
+                */
+               protected abstract void onFileSelected(AjaxRequestTarget 
target, List<FileDescription> fileDescriptions);
+
+               @Override
+               protected void updateAjaxAttributes(AjaxRequestAttributes 
attributes) {
+                       super.updateAjaxAttributes(attributes);
+                       attributes.getAjaxCallListeners().add(new 
IAjaxCallListener()
+                       {
+                               @Override
+                               public CharSequence getPrecondition(Component 
component)
+                               {
+                                       return "if (this.files) {  window." + 
component.getMarkupId() + "_files = this.files; return true; } " +
+                                                       "else return false;";
+                               }
+                       });
+
+                       String varName = 
"window."+getComponent().getMarkupId()+"_files";
+                       attributes.getDynamicExtraParameters().add("var files = 
{};" +
+                                       "var fileNames = [];" +
+                                       "var fileSizes = [];" +
+                                       "var lastModifiedDates = [];" +
+                                       "var mimeTypes = [];" +
+                                       "for (var i = 0; i < 
"+varName+".length; i++) {" +
+                                                       " f = " + varName + 
"[i];" +
+                                                       
"fileNames.push(f.name);" +
+                                                       
"fileSizes.push(f.size);" +
+                                                       
"lastModifiedDates.push(f.lastModified);" +
+                                                       
"mimeTypes.push(f.type);" +
+                                       "};" +
+                                       "var info = {" +
+                                                       "'fileName': fileNames, 
" +
+                                                       "'fileSize': fileSizes, 
" +
+                                                       "'lastModified': 
lastModifiedDates, " +
+                                                       "'mimeType': mimeTypes, 
" +
+                                                       "};"
+                                       + "console.log(info);"

Review comment:
       Debug leftover




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to