dropfile basic widget added
authorVásáry Dániel <vasary@elgekko.net>
Thu, 15 Feb 2024 21:04:07 +0000 (22:04 +0100)
committerVásáry Dániel <vasary@elgekko.net>
Thu, 15 Feb 2024 21:04:07 +0000 (22:04 +0100)
lis-ui/src/main/java/hu/user/lis/ui/editor/widget/Dropupload.java [new file with mode: 0644]

diff --git a/lis-ui/src/main/java/hu/user/lis/ui/editor/widget/Dropupload.java b/lis-ui/src/main/java/hu/user/lis/ui/editor/widget/Dropupload.java
new file mode 100644 (file)
index 0000000..1bdb086
--- /dev/null
@@ -0,0 +1,169 @@
+package hu.user.lis.ui.editor.widget;
+
+import org.zkoss.lang.Generics;
+import org.zkoss.lang.Objects;
+import org.zkoss.util.media.Media;
+import org.zkoss.zk.au.AuRequest;
+import org.zkoss.zk.ui.Component;
+import org.zkoss.zk.ui.Desktop;
+import org.zkoss.zk.ui.event.Events;
+import org.zkoss.zk.ui.event.UploadEvent;
+import org.zkoss.zk.ui.sys.ContentRenderer;
+import org.zkoss.zul.impl.XulElement;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+
+
+public class Dropupload
+        extends XulElement {
+    private static final String DEFAULT_DETECTION = "browser";
+
+    static {
+        addClientEvent(Dropupload.class, "onUpload", 1);
+    }
+
+    private int _maxsize;
+    private String _viewerClass;
+    private String _detection = "browser";
+
+
+    private String _content;
+
+
+    private boolean _native;
+
+
+    private Component _anchor;
+
+    private static Media[] parseResult(List<Media> result) {
+        if (result != null) {
+
+            for (Iterator<Media> it = result.iterator(); it.hasNext(); ) {
+                Media media = it.next();
+                if (media != null && media.inMemory() && media.isBinary()) {
+                    String nm = media.getName();
+                    if (nm == null || nm.length() == 0) {
+                        byte[] bs = media.getByteData();
+                        if (bs == null || bs.length == 0) {
+                            it.remove();
+                        }
+                    }
+                }
+            }
+
+            if (!result.isEmpty())
+                return result.toArray(new Media[result.size()]);
+        }
+        return null;
+    }
+
+    public int getMaxsize() {
+        return this._maxsize;
+    }
+
+    public void setMaxsize(int value) {
+        if (value < 0) {
+            value = -1;
+        }
+
+        if (this._maxsize != value) {
+            this._maxsize = value;
+            setAttribute("org.zkoss.zk.upload.maxsize", Integer.valueOf(value));
+            smartUpdate("maxsize", this._maxsize);
+        }
+    }
+
+    public String getDetection() {
+        return this._detection;
+    }
+
+    public void setDetection(String value) {
+        if (!this._detection.equals(value)) {
+            this._detection = value;
+            smartUpdate("detection", this._detection);
+        }
+    }
+
+    public String getViewerClass() {
+        return this._viewerClass;
+    }
+
+    public void setViewerClass(String value) {
+        if (value != null && value.length() == 0) {
+            value = null;
+        }
+
+        if (!Objects.equals(this._viewerClass, value)) {
+            this._viewerClass = value;
+            smartUpdate("viewerClass", this._viewerClass);
+        }
+    }
+
+    public String getContent() {
+        return this._content;
+    }
+
+    public void setContent(String value) {
+        if (value != null && !value.equals(this._content)) {
+            this._content = value;
+            smartUpdate("content", this._content);
+        }
+    }
+
+    public boolean isNative() {
+        return this._native;
+    }
+
+    public void setNative(boolean value) {
+        if (value != this._native) {
+            this._native = value;
+            smartUpdate("native", this._native);
+        }
+    }
+
+    public Component getAnchor() {
+        return this._anchor;
+    }
+
+    public void setAnchor(Component anchor) {
+        if (anchor != this._anchor) {
+            this._anchor = anchor;
+            smartUpdate("anchorUuid", (this._anchor != null) ? this._anchor.getUuid() : "");
+        }
+    }
+
+    protected void renderProperties(ContentRenderer renderer) throws IOException {
+
+        if (this._maxsize == 0) {
+            this._maxsize = getDesktop().getWebApp().getConfiguration().getMaxUploadSize();
+        }
+
+        super.renderProperties(renderer);
+        if (!Objects.equals(this._detection, "browser")) {
+            render(renderer, "detection", this._detection);
+        }
+        render(renderer, "maxsize", Integer.valueOf(this._maxsize));
+        render(renderer, "viewerClass", this._viewerClass);
+        render(renderer, "content", this._content);
+        render(renderer, "native", this._native);
+        if (this._anchor != null) {
+            render(renderer, "anchorUuid", (this._anchor != null) ? this._anchor.getUuid() : "");
+        }
+    }
+
+    public void service(AuRequest request, boolean everError) {
+        String cmd = request.getCommand();
+        if ("onUpload".equals(cmd)) {
+            Desktop desktop = getDesktop();
+            List<Media> result = Generics.cast((List) desktop.getAttribute(getUuid()));
+            desktop.removeAttribute(getUuid());
+            UploadEvent uploadEvent = new UploadEvent("onUpload", desktop.getComponentByUuid(getUuid()), parseResult(result));
+
+            Events.postEvent(uploadEvent);
+        } else {
+            super.service(request, everError);
+        }
+    }
+}