19 package org.turro.file.zul.navigator;
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;
62 private final File file;
63 private final boolean readOnly;
64 private Detail detail;
66 public FileRow(File file,
boolean readOnly) {
68 this.readOnly = readOnly;
70 setStyle(
"cursor:pointer");
78 private void loadData() {
79 getChildren().clear();
81 Hlayout hbox =
new Hlayout();
83 A a =
new A(file.getName(), getMimeImage(file));
85 a.addEventListener(Events.ON_CLICK,
new EventListener() {
87 public void onEvent(Event event)
throws Exception {
88 detail.setOpen(!detail.isOpen());
93 PreviewButton preview =
new PreviewButton(file);
94 preview.setWidth(
"20px");
95 hbox.appendChild(preview);
97 appendChild(getSizeComponent());
98 appendChild(
new Label(DateFormats.format(
new Date(file.lastModified()),
false)));
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";
106 return "/_internal/system/mime/" + ext +
".png";
110 private boolean _listenerAttached =
false;
111 private boolean _onOpenTriggered =
false;
113 private void addDetail() {
114 detail =
new Detail();
117 detail.addEventListener(Events.ON_OPEN,
new EventListener() {
119 public void onEvent(Event event)
throws Exception {
120 _onOpenTriggered =
true;
125 if(!_listenerAttached) {
126 addEventListener(Events.ON_CLICK,
new EventListener<Event>() {
128 public void onEvent(Event event) throws Exception {
129 if(_onOpenTriggered) {
130 _onOpenTriggered = false;
133 detail.setOpen(!detail.isOpen());
137 _listenerAttached =
true;
141 private void fillDetail() {
142 if(detail.getChildren().isEmpty()) {
143 Application app = Application.getApplication();
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() {
159 public void onEvent(Event event)
throws Exception {
160 InputDialog.getInput(
164 new InputField(
"Name", file.getName(), null, 0)
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()));
182 buttons.appendChild(edit);
183 buttons.appendChild(
new Space());
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() {
190 public void onEvent(Event event)
throws Exception {
191 new FileWrapper(file).download();
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() {
200 public void onEvent(Event event)
throws Exception {
201 SendAttachments sa =
new SendAttachments(I_.get(
"Attachment")) {
203 protected void fillAttachment(File attachment, Object entity) {
205 FileUtils.copyFile((File) entity, attachment);
206 }
catch (IOException ex) {
207 Logger.getLogger(FileRow.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(
null), ex);
211 sa.addAttachment(file, file.getName(), file.getName());
212 sa.sendAttachments(
null);
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() {
222 public void onEvent(Event event)
throws Exception {
223 FileClipboard.copyFile(file);
226 buttons.appendChild(copy);
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() {
233 public void onEvent(Event event)
throws Exception {
234 FileClipboard.cutFile(file.getName(), file);
237 buttons.appendChild(cut);
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() {
245 public void onEvent(Event event)
throws Exception {
246 Messages.confirmDeletion().show(() -> {
247 FileUtil.deleteFile(file);
248 FileRow.this.detach();
252 buttons.appendChild(
delete);
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);
263 private Toolbarbutton doButton(
final FileAction action) {
265 Toolbarbutton tb =
new Toolbarbutton(action.getLabel(), action.getImage());
266 tb.addEventListener(Events.ON_CLICK,
new EventListener<Event>() {
268 public void onEvent(Event event) throws Exception {
277 private Component getSizeComponent() {
278 return new Label(
new org.turro.formatter.BytesFormatter(file.length()).toString());
FileRow(File file, boolean readOnly)