BrightSide Workbench Full Report + Source Code
zul/tree/AttachTree.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.tree;
19 
20 import java.util.EnumSet;
21 import java.util.Set;
22 import org.turro.attach.entity.Attachment;
23 import org.turro.attach.search.AttachResults;
24 import org.turro.elephant.context.Application;
25 import org.turro.i18n.I_;
26 import org.zkoss.zk.ui.event.Event;
27 import org.zkoss.zk.ui.event.EventListener;
28 import org.zkoss.zk.ui.event.Events;
29 import org.zkoss.zul.Checkbox;
30 import org.zkoss.zul.Separator;
31 import org.zkoss.zul.Space;
32 import org.zkoss.zul.Textbox;
33 import org.zkoss.zul.Toolbar;
34 import org.zkoss.zul.Toolbarbutton;
35 import org.zkoss.zul.Tree;
36 import org.zkoss.zul.Treechildren;
37 import org.zkoss.zul.Treecol;
38 import org.zkoss.zul.Treecols;
39 
44 @Deprecated
45 public class AttachTree extends Tree {
46 
47  private Treechildren children;
48  private Set<AttachColumn> columns;
49  private Toolbar toolbar;
50  private Toolbarbutton addFolderButton, uploadButton, downloadButton,
51  editButton, /*excelButton,*/ deleteButton;
52  private AttachListener attachListener;
53  private String title;
54  private AttachResults results;
55  private boolean onlyDownload = false, publicOnly = false;
56 
57  public AttachTree() {
59  }
60 
61  public AttachTree(String title) {
63  }
64 
65  public AttachTree(String title, Set<AttachColumn> columns, boolean publicOnly) {
66  super();
67  this.title = title;
68  this.columns = columns;
69  addColumns();
70  addChildrenSpace();
71  results = new AttachResults();
72  results.setPublicOnly(publicOnly);
73  results.markAll();
74  results.setCkOthers(false);
75  }
76 
78  return attachListener;
79  }
80 
81  public void setAttachListener(AttachListener listener) {
82  this.attachListener = listener;
83  }
84 
85  public void setAddToolbar(boolean addToolbar) {
86  if(addToolbar) {
87  toolbar = new Toolbar();
88  getParent().insertBefore(toolbar, AttachTree.this);
89  if(!onlyDownload) {
90  addSearchbarButtons();
91  toolbar.appendChild(new Space());
92  }
93  addToolbarButtons();
94  }
95  addEventListener(Events.ON_SELECT, new EventListener() {
96  @Override
97  public void onEvent(Event event) throws Exception {
98  updateButtons();
99  if(getSelectedItem() instanceof AttachFolder) {
100  ((AttachFolder) getSelectedItem()).showContents();
101  } else if(onlyDownload && getSelectedItem() instanceof AttachFile) {
102  ((AttachFile) getSelectedItem()).doDownload();
103  }
104  }
105  });
106  }
107 
108  public boolean isOnlyDownload() {
109  return onlyDownload;
110  }
111 
112  public void setOnlyDownload(boolean onlyDownload) {
113  this.onlyDownload = onlyDownload;
114  }
115 
116  public AttachFolder addFolder(String label, String path, boolean loadOnDemand) {
117  AttachFolder af = new AttachFolder(label, path, loadOnDemand, columns);
118  children.appendChild(af);
119  af.doProcessors();
120  return af;
121  }
122 
123  public Set<AttachColumn> getColumns() {
124  return columns;
125  }
126 
128  if(getSelectedItem() instanceof AttachFile) {
129  return ((AttachFile) getSelectedItem()).getAttachment();
130  }
131  return null;
132  }
133 
135  return results;
136  }
137 
138  private void addColumns() {
139  Treecols cols = new Treecols();
140  cols.setSizable(true);
141  appendChild(cols);
142 
143  Treecol col = new Treecol();
144  col.setLabel(title == null ? "" : title);
145  cols.appendChild(col);
146 
147  if(columns.contains(AttachColumn.ATTACH_SIZE)) {
148  col = new Treecol();
149  col.setLabel(I_.get("Size"));
150  col.setWidth("70px");
151  cols.appendChild(col);
152  }
153  if(columns.contains(AttachColumn.ATTACH_DATE)) {
154  col = new Treecol();
155  col.setLabel(I_.get("Date"));
156  col.setWidth("90px");
157  cols.appendChild(col);
158  }
159  if(columns.contains(AttachColumn.ATTACH_VALIDATED)) {
160  col = new Treecol();
161  col.setLabel(I_.get("Validated"));
162  col.setWidth("25px");
163  cols.appendChild(col);
164  }
165  if(columns.contains(AttachColumn.ATTACH_OWNER)) {
166  col = new Treecol();
167  col.setLabel(I_.get("Owner"));
168  col.setWidth("200px");
169  cols.appendChild(col);
170  }
171  }
172 
173  private void addChildrenSpace() {
174  children = new Treechildren();
175  appendChild(children);
176  }
177 
178  private void addToolbarButtons() {
179  if(!onlyDownload && getCanNew()) {
180  addFolderButton = new Toolbarbutton(
181  I_.get("Add folder"),
182  "/_zul/images/new.png"
183  );
184  toolbar.appendChild(addFolderButton);
185  addFolderButton.addEventListener(Events.ON_CLICK, new EventListener() {
186  @Override
187  public void onEvent(Event event) throws Exception {
188  if(AttachTree.this.getSelectedItem() instanceof AttachFolder) {
189  ((AttachFolder) AttachTree.this.getSelectedItem()).doAddFolder();
190  updateButtons();
191  }
192  }
193 
194  });
195 
196  uploadButton = new Toolbarbutton(
197  I_.get("Upload"),
198  "/_zul/images/upload.png"
199  );
200  toolbar.appendChild(uploadButton);
201  uploadButton.addEventListener(Events.ON_CLICK, new EventListener() {
202  @Override
203  public void onEvent(Event event) throws Exception {
204  if(AttachTree.this.getSelectedItem() instanceof AttachFolder) {
205  ((AttachFolder) AttachTree.this.getSelectedItem()).doUpload();
206  updateButtons();
207  }
208  }
209 
210  });
211  }
212 
213  downloadButton = new Toolbarbutton(
214  I_.get("Download"),
215  "/_zul/images/download.png"
216  );
217  toolbar.appendChild(downloadButton);
218  downloadButton.addEventListener(Events.ON_CLICK, new EventListener() {
219 
220  @Override
221  public void onEvent(Event event) throws Exception {
222  if(AttachTree.this.getSelectedItem() instanceof AttachFile) {
223  ((AttachFile) AttachTree.this.getSelectedItem()).doDownload();
224  updateButtons();
225  }
226  }
227 
228  });
229 
230  if(!onlyDownload) {
231  if(getCanEdit()) {
232  editButton = new Toolbarbutton(
233  I_.get("Edit"),
234  "/_zul/images/edit.png"
235  );
236  toolbar.appendChild(editButton);
237  editButton.addEventListener(Events.ON_CLICK, new EventListener() {
238 
239  @Override
240  public void onEvent(Event event) throws Exception {
241  if(AttachTree.this.getSelectedItem() instanceof AttachFile) {
242  ((AttachFile) AttachTree.this.getSelectedItem()).doEdit();
243  updateButtons();
244  }
245  }
246 
247  });
248 
249 // excelButton = new Toolbarbutton(
250 // I_.get("Edit sheet"),
251 // "/_zul/images/edit.png"
252 // );
253 // toolbar.appendChild(excelButton);
254 // excelButton.addEventListener(Events.ON_CLICK, new EventListener() {
255 //
256 // public void onEvent(Event event) throws Exception {
257 // if(AttachTree.this.getSelectedItem() instanceof AttachFile) {
258 // new AttachSheet((AttachFile) AttachTree.this.getSelectedItem()).edit();
259 // updateButtons();
260 // }
261 // }
262 //
263 // });
264  }
265 
266  if(getCanDelete()) {
267  deleteButton = new Toolbarbutton(
268  I_.get("Delete"),
269  "/_zul/images/edit-delete.png"
270  );
271  toolbar.appendChild(deleteButton);
272  deleteButton.addEventListener(Events.ON_CLICK, new EventListener() {
273 
274  @Override
275  public void onEvent(Event event) throws Exception {
276  if(AttachTree.this.getSelectedItem() instanceof AttachFile) {
277  ((AttachFile) AttachTree.this.getSelectedItem()).doDelete();
278  updateButtons();
279  }
280  }
281 
282  });
283  }
284  }
285 
286  updateButtons();
287  }
288 
289  public void updateButtons() {
290  if(toolbar != null) {
291  boolean isFolder = getSelectedItem() instanceof AttachFolder,
292  isFile = getSelectedItem() instanceof AttachFile;
293  if(!onlyDownload) {
294  if(addFolderButton != null) addFolderButton.setVisible(isFolder);
295  if(uploadButton != null) uploadButton.setVisible(isFolder);
296  }
297  downloadButton.setVisible(isFile);
298  if(!onlyDownload) {
299  if(editButton != null) editButton.setVisible(isFile);
300 // if(excelButton != null) excelButton.setVisible(isFile && new AttachSheet((AttachFile) getSelectedItem()).isSheet());
301  if(deleteButton != null) deleteButton.setVisible(isFile);
302  }
303  }
304  }
305 
306  private void addSearchbarButtons() {
307 
308  if(results != null) {
309 
310  if(Application.getApplication().isInRole("attach-show:all")) {
311  final Checkbox showAll = new Checkbox(I_.get("Others"));
312  showAll.setChecked(results.isCkOthers());
313  showAll.setStyle("font-size:11px");
314  showAll.addEventListener(Events.ON_CHECK, new EventListener() {
315  @Override
316  public void onEvent(Event event) throws Exception {
317  results.setCkOthers(showAll.isChecked());
318  }
319  });
320  toolbar.appendChild(showAll);
321  }
322 
323  toolbar.appendChild(new Separator("vertical"));
324 
325  final Checkbox showNonValidated = new Checkbox(I_.get("Non validated"));
326  showNonValidated.setChecked(results.isCkNonValidated());
327  showNonValidated.setStyle("font-size:11px");
328  showNonValidated.addEventListener(Events.ON_CHECK, new EventListener() {
329  @Override
330  public void onEvent(Event event) throws Exception {
331  results.setCkNonValidated(showNonValidated.isChecked());
332  }
333  });
334  toolbar.appendChild(showNonValidated);
335 
336  toolbar.appendChild(new Separator("vertical"));
337 
338  final Textbox attachValue = new Textbox(results.getAttachValue());
339  attachValue.setStyle("font-size:11px");
340  attachValue.addEventListener(Events.ON_CHANGE, new EventListener() {
341  @Override
342  public void onEvent(Event event) throws Exception {
343  results.setAttachValue(attachValue.getText());
344  }
345  });
346  attachValue.addEventListener(Events.ON_OK, new EventListener() {
347  @Override
348  public void onEvent(Event event) throws Exception {
349  for(Object o : children.getChildren()) {
350  if(o instanceof AttachFolder) {
351  ((AttachFolder) o).reloadContents();
352  }
353  }
354  }
355  });
356  toolbar.appendChild(attachValue);
357 
358  Toolbarbutton searchButton = new Toolbarbutton(
359  I_.get("Search"),
360  "/_zul/images/search.png"
361  );
362  searchButton.addEventListener(Events.ON_CLICK, new EventListener() {
363  @Override
364  public void onEvent(Event event) throws Exception {
365  for(Object o : children.getChildren()) {
366  if(o instanceof AttachFolder) {
367  ((AttachFolder) o).reloadContents();
368  }
369  }
370  }
371  });
372  toolbar.appendChild(searchButton);
373 
374  }
375  }
376 
377  public boolean getCanDelete() {
378  return Application.getApplication().isInRole("attach:delete");
379  }
380 
381  public boolean getCanNew() {
382  return Application.getApplication().isInRole("attach:new");
383  }
384 
385  public boolean getCanEdit() {
386  return Application.getApplication().isInRole("attach:edit");
387  }
388 
389 }
void setPublicOnly(boolean publicOnly)
void setAttachValue(String attachValue)
void setCkNonValidated(boolean ckNonValidated)
AttachTree(String title, Set< AttachColumn > columns, boolean publicOnly)
AttachFolder addFolder(String label, String path, boolean loadOnDemand)
void setOnlyDownload(boolean onlyDownload)
void setAttachListener(AttachListener listener)
static String get(String msg)
Definition: I_.java:41