BrightSide Workbench Full Report + Source Code
FileRow.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.Date;
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.DateFormats;
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.file.zul.PreviewButton;
38 import org.turro.i18n.I_;
39 import org.turro.mail.recipients.SendAttachments;
40 import org.turro.zkoss.dialog.InputDialog;
41 import org.turro.zkoss.dialog.InputField;
42 import org.zkoss.zk.ui.Component;
43 import org.zkoss.zk.ui.event.Event;
44 import org.zkoss.zk.ui.event.EventListener;
45 import org.zkoss.zk.ui.event.Events;
46 import org.zkoss.zul.A;
47 import org.zkoss.zul.Detail;
48 import org.zkoss.zul.Hbox;
49 import org.zkoss.zul.Hlayout;
50 import org.zkoss.zul.Label;
51 import org.zkoss.zul.Row;
52 import org.zkoss.zul.Space;
53 import org.zkoss.zul.Toolbarbutton;
54 import org.zkoss.zul.Vbox;
55 
60 public class FileRow extends Row {
61 
62  private final File file;
63  private final boolean readOnly;
64  private Detail detail;
65 
66  public FileRow(File file, boolean readOnly) {
67  this.file = file;
68  this.readOnly = readOnly;
69  setDraggable("true");
70  setStyle("cursor:pointer");
71  loadData();
72  }
73 
74  public File getFile() {
75  return file;
76  }
77 
78  private void loadData() {
79  getChildren().clear();
80  addDetail();
81  Hlayout hbox = new Hlayout();
82  hbox.setHflex("1");
83  A a = new A(file.getName(), getMimeImage(file));
84  a.setHflex("true");
85  a.addEventListener(Events.ON_CLICK, new EventListener() {
86  @Override
87  public void onEvent(Event event) throws Exception {
88  detail.setOpen(!detail.isOpen());
89  fillDetail();
90  }
91  });
92  hbox.appendChild(a);
93  PreviewButton preview = new PreviewButton(file);
94  preview.setWidth("20px");
95  hbox.appendChild(preview);
96  appendChild(hbox);
97  appendChild(getSizeComponent());
98  appendChild(new Label(DateFormats.format(new Date(file.lastModified()), false)));
99  }
100 
101  private String getMimeImage(File file) {
102  String ext = FileUtil.getExtension(file);
103  if(ext == null && !new File(ElephantContext.getRealPath("/_internal/system/mime/" + ext + ".png")).exists()) {
104  return "/_internal/system/mime/empty.png";
105  } else {
106  return "/_internal/system/mime/" + ext + ".png";
107  }
108  }
109 
110  private boolean _listenerAttached = false;
111  private boolean _onOpenTriggered = false;
112 
113  private void addDetail() {
114  detail = new Detail();
115  appendChild(detail);
116 
117  detail.addEventListener(Events.ON_OPEN, new EventListener() {
118  @Override
119  public void onEvent(Event event) throws Exception {
120  _onOpenTriggered = true;
121  fillDetail();
122  }
123  });
124 
125  if(!_listenerAttached) {
126  addEventListener(Events.ON_CLICK, new EventListener<Event>() {
127  @Override
128  public void onEvent(Event event) throws Exception {
129  if(_onOpenTriggered) {
130  _onOpenTriggered = false;
131  return;
132  }
133  detail.setOpen(!detail.isOpen());
134  fillDetail();
135  }
136  });
137  _listenerAttached = true;
138  }
139  }
140 
141  private void fillDetail() {
142  if(detail.getChildren().isEmpty()) {
143  Application app = Application.getApplication();
144 
145  Vbox vbox = new Vbox();
146  vbox.setSclass("detailBox");
147  vbox.setStyle("background-image:url('" +
148  ElephantContext.getCurrent().getContextPath() +
149  "/_zul/images/bg/sunken.png')");
150  detail.appendChild(vbox);
151  Hbox buttons = new Hbox();
152  vbox.appendChild(buttons);
153  if(app.isInRole("file-attach:edit") && !readOnly) {
154  Toolbarbutton edit = new Toolbarbutton();
155  edit.setImage("/_zul/images/properties.png");
156  edit.setTooltiptext(I_.get("Properties"));
157  edit.addEventListener(Events.ON_CLICK, new EventListener() {
158  @Override
159  public void onEvent(Event event) throws Exception {
160  InputDialog.getInput(
161  getPage(),
162  I_.get("File"),
163  new InputField[] {
164  new InputField("Name", file.getName(), null, 0)
165  }, new Command() {
166  @Override
167  public Object execute(Context context) {
168  InputField[] fields = (InputField[]) context.get("fields");
169  if(fields.length > 0) {
170  for(InputField f : fields) {
171  if("Name".equals(f.getLabel())) {
172  file.renameTo(new File(FileUtil.getParentPath(file) + "/" + (String) f.getValue()));
173  }
174  }
175  loadData();
176  }
177  return null;
178  }
179  });
180  }
181  });
182  buttons.appendChild(edit);
183  buttons.appendChild(new Space());
184  }
185  Toolbarbutton download = new Toolbarbutton();
186  download.setImage("/_zul/images/download.png");
187  download.setTooltiptext(I_.get("Download"));
188  download.addEventListener(Events.ON_CLICK, new EventListener() {
189  @Override
190  public void onEvent(Event event) throws Exception {
191  new FileWrapper(file).download();
192  }
193  });
194  buttons.appendChild(download);
195  Toolbarbutton send = new Toolbarbutton();
196  send.setImage("/_zul/images/mail_send.png");
197  send.setTooltiptext(I_.get("Send notification"));
198  send.addEventListener(Events.ON_CLICK, new EventListener() {
199  @Override
200  public void onEvent(Event event) throws Exception {
201  SendAttachments sa = new SendAttachments(I_.get("Attachment")) {
202  @Override
203  protected void fillAttachment(File attachment, Object entity) {
204  try {
205  FileUtils.copyFile((File) entity, attachment);
206  } catch (IOException ex) {
207  Logger.getLogger(FileRow.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
208  }
209  }
210  };
211  sa.addAttachment(file, file.getName(), file.getName());
212  sa.sendAttachments(null);
213  }
214  });
215  buttons.appendChild(send);
216  buttons.appendChild(new Space());
217  Toolbarbutton copy = new Toolbarbutton();
218  copy.setImage("/_zul/images/edit-copy.png");
219  copy.setTooltiptext(I_.get("Copy"));
220  copy.addEventListener(Events.ON_CLICK, new EventListener() {
221  @Override
222  public void onEvent(Event event) throws Exception {
223  FileClipboard.copyFile(file);
224  }
225  });
226  buttons.appendChild(copy);
227  if(!readOnly) {
228  Toolbarbutton cut = new Toolbarbutton();
229  cut.setImage("/_zul/images/edit-cut.png");
230  cut.setTooltiptext(I_.get("Cut"));
231  cut.addEventListener(Events.ON_CLICK, new EventListener() {
232  @Override
233  public void onEvent(Event event) throws Exception {
234  FileClipboard.cutFile(file.getName(), file);
235  }
236  });
237  buttons.appendChild(cut);
238  }
239  if(app.isInRole("file-attach:delete") && !readOnly) {
240  Toolbarbutton delete = new Toolbarbutton();
241  delete.setImage("/_zul/images/edit-delete.png");
242  delete.setTooltiptext(I_.get("Delete"));
243  delete.addEventListener(Events.ON_CLICK, new EventListener() {
244  @Override
245  public void onEvent(Event event) throws Exception {
246  Messages.confirmDeletion().show(() -> {
247  FileUtil.deleteFile(file);
248  FileRow.this.detach();
249  });
250  }
251  });
252  buttons.appendChild(delete);
253  }
254  buttons.appendChild(new Space());
255  FileWrapper wrapper = FileWrapper.getFileByType(file);
256  Toolbarbutton editor = doButton(wrapper.getAction(I_.get("Edit")));
257  if(editor != null) buttons.appendChild(editor);
258  Toolbarbutton unzip = doButton(wrapper.getAction(I_.get("Unzip")));
259  if(unzip != null) buttons.appendChild(unzip);
260  }
261  }
262 
263  private Toolbarbutton doButton(final FileAction action) {
264  if(action != null) {
265  Toolbarbutton tb = new Toolbarbutton(action.getLabel(), action.getImage());
266  tb.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
267  @Override
268  public void onEvent(Event event) throws Exception {
269  action.doAction();
270  }
271  });
272  return tb;
273  }
274  return null;
275  }
276 
277  private Component getSizeComponent() {
278  return new Label(new org.turro.formatter.BytesFormatter(file.length()).toString());
279  }
280 }
FileRow(File file, boolean readOnly)
Definition: FileRow.java:66