BrightSide Workbench Full Report + Source Code
WorksheetGrid.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.dossier.zul.worksheet;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Objects;
26 import org.turro.dossier.db.DossierPU;
27 import org.turro.dossier.dossier.DossierData;
28 import org.turro.dossier.entity.Issue;
29 import org.turro.dossier.entity.Worksheet;
30 import org.turro.dossier.issue.IssueWrapper;
31 import org.turro.dossier.search.IssueResults;
32 import org.turro.dossier.util.WorksheetUtil;
33 import org.turro.dossier.zul.menu.DossierMenu;
34 import org.turro.elephant.context.Application;
35 import org.turro.i18n.I_;
36 import org.turro.plugin.contacts.IContact;
37 import org.turro.zkoss.grid.PagingGrid;
38 import org.turro.zkoss.label.LabelExtended;
39 import org.zkoss.lang.Strings;
40 import org.zkoss.zk.ui.event.DropEvent;
41 import org.zkoss.zk.ui.event.Event;
42 import org.zkoss.zk.ui.event.EventListener;
43 import org.zkoss.zk.ui.event.Events;
44 import org.zkoss.zul.Column;
45 import org.zkoss.zul.Columns;
46 import org.zkoss.zul.Hlayout;
47 import org.zkoss.zul.Image;
48 import org.zkoss.zul.Label;
49 import org.zkoss.zul.ListModelList;
50 import org.zkoss.zul.Row;
51 import org.zkoss.zul.RowRenderer;
52 import org.zkoss.zul.Vlayout;
53 
58 public class WorksheetGrid extends PagingGrid {
59 
60  private boolean needSave;
61  private final Application app;
62  private ListModelList<Worksheet> worksheetModel;
63 
64  public WorksheetGrid() {
66  addColumns();
67  }
68 
69  public boolean isNeedSave() {
70  return needSave;
71  }
72 
73  public void setNeedSave(boolean needSave) {
74  this.needSave = needSave;
75  }
76 
77  public ListModelList<Worksheet> getWorksheetModel() {
78  return worksheetModel;
79  }
80 
81  public void setValues(IContact contact) {
82  needSave = false;
83  if(contact.isValid()) {
84  List<Worksheet> list = (List<Worksheet>) WorksheetUtil.getWorksheet(contact.getId());
85  WorksheetUtil.clearDone(list, contact);
86  if(list.isEmpty()) {
87  IssueResults results = new IssueResults();
88  results.setByParticipant(contact);
89  results.setDossierId(0L);
90  int order = 0;
91  for(IssueWrapper iw : results.getIssueList()) {
92  if(iw.getRelevanceOrderByContact() == 1) {
93  Worksheet ws = new Worksheet();
94  ws.setIdContact(contact.getId());
95  ws.setIssue(iw.getIssue());
96  ws.setSheetOrder(order++);
97  list.add(ws);
98  }
99  }
100  needSave = true;
101  }
102  addRows(list);
103  } else {
104  addRows(new ArrayList<Worksheet>());
105  }
106  }
107 
108  public void addValue(IContact contact, IssueWrapper iw) {
109  if(contact.isValid()) {
110  ListModelList list = getWorksheetModel();
111  if(iw.getRelevanceOrderByContact() == 1) {
112  Worksheet ws = new Worksheet();
113  ws.setIdContact(contact.getId());
114  ws.setIssue(iw.getIssue());
115  ws.setSheetOrder(list.getSize());
116  list.add(ws);
117  }
118  needSave = true;
119  }
120  }
121 
122  public Collection<IssueWrapper> getNotInGrid(IContact contact) {
123  ArrayList<IssueWrapper> list = new ArrayList<>();
124  if(contact.isValid()) {
125  IssueResults results = new IssueResults();
126  results.setByParticipant(contact);
127  results.setDossierId(0L);
128  for(IssueWrapper iw : results.getIssueList()) {
129  if(iw.getRelevanceOrderByContact() == 1) {
130  boolean exists = false;
131  for(Worksheet worksheet : getWorksheetModel()) {
132  if(Objects.equals(worksheet.getIssue().getId(), iw.getIssue().getId())) {
133  exists = true;
134  break;
135  }
136  }
137  if(!exists) {
138  list.add(iw);
139  }
140  }
141  }
142  }
143  return list;
144  }
145 
146  private void addColumns() {
147  Columns cols = new Columns();
148  cols.setSizable(true);
149  cols.setMenupopup("auto");
150  appendChild(cols);
151 
152  Column col = new Column("#");
153  col.setWidth("80px");
154  cols.appendChild(col);
155  col = new Column(I_.get("Description"));
156  col.setHflex("3");
157  cols.appendChild(col);
158  col = new Column(I_.get("Reporter"));
159  col.setHflex("1");
160  cols.appendChild(col);
161  if(app.isInRole("worksheet:delete")) {
162  col = new Column();
163  col.setWidth("30px");
164  cols.appendChild(col);
165  }
166  }
167 
168  private void addRows(List<Worksheet> workSheets) {
169  setRowRenderer(new RowRenderer<Worksheet>() {
170  @Override
171  public void render(Row row, Worksheet workSheet, int index) {
172  fillRow(row, workSheet);
173  }
174  });
175  worksheetModel = new ListModelList<>(workSheets);
176  setModel(worksheetModel);
177  setRowCount(workSheets.size());
178  }
179 
180  private void fillRow(final Row row, final Worksheet worksheet) {
181  row.setDraggable("row");
182  row.setDroppable("row");
183  final Issue issue = worksheet.getIssue();
184  final IssueWrapper iw = new IssueWrapper(issue);
185  DossierData dd = iw.getData();
186  row.appendChild(new Label(issue.getId() + ""));
187  Vlayout vbox = new Vlayout();
188  row.appendChild(vbox);
189  LabelExtended lext = new LabelExtended();
190  lext.setSclass("issue");
191  lext.setMultiline(true);
192  lext.setValue(Strings.isEmpty(issue.getDescription()) ?
193  I_.get("No description!") : issue.getDescription());
194  if(app.isInRole("issue:show")) {
195  lext.setStyle("cursor:pointer");
196  lext.addEventListener(Events.ON_CLICK, new EventListener() {
197  @Override
198  public void onEvent(Event event) throws Exception {
199  DossierMenu.showIssue(issue.getId());
200  }
201  });
202  }
203  vbox.appendChild(lext);
204  vbox.appendChild(new Label(issue.getDossier().getFullDescription()));
205  if(dd.isExpenses()) {
206  Hlayout hbox = new Hlayout();
207  Label label = new Label(I_.get("Expenses"));
208  label.setStyle("color:navy;font-size:11px");
209  hbox.appendChild(label);
210  label = new Label(dd.getExpensesString());
211  label.setStyle("color:#666;font-size:11px");
212  hbox.appendChild(label);
213  vbox.appendChild(hbox);
214  }
215  if(dd.isHours()) {
216  Hlayout hbox = new Hlayout();
217  Label label = new Label(I_.get("Hours"));
218  label.setStyle("color:navy;font-size:11px");
219  hbox.appendChild(label);
220  label = new Label(dd.getHoursString());
221  label.setStyle("color:#666;font-size:11px");
222  hbox.appendChild(label);
223  vbox.appendChild(hbox);
224  }
225  if(dd.isPrice()) {
226  Hlayout hbox = new Hlayout();
227  Label label = new Label(I_.get("Price"));
228  label.setStyle("color:navy;font-size:11px");
229  hbox.appendChild(label);
230  label = new Label(dd.getPriceString());
231  label.setStyle("color:#666;font-size:11px");
232  hbox.appendChild(label);
233  vbox.appendChild(hbox);
234  }
235  row.appendChild(new Label(iw.getReporterString()));
236  if(app.isInRole("worksheet:delete")) {
237  Image img = new Image("/_zul/images/edit-delete.png");
238  img.setStyle("cursor:pointer");
239  img.addEventListener(Events.ON_CLICK, new EventListener() {
240  @Override
241  public void onEvent(Event event) throws Exception {
242  Worksheet ws = worksheetModel.get(row.getIndex());
243  worksheetModel.remove(row.getIndex());
244  if(ws.getId() != null && ws.getId() > 0) {
245  new DossierPU().deleteObject(ws);
246  }
247  }
248  });
249  row.appendChild(img);
250  }
251 
252  // Drag and Drop
253 
254  row.addEventListener(Events.ON_DROP, new EventListener<DropEvent>() {
255  @Override
256  public void onEvent(DropEvent event) throws Exception {
257  Row dragged = (Row) event.getDragged();
258  Row dropped = (Row) event.getTarget();
259  Worksheet ws = worksheetModel.get(dragged.getIndex());
260  worksheetModel.remove(dragged.getIndex());
261  worksheetModel.add(dropped.getIndex(), ws);
262  updateOrderValues();
263  needSave = true;
264  Events.postEvent(new Event(Events.ON_CHANGE, WorksheetGrid.this));
265  }
266  });
267  }
268 
269  private void updateOrderValues() {
270  Iterator<Worksheet> it = worksheetModel.iterator();
271  int index = 1;
272  while(it.hasNext()) {
273  it.next().setSheetOrder(index++);
274  }
275  }
276 
277 }
void setSheetOrder(int sheetOrder)
Definition: Worksheet.java:70
void setIdContact(String idContact)
Definition: Worksheet.java:61
java.util.List< IssueWrapper > getIssueList()
void setByParticipant(IContact byParticipant)
static void clearDone(List< Worksheet > list, IContact contact)
static Collection< Worksheet > getWorksheet(String idContact)
ListModelList< Worksheet > getWorksheetModel()
void addValue(IContact contact, IssueWrapper iw)
Collection< IssueWrapper > getNotInGrid(IContact contact)
static String get(String msg)
Definition: I_.java:41