BrightSide Workbench Full Report + Source Code
AttachmentRow.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.attach.zul.navigator;
19 
20 import java.io.File;
21 import org.turro.action.Plugins;
22 import org.turro.attach.db.AttachPU;
23 import org.turro.attach.entity.Attachment;
24 import org.turro.attach.version.AttachVersionSet;
25 import org.turro.attach.version.AttachmentData;
26 import org.turro.attach.zul.AttachmentUtil;
27 import org.turro.attach.zul.PreviewButton;
28 import org.turro.auth.Authentication;
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.DateFormats;
34 import org.turro.elephant.util.Messages;
35 import org.turro.i18n.I_;
36 import org.turro.log.ILogButton;
37 import org.turro.log.SystemLogger;
38 import org.turro.plugin.acceptance.IAcceptanceCtrl;
39 import org.turro.tags.Tags;
40 import org.turro.wd.entities.AttachWd;
41 import org.turro.mail.recipients.SendAttachments;
42 import org.turro.zkoss.dialog.InputDialog;
43 import org.turro.zkoss.dialog.InputField;
44 import org.turro.zkoss.label.LabelTypes;
45 import org.zkoss.zk.ui.Component;
46 import org.zkoss.zk.ui.event.Event;
47 import org.zkoss.zk.ui.event.EventListener;
48 import org.zkoss.zk.ui.event.Events;
49 import org.zkoss.zul.*;
50 
55 public class AttachmentRow extends Row {
56 
57  private final AttachVersionSet avs;
58  private final boolean readOnly;
59  private Attachment attachment;
60  private Detail detail;
61 
62  public AttachmentRow(AttachVersionSet avs, boolean readOnly) {
63  this.avs = avs;
64  this.readOnly = readOnly;
65  attachment = avs.first();
66  setDraggable("true");
67  setStyle("cursor:pointer");
68  loadData();
69  }
70 
72  return avs;
73  }
74 
75  private void loadData() {
76  getChildren().clear();
77  if(!attachment.isValidated()) {
78  setSclass("draft");
79  } else {
80  setSclass(null);
81  }
82  addDetail();
83  Hlayout hbox = new Hlayout();
84  hbox.setHflex("1");
85  A a = new A(attachment.getFileName(), getMimeImage(attachment));
86  a.setHflex("true");
87  a.addEventListener(Events.ON_CLICK, new EventListener() {
88  @Override
89  public void onEvent(Event event) throws Exception {
90  detail.setOpen(!detail.isOpen());
91  fillDetail();
92  }
93  });
94  hbox.appendChild(a);
95  if(attachment.isLocked()) {
96  Image locked = new Image("/_zul/images/locked.png");
97  locked.setWidth("20px");
98  hbox.appendChild(locked);
99  }
100  PreviewButton preview = new PreviewButton(attachment);
101  preview.setWidth("20px");
102  hbox.appendChild(preview);
103  PublishedButton published = new PublishedButton(attachment);
104  published.setWidth("20px");
105  hbox.appendChild(published);
106  appendChild(hbox);
107  appendChild(getSizeComponent());
108  appendChild(new Label(DateFormats.format(attachment.getModification(), false)));
109  }
110 
111  private String getMimeImage(Attachment attachment) {
112  if(!new File(ElephantContext.getRealPath("/_internal/system/mime/" + attachment.getFileExtension() + ".png")).exists()) {
113  return "/_internal/system/mime/empty.png";
114  } else {
115  return "/_internal/system/mime/" + attachment.getFileExtension() + ".png";
116  }
117  }
118 
119  private boolean _listenerAttached = false;
120  private boolean _onOpenTriggered = false;
121 
122  private void addDetail() {
123  detail = new Detail();
124  appendChild(detail);
125 
126  detail.addEventListener(Events.ON_OPEN, new EventListener() {
127  @Override
128  public void onEvent(Event event) throws Exception {
129  _onOpenTriggered = true;
130  fillDetail();
131  }
132  });
133 
134  if(!_listenerAttached) {
135  addEventListener(Events.ON_CLICK, new EventListener<Event>() {
136  @Override
137  public void onEvent(Event event) throws Exception {
138  if(_onOpenTriggered) {
139  _onOpenTriggered = false;
140  return;
141  }
142  detail.setOpen(!detail.isOpen());
143  fillDetail();
144  }
145  });
146  _listenerAttached = true;
147  }
148  }
149 
150  private void fillDetail() {
151  if(detail.getChildren().isEmpty()) {
152  final Application app = Application.getApplication();
153 
154  Vbox vbox = new Vbox();
155  vbox.setSclass("detailBox");
156  vbox.setStyle("background-image:url('" +
157  ElephantContext.getCurrent().getContextPath() +
158  "/_zul/images/bg/sunken.png')");
159  detail.appendChild(vbox);
160  Hlayout buttons = new Hlayout();
161  buttons.setSclass("z-valign-middle");
162  vbox.appendChild(buttons);
163  if(app.isInRole("attach:edit") && !readOnly) {
164  Toolbarbutton edit = new Toolbarbutton();
165  edit.setImage("/_zul/images/properties.png");
166  edit.setTooltiptext(I_.get("Properties"));
167  edit.addEventListener(Events.ON_CLICK, new EventListener() {
168  @Override
169  public void onEvent(Event event) throws Exception {
170  InputDialog.getInput(
171  getPage(),
172  I_.get("Attachment") + " " + attachment.getId(),
173  new InputField[] {
174  new InputField("Name", attachment.getFileName(), null, 0),
175  new InputField("Requires acceptance", attachment.isRequiresAcceptance(), null, 0),
176  new InputField("Watermark", attachment.isWatermark(), null, 0)
177  }, new Command() {
178  @Override
179  public Object execute(Context context) {
180  InputField[] fields = (InputField[]) context.get("fields");
181  if(fields.length > 0) {
182  for(InputField f : fields) {
183  if("Name".equals(f.getLabel())) {
184  attachment.setFileName((String) f.getValue());
185  } else if("Requires acceptance".equals(f.getLabel())) {
186  attachment.setRequiresAcceptance((Boolean) f.getValue());
187  } else if("Watermark".equals(f.getLabel())) {
188  attachment.setWatermark((Boolean) f.getValue());
189  }
190  }
191  for(Attachment a : avs) {
192  a.setFileName(attachment.getFileName());
193  a.setRequiresAcceptance(attachment.isRequiresAcceptance());
194  a.setWatermark(attachment.isWatermark());
195  a = new AttachPU().saveObject(a);
196  }
197  loadData();
198  }
199  return null;
200  }
201  });
202  }
203  });
204  buttons.appendChild(edit);
205  buttons.appendChild(new Space());
206  }
207  Toolbarbutton download = new Toolbarbutton();
208  download.setImage("/_zul/images/download.png");
209  download.setTooltiptext(I_.get("Download"));
210  download.addEventListener(Events.ON_CLICK, new EventListener() {
211  @Override
212  public void onEvent(Event event) throws Exception {
213  AttachmentUtil.download(attachment);
214  }
215  });
216  buttons.appendChild(download);
217  Toolbarbutton downlock = new Toolbarbutton();
218  downlock.setImage("/_zul/images/download-locking.png");
219  downlock.setTooltiptext(I_.get("Download for editing"));
220  downlock.setDisabled(attachment.isLocked());
221  downlock.addEventListener(Events.ON_CLICK, new EventListener() {
222  @Override
223  public void onEvent(Event event) throws Exception {
224  AttachmentUtil.download(attachment);
225  attachment.setLocked(true);
226  attachment.setLocker(Authentication.getIContact().getId());
227  attachment = new AttachPU().saveObject(attachment);
228  loadData();
229  }
230  });
231  buttons.appendChild(downlock);
232  Toolbarbutton send = new Toolbarbutton();
233  send.setImage("/_zul/images/mail_send.png");
234  send.setTooltiptext(I_.get("Send notification"));
235  send.addEventListener(Events.ON_CLICK, new EventListener() {
236  @Override
237  public void onEvent(Event event) throws Exception {
238  SendAttachments sa = new SendAttachments(I_.get("Attachment")) {
239  @Override
240  protected void fillAttachment(File attachment, Object entity) {
241  AttachWd.writeToFile((Attachment) entity, attachment);
242  }
243  };
244  sa.addAttachment(attachment, AttachPU.getObjectPath(attachment), attachment.getFileName());
245  sa.sendAttachments(null);
246  }
247  });
248  buttons.appendChild(send);
249  buttons.appendChild(new Space());
250  Toolbarbutton copy = new Toolbarbutton();
251  copy.setImage("/_zul/images/edit-copy.png");
252  copy.setTooltiptext(I_.get("Copy"));
253  copy.addEventListener(Events.ON_CLICK, new EventListener() {
254  @Override
255  public void onEvent(Event event) throws Exception {
256  AttachmentUtil.copyAttachment(avs);
257  }
258  });
259  buttons.appendChild(copy);
260  if(!readOnly) {
261  Toolbarbutton cut = new Toolbarbutton();
262  cut.setImage("/_zul/images/edit-cut.png");
263  cut.setTooltiptext(I_.get("Cut"));
264  cut.addEventListener(Events.ON_CLICK, new EventListener() {
265  @Override
266  public void onEvent(Event event) throws Exception {
267  AttachmentUtil.cutAttachment(attachment.getFileName(), avs);
268  }
269  });
270  buttons.appendChild(cut);
271  }
272  if(!readOnly) {
273  Toolbarbutton delete = new Toolbarbutton();
274  delete.setImage("/_zul/images/edit-delete.png");
275  delete.setTooltiptext(I_.get("Delete"));
276  delete.addEventListener(Events.ON_CLICK, new EventListener() {
277  @Override
278  public void onEvent(Event event) throws Exception {
279  Messages.confirmDeletion().show(() -> {
280  for(Attachment a : avs) {
281  if(a.getPath().startsWith("/Trash")) {
282  if(app.isInRole("attach:delete")) {
283  new AttachPU().deleteObject(a);
284  }
285  } else {
286  a.setPath("/Trash" + a.getPath());
287  new AttachPU().saveObject(a);
288  }
289  }
290  AttachmentRow.this.detach();
291  });
292  }
293  });
294  buttons.appendChild(delete);
295  }
296  buttons.appendChild(new Space());
297  ILogButton log = SystemLogger.getButton(null, attachment);
298  buttons.appendChild((Component) log);
299  IAcceptanceCtrl accept = Plugins.loadImplementation(IAcceptanceCtrl.class);//PluginChecker.get("acceptctrl");
300  accept.setEntity(attachment);
301  buttons.appendChild((Component) accept);
302  buttons.appendChild(new Space());
303 // TagItCtrl tagIt = Plugins.loadImplementation(TagItCtrl.class, "label");//(TagItCtrl) PluginChecker.get("tagitlabel");
304 // tagIt.setEntity(attachment);
305 // if(tagIt instanceof AfterCompose) {
306 // ((AfterCompose) tagIt).afterCompose();
307 // }
308  buttons.appendChild((Component) Tags.getControl(attachment));
309  vbox.appendChild(new Separator());
310  vbox.appendChild(LabelTypes.getCaptionLabel(I_.get("Versions")));
311  AttachVersionGrid avg = new AttachVersionGrid(avs, readOnly);
312  avg.setHflex("1");
313  vbox.appendChild(avg);
314  }
315  }
316 
317  private Component getSizeComponent() {
318  AttachmentData fd = avs.getData();
319  Vlayout vbox = new Vlayout();
320  vbox.setSpacing("0px");
321  vbox.setStyle("text-align:right");
322  vbox.appendChild(new Label(new org.turro.formatter.BytesFormatter(fd.getSize()).toString()));
323  if(fd.getSize() != fd.getVersionedSize()) {
324  vbox.appendChild(LabelTypes.getTinyLabel(new org.turro.formatter.BytesFormatter(fd.getVersionedSize()).toString()));
325  }
326  return vbox;
327  }
328 }
AttachmentRow(AttachVersionSet avs, boolean readOnly)