reiern70 commented on a change in pull request #467:
URL: https://github.com/apache/wicket/pull/467#discussion_r611436687



##########
File path: 
wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/OnFilesSelectedBehavior.java
##########
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.form.upload;
+
+import java.util.ArrayList;
+import java.util.List;
+import javax.servlet.http.HttpServletRequest;
+import org.apache.wicket.Component;
+import org.apache.wicket.WicketRuntimeException;
+import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
+import org.apache.wicket.ajax.attributes.IAjaxCallListener;
+import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
+import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
+import org.apache.wicket.markup.head.IHeaderResponse;
+import org.apache.wicket.markup.head.JavaScriptHeaderItem;
+import org.apache.wicket.request.IRequestParameters;
+import org.apache.wicket.request.Request;
+import org.apache.wicket.request.cycle.RequestCycle;
+import org.apache.wicket.request.resource.JavaScriptResourceReference;
+import org.apache.wicket.request.resource.ResourceReference;
+import org.apache.wicket.util.lang.Args;
+import org.apache.wicket.util.string.StringValue;
+import org.danekja.java.util.function.serializable.SerializableBiConsumer;
+import org.danekja.java.util.function.serializable.SerializableConsumer;
+import com.github.openjson.JSONArray;
+import com.github.openjson.JSONObject;
+import com.github.openjson.JSONTokener;
+
+/**
+ * {@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 abstract class OnFilesSelectedBehavior extends OnChangeAjaxBehavior {
+
+    private static final long serialVersionUID = 1L;
+
+    private static final ResourceReference JS = new 
JavaScriptResourceReference(OnFilesSelectedBehavior.class, 
"OnFilesSelectedBehavior.js");
+
+    @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();
+        // data is streamed as JSON.
+        StringValue fileInfos = parameters.getParameterValue("fileInfos");
+        JSONArray jsonArray = new JSONArray(fileInfos.toString());
+        for (int i = 0; i < jsonArray.length(); i++) {
+            fileDescriptions.add(new 
FileDescription((JSONObject)jsonArray.get(i)));
+        }
+        onSelected(target, fileDescriptions);
+    }
+
+
+    /**
+     * 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 onSelected(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 "return Wicket.OnFilesSelected.precondition(this, '" + 
component.getMarkupId() + "');";
+            }
+        });
+        attributes.getDynamicExtraParameters().add("return 
Wicket.OnFilesSelected.collectFilesDetails('" + getComponent().getMarkupId() + 
"');");
+    }
+
+    @Override
+    public void renderHead(Component component, IHeaderResponse response)
+    {
+        response.render(JavaScriptHeaderItem.forReference(JS));
+        super.renderHead(component, response);
+    }
+
+    /**
+     * Creates an {@link OnFilesSelectedBehavior} based on lambda expressions
+     *
+     * @param select {@link SerializableBiConsumer}
+     *
+     * @return the {@link 
org.apache.wicket.markup.html.form.upload.OnFilesSelectedBehavior} behavior
+     */
+    public static OnFilesSelectedBehavior select(
+            SerializableBiConsumer<AjaxRequestTarget, List<FileDescription>> 
select)

Review comment:
       
![image](https://user-images.githubusercontent.com/462655/114366373-64250e00-9b49-11eb-8a87-238ffff57d87.png)
   
   ?




-- 
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