BrightSide Workbench Full Report + Source Code
ContractTaxesGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.Tax;
26 import org.turro.financials.entity.TaxType;
27 import org.turro.i18n.I_;
28 import org.turro.jpa.entity.EntityCollections;
29 import org.turro.zkoss.grid.CollectionGrid;
30 import org.turro.zkoss.grid.EditableCell;
31 import org.zkoss.zk.ui.HtmlBasedComponent;
32 import org.zkoss.zk.ui.ext.AfterCompose;
33 import org.zkoss.zul.Row;
34 
39 public class ContractTaxesGrid extends CollectionGrid<Tax> implements AfterCompose {
40 
41  private Contract contract;
42 
43  public ContractTaxesGrid() {
44  super();
45  }
46 
47  public ContractTaxesGrid(Contract contract) {
48  super(contract.getTaxes());
49  this.contract = contract;
50  addColumns();
51  }
52 
53  public void setContract(Contract contract) {
54  this.contract = contract;
55  setCollection(contract.getTaxes());
56  addColumns();
57  }
58 
59  @Override
60  protected boolean isValid(Tax v) {
61  return !v.isEmpty();
62  }
63 
64  @Override
65  protected void initiateRow(Row row, Tax value) {
66  if(value == null) {
67  value = new Tax();
68  value.setContract(contract);
69  contract.getTaxes().add(value);
70  }
71  row.setValue(value);
72  }
73 
74  @Override
75  protected boolean deleteRow(Row row) {
76  Tax tax = (Tax) row.getValue();
77  EntityCollections.entities(contract.getTaxes()).remove(tax);
78  tax.setContract(null);
79  return true;
80  }
81 
82  @Override
83  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
84  if(editableCell.getCellIndex() == 0) {
85  Object value = getCellValue(editableCell);
86  TaxTypeListbox ttl = new TaxTypeListbox();
87  ttl.setMold("select");
88  if(value != null) ttl.setObjectValue((TaxType) value);
89  return ttl;
90  }
91  return super.createEditor(editableCell);
92  }
93 
94  @Override
95  protected String formatCell(EditableCell editableCell, Object value) {
96  if(editableCell.getCellIndex() == 0) {
97  TaxType tt = (TaxType) value;
98  if(tt != null) {
99  return I_.get(tt.getLabel());
100  }
101  return "";
102  }
103  return super.formatCell(editableCell, value);
104  }
105 
106  private void addColumns() {
107  try {
108  addColumn(I_.get("Type"), TaxType.class, "taxType", null, 0, false, false).setHflex("3");
109  addColumn(I_.get("Tax"), "double", "tax", null, 2, false, false).setHflex("1");
110  addColumn(I_.get("Equivalence surcharge"), "double", "equivalenceSurcharge", null, 2, false, false).setHflex("1");
111  } catch (ClassNotFoundException ex) {
112  Logger.getLogger(ContractTaxesGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
113  }
114  }
115 
116 }
String formatCell(EditableCell editableCell, Object value)
HtmlBasedComponent createEditor(EditableCell editableCell)
void setContract(Contract contract)
Definition: Tax.java:52
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)