19 package org.turro.attach.zul.control;
21 import java.io.ByteArrayInputStream;
23 import java.nio.file.Paths;
24 import java.util.Collection;
25 import java.util.function.Consumer;
26 import org.turro.string.Strings;
27 import org.turro.action.IEntityHolder;
28 import org.turro.attach.db.AttachPU;
29 import org.turro.attach.entity.Attachment;
30 import org.turro.attach.search.AttachResults;
31 import org.turro.attach.zul.AttachmentUtil;
32 import org.turro.attach.zul.navigator.AttachFolder;
33 import org.turro.attach.zul.navigator.AttachNavigatorGrid;
34 import org.turro.attach.zul.navigator.AttachTree;
35 import org.turro.elephant.context.Application;
36 import org.turro.elephant.util.Messages;
37 import org.turro.entities.Entities;
38 import org.turro.entities.IElephantEntity;
39 import org.turro.i18n.I_;
40 import org.turro.wd.entities.AttachWd;
41 import org.turro.mail.recipients.SendAttachments;
42 import org.turro.zip.Compress;
43 import org.turro.zkoss.dialog.Windows;
44 import org.zkoss.zk.ui.Executions;
45 import org.zkoss.zk.ui.IdSpace;
46 import org.zkoss.zk.ui.event.UploadEvent;
47 import org.zkoss.zk.ui.ext.AfterCompose;
48 import org.zkoss.zk.ui.select.Selectors;
49 import org.zkoss.zk.ui.select.annotation.Listen;
50 import org.zkoss.zk.ui.select.annotation.Wire;
51 import org.zkoss.zul.Checkbox;
52 import org.zkoss.zul.Filedownload;
53 import org.zkoss.zul.Panel;
54 import org.zkoss.zul.Textbox;
55 import org.zkoss.zul.Toolbarbutton;
56 import org.zkoss.zul.West;
64 private static final String SECURED =
"/#secure#/#forbidden#";
67 private boolean publicOnly, readOnly, treeCollapsed;
69 private String label, path;
72 private West panelFolder;
75 private Textbox searchValue;
78 private Toolbarbutton search;
81 private Checkbox validated;
84 private Toolbarbutton levelUp;
87 private Toolbarbutton newFolder;
90 private Toolbarbutton upload;
93 private Toolbarbutton send;
96 private Toolbarbutton download;
99 private Toolbarbutton copy;
102 private Toolbarbutton cut;
105 private Toolbarbutton paste;
108 private Toolbarbutton
delete;
113 @Wire(
"#attachmentGrid")
116 @Listen(
"onSelect = #treeFolder")
123 @Listen(
"onChange = #searchValue")
128 @Listen(
"onClick = #search; onOK = #searchValue")
134 @Listen(
"onCheck = #validated")
141 @Listen(
"onClick = #levelUp")
143 if(currentFolder !=
null && currentFolder.getParentItem() !=
null) {
144 currentFolder = (
AttachFolder) currentFolder.getParentItem();
145 treeFolder.setSelectedItem(currentFolder);
151 @Listen(
"onClick = #newFolder")
153 if(currentFolder !=
null) {
159 @Listen(
"onUpload = #upload")
160 public
void onUpload(UploadEvent event) throws Exception {
161 if(currentFolder !=
null) {
163 currentFolder.
addMedias(event.getMedias());
168 @Listen(
"onClick = #send")
172 protected void fillAttachment(File attachment, Object entity) {
185 @Listen(
"onClick = #download")
189 File targetZip = File.createTempFile(
"attach_",
"_folder.zip");
191 a -> Paths.get(a.getPath(), a.getFileName()),
192 a ->
new ByteArrayInputStream(a.getWithContent().getAttachContent().getFileContent()));
193 Filedownload.save(targetZip,
"application/zip");
197 @Listen(
"onClick = #copy")
202 @Listen(
"onClick = #cut")
203 public
void onCut() throws Exception {
207 @Listen(
"onClick = #paste")
210 treeFolder.setSelectedItem(currentFolder);
213 paste.setDisabled(
true);
216 @Listen(
"onClick = #delete")
222 for(Attachment a : deleted) {
223 treeFolder.getAttachListener().deleteDone(a);
226 treeFolder.setSelectedItem(currentFolder);
232 @Listen(
"onUpload = #dropUpload")
233 public
void onEvent(UploadEvent event) throws Exception {
234 if(currentFolder !=
null) {
235 currentFolder.addMedias(event.getMedias());
236 attachmentGrid.setCurrentFolder(currentFolder);
241 Executions.createComponents(
"/WEB-INF/_zul/bs/comps/attachment/attachmentControl.zul",
this,
null);
242 Selectors.wireComponents(
this,
this,
false);
243 Selectors.wireEventListeners(
this,
this);
252 this.publicOnly = publicOnly;
260 this.readOnly = readOnly;
264 return treeCollapsed;
268 this.treeCollapsed = treeCollapsed;
285 treeFolder.resetTree();
292 if(Strings.isBlank(path)) {
297 treeFolder.setDoProcessors(
false);
298 treeFolder.resetTree();
299 attachmentGrid.setCurrentFolder(
null);
315 panelFolder.setOpen(!treeCollapsed);
316 if(!SECURED.equals(path)) {
318 panelFolder.setTitle(label);
323 panelFolder.setTitle(
"");
324 treeFolder.resetTree();
325 attachmentGrid.setCurrentFolder(
null);
326 newFolder.setDisabled(
true);
327 upload.setDisabled(
true);
328 send.setDisabled(
true);
329 levelUp.setDisabled(
true);
330 copy.setDisabled(
true);
331 cut.setDisabled(
true);
332 paste.setDisabled(
true);
333 delete.setDisabled(
true);
337 private void initResults() {
340 results.setCkOthers(
false);
341 results.setPublicOnly(publicOnly);
344 private void initComponents() {
345 treeFolder.setResults(results);
346 searchValue.setValue(results.getAttachValue());
347 validated.setChecked(results.isCkNonValidated());
348 attachmentGrid.setReadOnly(readOnly);
353 currentFolder = treeFolder.selectFirst();
355 attachmentGrid.setCurrentFolder(currentFolder);
358 private void updateButtons() {
359 newFolder.setDisabled(currentFolder ==
null || readOnly);
360 upload.setDisabled(currentFolder ==
null || readOnly);
363 levelUp.setDisabled(currentFolder ==
null || currentFolder.getParentItem() ==
null);
364 copy.setDisabled(currentFolder ==
null);
365 cut.setDisabled(currentFolder ==
null || readOnly);
368 delete.setDisabled(currentFolder ==
null || currentFolder.getParentFolder() ==
null || readOnly);
374 public static void showFor(Object entity, Consumer<Windows> onClose) {
377 ac.setHeight(
"100%");
static String getObjectPath(Object object)
void setAttachValue(String attachValue)
void setCkNonValidated(boolean ckNonValidated)
static void copyFolder(Object value)
static boolean hasClipboard()
static void cutFolder(String name, final Object value)
static Collection< Attachment > delete(String path)
static void paste(String toPath)
void setLabel(String label)
AttachTree getTreeFolder()
boolean isTreeCollapsed()
void setEntity(Object entity)
void setTreeCollapsed(boolean treeCollapsed)
void onUpload(UploadEvent event)
void setReadOnly(boolean readOnly)
static void showFor(Object entity, Consumer< Windows > onClose)
void setPath(String path)
void setPublicOnly(Boolean publicOnly)
Collection< Attachment > getAllAttachments()
void addMedias(Media[] medias)
AttachFolder addFolder(String label, String path)
Collection< Attachment > getAttachments()
AttachFolder getCurrentFolder()
void setCurrentFolder(AttachFolder currentFolder)
AttachFolder getSelectedFolder()
AttachNavigatorListener getAttachListener()
boolean isInRole(String role)
static Application getApplication()
static Messages confirmDeletion()
static IElephantEntity getController(String path)
static String get(String msg)
void addAttachment(Object entity, String path, String name)
void sendAttachments(Collection< IContact > contacts)
static void writeToFile(Attachment attachment, File file)
static Windows title(String title)
Windows addComponent(HtmlBasedComponent component)
Windows width(String width)
Windows onClose(Consumer< Windows > onClose)
Windows height(String height)