19 package org.turro.file.zul.navigator;
22 import java.io.IOException;
23 import java.util.Collection;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.amic.util.file.FileUtil;
27 import org.apache.commons.io.FileUtils;
28 import static org.apache.commons.io.FileUtils.isSymlink;
29 import org.turro.command.Command;
30 import org.turro.command.Context;
31 import org.turro.elephant.context.Application;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.util.Messages;
34 import org.turro.file.FileWrapper;
35 import org.turro.file.action.FileAction;
36 import org.turro.file.zul.FileClipboard;
37 import org.turro.i18n.I_;
38 import org.turro.zkoss.dialog.InputDialog;
39 import org.turro.zkoss.dialog.InputField;
40 import org.zkoss.zk.ui.Component;
41 import org.zkoss.zk.ui.event.Event;
42 import org.zkoss.zk.ui.event.EventListener;
43 import org.zkoss.zk.ui.event.Events;
44 import org.zkoss.zul.A;
45 import org.zkoss.zul.Detail;
46 import org.zkoss.zul.Hlayout;
47 import org.zkoss.zul.Label;
48 import org.zkoss.zul.Row;
49 import org.zkoss.zul.Space;
50 import org.zkoss.zul.Toolbarbutton;
51 import org.zkoss.zul.Vbox;
64 setStyle(
"cursor:pointer");
66 A a =
new A(folder.getLabel(),
"/_zul/images/folder.png");
67 a.addEventListener(Events.ON_CLICK,
new EventListener() {
69 public void onEvent(Event event)
throws Exception {
70 folder.
getTree().setSelectedItem(folder);
71 Events.postEvent(
new Event(Events.ON_SELECT, folder.
getTree()));
75 appendChild(getSizeComponent());
76 appendChild(
new Label(
""));
79 private boolean _listenerAttached =
false;
80 private boolean _onOpenTriggered =
false;
82 private void addDetail() {
83 final Detail detail =
new Detail();
86 detail.addEventListener(Events.ON_OPEN,
new EventListener() {
88 public void onEvent(Event event)
throws Exception {
89 _onOpenTriggered =
true;
94 if(!_listenerAttached) {
95 addEventListener(Events.ON_CLICK,
new EventListener<Event>() {
97 public void onEvent(Event event) throws Exception {
98 if(_onOpenTriggered) {
99 _onOpenTriggered = false;
102 folder.getTree().setSelectedItem(folder);
103 Events.postEvent(new Event(Events.ON_SELECT, folder.getTree()));
106 _listenerAttached =
true;
110 private void fillDetail(Detail detail) {
111 if(detail.getChildren().isEmpty()) {
112 Application app = Application.getApplication();
114 Vbox vbox =
new Vbox();
115 vbox.setSclass(
"detailBox");
116 vbox.setStyle(
"background-image:url('" +
117 ElephantContext.getCurrent().getContextPath() +
118 "/_zul/images/bg/sunken.png')");
119 detail.appendChild(vbox);
121 Hlayout buttons =
new Hlayout();
122 vbox.appendChild(buttons);
123 if(app.isInRole(
"file-attach:edit")) {
124 Toolbarbutton edit =
new Toolbarbutton();
125 edit.setImage(
"/_zul/images/properties.png");
126 edit.setTooltiptext(I_.get(
"Properties"));
127 edit.addEventListener(Events.ON_CLICK,
new EventListener() {
129 public void onEvent(Event event)
throws Exception {
130 InputDialog.getInput(
134 new InputField(
"Name", folder.getRealFolder().getName(), null, 0)
137 public Object execute(Context context) {
138 InputField[] fields = (InputField[]) context.get(
"fields");
139 if(fields.length > 0) {
140 for(InputField f : fields) {
141 if(
"Name".equals(f.getLabel())) {
143 Events.postEvent(
new Event(Events.ON_CHANGE, folder.
getTree()));
152 buttons.appendChild(edit);
153 buttons.appendChild(
new Space());
155 Toolbarbutton copy =
new Toolbarbutton(I_.get(
"Copy"),
"/_zul/images/edit-copy.png");
156 copy.addEventListener(Events.ON_CLICK,
new EventListener() {
158 public void onEvent(Event event)
throws Exception {
162 buttons.appendChild(copy);
163 Toolbarbutton cut =
new Toolbarbutton(I_.get(
"Cut"),
"/_zul/images/edit-cut.png");
164 cut.addEventListener(Events.ON_CLICK,
new EventListener() {
166 public void onEvent(Event event)
throws Exception {
167 FileClipboard.cutFolder(folder.getLabel(), folder.
getRealFolder());
170 buttons.appendChild(cut);
171 final Toolbarbutton paste =
new Toolbarbutton(I_.get(
"Paste"),
"/_zul/images/edit-paste.png");
172 paste.setDisabled(!FileClipboard.hasClipboard());
173 paste.addEventListener(Events.ON_CLICK,
new EventListener() {
175 public void onEvent(Event event)
throws Exception {
177 paste.setDisabled(
true);
180 buttons.appendChild(paste);
181 if(app.isInRole(
"file-attach:delete")) {
182 Toolbarbutton
delete =
new Toolbarbutton(I_.get(
"Delete"),
"/_zul/images/edit-delete.png");
183 delete.addEventListener(Events.ON_CLICK,
new EventListener() {
185 public void onEvent(Event event)
throws Exception {
186 Messages.confirmDeletion().show(() -> {
189 FolderRow.this.detach();
190 Events.postEvent(
new Event(Events.ON_CHANGE, folder.
getTree()));
191 }
catch (IOException ex) {
192 Logger.getLogger(FolderRow.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
197 buttons.appendChild(
delete);
199 buttons.appendChild(
new Space());
200 FileWrapper wrapper = FileWrapper.getFileByType(folder.
getRealFolder());
201 Toolbarbutton gallery = doButton(wrapper.getAction(I_.get(
"Create a gallery")));
202 if(gallery !=
null) buttons.appendChild(gallery);
203 Toolbarbutton zip = doButton(wrapper.getAction(I_.get(
"Zip")));
204 if(zip !=
null) buttons.appendChild(zip);
208 private Toolbarbutton doButton(
final FileAction action) {
210 Toolbarbutton tb =
new Toolbarbutton(action.getLabel(), action.getImage());
211 tb.addEventListener(Events.ON_CLICK,
new EventListener<Event>() {
213 public void onEvent(Event event) throws Exception {
222 private Component getSizeComponent() {
223 String size =
new org.turro.formatter.BytesFormatter(sizeOfDirectory(folder.
getRealFolder())).toString();
224 return new Label(size);
227 private long sizeOfDirectory(File directory) {
228 final Collection<File> files = folder.
getFilesFrom(directory);
230 for(
final File file : files) {
231 if (!isSymlink(file)) {
232 size += sizeOf(file);
241 private long sizeOf(File file) {
242 if(file.isDirectory()) {
243 return sizeOfDirectory(file);
245 return file.length();
Collection< File > getFilesFrom(File folder)
FolderRow(final FileFolder folder)