BrightSide Workbench Full Report + Source Code
IssueParticipantGrid.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 org.turro.string.Strings;
21 import org.turro.action.Contacts;
22 import org.turro.auth.Authentication;
23 import org.turro.contacts.Contact;
24 import org.turro.contacts.util.ContactCombobox;
25 import org.turro.dossier.entity.Issue;
26 import org.turro.dossier.entity.IssueParticipant;
27 import org.turro.dossier.entity.IssueParticipantRole;
28 import org.turro.dossier.issue.IssueWrapper;
29 import org.turro.i18n.I_;
30 import org.turro.plugin.contacts.IContact;
31 import org.turro.registry.ChangeCategory;
32 import org.turro.registry.Changes;
33 import org.turro.registry.UniqueString;
34 import org.turro.zkoss.grid.CollectionGrid;
35 import org.turro.zkoss.grid.EditableCell;
36 import org.zkoss.zk.ui.HtmlBasedComponent;
37 import org.zkoss.zk.ui.ext.AfterCompose;
38 import org.zkoss.zul.Row;
39 
44 public class IssueParticipantGrid extends CollectionGrid<IssueParticipant> implements AfterCompose {
45 
46  private Issue issue;
47  private Changes changes;
48 
50  super();
51  setSizedByContent(true);
52  }
53 
54  public IssueParticipantGrid(Issue issue) {
55  super(issue.getParticipants());
56  setSizedByContent(true);
57  this.issue = issue;
58  addColumns();
59  }
60 
61  public Changes getChanges() {
62  return changes;
63  }
64 
65  public void setChanges(Changes changes) {
66  this.changes = changes;
67  }
68 
69  public void setIssue(Issue issue) {
70  this.issue = issue;
71  if(getCollection() == null || getCollection().isEmpty()) {
72  IssueWrapper wrapper = new IssueWrapper(issue);
73  if(!wrapper.hasReporter()) {
75  if(changes != null) {
76  ChangeCategory cc = new ChangeCategory(4, I_.get("Participants"));
77  changes.addChange(new UniqueString(cc, I_.byKey(ip.getRole().toString()), ip.getName(), true));
78  }
79  }
81  addColumns();
82  }
83  }
84 
85  @Override
86  protected boolean isValid(IssueParticipant v) {
87  return !Strings.isBlank(v.getIdContact());
88  }
89 
90  @Override
91  protected void initiateRow(Row row, IssueParticipant value) {
92  if(value == null) {
93  IssueWrapper iw = new IssueWrapper(issue);
94  value = new IssueParticipant();
95  value.setIssue(issue);
96  if(!iw.hasReporter()) {
98  } else if(!iw.hasResponsible()) {
100  } else {
102  }
103  issue.getParticipants().add(value);
104  }
105  row.setValue(value);
106  }
107 
108  @Override
109  protected boolean deleteRow(Row row) {
110  if(changes != null) {
111  IssueParticipant ip = (IssueParticipant) row.getValue();
112  ChangeCategory cc = new ChangeCategory(4, I_.get("Participants"));
113  changes.addChange(new UniqueString(cc, I_.byKey(ip.getRole().toString()), "-" + ip.getName(), true));
114  }
115  issue.getParticipants().remove((IssueParticipant) row.getValue());
116  return true;
117  }
118 
119  @Override
120  protected void rowChanged(Row row) {
121  super.rowChanged(row);
122  if(changes != null) {
123  IssueParticipant ip = (IssueParticipant) row.getValue();
124  if(ip != null) {
125  IContact c = ip.getIContact();
126  if(c.isValid()) {
127  ChangeCategory cc = new ChangeCategory(4, I_.get("Participants"));
128  changes.addChange(new UniqueString(cc, I_.byKey(ip.getRole().toString()), c.getName(), true));
129  }
130  }
131  }
132  }
133 
134  @Override
135  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
136  if(editableCell.getCellIndex() == 0) {
137  Object value = getCellValue(editableCell);
138  ContactCombobox cdc = new ContactCombobox();
139  cdc.setRole("issue:list");
140  if(value != null) cdc.setIdContact((String) value);
141  return cdc;
142  } else if(editableCell.getCellIndex() == 1) {
143  Object value = getCellValue(editableCell);
145  cdc.setMold("select");
146  cdc.setObjectValue((IssueParticipantRole) value);
147  return cdc;
148  }
149  return super.createEditor(editableCell);
150  }
151 
152  @Override
153  protected String formatCell(EditableCell editableCell, Object value) {
154  if(editableCell.getCellIndex() == 0) {
155  IContact ci = Contacts.getContactById((String) value);
156  if(ci.isValid()) {
157  return ci.getName();
158  }
159  return "";
160  } else if(editableCell.getCellIndex() == 1) {
161  if(value instanceof IssueParticipantRole) {
162  return I_.byKey(((IssueParticipantRole) value).toString());
163  }
164  return "";
165  }
166  return super.formatCell(editableCell, value);
167  }
168 
169  @Override
170  protected Object getEditorValue(EditableCell editableCell) {
171  if(editableCell.getCellIndex() == 0) {
172  HtmlBasedComponent hbc = (HtmlBasedComponent) editableCell.getEditor();
173  if(hbc instanceof ContactCombobox) {
174  Contact c = ((ContactCombobox) hbc).getContact();
175  if(c != null) {
176  ((IssueParticipant) editableCell.getRow().getValue()).setName(c.getName());
177  return c.getId();
178  }
179  }
180  if(editableCell.getEditor() instanceof ContactCombobox) {
181  return ((ContactCombobox) editableCell.getEditor()).getIdContact();
182  }
183  return null;
184  }
185  return super.getEditorValue(editableCell);
186  }
187 
188  private void addColumns() {
189  addColumn(I_.get("Participant"), String.class,
190  "idContact", null, 0, false, false).setHflex("7");
191  addColumn(I_.get("Role"), IssueParticipantRole.class,
192  "role", null, 0, false, false).setHflex("3");
193  }
194 
195 }
static IContact getContactById(String id)
Definition: Contacts.java:72
void setRole(IssueParticipantRole role)
IssueParticipant addReporter(Object contact)
Definition: Issue.java:397
Set< IssueParticipant > getParticipants()
Definition: Issue.java:114
void initiateRow(Row row, IssueParticipant value)
HtmlBasedComponent createEditor(EditableCell editableCell)
String formatCell(EditableCell editableCell, Object value)
static String byKey(String key)
Definition: I_.java:83
static String get(String msg)
Definition: I_.java:41
void setCollection(Collection< V > collection)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
Object getCellValue(EditableCell editableCell)