BrightSide Workbench Full Report + Source Code
file/zul/repository/FolderRow.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.repository;
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 org.turro.command.Command;
29 import org.turro.command.Context;
30 import org.turro.elephant.context.Application;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.elephant.util.Messages;
33 import org.turro.file.FileWrapper;
34 import org.turro.file.action.FileAction;
35 import org.turro.file.zul.FileClipboard;
36 import org.turro.i18n.I_;
37 import org.turro.zkoss.dialog.InputDialog;
38 import org.turro.zkoss.dialog.InputField;
39 import org.zkoss.zk.ui.Component;
40 import org.zkoss.zk.ui.event.Event;
41 import org.zkoss.zk.ui.event.EventListener;
42 import org.zkoss.zk.ui.event.Events;
43 import org.zkoss.zul.A;
44 import org.zkoss.zul.Detail;
45 import org.zkoss.zul.Hlayout;
46 import org.zkoss.zul.Label;
47 import org.zkoss.zul.Row;
48 import org.zkoss.zul.Space;
49 import org.zkoss.zul.Toolbarbutton;
50 import org.zkoss.zul.Vbox;
51 
56 public class FolderRow extends Row {
57 
58  private final RepositoryItem folder;
59 
60  public FolderRow(final RepositoryItem folder) {
61  setValue(folder);
62  this.folder = folder;
63  setStyle("cursor:pointer");
64  addDetail();
65  A a = new A(folder.getLabel(), "/_zul/images/folder.png");
66  a.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
67  @Override
68  public void onEvent(Event event) throws Exception {
69  folder.getTree().setSelectedItem(folder);
70  Events.postEvent(new Event(Events.ON_SELECT, folder.getTree()));
71  }
72  });
73  appendChild(a);
74  appendChild(getSizeComponent());
75  appendChild(new Label(""));
76  }
77 
78  private boolean _listenerAttached = false;
79  private boolean _onOpenTriggered = false;
80 
81  private void addDetail() {
82  final Detail detail = new Detail();
83  appendChild(detail);
84 
85  detail.addEventListener(Events.ON_OPEN, new EventListener() {
86  @Override
87  public void onEvent(Event event) throws Exception {
88  _onOpenTriggered = true;
89  fillDetail(detail);
90  }
91  });
92 
93  if(!_listenerAttached) {
94  addEventListener(Events.ON_CLICK, new EventListener<Event>() {
95  @Override
96  public void onEvent(Event event) throws Exception {
97  if(_onOpenTriggered) {
98  _onOpenTriggered = false;
99  return;
100  }
101  folder.getTree().setSelectedItem(folder);
102  Events.postEvent(new Event(Events.ON_SELECT, folder.getTree()));
103  }
104  });
105  _listenerAttached = true;
106  }
107  }
108 
109  private void fillDetail(Detail detail) {
110  if(detail.getChildren().isEmpty()) {
111  Application app = Application.getApplication();
112 
113  Vbox vbox = new Vbox();
114  vbox.setSclass("detailBox");
115  vbox.setStyle("background-image:url('" +
116  ElephantContext.getCurrent().getContextPath() +
117  "/_zul/images/bg/sunken.png')");
118  detail.appendChild(vbox);
119 
120  Hlayout buttons = new Hlayout();
121  vbox.appendChild(buttons);
122  if(app.isInRole("file-attach:edit")) {
123  Toolbarbutton edit = new Toolbarbutton();
124  edit.setImage("/_zul/images/properties.png");
125  edit.setTooltiptext(I_.get("Properties"));
126  edit.addEventListener(Events.ON_CLICK, new EventListener() {
127  @Override
128  public void onEvent(Event event) throws Exception {
129  InputDialog.getInput(
130  getPage(),
131  I_.get("File"),
132  new InputField[]{
133  new InputField("Name", folder.getRealFolder().getName(), null, 0)
134  }, new Command() {
135  @Override
136  public Object execute(Context context) {
137  InputField[] fields = (InputField[]) context.get("fields");
138  if(fields.length > 0) {
139  for(InputField f : fields) {
140  if("Name".equals(f.getLabel())) {
141  folder.getRealFolder().renameTo(new File(FileUtil.getParentPath(folder.getRealFolder()) + "/" + (String) f.getValue()));
142  Events.postEvent(new Event(Events.ON_CHANGE, folder.getTree()));
143  }
144  }
145  }
146  return null;
147  }
148  });
149  }
150  });
151  buttons.appendChild(edit);
152  buttons.appendChild(new Space());
153  }
154  Toolbarbutton copy = new Toolbarbutton(I_.get("Copy"), "/_zul/images/edit-copy.png");
155  copy.addEventListener(Events.ON_CLICK, new EventListener() {
156  @Override
157  public void onEvent(Event event) throws Exception {
158  FileClipboard.copyFolder(folder.getRealFolder());
159  }
160  });
161  buttons.appendChild(copy);
162  Toolbarbutton cut = new Toolbarbutton(I_.get("Cut"), "/_zul/images/edit-cut.png");
163  cut.addEventListener(Events.ON_CLICK, new EventListener() {
164  @Override
165  public void onEvent(Event event) throws Exception {
166  FileClipboard.cutFolder(folder.getLabel(), folder.getRealFolder());
167  }
168  });
169  buttons.appendChild(cut);
170  final Toolbarbutton paste = new Toolbarbutton(I_.get("Paste"), "/_zul/images/edit-paste.png");
171  paste.setDisabled(!FileClipboard.hasClipboard());
172  paste.addEventListener(Events.ON_CLICK, new EventListener() {
173  @Override
174  public void onEvent(Event event) throws Exception {
175  FileClipboard.paste(folder.getRealFolder());
176  paste.setDisabled(true);
177  }
178  });
179  buttons.appendChild(paste);
180  if(app.isInRole("file-attach:delete")) {
181  Toolbarbutton delete = new Toolbarbutton(I_.get("Delete"), "/_zul/images/edit-delete.png");
182  delete.addEventListener(Events.ON_CLICK, new EventListener() {
183  @Override
184  public void onEvent(Event event) throws Exception {
185  Messages.confirmDeletion().show(() -> {
186  try {
187  FileUtils.deleteDirectory(folder.getRealFolder());
188  FolderRow.this.detach();
189  Events.postEvent(new Event(Events.ON_CHANGE, folder.getTree()));
190  } catch (IOException ex) {
191  Logger.getLogger(FolderRow.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
192  }
193  });
194  }
195  });
196  buttons.appendChild(delete);
197  }
198  buttons.appendChild(new Space());
199  FileWrapper wrapper = FileWrapper.getFileByType(folder.getRealFolder());
200  Toolbarbutton gallery = doButton(wrapper.getAction(I_.get("Create a gallery")));
201  if(gallery != null) buttons.appendChild(gallery);
202  Toolbarbutton zip = doButton(wrapper.getAction(I_.get("Zip")));
203  if(zip != null) buttons.appendChild(zip);
204  }
205  }
206 
207  private Toolbarbutton doButton(final FileAction action) {
208  if(action != null) {
209  Toolbarbutton tb = new Toolbarbutton(action.getLabel(), action.getImage());
210  tb.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
211  @Override
212  public void onEvent(Event event) throws Exception {
213  action.doAction();
214  }
215  });
216  return tb;
217  }
218  return null;
219  }
220 
221  private Component getSizeComponent() {
222  String size = new org.turro.formatter.BytesFormatter(sizeOfDirectory(folder.getRealFolder())).toString();
223  return new Label(size);
224  }
225 
226  private long sizeOfDirectory(File directory) {
227  final Collection<File> files = folder.getFilesFrom(directory);
228  long size = 0;
229  for(final File file : files) {
230  if (!FileUtils.isSymlink(file)) {
231  size += sizeOf(file);
232  if (size < 0) {
233  break;
234  }
235  }
236  }
237  return size;
238  }
239 
240  private long sizeOf(File file) {
241  if(file.isDirectory()) {
242  return sizeOfDirectory(file);
243  } else {
244  return file.length();
245  }
246  }
247 
248 }