BrightSide Workbench Full Report + Source Code
CategoryParticipantGrid.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.dossier;
20 
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.string.Strings;
24 import org.turro.action.Contacts;
25 import org.turro.contacts.Contact;
26 import org.turro.contacts.util.ContactCombobox;
27 import org.turro.dossier.dossier.ParticipantSet;
28 import org.turro.dossier.entity.Category;
29 import org.turro.dossier.entity.CategoryParticipant;
30 import org.turro.dossier.entity.ParticipantRole;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.elephant.util.Images;
33 import org.turro.i18n.I_;
34 import org.turro.jpa.entity.EntityCollections;
35 import org.turro.plugin.contacts.IContact;
36 import org.turro.zkoss.grid.ChangeValueCallback;
37 import org.turro.zkoss.grid.CollectionGrid;
38 import org.turro.zkoss.grid.EditableCell;
39 import org.turro.zkoss.grid.EditableColumn;
40 import org.zkoss.zk.ui.HtmlBasedComponent;
41 import org.zkoss.zk.ui.util.Clients;
42 import org.zkoss.zul.Row;
43 
48 public class CategoryParticipantGrid extends CollectionGrid<CategoryParticipant> {
49 
50  private Category category;
51 
53  addColumns();
54  }
55 
56  public CategoryParticipantGrid(Category category) {
57  super(new ParticipantSet<>(category.getParticipants()));
58  this.category = category;
59  addColumns();
60  }
61 
62  public void setCategory(Category category) {
63  this.category = category;
64  if(category != null) {
66  } else {
67  updateCollection(null);
68  }
69  }
70 
71  @Override
72  protected void initiateRow(Row row, CategoryParticipant value) {
73  if(value == null) {
74  value = new CategoryParticipant();
75  value.setCategory(category);
76  if(category != null) {
77  category.getParticipants().add(value);
78  }
79  }
80  row.setValue(value);
81  }
82 
83  @Override
84  protected boolean deleteRow(Row row) {
85  CategoryParticipant p = (CategoryParticipant) row.getValue();
86  EntityCollections.entities(category.getParticipants()).remove(p);
87  p.setCategory(null);
88  return true;
89  }
90 
91  @Override
92  protected boolean isValid(CategoryParticipant v) {
93  return !v.isEmpty();
94  }
95 
96  @Override
97  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
98  if(editableCell.getCellIndex() == 0) {
99  Object value = getCellValue(editableCell);
100  ContactCombobox cdc = new ContactCombobox();
101  cdc.setRole("dossier:list");
102  if(value != null) cdc.setIdContact((String) value);
103  return cdc;
104  } else if(editableCell.getCellIndex() == 1) {
105  Object value = getCellValue(editableCell);
107  cit.setMold("select");
108  if(value != null) cit.setObjectValue((ParticipantRole) value);
109  return cit;
110  } else if(editableCell.getCellIndex() == 2) {
111  Object value = getCellValue(editableCell);
113  if(value != null) dcb.setObjectValue((String) value);
114  return dcb;
115  }
116  return super.createEditor(editableCell);
117  }
118 
119  @Override
120  protected String formatCell(EditableCell editableCell, Object value) {
121  if(editableCell.getCellIndex() == 0) {
122  IContact contact = Contacts.getContactById((String) value);
123  if(contact.isValid()) {
124  return contact.getName();
125  }
126  return "";
127  }
128  return super.formatCell(editableCell, value);
129  }
130 
131  @Override
132  protected Object getEditorValue(EditableCell editableCell) {
133  if(editableCell.getCellIndex() == 0) {
134  HtmlBasedComponent hbc = (HtmlBasedComponent) editableCell.getEditor();
135  if(hbc instanceof ContactCombobox) {
136  Contact c = ((ContactCombobox) hbc).getContact();
137  if(c != null) {
138  ((CategoryParticipant) editableCell.getRow().getValue()).setName(c.getName());
139  return c.getId();
140  }
141  return ((ContactCombobox) hbc).getIdContact();
142  }
143  return null;
144  }
145  return super.getEditorValue(editableCell);
146  }
147 
148  private void addColumns() {
149  try {
150  EditableColumn col = addColumn(I_.get("Contact"), String.class, "idContact", null, 0, false, false);
152  @Override
153  public boolean check(Object newValue, Object rowValue) {
154  if(!Strings.isBlank((String) newValue) && ((String) newValue).equals(((CategoryParticipant) rowValue).getIdContact())) {
155  Clients.showNotification(I_.format("%s already exists", ((CategoryParticipant) rowValue).getName()));
156  return true;
157  }
158  return false;
159  }
160  });
161  col.setHflex("2");
162  addColumn(I_.get("Role"), ParticipantRole.class, "role", null, 0, false, false).setHflex("1");
163  addColumn(I_.get("Discriminator"), String.class, "discriminator", null, 0, false, false).setHflex("2");
164  addColumn(Images.getImage("clip"), I_.get("Show all attachments"), "boolean", "showAllAttachments", null, 0, true, false).setWidth("25px");
165  addColumn(Images.getImage("issue"), I_.get("Show all issues"), "boolean", "showAllIssues", null, 0, true, false).setWidth("25px");
166  addColumn(Images.getImage("mail_forward"), I_.get("Receive all emails"), "boolean", "receiveAllEmails", null, 0, true, false).setWidth("25px");
167  addColumn(Images.getImage("contacts"), I_.get("Show participants"), "boolean", "showParticipants", null, 0, true, false).setWidth("25px");
168  addColumn(Images.getImage("accepted"), I_.get("Binding votes"), "boolean", "bindingVote", null, 0, true, false).setWidth("25px");
169  addColumn(Images.getImage("driver"), I_.get("Driver"), "boolean", "driver", null, 0, true, false).setWidth("25px");
170  addColumn(Images.getImage("coordinator"), I_.get("Coordinator"), "boolean", "coordinator", null, 0, true, false).setWidth("25px");
171  addColumn(Images.getImage("beneficiary"), I_.get("Beneficiary"), "boolean", "beneficiary", null, 0, true, false).setWidth("25px");
172  addColumn(Images.getImage("offerer"), I_.get("Offerer"), "boolean", "offerer", null, 0, true, false).setWidth("25px");
173  addColumn(Images.getImage("research"), I_.get("R&D"), "boolean", "research", null, 0, true, false).setWidth("25px");
174  addColumn(Images.getImage("funding"), I_.get("Funding"), "boolean", "funding", null, 0, true, false).setWidth("25px");
175  addColumn(Images.getImage("support"), I_.get("Support"), "boolean", "support", null, 0, true, false).setWidth("25px");
176  addColumn(Images.getImage("consortium"), I_.get("Consortium"), "boolean", "consortium", null, 0, true, false).setWidth("25px");
177  addColumn(Images.getImage("admin"), I_.get("Administrator"), "boolean", "admin", null, 0, true, false).setWidth("25px");
178  } catch (ClassNotFoundException ex) {
179  Logger.getLogger(DossierVersionGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
180  }
181  }
182 
183 }
static IContact getContactById(String id)
Definition: Contacts.java:72
Set< CategoryParticipant > getParticipants()
Definition: Category.java:116
void initiateRow(Row row, CategoryParticipant value)
HtmlBasedComponent createEditor(EditableCell editableCell)
String formatCell(EditableCell editableCell, Object value)
static String format(String msg, Object... arguments)
Definition: I_.java:49
static String get(String msg)
Definition: I_.java:41
static EntityCollections entities(Collection values)
void updateCollection(Collection< V > collection)
void setOnCheckDuplicate(ChangeValueCallback onCheckDuplicate)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
Object getCellValue(EditableCell editableCell)