BrightSide Workbench Full Report + Source Code
attach/zul/navigator/FolderRow.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 org.turro.attach.zul.AttachmentUtil;
21 import org.turro.elephant.context.Application;
22 import org.turro.elephant.context.ElephantContext;
23 import org.turro.elephant.util.Messages;
24 import org.turro.i18n.I_;
25 import org.turro.util.CompareUtil;
26 import org.turro.zkoss.label.LabelTypes;
27 import org.zkoss.zk.ui.Component;
28 import org.zkoss.zk.ui.event.Event;
29 import org.zkoss.zk.ui.event.EventListener;
30 import org.zkoss.zk.ui.event.Events;
31 import org.zkoss.zul.*;
32 
37 public class FolderRow extends Row {
38 
39  private AttachFolder folder;
40 
41  public FolderRow(final AttachFolder folder) {
42  setValue(folder);
43  this.folder = folder;
44  setStyle("cursor:pointer");
45  addDetail();
46  A a = new A(folder.getLabel(), "/_zul/images/folder.png");
47  a.addEventListener(Events.ON_CLICK, new EventListener() {
48  @Override
49  public void onEvent(Event event) throws Exception {
50  folder.getTree().setSelectedItem(folder);
51  Events.postEvent(new Event(Events.ON_SELECT, folder.getTree()));
52  }
53  });
54  appendChild(a);
55  appendChild(getSizeComponent());
56  appendChild(new Label(""));
57  }
58 
59  private boolean _listenerAttached = false;
60  private boolean _onOpenTriggered = false;
61 
62  private void addDetail() {
63  final Detail detail = new Detail();
64  appendChild(detail);
65 
66  detail.addEventListener(Events.ON_OPEN, new EventListener() {
67  @Override
68  public void onEvent(Event event) throws Exception {
69  _onOpenTriggered = true;
70  fillDetail(detail);
71  }
72  });
73 
74  if(!_listenerAttached) {
75  addEventListener(Events.ON_CLICK, new EventListener<Event>() {
76  @Override
77  public void onEvent(Event event) throws Exception {
78  if(_onOpenTriggered) {
79  _onOpenTriggered = false;
80  return;
81  }
82  folder.getTree().setSelectedItem(folder);
83  Events.postEvent(new Event(Events.ON_SELECT, folder.getTree()));
84  }
85  });
86  _listenerAttached = true;
87  }
88  }
89 
90  private void fillDetail(Detail detail) {
91  if(detail.getChildren().isEmpty()) {
92  Application app = Application.getApplication();
93 
94  Vbox vbox = new Vbox();
95  vbox.setSclass("detailBox");
96  vbox.setStyle("background-image:url('" +
97  ElephantContext.getCurrent().getContextPath() +
98  "/_zul/images/bg/sunken.png')");
99  detail.appendChild(vbox);
100 
101  Hlayout buttons = new Hlayout();
102  vbox.appendChild(buttons);
103  Toolbarbutton copy = new Toolbarbutton(I_.get("Copy"), "/_zul/images/edit-copy.png");
104  copy.addEventListener(Events.ON_CLICK, new EventListener() {
105  @Override
106  public void onEvent(Event event) throws Exception {
107  AttachmentUtil.copyFolder(folder.getPath());
108  }
109  });
110  buttons.appendChild(copy);
111  Toolbarbutton cut = new Toolbarbutton(I_.get("Cut"), "/_zul/images/edit-cut.png");
112  cut.addEventListener(Events.ON_CLICK, new EventListener() {
113  @Override
114  public void onEvent(Event event) throws Exception {
115  AttachmentUtil.cutFolder(folder.getLabel(), folder.getPath());
116  }
117  });
118  buttons.appendChild(cut);
119  final Toolbarbutton paste = new Toolbarbutton(I_.get("Paste"), "/_zul/images/edit-paste.png");
120  paste.setDisabled(!AttachmentUtil.hasClipboard());
121  paste.addEventListener(Events.ON_CLICK, new EventListener() {
122  @Override
123  public void onEvent(Event event) throws Exception {
124  AttachmentUtil.paste(folder.getPath());
125  paste.setDisabled(true);
126  }
127  });
128  buttons.appendChild(paste);
129  if(app.isInRole("attach:delete")) {
130  Toolbarbutton delete = new Toolbarbutton(I_.get("Delete"), "/_zul/images/edit-delete.png");
131  delete.addEventListener(Events.ON_CLICK, new EventListener() {
132  @Override
133  public void onEvent(Event event) throws Exception {
134  Messages.confirmDeletion().show(() -> {
135  AttachmentUtil.delete(folder.getPath());
136  FolderRow.this.detach();
137  });
138  }
139  });
140  buttons.appendChild(delete);
141  }
142  }
143  }
144 
145  private Component getSizeComponent() {
146  FolderData fd = new FolderData(folder.getPath());
147  Vlayout vbox = new Vlayout();
148  vbox.setSpacing("0px");
149  vbox.setStyle("text-align:right");
150  String size = new org.turro.formatter.BytesFormatter(fd.getSize()).toString(),
151  versioned = new org.turro.formatter.BytesFormatter(fd.getVersionedSize()).toString();
152  vbox.appendChild(new Label(size));
153  if(CompareUtil.compare(size, versioned) != 0) {
154  vbox.appendChild(LabelTypes.getTinyLabel(versioned));
155  }
156  return vbox;
157  }
158 
159 }