From 9d66c6367c12b221cdec8945ef572455cd98e15b Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1s=C3=A1ry=20D=C3=A1niel?= Date: Thu, 15 Feb 2024 22:04:07 +0100 Subject: [PATCH] dropfile basic widget added --- .../user/lis/ui/editor/widget/Dropupload.java | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 lis-ui/src/main/java/hu/user/lis/ui/editor/widget/Dropupload.java 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 index 0000000..1bdb086 --- /dev/null +++ b/lis-ui/src/main/java/hu/user/lis/ui/editor/widget/Dropupload.java @@ -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 result) { + if (result != null) { + + for (Iterator 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 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); + } + } +} -- 2.54.0