BrightSide Workbench Full Report + Source Code
FileControl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.file.zul.control;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.turro.string.Strings;
26 import org.apache.commons.io.FileUtils;
27 import org.turro.action.IEntityHolder;
28 import org.turro.elephant.context.Application;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.util.Messages;
31 import org.turro.entities.Entities;
32 import org.turro.entities.IElephantEntity;
33 import org.turro.file.util.FileAttach;
34 import org.turro.file.zul.FileClipboard;
35 import org.turro.file.zul.navigator.FileFolder;
36 import org.turro.file.zul.navigator.FileNavigatorGrid;
37 import org.turro.file.zul.navigator.FileTree;
38 import org.turro.i18n.I_;
39 import org.turro.util.Chars;
40 import org.turro.mail.recipients.SendAttachments;
41 import org.turro.zip.Compress;
42 import org.zkoss.zk.ui.Executions;
43 import org.zkoss.zk.ui.IdSpace;
44 import org.zkoss.zk.ui.event.UploadEvent;
45 import org.zkoss.zk.ui.ext.AfterCompose;
46 import org.zkoss.zk.ui.select.Selectors;
47 import org.zkoss.zk.ui.select.annotation.Listen;
48 import org.zkoss.zk.ui.select.annotation.Wire;
49 import org.zkoss.zul.Filedownload;
50 import org.zkoss.zul.Menuitem;
51 import org.zkoss.zul.Panel;
52 import org.zkoss.zul.Textbox;
53 import org.zkoss.zul.Toolbarbutton;
54 import org.zkoss.zul.West;
55 
60 public class FileControl extends Panel implements IEntityHolder, IdSpace, AfterCompose {
61 
62  private static final String SECURED = "/#secure#/#forbidden#";
63 
64  private boolean publicOnly, readOnly, treeCollapsed, entity;
65  private FileFolder currentFolder;
66  private String label, path;
67  private FileResults results;
68 
69  @Wire("#panelFolder")
70  private West panelFolder;
71 
72  @Wire("#searchValue")
73  private Textbox searchValue;
74 
75  @Wire("#regexp")
76  private Menuitem regexp;
77 
78  @Wire("#fullpath")
79  private Menuitem fullpath;
80 
81  @Wire("#partial")
82  private Menuitem partial;
83 
84  @Wire("#levelUp")
85  private Toolbarbutton levelUp;
86 
87  @Wire("#newFolder")
88  private Toolbarbutton newFolder;
89 
90  @Wire("#upload")
91  private Toolbarbutton upload;
92 
93  @Wire("#send")
94  private Toolbarbutton send;
95 
96  @Wire("#download")
97  private Toolbarbutton download;
98 
99  @Wire("#copy")
100  private Toolbarbutton copy;
101 
102  @Wire("#cut")
103  private Toolbarbutton cut;
104 
105  @Wire("#paste")
106  private Toolbarbutton paste;
107 
108  @Wire("#delete")
109  private Toolbarbutton delete;
110 
111  @Wire("#treeFolder")
112  private FileTree treeFolder;
113 
114  @Wire("#fileGrid")
115  private FileNavigatorGrid fileGrid;
116 
117  @Listen("onChange = #treeFolder")
118  public void onChangeFolder() {
119  currentFolder = (FileFolder) treeFolder.getSelectedFolder().getParentFolder();
120  if(currentFolder == null) {
121  treeFolder.reloadContents();
122  treeFolder.selectFirst();
123  currentFolder = treeFolder.getSelectedFolder();
124  } else {
125  currentFolder.reloadContents();
126  fileGrid.setCurrentFolder(currentFolder);
127  }
128  updateButtons();
129  }
130 
131  @Listen("onSelect = #treeFolder")
132  public void onSelectFolder() {
133  currentFolder = treeFolder.getSelectedFolder();
134  updateButtons();
135  fileGrid.setCurrentFolder(currentFolder);
136  }
137 
138  @Listen("onChange = #searchValue")
139  public void onSearchValue() {
140  results.setSearchValue(searchValue.getValue());
141  }
142 
143  @Listen("onCheck = #regexp")
144  public void onRegexp() {
145  results.setRegexp(regexp.isChecked());
146  }
147 
148  @Listen("onCheck = #fullpath")
149  public void onFullpath() {
150  results.setPath(fullpath.isChecked());
151  }
152 
153  @Listen("onCheck = #partial")
154  public void onPartial() {
155  results.setPartial(partial.isChecked());
156  }
157 
158  @Listen("onClick = #search; onOK = #searchValue")
159  public void onSearch() {
160  treeFolder.reloadContents();
161  selectFirst();
162  }
163 
164  @Listen("onClick = #levelUp")
165  public void onLevelUp() throws Exception {
166  if(currentFolder != null && currentFolder.getParentItem() != null) {
167  currentFolder = (FileFolder) currentFolder.getParentItem();
168  treeFolder.setSelectedItem(currentFolder);
169  fileGrid.setCurrentFolder(currentFolder);
170  updateButtons();
171  }
172  }
173 
174  @Listen("onClick = #newFolder")
175  public void onNewFolder() throws Exception {
176  if(currentFolder != null) {
177  currentFolder.doAddFolder();
178  currentFolder.reloadContents();
179  fileGrid.setCurrentFolder(currentFolder);
180  }
181  }
182 
183  @Listen("onUpload = #upload")
184  public void onUpload(UploadEvent event) throws Exception {
185  if(currentFolder != null) {
186  currentFolder.addMedias(event.getMedias());
187  fileGrid.setCurrentFolder(currentFolder);
188  }
189  }
190 
191  @Listen("onClick = #send")
192  public void onSend() throws Exception {
193  SendAttachments sa = new SendAttachments(I_.get("Attachment")) {
194  @Override
195  protected void fillAttachment(File attachment, Object entity) {
196  try {
197  FileUtils.copyFile((File) entity, attachment);
198  } catch (IOException ex) {
199  Logger.getLogger(FileControl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
200  }
201  }
202  };
203  FileFolder af = fileGrid.getCurrentFolder();
204  if(af != null) {
205  for(File file : af.getFiles()) {
206  sa.addAttachment(file, file.getName(), file.getName());
207  }
208  }
209  sa.sendAttachments(null);
210  }
211 
212  @Listen("onClick = #download")
213  public void onDownload() throws Exception {
214  FileFolder af = fileGrid.getCurrentFolder();
215  if(af != null) {
216  File targetZip = File.createTempFile("attach_", "_folder.zip");
217  Compress.zip(af.getRealFolder().toPath(), targetZip.toPath());
218  Filedownload.save(targetZip, "application/zip");
219  }
220  }
221 
222  @Listen("onClick = #copy")
223  public void onCopy() throws Exception {
224  FileClipboard.copyFolder(currentFolder.getRealFolder());
225  }
226 
227  @Listen("onClick = #cut")
228  public void onCut() throws Exception {
229  FileClipboard.cutFolder(currentFolder.getLabel(), currentFolder.getRealFolder());
230  }
231 
232  @Listen("onClick = #paste")
233  public void onPaste() throws Exception {
234  FileClipboard.paste(currentFolder.getRealFolder());
235  treeFolder.setSelectedItem(currentFolder);
236  currentFolder.reloadContents();
237  fileGrid.setCurrentFolder(currentFolder);
238  paste.setDisabled(true);
239  }
240 
241  @Listen("onClick = #delete")
242  public void onDelete() throws Exception {
243  if(Application.getApplication().isInRole("file-attach:delete")) {
244  Messages.confirmDeletion().show(() -> {
245  for(File f : currentFolder.getFiles()) {
246  f.delete();
247  if(treeFolder.getFileListener() != null) {
248  treeFolder.getFileListener().deleteDone(f);
249  }
250  }
251  treeFolder.setSelectedItem(currentFolder);
252  fileGrid.setCurrentFolder(currentFolder);
253  });
254  }
255  }
256 
257  @Listen("onUpload = #dropUpload")
258  public void onEvent(UploadEvent event) throws Exception {
259  if(currentFolder != null) {
260  currentFolder.addMedias(event.getMedias());
261  fileGrid.setCurrentFolder(currentFolder);
262  }
263  }
264 
265  public FileControl() {
266  Executions.createComponents("/WEB-INF/_zul/bs/comps/attachment/fileControl.zul", this, null);
267  Selectors.wireComponents(this, this, false);
268  Selectors.wireEventListeners(this, this);
269  path = SECURED;
270  }
271 
272  public Boolean getPublicOnly() {
273  return publicOnly;
274  }
275 
276  public void setPublicOnly(Boolean publicOnly) {
277  this.publicOnly = publicOnly;
278  }
279 
280  public boolean isReadOnly() {
281  return readOnly;
282  }
283 
284  public void setReadOnly(boolean readOnly) {
285  this.readOnly = readOnly;
286  }
287 
288  public boolean isTreeCollapsed() {
289  return treeCollapsed;
290  }
291 
292  public void setTreeCollapsed(boolean treeCollapsed) {
293  this.treeCollapsed = treeCollapsed;
294  }
295 
296  public String getLabel() {
297  return label;
298  }
299 
300  public void setLabel(String label) {
301  this.label = label;
302  }
303 
304  public String getPath() {
305  return path;
306  }
307 
308  public void setPath(String path) {
309  this.path = path;
310  treeFolder.resetTree();
311  }
312 
313  @Override
314  public void setEntity(Object entity) {
315  IElephantEntity iee = Entities.getController(entity);
316  path = iee.getPath();
317  this.entity = true;
318  if(Strings.isBlank(path)) {
319  path = SECURED;
320  } else {
321  label = iee.getName();
322  }
323  treeFolder.setDoProcessors(false);
324  treeFolder.resetTree();
325  fileGrid.setCurrentFolder(null);
326  }
327 
328  @Override
329  public void refreshControls() {
330  afterCompose();
331  }
332 
334  return treeFolder;
335  }
336 
337  @Override
338  public void afterCompose() {
339  initResults();
340  initComponents();
341  panelFolder.setOpen(!treeCollapsed);
342  if(!SECURED.equals(path)) {
343  if(entity) {
344  FileFolder publicFolder = treeFolder.addFolder(I_.get("Publishable") +
345  Chars.forward().spaced().toString() + label, new FileAttach(path).getPublishable(), true);
346  panelFolder.setTitle(label);
347  publicFolder.showContents();
348  if(Application.getApplication().isInRole("file-attach:private")) {
349  FileFolder privateFolder = treeFolder.addFolder(label, new FileAttach(path).getPrivate(), true);
350  panelFolder.setTitle(label);
351  privateFolder.showContents();
352  }
353  } else {
354  if(!Strings.isBlank(path)) {
355  String[] paths = path.split("\\|");
356  String[] labels = label.split("\\|");
357  for(int i = 0; i < paths.length; i++) {
358  FileFolder rootFolder = treeFolder.addFolder(labels[i], paths[i], true);
359  panelFolder.setTitle(labels[i]);
360  rootFolder.showContents();
361  }
362  }
363  }
364  selectFirst();
365  delete.setDisabled(!Application.getApplication().isInRole("file-attach:delete") || readOnly);
366  } else {
367  panelFolder.setTitle("");
368  treeFolder.resetTree();
369  fileGrid.setCurrentFolder(null);
370  newFolder.setDisabled(true);
371  upload.setDisabled(true);
372  send.setDisabled(true);
373  levelUp.setDisabled(true);
374  copy.setDisabled(true);
375  cut.setDisabled(true);
376  paste.setDisabled(true);
377  delete.setDisabled(true);
378  }
379  }
380 
381  private void initResults() {
382  results = new FileResults();
383  }
384 
385  private void initComponents() {
386  treeFolder.setResults(results);
387  searchValue.setValue(results.getSearchValue());
388  regexp.setChecked(results.isRegexp());
389  fullpath.setChecked(results.isPath());
390  partial.setChecked(results.isPartial());
391  fileGrid.setReadOnly(readOnly);
392  updateButtons();
393  }
394 
395  public void selectFirst() {
396  currentFolder = treeFolder.selectFirst();
397  updateButtons();
398  fileGrid.setCurrentFolder(currentFolder);
399  }
400 
401  private void updateButtons() {
402  newFolder.setDisabled(currentFolder == null || readOnly);
403  upload.setDisabled(currentFolder == null || readOnly);
404  send.setDisabled(currentFolder == null || !Application.getApplication().isInRole("attach:validate"));
405  download.setDisabled(currentFolder == null || !Application.getApplication().isInRole("attach:validate"));
406  levelUp.setDisabled(currentFolder == null || currentFolder.getParentItem() == null);
407  copy.setDisabled(currentFolder == null);
408  cut.setDisabled(currentFolder == null || readOnly);
409  paste.setDisabled(!FileClipboard.hasClipboard() || readOnly);
410  if(delete != null && Application.getApplication().isInRole("file-attach:delete")) {
411  delete.setDisabled(currentFolder == null || currentFolder.getParentFolder() == null || readOnly);
412  }
413  }
414 }
static Messages confirmDeletion()
Definition: Messages.java:87
static IElephantEntity getController(String path)
Definition: Entities.java:78
static void copyFolder(File value)
static void paste(File folder)
static void cutFolder(String name, final File value)
void setTreeCollapsed(boolean treeCollapsed)
void setPublicOnly(Boolean publicOnly)
void setSearchValue(String searchValue)
FileFolder addFolder(String label, String path)
void setCurrentFolder(FileFolder currentFolder)
static String get(String msg)
Definition: I_.java:41
void addAttachment(Object entity, String path, String name)
void sendAttachments(Collection< IContact > contacts)