BrightSide Workbench Full Report + Source Code
AttachNavigator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.attach.zul.navigator;
19 
20 import java.io.File;
21 import java.util.Collection;
22 import org.turro.attach.db.AttachPU;
23 import org.turro.attach.entity.Attachment;
24 import org.turro.attach.search.AttachResults;
25 import org.turro.attach.zul.AttachmentUtil;
26 import org.turro.elephant.context.Application;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.elephant.util.Images;
29 import org.turro.elephant.util.Messages;
30 import org.turro.i18n.I_;
31 import org.turro.wd.entities.AttachWd;
32 import org.turro.mail.recipients.SendAttachments;
33 import org.zkoss.zk.ui.event.Event;
34 import org.zkoss.zk.ui.event.EventListener;
35 import org.zkoss.zk.ui.event.Events;
36 import org.zkoss.zk.ui.event.UploadEvent;
37 import org.zkoss.zk.ui.ext.AfterCompose;
38 import org.zkoss.zkmax.zul.Dropupload;
39 import org.zkoss.zul.*;
40 
46 @Deprecated
47 public class AttachNavigator extends Panel implements AfterCompose {
48 
49  private Borderlayout layout;
50  private Center center;
51  private West west;
52  private North north;
53  private Panel centerPanel, navPanel;
54  private AttachTree attachTree;
55  private AttachNavigatorGrid attachGrid;
56  private Toolbar toolbar, searchbar;
57  private Toolbarbutton addFolderButton, uploadButton, sendButton, upButton,
58  copyButton, cutButton, pasteButton, deleteButton;
59  private A wdButton;
60  private Dropupload dropupload;
61  private AttachResults results;
62  private AttachFolder currentFolder;
63  private boolean vertical = false, workingDirOption = true, publicOnly = false;
64 
65  public AttachNavigator() {
66  }
67 
68  public boolean isVertical() {
69  return vertical;
70  }
71 
72  public void setVertical(boolean vertical) {
73  this.vertical = vertical;
74  }
75 
76  public boolean isWorkingDirOption() {
77  return workingDirOption;
78  }
79 
80  public void setWorkingDirOption(boolean workingDirOption) {
81  this.workingDirOption = workingDirOption;
82  }
83 
84  public boolean isPublicOnly() {
85  return publicOnly;
86  }
87 
88  public void setPublicOnly(boolean publicOnly) {
89  this.publicOnly = publicOnly;
90  }
91 
93  return attachTree;
94  }
95 
96  private void addComponents() {
97  if(getPanelchildren() == null) {
98  appendChild(new Panelchildren());
99  } else {
100  getPanelchildren().getChildren().clear();
101  }
102 
103  layout = new Borderlayout();
104  getPanelchildren().appendChild(layout);
105 
106  if(vertical) {
107  north = new North();
108  north.setBorder("none");
109  north.setSplittable(true);
110  north.setSize("150px");
111  layout.appendChild(north);
112  } else {
113  west = new West();
114  west.setBorder("none");
115  west.setSplittable(true);
116  west.setSize("280px");
117  layout.appendChild(west);
118  }
119  center = new Center();
120  center.setBorder("none");
121  layout.appendChild(center);
122 
123  centerPanel = new Panel();
124  centerPanel.setVflex("true");
125  centerPanel.appendChild(new Panelchildren());
126 
127  center.appendChild(centerPanel);
128 
129  navPanel = new Panel();
130  navPanel.setVflex("true");
131  navPanel.appendChild(new Panelchildren());
132 
133  if(vertical) {
134  north.appendChild(navPanel);
135  } else {
136  west.appendChild(navPanel);
137  }
138 
139  attachTree = new AttachTree();
140  attachTree.setSclass("treeMenu");
141  attachTree.setVflex(true);
142  attachTree.setResults(results);
143  attachTree.addEventListener(Events.ON_SELECT, new EventListener() {
144  @Override
145  public void onEvent(Event event) throws Exception {
146  currentFolder = attachTree.getSelectedFolder();
147  updateButtons();
148  attachGrid.setCurrentFolder(currentFolder);
149  }
150  });
151 
152  navPanel.getPanelchildren().appendChild(attachTree);
153  attachTree.afterCompose();
154 
155  searchbar = new Toolbar();
156  navPanel.addToolbar("tbar", searchbar);
157 
158  final Textbox attachValue = new Textbox(results.getAttachValue());
159  attachValue.setStyle("font-size:11px");
160  attachValue.addEventListener(Events.ON_CHANGE, new EventListener() {
161  @Override
162  public void onEvent(Event event) throws Exception {
163  results.setAttachValue(attachValue.getText());
164  }
165  });
166  attachValue.addEventListener(Events.ON_OK, new EventListener() {
167  @Override
168  public void onEvent(Event event) throws Exception {
169  attachTree.reloadContents();
170  selectFirst();
171  }
172  });
173  searchbar.appendChild(attachValue);
174 
175  Toolbarbutton searchButton = new Toolbarbutton();
176  searchButton.setImage("/_zul/images/search.png");
177  searchButton.setTooltiptext(I_.get("Search"));
178  searchButton.addEventListener(Events.ON_CLICK, new EventListener() {
179  @Override
180  public void onEvent(Event event) throws Exception {
181  attachTree.reloadContents();
182  selectFirst();
183  }
184  });
185  searchbar.appendChild(searchButton);
186 
187  final Checkbox showNonValidated = new Checkbox(I_.get("Non validated"));
188  showNonValidated.setChecked(results.isCkNonValidated());
189  showNonValidated.setStyle("font-size:11px");
190  showNonValidated.addEventListener(Events.ON_CHECK, new EventListener() {
191  @Override
192  public void onEvent(Event event) throws Exception {
193  results.setCkNonValidated(showNonValidated.isChecked());
194  }
195  });
196  searchbar.appendChild(showNonValidated);
197 
198  if(workingDirOption) {
199  searchbar.appendChild(new Space());
200 
201  wdButton = new A(I_.get("Create on Working directory"), "/_zul/images/working-dir-move.png");
202  wdButton.setTarget("_new");
203  searchbar.appendChild(wdButton);
204 
205 // wdButton = new Toolbarbutton(I_.get("Create on Working directory"), "/_zul/images/working-dir-move.png");
206 // wdButton.setDisabled(true);
207 // searchbar.appendChild(wdButton);
208 // wdButton.addEventListener(Events.ON_CLICK, new EventListener() {
209 // @Override
210 // public void onEvent(Event event) throws Exception {
211 // //AttachMenu.createWorkDirectoryFor(currentFolder.getLabel(), currentFolder.getPath());
212 // AttachMenu.createJnlpCallFor(currentFolder.getLabel(), currentFolder.getPath());
213 // }
214 // });
215  }
216 
217  toolbar = new Toolbar();
218  centerPanel.addToolbar("tbar", toolbar);
219 
220  upButton = new Toolbarbutton();
221  upButton.setImage("/_zul/images/arrow-up.png");
222  upButton.setDisabled(true);
223  toolbar.appendChild(upButton);
224  upButton.addEventListener(Events.ON_CLICK, new EventListener() {
225  @Override
226  public void onEvent(Event event) throws Exception {
227  if(currentFolder != null && currentFolder.getParentItem() != null) {
228  currentFolder = (AttachFolder) currentFolder.getParentItem();
229  attachTree.setSelectedItem(currentFolder);
230  attachGrid.setCurrentFolder(currentFolder);
231  updateButtons();
232  }
233  }
234  });
235 
236  toolbar.appendChild(new Space());
237 
238  addFolderButton = new Toolbarbutton(I_.get("Add folder"), "/_zul/images/folder-new.png");
239  addFolderButton.setDisabled(true);
240  toolbar.appendChild(addFolderButton);
241  addFolderButton.addEventListener(Events.ON_CLICK, new EventListener() {
242  @Override
243  public void onEvent(Event event) throws Exception {
244  if(currentFolder != null) {
245  currentFolder.doAddFolder();
246  attachGrid.setCurrentFolder(currentFolder);
247  }
248  }
249  });
250 
251  uploadButton = new Toolbarbutton(I_.get("Upload"), "/_zul/images/upload.png");
252  uploadButton.setDisabled(true);
253  toolbar.appendChild(uploadButton);
254  uploadButton.addEventListener(Events.ON_CLICK, new EventListener() {
255  @Override
256  public void onEvent(Event event) throws Exception {
257  if(currentFolder != null) {
258  currentFolder.doUpload();
259  attachGrid.setCurrentFolder(currentFolder);
260  }
261  }
262 
263  });
264 
265  sendButton = new Toolbarbutton(I_.get("Send notification"), "/_zul/images/mail_send.png");
266  sendButton.setDisabled(true);
267  toolbar.appendChild(sendButton);
268  sendButton.addEventListener(Events.ON_CLICK, new EventListener() {
269  @Override
270  public void onEvent(Event event) throws Exception {
271  SendAttachments sa = new SendAttachments(I_.get("Attachment")) {
272  @Override
273  protected void fillAttachment(File attachment, Object entity) {
274  AttachWd.writeToFile((Attachment) entity, attachment);
275  }
276  };
277  AttachFolder af = attachGrid.getCurrentFolder();
278  if(af != null) {
279  for(Attachment a : af.getAttachments()) {
280  sa.addAttachment(a, AttachPU.getObjectPath(a), a.getFileName());
281  }
282  }
283  sa.sendAttachments(null);
284  }
285  });
286 
287  toolbar.appendChild(new Space());
288 
289  copyButton = new Toolbarbutton(I_.get("Copy"), "/_zul/images/edit-copy.png");
290  copyButton.setDisabled(true);
291  copyButton.addEventListener(Events.ON_CLICK, new EventListener() {
292  @Override
293  public void onEvent(Event event) throws Exception {
294  AttachmentUtil.copyFolder(currentFolder.getPath());
295  }
296  });
297  toolbar.appendChild(copyButton);
298 
299  cutButton = new Toolbarbutton(I_.get("Cut"), "/_zul/images/edit-cut.png");
300  cutButton.setDisabled(true);
301  cutButton.addEventListener(Events.ON_CLICK, new EventListener() {
302  @Override
303  public void onEvent(Event event) throws Exception {
304  AttachmentUtil.cutFolder(currentFolder.getLabel(), currentFolder.getPath());
305  }
306  });
307  toolbar.appendChild(cutButton);
308 
309  pasteButton = new Toolbarbutton(I_.get("Paste"), "/_zul/images/edit-paste.png");
310  pasteButton.setDisabled(true);
311  toolbar.appendChild(pasteButton);
312  pasteButton.addEventListener(Events.ON_CLICK, new EventListener() {
313  @Override
314  public void onEvent(Event event) throws Exception {
315  AttachmentUtil.paste(currentFolder.getPath());
316  attachTree.setSelectedItem(currentFolder);
317  currentFolder.reloadContents();
318  attachGrid.setCurrentFolder(currentFolder);
319  pasteButton.setDisabled(true);
320  }
321  });
322 
323  if(Application.getApplication().isInRole("attach:delete")) {
324  deleteButton = new Toolbarbutton(I_.get("Delete"), "/_zul/images/edit-delete.png");
325  deleteButton.setDisabled(true);
326  deleteButton.addEventListener(Events.ON_CLICK, new EventListener() {
327  @Override
328  public void onEvent(Event event) throws Exception {
329  Messages.confirmDeletion().show(() -> {
330  Collection<Attachment> deleted = AttachmentUtil.delete(currentFolder.getPath());
331  if(attachTree.getAttachListener() != null) {
332  for(Attachment a : deleted) {
333  attachTree.getAttachListener().deleteDone(a);
334  }
335  }
336  attachTree.setSelectedItem(currentFolder);
337  attachGrid.setCurrentFolder(currentFolder);
338  });
339  }
340  });
341  toolbar.appendChild(deleteButton);
342  }
343 
344  dropupload = new Dropupload();
345  dropupload.setDetection("browser");
346  dropupload.setNative(true);
347  dropupload.setContent(
348  "<table border='0' cellpadding='10' style='width:100%;background-color:#e1e1e1;'><tr><td style='width:84px;border:none;'><img border='0' src='" +
349  ElephantContext.getRootWebPath() + Images.getImage("64/downloads") +
350  "'/></td><td style='border:none;' class='textShadow textCaption1'>" + I_.get("Drop files") + "</td></tr></table>");
351  dropupload.setMaxsize(-1);
352  dropupload.setHeight("84px");
353  dropupload.setWidth("100%");
354  dropupload.addEventListener(Events.ON_UPLOAD, new EventListener<UploadEvent>() {
355  @Override
356  public void onEvent(UploadEvent event) throws Exception {
357  if(currentFolder != null) {
358  currentFolder.addMedias(event.getMedias());
359  attachGrid.setCurrentFolder(currentFolder);
360  }
361  }
362  });
363  centerPanel.getPanelchildren().appendChild(dropupload);
364 
365  attachGrid = new AttachNavigatorGrid();
366  attachGrid.setVflex(true);
367 
368  centerPanel.getPanelchildren().appendChild(attachGrid);
369  attachGrid.afterCompose();
370  }
371 
372  private void initResults() {
373  results = new AttachResults();
374  results.markAll();
375  results.setCkOthers(false);
376  results.setPublicOnly(publicOnly);
377  }
378 
379  private void updateButtons() {
380  addFolderButton.setDisabled(currentFolder == null);
381  uploadButton.setDisabled(currentFolder == null);
382  sendButton.setDisabled(currentFolder == null);
383  upButton.setDisabled(currentFolder == null || currentFolder.getParentItem() == null);
384  copyButton.setDisabled(currentFolder == null);
385  cutButton.setDisabled(currentFolder == null);
386  pasteButton.setDisabled(!AttachmentUtil.hasClipboard());
387  if(deleteButton != null) {
388  deleteButton.setDisabled(currentFolder == null || currentFolder.getParentFolder() == null);
389  }
390 // if(wdButton != null) {
391 // wdButton.setDisabled(currentFolder == null || currentFolder.getParentItem() != null);
392 // }
393  }
394 
395  @Override
396  public void afterCompose() {
397  initResults();
398  addComponents();
399  }
400 
401  public void selectFirst() {
402  currentFolder = attachTree.selectFirst();
403  updateButtons();
404  attachGrid.setCurrentFolder(currentFolder);
405  if(wdButton != null) {
406  //wdButton.setHref(AttachMenu.createJnlpCallFor(currentFolder.getLabel(), currentFolder.getPath()));
407  }
408  }
409 
410 }
void setPublicOnly(boolean publicOnly)
void setAttachValue(String attachValue)
void setCkNonValidated(boolean ckNonValidated)
void setWorkingDirOption(boolean workingDirOption)