BrightSide Workbench Full Report + Source Code
AttachVersionGrid.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 org.turro.action.Contacts;
21 import org.turro.attach.db.AttachPU;
22 import org.turro.attach.entity.Attachment;
23 import org.turro.attach.version.AttachVersionSet;
24 import org.turro.attach.zul.AttachmentUtil;
25 import org.turro.attach.zul.PreviewButton;
26 import org.turro.elephant.context.Application;
27 import org.turro.elephant.util.DateFormats;
28 import org.turro.elephant.util.Messages;
29 import org.turro.i18n.I_;
30 import org.turro.plugin.contacts.IContact;
31 import org.turro.zkoss.label.LabelEditable;
32 import org.turro.zkoss.layout.GridLayout;
33 import org.zkoss.zk.ui.event.Event;
34 import org.zkoss.zk.ui.event.EventListener;
35 import org.zkoss.zk.ui.event.Events;
36 import org.zkoss.zul.*;
37 
42 public class AttachVersionGrid extends GridLayout {
43 
44  private AttachVersionSet avs;
45  private boolean readOnly;
46 
47  public AttachVersionGrid(AttachVersionSet avs, boolean readOnly) {
48  this.avs = avs;
49  this.readOnly = readOnly;
50  setSclass(getSclass() + " attachVersions");
51  setColumns("30px,min,35px,min,min,min,1,min");
52  addRows();
53  }
54 
55  private void addRows() {
56  Rows rows = getRows(true);
58  for(final Attachment a : avs) {
59  if(a.isLocked()) {
60  Row blocked = new Row();
61  rows.appendChild(blocked);
62  blocked.appendChild(new Image("/_zul/images/locked.png"));
63  IContact contact = Contacts.getEmpty();
64  if(contact != null) {
65  contact.loadById(a.getLocker());
66  if(contact.isValid()) {
67  blocked.appendChild(new Label(contact.getName()));
68  }
69  }
70  }
71  final Row row = new Row();
72 
73  rows.appendChild(row);
74  if(!a.isValidated()) {
75  row.setSclass("draft");
76  }
77  row.setValue(a);
78  A down = new A(null, "/_zul/images/download.png");
79  down.addEventListener(Events.ON_CLICK, new EventListener() {
80  @Override
81  public void onEvent(Event event) throws Exception {
82  AttachmentUtil.download(a);
83  }
84  });
85  row.appendChild(down);
86  final Datebox dbox = new Datebox(a.getModification());
87  dbox.setWidth("150px");
88  dbox.setFormat(DateFormats.getDefaultDateTimeFormat());
89  dbox.setInplace(true);
90  dbox.setReadonly(!app.isInRole("attach:date"));
91  dbox.addEventListener(Events.ON_CHANGE, new EventListener() {
92  @Override
93  public void onEvent(Event event) throws Exception {
94  a.setModification(dbox.getValue());
95  new AttachPU().saveObject(a);
96  }
97  });
98  row.appendChild(dbox);
99  PreviewButton ap = new PreviewButton(a);
100  row.appendChild(ap);
101  final Checkbox validated = new Checkbox();
102  validated.setChecked(a.isValidated());
103  validated.setDisabled(!app.isInRole("attach:validate"));
104  validated.addEventListener(Events.ON_CHECK, new EventListener() {
105  @Override
106  public void onEvent(Event event) throws Exception {
107  a.setValidated(validated.isChecked());
108  new AttachPU().saveObject(a);
109  if(!a.isValidated()) {
110  row.setSclass("draft");
111  } else {
112  row.setSclass(null);
113  }
114  }
115  });
116  row.appendChild(validated);
117  row.appendChild(new Label(new org.turro.formatter.BytesFormatter(a.getFileSize()).toString()));
118  IContact contact = Contacts.getEmpty();
119  if(contact != null) {
120  contact.loadByLogin(a.getOwner());
121  row.appendChild(new Label(contact.getName()));
122  } else {
123  row.appendChild(new Space());
124  }
125  if(app.isInRole("attach:edit")) {
126  final LabelEditable comment = new LabelEditable(a.getComment(), "/_zul/images/comment.png");
127  comment.setWidth("100%");
128  comment.setEditorCols(-1);
129  comment.addEventListener(Events.ON_CHANGE, new EventListener() {
130  @Override
131  public void onEvent(Event event) throws Exception {
132  a.setComment(comment.getLabel());
133  new AttachPU().saveObject(a);
134  }
135  });
136  row.appendChild(comment);
137  comment.afterCompose();
138  } else {
139  row.appendChild(new Label(a.getComment()));
140  }
141  if(a.getId() != avs.first().getId() && !readOnly) {
142  A delete = new A();
143  delete.setWidth("30px");
144  delete.setImage("/_zul/images/edit-delete.png");
145  delete.setTooltiptext(I_.get("Delete"));
146  delete.addEventListener(Events.ON_CLICK, new EventListener() {
147  @Override
148  public void onEvent(Event event) throws Exception {
149  Messages.confirmDeletion().show(() -> {
150  if(a.getPath().startsWith("/Trash")) {
151  if(app.isInRole("attach:delete")) {
152  new AttachPU().deleteObject(a);
153  }
154  } else {
155  a.setPath("/Trash" + a.getPath());
156  new AttachPU().saveObject(a);
157  }
158  row.detach();
159  });
160  }
161  });
162  row.appendChild(delete);
163  } else {
164  row.appendChild(new Space());
165  }
166  }
167 
168  invalidate();
169  }
170 
171 }
static IContact getEmpty()
Definition: Contacts.java:56
AttachVersionGrid(AttachVersionSet avs, boolean readOnly)
Rows getRows(boolean create)
Object loadByLogin(String login)