BrightSide Workbench Full Report + Source Code
file/zul/navigator/FolderRow.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 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.navigator;
20 
21 import java.io.File;
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;
52 
57 public class FolderRow extends Row {
58 
59  private final FileFolder folder;
60 
61  public FolderRow(final FileFolder folder) {
62  setValue(folder);
63  this.folder = folder;
64  setStyle("cursor:pointer");
65  addDetail();
66  A a = new A(folder.getLabel(), "/_zul/images/folder.png");
67  a.addEventListener(Events.ON_CLICK, new EventListener() {
68  @Override
69  public void onEvent(Event event) throws Exception {
70  folder.getTree().setSelectedItem(folder);
71  Events.postEvent(new Event(Events.ON_SELECT, folder.getTree()));
72  }
73  });
74  appendChild(a);
75  appendChild(getSizeComponent());
76  appendChild(new Label(""));
77  }
78 
79  private boolean _listenerAttached = false;
80  private boolean _onOpenTriggered = false;
81 
82  private void addDetail() {
83  final Detail detail = new Detail();
84  appendChild(detail);
85 
86  detail.addEventListener(Events.ON_OPEN, new EventListener() {
87  @Override
88  public void onEvent(Event event) throws Exception {
89  _onOpenTriggered = true;
90  fillDetail(detail);
91  }
92  });
93 
94  if(!_listenerAttached) {
95  addEventListener(Events.ON_CLICK, new EventListener<Event>() {
96  @Override
97  public void onEvent(Event event) throws Exception {
98  if(_onOpenTriggered) {
99  _onOpenTriggered = false;
100  return;
101  }
102  folder.getTree().setSelectedItem(folder);
103  Events.postEvent(new Event(Events.ON_SELECT, folder.getTree()));
104  }
105  });
106  _listenerAttached = true;
107  }
108  }
109 
110  private void fillDetail(Detail detail) {
111  if(detail.getChildren().isEmpty()) {
112  Application app = Application.getApplication();
113 
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);
120 
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() {
128  @Override
129  public void onEvent(Event event) throws Exception {
130  InputDialog.getInput(
131  getPage(),
132  I_.get("File"),
133  new InputField[] {
134  new InputField("Name", folder.getRealFolder().getName(), null, 0)
135  }, new Command() {
136  @Override
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())) {
142  folder.getRealFolder().renameTo(new File(FileUtil.getParentPath(folder.getRealFolder()) + "/" + (String) f.getValue()));
143  Events.postEvent(new Event(Events.ON_CHANGE, folder.getTree()));
144  }
145  }
146  }
147  return null;
148  }
149  });
150  }
151  });
152  buttons.appendChild(edit);
153  buttons.appendChild(new Space());
154  }
155  Toolbarbutton copy = new Toolbarbutton(I_.get("Copy"), "/_zul/images/edit-copy.png");
156  copy.addEventListener(Events.ON_CLICK, new EventListener() {
157  @Override
158  public void onEvent(Event event) throws Exception {
159  FileClipboard.copyFolder(folder.getRealFolder());
160  }
161  });
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() {
165  @Override
166  public void onEvent(Event event) throws Exception {
167  FileClipboard.cutFolder(folder.getLabel(), folder.getRealFolder());
168  }
169  });
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() {
174  @Override
175  public void onEvent(Event event) throws Exception {
176  FileClipboard.paste(folder.getRealFolder());
177  paste.setDisabled(true);
178  }
179  });
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() {
184  @Override
185  public void onEvent(Event event) throws Exception {
186  Messages.confirmDeletion().show(() -> {
187  try {
188  FileUtils.deleteDirectory(folder.getRealFolder());
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);
193  }
194  });
195  }
196  });
197  buttons.appendChild(delete);
198  }
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);
205  }
206  }
207 
208  private Toolbarbutton doButton(final FileAction action) {
209  if(action != null) {
210  Toolbarbutton tb = new Toolbarbutton(action.getLabel(), action.getImage());
211  tb.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
212  @Override
213  public void onEvent(Event event) throws Exception {
214  action.doAction();
215  }
216  });
217  return tb;
218  }
219  return null;
220  }
221 
222  private Component getSizeComponent() {
223  String size = new org.turro.formatter.BytesFormatter(sizeOfDirectory(folder.getRealFolder())).toString();
224  return new Label(size);
225  }
226 
227  private long sizeOfDirectory(File directory) {
228  final Collection<File> files = folder.getFilesFrom(directory);
229  long size = 0;
230  for(final File file : files) {
231  if (!isSymlink(file)) {
232  size += sizeOf(file);
233  if (size < 0) {
234  break;
235  }
236  }
237  }
238  return size;
239  }
240 
241  private long sizeOf(File file) {
242  if(file.isDirectory()) {
243  return sizeOfDirectory(file);
244  } else {
245  return file.length();
246  }
247  }
248 
249 }
Collection< File > getFilesFrom(File folder)