BrightSide Workbench Full Report + Source Code
ContractedGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2018 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.financials.contract;
20 
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.elephant.context.ElephantContext;
24 import org.turro.financials.entity.Contract;
25 import org.turro.financials.entity.Contracted;
26 import org.turro.financials.entity.Product;
27 import org.turro.financials.product.ProductCombobox;
28 import org.turro.i18n.I_;
29 import org.turro.jpa.entity.EntityCollections;
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 ContractedGrid extends CollectionGrid<Contracted> implements AfterCompose {
41 
42  private Contract contract;
43 
44  public ContractedGrid() {
45  super();
46  addColumns();
47  }
48 
49  public Contract getContract() {
50  return contract;
51  }
52 
53  public void setContract(Contract contract) {
54  this.contract = contract;
55  setCollection(contract.getContracted());
56  }
57 
58  @Override
59  protected void initiateRow(Row row, Contracted value) {
60  if(value == null) {
61  value = new Contracted();
62  value.setContract(contract);
63  value.setQuantity(1.0);
64  contract.getContracted().add(value);
65  }
66  row.setValue(value);
67  }
68 
69  @Override
70  protected boolean deleteRow(Row row) {
71  Contracted contracted = (Contracted) row.getValue();
72  EntityCollections.entities(contract.getContracted()).remove(contracted);
73  contracted.setContract(null);
74  return true;
75  }
76 
77  @Override
78  protected boolean isValid(Contracted v) {
79  return !v.isEmpty();
80  }
81 
82  @Override
83  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
84  if(editableCell.getCellIndex() == 0) {
85  Object value = getCellValue(editableCell);
86  ProductCombobox cdc = new ProductCombobox();
87  if(value != null) cdc.setObjectValue((Product) value);
88  return cdc;
89  }
90  return super.createEditor(editableCell);
91  }
92 
93  @Override
94  protected String formatCell(EditableCell editableCell, Object value) {
95  if(editableCell.getCellIndex() == 0) {
96  Product prod = ((Product) value);
97  if(prod != null) {
98  return prod.getProductString();
99  }
100  return "";
101  }
102  return super.formatCell(editableCell, value);
103  }
104 
105  private void addColumns() {
106  try {
107  addColumn(I_.get("Product"), Product.class,
108  "product", null, 0, false, false).setWidth("35%");
109  addColumn(I_.get("Concept"), String.class,
110  "concept", null, 0, false, false).setWidth("35%");
111  addColumn(I_.get("Price"), "double",
112  "price", null, 0, false, false).setWidth("15%");
113  addColumn(I_.get("Quantity"), "double",
114  "quantity", null, 0, false, false).setWidth("15%");
115  } catch (ClassNotFoundException ex) {
116  Logger.getLogger(ContractedGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
117  }
118  }
119 
120 }
String formatCell(EditableCell editableCell, Object value)
void initiateRow(Row row, Contracted value)
HtmlBasedComponent createEditor(EditableCell editableCell)
Set< Contracted > getContracted()
Definition: Contract.java:312
void setContract(Contract contract)
Definition: Contracted.java:64
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)