BrightSide Workbench Full Report + Source Code
viewer/DossierGrid.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.dossier.zul.viewer;
19 
20 import java.util.List;
21 import java.util.Map;
22 import org.amic.util.string.Strings;
23 import org.turro.contacts.util.ContactCombobox;
24 import org.turro.dossier.db.DossierPU;
25 import org.turro.dossier.entity.Category;
26 import org.turro.dossier.entity.Dossier;
27 import org.turro.dossier.entity.ParticipantRole;
28 import org.turro.elephant.context.Application;
29 import org.turro.i18n.I_;
30 import org.turro.plugin.contacts.IContact;
31 import org.turro.zkoss.grid.GroupExtended;
32 import org.turro.zkoss.grid.PagingGrid;
33 import org.turro.zkoss.layout.HSeparator;
34 import org.zkoss.zk.ui.event.Event;
35 import org.zkoss.zk.ui.event.EventListener;
36 import org.zkoss.zk.ui.event.Events;
37 import org.zkoss.zk.ui.ext.AfterCompose;
38 import org.zkoss.zul.Checkbox;
39 import org.zkoss.zul.Column;
40 import org.zkoss.zul.Columns;
41 import org.zkoss.zul.Group;
42 import org.zkoss.zul.Hbox;
43 import org.zkoss.zul.Label;
44 import org.zkoss.zul.Listbox;
45 import org.zkoss.zul.Listitem;
46 import org.zkoss.zul.Row;
47 import org.zkoss.zul.Rows;
48 import org.zkoss.zul.Textbox;
49 import org.zkoss.zul.Toolbar;
50 import org.zkoss.zul.Toolbarbutton;
51 import org.zkoss.zul.Vbox;
52 
57 public class DossierGrid extends PagingGrid implements AfterCompose {
58 
59  private Rows rows;
60  private Toolbar toolbar;
61  private Toolbarbutton searchButton;
62  private DossierViewpoint viewpoint;
63  private String searchValue = "", issueColumns;
64  private boolean onlyActive = true, searchBar = true;
65  private IContact fixedContact;
66  private String fixedViewpoint;
67  private long fixedCategoryId = -1;
68  private boolean showFields = true, showIssues = true, showAttachments = true, divToolbar = false;
69 
70  public DossierGrid() {
71  viewpoint = new DossierViewpoint();
73  addColumns();
74  }
75 
76  public void setFixedCategoryId(long fixedCategoryId) {
77  this.fixedCategoryId = fixedCategoryId;
78  if(fixedCategoryId == 0) {
79  viewpoint.setCategory(null);
80  } else if(fixedCategoryId > 0) {
81  viewpoint.setCategory(new DossierPU().find(Category.class, fixedCategoryId));
82  }
83  }
84 
85  public void setFixedContact(IContact fixedContact) {
86  this.fixedContact = fixedContact;
87  viewpoint.setContact(fixedContact);
88  }
89 
90  public void setFixedViewpoint(String fixedViewpoint) {
91  this.fixedViewpoint = fixedViewpoint;
92  if(!Strings.isBlank(fixedViewpoint)) {
93  viewpoint.setViewpoint(ParticipantRole.valueOf(fixedViewpoint));
94  }
95  }
96 
97  public void setDivToolbar(boolean divToolbar) {
98  this.divToolbar = divToolbar;
99  }
100 
101  public boolean isShowAttachments() {
102  return showAttachments;
103  }
104 
105  public void setShowAttachments(boolean showAttachments) {
106  this.showAttachments = showAttachments;
107  }
108 
109  public boolean isShowFields() {
110  return showFields;
111  }
112 
113  public void setShowFields(boolean showFields) {
114  this.showFields = showFields;
115  }
116 
117  public boolean isShowIssues() {
118  return showIssues;
119  }
120 
121  public void setShowIssues(boolean showIssues) {
122  this.showIssues = showIssues;
123  }
124 
125  public void setSearchValue(String searchValue) {
126  this.searchValue = searchValue;
127  }
128 
129  public void setContact(IContact contact) {
130  viewpoint.setContact(contact);
131  }
132 
133  public void setIssueColumns(String issueColumns) {
134  this.issueColumns = issueColumns;
135  }
136 
137  public void setSearchBar(boolean searchBar) {
138  this.searchBar = searchBar;
139  }
140 
142  return viewpoint;
143  }
144 
145  private void addColumns() {
146  Columns cols = new Columns();
147  cols.setVisible(false);
148  appendChild(cols);
149 
150  addDetailColumn();
151  Column col = new Column(I_.get("Dossiers"));
152  cols.appendChild(col);
153  }
154 
155  private void addRows(Map<IContact, List<Dossier>> dossiers) {
156 
157  int count = 0;
158  for(List l : dossiers.values()) {
159  count += l.size();
160  }
161  setRowCount(count);
162 
163  if(rows != null) removeChild(rows);
164 
165  rows = new Rows();
166  appendChild(rows);
167 
168  if(dossiers.keySet().isEmpty()) {
169  Row row = new Row();
170  row.appendChild(new Label(""));
171  row.appendChild(new Label(I_.get("Empty list")));
172  rows.appendChild(row);
173  } else {
174  for(final IContact subject : dossiers.keySet()) {
175  createGroup(subject);
176  for(final Dossier dossier : dossiers.get(subject)) {
177  DossierRow row = new DossierRow();
178  rows.appendChild(row);
179  row.setIssueColumns(issueColumns);
180  row.setValue(dossier);
181  }
182  }
183  }
184 
185  invalidate();
186  }
187 
188  private void createGroup(IContact subject) {
189  String label = subject.getName();
190  Group group = new GroupExtended(label);
191  rows.appendChild(group);
192  }
193 
194  private void addSearchBar() {
195 
196  if(toolbar != null) getParent().removeChild(toolbar);
197 
198  if(!searchBar) return;
199 
200  toolbar = new Toolbar();
201  getParent().insertBefore(toolbar, this);
202  toolbar.setStyle("font-size:11px");
203 
204  Vbox toolbox = new Vbox();
205  toolbar.appendChild(toolbox);
206 
207  Hbox firstRow = new Hbox();
208  firstRow.setSpacing("10px");
209  toolbox.appendChild(firstRow);
210 
211  Hbox secondRow = new Hbox();
212  if(divToolbar) {
213  secondRow.setSpacing("10px");
214  toolbox.appendChild(secondRow);
215  }
216 
217  if(viewpoint.getContact() != null) {
218 
219  final ContactCombobox contactCombo = new ContactCombobox();
220  final CategoryListbox categoryListbox = new CategoryListbox();
221  Vbox vbox;
222 
223  if(Strings.isBlank(fixedViewpoint)) {
224  vbox = new Vbox();
225  firstRow.appendChild(vbox);
226 
227  vbox.appendChild(new Label(I_.get("Viewpoint")));
228 
229  final Listbox role = new Listbox();
230  role.setStyle("font-size:11px");
231  role.setMold("select");
232  role.addEventListener(Events.ON_SELECT, new EventListener() {
233  @Override
234  public void onEvent(Event event) throws Exception {
235  viewpoint.setViewpoint((ParticipantRole) role.getSelectedItem().getValue());
236  if(contactCombo != null) {
237  contactCombo.setRole(contactComboRole());
238  categoryListbox.setViewpoint(viewpoint);
239  }
240  }
241  });
242  vbox.appendChild(role);
243 
244  for(ParticipantRole pr : ParticipantRole.values()) {
245  Listitem li = new Listitem(I_.byKey(pr.toString()));
246  li.setValue(pr);
247  li.setSelected(li.getValue().equals(viewpoint.getViewpoint()));
248  role.appendChild(li);
249  }
250  }
251 
252  if(fixedCategoryId == -1) {
253  vbox = new Vbox();
254  firstRow.appendChild(vbox);
255 
256  vbox.appendChild(new Label(I_.get("Category")));
257 
258  categoryListbox.setStyle("font-size:11px");
259  categoryListbox.setMold("select");
260  categoryListbox.setViewpoint(viewpoint);
261  categoryListbox.addEventListener(Events.ON_SELECT, new EventListener() {
262  @Override
263  public void onEvent(Event event) throws Exception {
264  viewpoint.setCategory(categoryListbox.getCategory());
265  }
266  });
267  vbox.appendChild(categoryListbox);
268  }
269 
270  if(checkContactCombo()) {
271  vbox = new Vbox();
272  if(divToolbar) {
273  secondRow.appendChild(vbox);
274  } else {
275  firstRow.appendChild(vbox);
276  }
277 
278  vbox.appendChild(new Label(I_.get("Contact")));
279 
280  contactCombo.setStyle("font-size:11px");
281  contactCombo.setCols(30);
282  contactCombo.setIContact(viewpoint.getContact());
283  contactCombo.setRole(contactComboRole());
284  contactCombo.addEventListener(Events.ON_CHANGE, new EventListener() {
285  @Override
286  public void onEvent(Event event) throws Exception {
287  viewpoint.setContact(contactCombo.getIContact());
288  categoryListbox.setViewpoint(viewpoint);
289  }
290  });
291  vbox.appendChild(contactCombo);
292  }
293 
294  vbox = new Vbox();
295  firstRow.appendChild(vbox);
296 
297  vbox.appendChild(new Label(I_.get("Text")));
298 
299  final Textbox searchValueInput = new Textbox(searchValue);
300  searchValueInput.setStyle("font-size:11px");
301  searchValueInput.setCols(15);
302  searchValueInput.addEventListener(Events.ON_CHANGE, new EventListener() {
303  @Override
304  public void onEvent(Event event) throws Exception {
305  searchValue = searchValueInput.getText();
306  searchDossiers();
307  }
308  });
309  searchValueInput.addEventListener(Events.ON_OK, new EventListener() {
310  @Override
311  public void onEvent(Event event) throws Exception {
312  searchValue = searchValueInput.getText();
313  searchDossiers();
314  }
315  });
316  vbox.appendChild(searchValueInput);
317 
318  vbox = new Vbox();
319  firstRow.appendChild(vbox);
320 
321  vbox.appendChild(new HSeparator("16px"));
322 
323  searchButton = new Toolbarbutton(
324  I_.get("Search"),
325  "/_zul/images/search.png"
326  );
327  searchButton.addEventListener(Events.ON_CLICK, new EventListener() {
328  @Override
329  public void onEvent(Event event) throws Exception {
330  searchDossiers();
331  }
332  });
333  vbox.appendChild(searchButton);
334 
335  vbox = new Vbox();
336  if(divToolbar && checkContactCombo()) {
337  secondRow.appendChild(vbox);
338  } else {
339  firstRow.appendChild(vbox);
340  }
341 
342  vbox.appendChild(new HSeparator("16px"));
343 
344  final Checkbox onlyActiveCheck = new Checkbox(I_.get("Only active"));
345  onlyActiveCheck.setChecked(onlyActive);
346  onlyActiveCheck.setStyle("font-size:11px");
347  onlyActiveCheck.addEventListener(Events.ON_CHECK, new EventListener() {
348  @Override
349  public void onEvent(Event event) throws Exception {
350  onlyActive = onlyActiveCheck.isChecked();
351  }
352  });
353  vbox.appendChild(onlyActiveCheck);
354 
355  }
356  }
357 
358  private boolean checkContactCombo() {
359  return fixedContact == null &&
360  (viewpoint.getViewpoint().equals(ParticipantRole.PARTICIPANT_SUBJECT) &&
361  Application.getApplication().isInRole("my-orders:subject")) ||
362  ((viewpoint.getViewpoint().equals(ParticipantRole.PARTICIPANT_ASSISTANT) ||
363  viewpoint.getViewpoint().equals(ParticipantRole.PARTICIPANT_OWNER)) &&
364  Application.getApplication().isInRole("dossier:all"));
365  }
366 
367  private String contactComboRole() {
368  if(viewpoint.getViewpoint().equals(ParticipantRole.PARTICIPANT_SUBJECT)) {
369  return null; //"my-orders:subject";
370  } else if(viewpoint.getViewpoint().equals(ParticipantRole.PARTICIPANT_ASSISTANT) ||
371  viewpoint.getViewpoint().equals(ParticipantRole.PARTICIPANT_OWNER)) {
372  return "dossier:list";
373  }
374  return null;
375  }
376 
377  private void searchDossiers() {
378  addRows(viewpoint.getDossiersMap(searchValue, onlyActive));
379  }
380 
381  @Override
382  public void afterCompose() {
383  addSearchBar();
384  searchDossiers();
385  }
386 
387 }
void setFixedCategoryId(long fixedCategoryId)
void setFixedViewpoint(String fixedViewpoint)
void setFixedContact(IContact fixedContact)
void setShowAttachments(boolean showAttachments)
Map< IContact, List< Dossier > > getDossiersMap(String searchValue, boolean onlyActive)
void setViewpoint(ParticipantRole viewpoint)
static String get(String msg)
Definition: I_.java:41