BrightSide Workbench Full Report + Source Code
issue/ParticipantGrid.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.issue;
19 
20 import java.util.Iterator;
21 import org.turro.contacts.Contact;
22 import org.turro.contacts.util.ContactCombobox;
23 import org.turro.contacts.zul.label.ContactInfo;
24 import org.turro.dossier.db.DossierPU;
25 import org.turro.dossier.entity.IDossierParticipant;
26 import org.turro.dossier.entity.Issue;
27 import org.turro.dossier.entity.IssueParticipant;
28 import org.turro.dossier.entity.IssueParticipantRole;
29 import org.turro.i18n.I_;
30 import org.turro.plugin.contacts.IContact;
31 import org.zkoss.zk.ui.event.Event;
32 import org.zkoss.zk.ui.event.EventListener;
33 import org.zkoss.zk.ui.event.Events;
34 import org.zkoss.zul.Column;
35 import org.zkoss.zul.Columns;
36 import org.zkoss.zul.Grid;
37 import org.zkoss.zul.Hbox;
38 import org.zkoss.zul.Image;
39 import org.zkoss.zul.Label;
40 import org.zkoss.zul.Listbox;
41 import org.zkoss.zul.Listitem;
42 import org.zkoss.zul.Row;
43 import org.zkoss.zul.Rows;
44 import org.zkoss.zul.Toolbar;
45 import org.zkoss.zul.Toolbarbutton;
46 
51 public class ParticipantGrid extends Grid {
52 
53  private Issue issue;
54  private boolean editable;
55  private Rows rows;
56  private Toolbar toolbar;
57  private Toolbarbutton editButton, addButton, saveButton, cancelButton;
58 
59  public ParticipantGrid() {
60  addColumns();
61  rows = new Rows();
62  appendChild(rows);
63  }
64 
65  public void setEditable(boolean editable) {
66  this.editable = editable;
67  if(issue != null) {
68  setIssue(issue);
69  }
70  updateButtons();
71  }
72 
73  public void setIssue(Issue issue) {
74  this.issue = issue;
75  rows.getChildren().clear();
76  if(issue != null) {
77  addRows();
78  }
79  }
80 
81  public void setAddToolbar(boolean addToolbar) {
82  if(addToolbar) {
83  toolbar = new Toolbar();
84  getParent().appendChild(toolbar);
85  addToolbarButtons();
86  }
87  }
88 
89  private void addColumns() {
90  Columns cols = new Columns();
91  cols.setSizable(true);
92  cols.setMenupopup("auto");
93  appendChild(cols);
94 
95  Column col = new Column(I_.get("Name"));
96  cols.appendChild(col);
97 
98  col = new Column(I_.get("Role"));
99  col.setWidth("180px");
100  cols.appendChild(col);
101  }
102 
103  public void addRows() {
104  if(editable) {
105  for(final IssueParticipant p : issue.getParticipants()) {
106  Row row = new Row();
107  row.setValue(p);
108  rows.appendChild(row);
109 
110  final ContactCombobox contact = new ContactCombobox();
111  contact.setRole("issue:list");
112  contact.setIContact(p.getIContact());
113  contact.addEventListener(Events.ON_CHANGE, new EventListener() {
114  @Override
115  public void onEvent(Event event) throws Exception {
116  IContact c = p.getIContact();
117  if(c.isValid()) {
118  p.setIdContact(c.getId());
119  p.setName(c.getName());
120  }
121  }
122 
123  });
124  row.appendChild(contact);
125 
126  final Listbox role = new Listbox();
127  role.setMold("select");
128  role.addEventListener(Events.ON_SELECT, new EventListener() {
129 
130  @Override
131  public void onEvent(Event event) throws Exception {
132  p.setRole((IssueParticipantRole) role.getSelectedItem().getValue());
133  }
134 
135  });
136  row.appendChild(role);
137 
138  for(IssueParticipantRole ipr : IssueParticipantRole.values()) {
139  Listitem li = new Listitem(I_.byKey(ipr.toString()));
140  li.setValue(ipr);
141  li.setSelected(li.getValue().equals(p.getRole()));
142  role.appendChild(li);
143  }
144  }
145  } else {
146  for(final Object p : issue.getFullParticipants()) {
147  Row row = new Row();
148  row.setValue(p);
149  rows.appendChild(row);
150 
151  if(p instanceof IDossierParticipant) {
153  if(cp.isShowAllIssues()) {
154  Hbox hbox = new Hbox();
155  row.appendChild(hbox);
156  hbox.appendChild(new Image("/_zul/images/forward.png"));
157  if(cp.isReceiveAllEmails()) {
158  hbox.appendChild(new Image("/_zul/images/mail_forward.png"));
159  }
160  hbox.appendChild(new ContactInfo((Contact) cp.getIContact().getContact()));
161  Label label = new Label(I_.byKey(cp.getRole().toString()));
162  row.appendChild(label);
163  }
164  } else if(p instanceof IssueParticipant) {
166  Hbox hbox = new Hbox();
167  row.appendChild(hbox);
168  hbox.appendChild(new Image("/_zul/images/mail_forward.png"));
169  hbox.appendChild(new ContactInfo((Contact) cp.getIContact().getContact()));
170  Label label = new Label(I_.byKey(cp.getRole().toString()));
171  row.appendChild(label);
172  }
173  }
174  }
175  }
176 
177  private void addToolbarButtons() {
178  editButton = new Toolbarbutton(
179  I_.get("Edit participants"),
180  "/_zul/images/edit.png"
181  );
182  editButton.addEventListener(Events.ON_CLICK, new EventListener() {
183 
184  @Override
185  public void onEvent(Event event) throws Exception {
186  ParticipantGrid.this.setEditable(true);
187  }
188 
189  });
190  toolbar.appendChild(editButton);
191 
192  saveButton = new Toolbarbutton(
193  I_.get("Save"),
194  "/_zul/images/save.png"
195  );
196  saveButton.addEventListener(Events.ON_CLICK, new EventListener() {
197 
198  @Override
199  public void onEvent(Event event) throws Exception {
200  ParticipantGrid.this.doSave();
201  }
202 
203  });
204  toolbar.appendChild(saveButton);
205 
206  cancelButton = new Toolbarbutton(
207  I_.get("Cancel"),
208  "/_zul/images/cancel.png"
209  );
210  cancelButton.addEventListener(Events.ON_CLICK, new EventListener() {
211 
212  @Override
213  public void onEvent(Event event) throws Exception {
214  ParticipantGrid.this.doCancel();
215  }
216 
217  });
218  toolbar.appendChild(cancelButton);
219 
220  addButton = new Toolbarbutton(
221  I_.get("Add participant"),
222  "/_zul/images/new.png"
223  );
224  addButton.addEventListener(Events.ON_CLICK, new EventListener() {
225  @Override
226  public void onEvent(Event event) throws Exception {
227  IssueParticipant ip = new IssueParticipant();
228  ip.setRole(IssueParticipantRole.ISSUE_ASSISTANT);
229  ip.setIssue(issue);
230  issue.getParticipants().add(ip);
231  setEditable(true);
232  }
233 
234  });
235  toolbar.appendChild(addButton);
236 
237  updateButtons();
238  }
239 
240  private void doCancel() {
241  issue = new DossierPU().find(Issue.class, issue.getId());
242  setEditable(false);
243  }
244 
245  private void doSave() {
246  Iterator<IssueParticipant> it = issue.getParticipants().iterator();
247  while(it.hasNext()) {
248  IssueParticipant ip = it.next();
249  if(!ip.getIContact().isValid()) {
250  it.remove();
251  } else {
252  ip.setIContact(ip.getIContact());
253  }
254  }
255  issue = new DossierPU().saveObject(issue);
256  setEditable(false);
257  }
258 
259  private void updateButtons() {
260  if(toolbar != null) {
261  editButton.setVisible(!editable);
262  addButton.setVisible(editable);
263  saveButton.setVisible(editable);
264  cancelButton.setVisible(editable);
265  }
266  }
267 }
ParticipantSet getFullParticipants()
Definition: Issue.java:461
Set< IssueParticipant > getParticipants()
Definition: Issue.java:114
static String byKey(String key)
Definition: I_.java:83
static String get(String msg)
Definition: I_.java:41