BrightSide Workbench Full Report + Source Code
RepositoryControl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.elephant.context.Application;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.util.Messages;
30 import org.turro.file.zul.FileClipboard;
31 import org.turro.file.zul.repository.RepositoryGrid;
32 import org.turro.file.zul.repository.RepositoryItem;
33 import org.turro.file.zul.repository.RepositoryTree;
34 import org.turro.i18n.I_;
35 import org.turro.mail.recipients.SendAttachments;
36 import org.turro.zip.Compress;
37 import org.zkoss.zk.ui.Executions;
38 import org.zkoss.zk.ui.IdSpace;
39 import org.zkoss.zk.ui.event.UploadEvent;
40 import org.zkoss.zk.ui.ext.AfterCompose;
41 import org.zkoss.zk.ui.select.Selectors;
42 import org.zkoss.zk.ui.select.annotation.Listen;
43 import org.zkoss.zk.ui.select.annotation.Wire;
44 import org.zkoss.zul.Filedownload;
45 import org.zkoss.zul.Menuitem;
46 import org.zkoss.zul.Panel;
47 import org.zkoss.zul.Textbox;
48 import org.zkoss.zul.Toolbarbutton;
49 import org.zkoss.zul.West;
50 
55 public class RepositoryControl extends Panel implements IdSpace, AfterCompose {
56 
57  private boolean publicOnly, readOnly, treeCollapsed, entity;
58  private RepositoryItem currentFolder;
59  private String label, path;
60  private FileResults results;
61 
62  @Wire("#panelFolder")
63  private West panelFolder;
64 
65  @Wire("#searchValue")
66  private Textbox searchValue;
67 
68  @Wire("#regexp")
69  private Menuitem regexp;
70 
71  @Wire("#fullpath")
72  private Menuitem fullpath;
73 
74  @Wire("#partial")
75  private Menuitem partial;
76 
77  @Wire("#levelUp")
78  private Toolbarbutton levelUp;
79 
80  @Wire("#newFolder")
81  private Toolbarbutton newFolder;
82 
83  @Wire("#upload")
84  private Toolbarbutton upload;
85 
86  @Wire("#send")
87  private Toolbarbutton send;
88 
89  @Wire("#download")
90  private Toolbarbutton download;
91 
92  @Wire("#copy")
93  private Toolbarbutton copy;
94 
95  @Wire("#cut")
96  private Toolbarbutton cut;
97 
98  @Wire("#paste")
99  private Toolbarbutton paste;
100 
101  @Wire("#delete")
102  private Toolbarbutton delete;
103 
104  @Wire("#treeFolder")
105  private RepositoryTree treeFolder;
106 
107  @Wire("#fileGrid")
108  private RepositoryGrid fileGrid;
109 
110  @Listen("onChange = #treeFolder")
111  public void onChangeFolder() {
112  currentFolder = (RepositoryItem) treeFolder.getSelectedFolder().getParentFolder();
113  if(currentFolder == null) {
114  treeFolder.reloadContents();
115  treeFolder.selectFirst();
116  currentFolder = treeFolder.getSelectedFolder();
117  } else {
118  currentFolder.reloadContents();
119  fileGrid.setCurrentFolder(currentFolder);
120  }
121  updateButtons();
122  }
123 
124  @Listen("onSelect = #treeFolder")
125  public void onSelectFolder() {
126  currentFolder = treeFolder.getSelectedFolder();
127  updateButtons();
128  fileGrid.setCurrentFolder(currentFolder);
129  }
130 
131  @Listen("onChange = #searchValue")
132  public void onSearchValue() {
133  results.setSearchValue(searchValue.getValue());
134  }
135 
136  @Listen("onCheck = #regexp")
137  public void onRegexp() {
138  results.setRegexp(regexp.isChecked());
139  }
140 
141  @Listen("onCheck = #fullpath")
142  public void onFullpath() {
143  results.setPath(fullpath.isChecked());
144  }
145 
146  @Listen("onCheck = #partial")
147  public void onPartial() {
148  results.setPartial(partial.isChecked());
149  }
150 
151  @Listen("onClick = #search; onOK = #searchValue")
152  public void onSearch() {
153  treeFolder.reloadContents();
154  selectFirst();
155  }
156 
157  @Listen("onClick = #levelUp")
158  public void onLevelUp() throws Exception {
159  if(currentFolder != null && currentFolder.getParentItem() != null) {
160  currentFolder = (RepositoryItem) currentFolder.getParentItem();
161  treeFolder.setSelectedItem(currentFolder);
162  fileGrid.setCurrentFolder(currentFolder);
163  updateButtons();
164  }
165  }
166 
167  @Listen("onClick = #newFolder")
168  public void onNewFolder() throws Exception {
169  if(currentFolder != null) {
170  currentFolder.doAddFolder();
171  currentFolder.reloadContents();
172  fileGrid.setCurrentFolder(currentFolder);
173  }
174  }
175 
176  @Listen("onUpload = #upload")
177  public void onUpload(UploadEvent event) throws Exception {
178  if(currentFolder != null) {
179  currentFolder.addMedias(event.getMedias());
180  fileGrid.setCurrentFolder(currentFolder);
181  }
182  }
183 
184  @Listen("onClick = #send")
185  public void onSend() throws Exception {
186  SendAttachments sa = new SendAttachments(I_.get("Attachment")) {
187  @Override
188  protected void fillAttachment(File attachment, Object entity) {
189  try {
190  FileUtils.copyFile((File) entity, attachment);
191  } catch (IOException ex) {
192  Logger.getLogger(FileControl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
193  }
194  }
195  };
196  RepositoryItem af = fileGrid.getCurrentFolder();
197  if(af != null) {
198  for(File file : af.getFiles()) {
199  sa.addAttachment(file, file.getName(), file.getName());
200  }
201  }
202  sa.sendAttachments(null);
203  }
204 
205  @Listen("onClick = #download")
206  public void onDownload() throws Exception {
207  RepositoryItem af = fileGrid.getCurrentFolder();
208  if(af != null) {
209  File targetZip = File.createTempFile("attach_", "_folder.zip");
210  Compress.zip(af.getRealFolder().toPath(), targetZip.toPath());
211  Filedownload.save(targetZip, "application/zip");
212  }
213  }
214 
215  @Listen("onClick = #copy")
216  public void onCopy() throws Exception {
217  FileClipboard.copyFolder(currentFolder.getRealFolder());
218  }
219 
220  @Listen("onClick = #cut")
221  public void onCut() throws Exception {
222  FileClipboard.cutFolder(currentFolder.getLabel(), currentFolder.getRealFolder());
223  }
224 
225  @Listen("onClick = #paste")
226  public void onPaste() throws Exception {
227  FileClipboard.paste(currentFolder.getRealFolder());
228  treeFolder.setSelectedItem(currentFolder);
229  currentFolder.reloadContents();
230  fileGrid.setCurrentFolder(currentFolder);
231  paste.setDisabled(true);
232  }
233 
234  @Listen("onClick = #delete")
235  public void onDelete() throws Exception {
236  if(Application.getApplication().isInRole("file-attach:delete")) {
237  Messages.confirmDeletion().show(() -> {
238  for(File f : currentFolder.getFiles()) {
239  f.delete();
240  if(treeFolder.getFileListener() != null) {
241  treeFolder.getFileListener().deleteDone(f);
242  }
243  }
244  treeFolder.setSelectedItem(currentFolder);
245  fileGrid.setCurrentFolder(currentFolder);
246  });
247  }
248  }
249 
250  @Listen("onUpload = #dropUpload")
251  public void onEvent(UploadEvent event) throws Exception {
252  if(currentFolder != null) {
253  currentFolder.addMedias(event.getMedias());
254  fileGrid.setCurrentFolder(currentFolder);
255  }
256  }
257 
258  public RepositoryControl() {
259  Executions.createComponents("/WEB-INF/_zul/bs/comps/attachment/repositoryControl.zul", this, null);
260  Selectors.wireComponents(this, this, false);
261  Selectors.wireEventListeners(this, this);
262  }
263 
264  public Boolean getPublicOnly() {
265  return publicOnly;
266  }
267 
268  public void setPublicOnly(Boolean publicOnly) {
269  this.publicOnly = publicOnly;
270  }
271 
272  public boolean isReadOnly() {
273  return readOnly;
274  }
275 
276  public void setReadOnly(boolean readOnly) {
277  this.readOnly = readOnly;
278  }
279 
280  public boolean isTreeCollapsed() {
281  return treeCollapsed;
282  }
283 
284  public void setTreeCollapsed(boolean treeCollapsed) {
285  this.treeCollapsed = treeCollapsed;
286  }
287 
288  public String getLabel() {
289  return label;
290  }
291 
292  public void setLabel(String label) {
293  this.label = label;
294  }
295 
296  public String getPath() {
297  return path;
298  }
299 
300  public void setPath(String path) {
301  this.path = path;
302  treeFolder.resetTree();
303  treeFolder.setPath(Strings.isBlank(path, "/"));
304  }
305 
307  return treeFolder;
308  }
309 
310  @Override
311  public void afterCompose() {
312  initResults();
313  initComponents();
314  panelFolder.setOpen(!treeCollapsed);
315  }
316 
317  private void initResults() {
318  results = new FileResults();
319  }
320 
321  private void initComponents() {
322  treeFolder.setResults(results);
323  searchValue.setValue(results.getSearchValue());
324  regexp.setChecked(results.isRegexp());
325  fullpath.setChecked(results.isPath());
326  partial.setChecked(results.isPartial());
327  fileGrid.setReadOnly(readOnly);
328  updateButtons();
329  }
330 
331  public void selectFirst() {
332  currentFolder = treeFolder.selectFirst();
333  updateButtons();
334  fileGrid.setCurrentFolder(currentFolder);
335  }
336 
337  private void updateButtons() {
338  newFolder.setDisabled(currentFolder == null || readOnly);
339  upload.setDisabled(currentFolder == null || readOnly);
340  send.setDisabled(currentFolder == null);
341  download.setDisabled(currentFolder == null || !Application.getApplication().isInRole("attach:validate"));
342  levelUp.setDisabled(currentFolder == null || currentFolder.getParentItem() == null);
343  copy.setDisabled(currentFolder == null);
344  cut.setDisabled(currentFolder == null || readOnly);
345  paste.setDisabled(!FileClipboard.hasClipboard() || readOnly);
346  if(delete != null && Application.getApplication().isInRole("file-attach:delete")) {
347  delete.setDisabled(currentFolder == null || currentFolder.getParentFolder() == null || readOnly);
348  }
349  }
350 }
351 
static Messages confirmDeletion()
Definition: Messages.java:87
static void copyFolder(File value)
static void paste(File folder)
static void cutFolder(String name, final File value)
void setSearchValue(String searchValue)
void setCurrentFolder(RepositoryItem currentFolder)
static String get(String msg)
Definition: I_.java:41
void addAttachment(Object entity, String path, String name)
void sendAttachments(Collection< IContact > contacts)