BrightSide Workbench Full Report + Source Code
ContractParticipantGrid.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.financials.contract;
19 
20 import org.turro.string.Strings;
21 import org.turro.action.Contacts;
22 import org.turro.contacts.Contact;
23 import org.turro.contacts.util.ContactCombobox;
24 import org.turro.financials.entity.Contract;
25 import org.turro.financials.entity.ContractInterventionType;
26 import org.turro.financials.entity.ContractParticipant;
27 import org.turro.i18n.I_;
28 import org.turro.jpa.entity.EntityCollections;
29 import org.turro.plugin.contacts.IContact;
30 import org.turro.zkoss.grid.CollectionGrid;
31 import org.turro.zkoss.grid.EditableCell;
32 import org.zkoss.zk.ui.HtmlBasedComponent;
33 import org.zkoss.zk.ui.ext.AfterCompose;
34 import org.zkoss.zul.Row;
35 
40 public class ContractParticipantGrid extends CollectionGrid<ContractParticipant> implements AfterCompose {
41 
42  private Contract contract;
43 
45  super();
46  }
47 
48  public ContractParticipantGrid(Contract contract) {
49  super(contract.getParticipants());
50  this.contract = contract;
51  addColumns();
52  }
53 
54  public void setContract(Contract contract) {
55  this.contract = contract;
56  setCollection(contract.getParticipants());
57  addColumns();
58  }
59 
60  @Override
61  protected boolean isValid(ContractParticipant v) {
62  return !Strings.isBlank(v.getIdContact());
63  }
64 
65  @Override
66  protected void initiateRow(Row row, ContractParticipant value) {
67  if(value == null) {
68  value = new ContractParticipant();
69  value.setContract(contract);
70  contract.getParticipants().add(value);
71  }
72  row.setValue(value);
73  }
74 
75  @Override
76  protected boolean deleteRow(Row row) {
77  ContractParticipant cp = (ContractParticipant) row.getValue();
78  EntityCollections.entities(contract.getParticipants()).remove(cp);
79  cp.setContract(null);
80  return true;
81  }
82 
83  @Override
84  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
85  if(editableCell.getCellIndex() == 0) {
86  Object value = getCellValue(editableCell);
87  ContactCombobox cdc = new ContactCombobox();
88  if(value != null) cdc.setIdContact((String) value);
89  return cdc;
90  } else if(editableCell.getCellIndex() == 1) {
91  Object value = getCellValue(editableCell);
93  cit.setMold("select");
94  if(value != null) cit.setObjectValue((ContractInterventionType) value);
95  return cit;
96  }
97  return super.createEditor(editableCell);
98  }
99 
100  @Override
101  protected String formatCell(EditableCell editableCell, Object value) {
102  if(editableCell.getCellIndex() == 0) {
103  IContact ci = Contacts.getContactById((String) value);
104  if(ci.isValid()) {
105  return ci.getName();
106  }
107  return "";
108  } else if(editableCell.getCellIndex() == 0) {
110  if(cit != null) {
111  return I_.byKey(cit.toString());
112  }
113  return "";
114  }
115  return super.formatCell(editableCell, value);
116  }
117 
118  @Override
119  protected Object getEditorValue(EditableCell editableCell) {
120  if(editableCell.getCellIndex() == 0) {
121  HtmlBasedComponent hbc = (HtmlBasedComponent) editableCell.getEditor();
122  if(hbc instanceof ContactCombobox) {
123  Contact c = ((ContactCombobox) hbc).getContact();
124  if(c != null) {
125  ((ContractParticipant) editableCell.getRow().getValue()).setName(c.getName());
126  return c.getId();
127  }
128  }
129  if(editableCell.getEditor() instanceof ContactCombobox) {
130  return ((ContactCombobox) editableCell.getEditor()).getIdContact();
131  }
132  return null;
133  }
134  return super.getEditorValue(editableCell);
135  }
136 
137  private void addColumns() {
138  addColumn(I_.get("Contact"), String.class, "idContact", null, 0, false, false).setHflex("3");
139  addColumn(I_.get("Intervention"), ContractInterventionType.class, "interventionType", null, 0, false, false).setHflex("1");
140  }
141 
142 }
static IContact getContactById(String id)
Definition: Contacts.java:72
void initiateRow(Row row, ContractParticipant value)
HtmlBasedComponent createEditor(EditableCell editableCell)
String formatCell(EditableCell editableCell, Object value)
Set< ContractParticipant > getParticipants()
Definition: Contract.java:260
static String byKey(String key)
Definition: I_.java:83
static String get(String msg)
Definition: I_.java:41
static EntityCollections entities(Collection values)
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)