BrightSide Workbench Full Report + Source Code
ProductContractorGrid.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.product;
19 
20 import java.util.logging.Level;
21 import java.util.logging.Logger;
22 import org.turro.string.Strings;
23 import org.turro.elephant.context.ElephantContext;
24 import org.turro.financials.contract.ContractCombobox;
25 import org.turro.financials.entity.Contract;
26 import org.turro.financials.entity.Product;
27 import org.turro.financials.entity.ProductByContractor;
28 import org.turro.i18n.I_;
29 import org.turro.zkoss.grid.CollectionGrid;
30 import org.turro.zkoss.grid.EditableCell;
31 import org.turro.zkoss.grid.EditableColumn;
32 import org.zkoss.zk.ui.HtmlBasedComponent;
33 import org.zkoss.zul.Row;
34 
39 public class ProductContractorGrid extends CollectionGrid<ProductByContractor> {
40 
41  private Product product;
42 
44  super();
45  }
46 
47  public ProductContractorGrid(Product product) {
48  super();
49  setProduct(product);
50  }
51 
52  public Product getProduct() {
53  return product;
54  }
55 
56  public void setProduct(Product product) {
57  this.product = product;
59  }
60 
61  public void updateProduct(Product product) {
62  clearTable();
63  setProduct(product);
64  afterCompose();
65  }
66 
67  @Override
68  public void afterCompose() {
69  addColumns();
70  super.afterCompose();
71  }
72 
73  @Override
74  protected void initiateRow(Row row, ProductByContractor value) {
75  if(value == null) {
76  value = new ProductByContractor();
77  value.setProduct(product);
78  product.getProductByContractors().add(value);
79  }
80  row.setValue(value);
81  }
82 
83  @Override
84  protected boolean deleteRow(Row row) {
85  product.getProductByContractors().remove((ProductByContractor) row.getValue());
86  return true;
87  }
88 
89  @Override
90  protected boolean isValid(ProductByContractor v) {
91  return v.getContract() != null && !Strings.isEmpty(v.getContractorCode());
92  }
93 
94  @Override
95  protected String formatCell(EditableCell editableCell, Object value) {
96  if(value instanceof Contract) {
97  Contract c = ((Contract) value);
98  return c.getName();
99  }
100  return super.formatCell(editableCell, value);
101  }
102 
103  @Override
104  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
105  if(editableCell.getColumn() instanceof EditableColumn) {
106  EditableColumn ec = (EditableColumn) editableCell.getColumn();
107  if(ec.getJavaClass().equals(Contract.class)) {
108  Object value = getCellValue(editableCell);
110  if(value != null) cc.setObjectValue((Contract) value);
111  return cc;
112  }
113  }
114  return super.createEditor(editableCell);
115  }
116 
117  @Override
118  protected void cellChanged(EditableCell editableCell, Object value) {
119  super.cellChanged(editableCell, value);
120  updateRow(editableCell.getRow());
121  }
122 
123  private void addColumns() {
124  try {
125  addColumn(I_.get("Contractor"), Contract.class, "contract",
126  null, 0, false, false);
127  addColumn(I_.get("Identifier"), String.class, "contractorCode",
128  null, 0, false, false).setWidth("200px");
129  addColumn(I_.get("Description"), String.class, "description",
130  null, 0, false, false);
131  addColumn(I_.get("Tax"), "double", "tax",
132  null, 2, false, false).setWidth("90px");
133  addColumn(I_.get("Price"), "double", "price",
134  null, 2, false, false).setWidth("120px");
135  } catch (ClassNotFoundException ex) {
136  Logger.getLogger(ProductContractorGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
137  }
138  }
139 
140 }
Set< ProductByContractor > getProductByContractors()
Definition: Product.java:92
String formatCell(EditableCell editableCell, Object value)
void cellChanged(EditableCell editableCell, Object value)
void initiateRow(Row row, ProductByContractor value)
HtmlBasedComponent createEditor(EditableCell editableCell)
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)