BrightSide Workbench Full Report + Source Code
ProductsGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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.erp.purchase;
20 
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.elephant.context.Application;
24 import org.turro.erp.entity.Task;
25 import org.turro.erp.entity.WorkOrder;
26 import org.turro.erp.reference.OrderReferenceGrid;
27 import org.turro.erp.reference.OrderReferencePositionListbox;
28 import org.turro.erp.task.TaskListbox;
29 import org.turro.financials.product.IProduct;
30 import org.turro.financials.product.ProductExtendedCombobox;
31 import org.turro.zkoss.grid.CollectionGrid;
32 import org.turro.zkoss.grid.EditableCell;
33 import org.turro.zkoss.grid.EditableColumn;
34 import org.zkoss.zk.ui.HtmlBasedComponent;
35 import org.zkoss.zk.ui.event.Event;
36 import org.zkoss.zk.ui.event.EventListener;
37 import org.zkoss.zk.ui.event.Events;
38 import org.zkoss.zk.ui.ext.AfterCompose;
39 import org.zkoss.zul.Row;
40 
45 public class ProductsGrid extends CollectionGrid<ProductsItem> implements AfterCompose {
46 
47  private long lastWorkOrderId;
48  private int lastOrderRef;
49  private Task lastTask;
50  private String lastConcept;
51  private boolean receipt;
52 
53  public ProductsGrid() {
54  addColumns();
55  }
56 
57  public boolean isReceipt() {
58  return receipt;
59  }
60 
61  public void setReceipt(boolean receipt) {
62  this.receipt = receipt;
63  }
64 
65  @Override
66  protected void initiateRow(Row row, ProductsItem value) {
67  if(value == null) {
68  value = new ProductsItem();
69  value.setWorkOrderId(lastWorkOrderId);
70  value.setOrderRef(lastOrderRef);
71  value.setTask(lastTask);
72  value.setConcept(lastConcept);
73  }
74  row.setValue(value);
75  }
76 
77  @Override
78  protected boolean deleteRow(Row row) {
79  return true;
80  }
81 
82  @Override
83  protected boolean isValid(ProductsItem v) {
84  return v.isValid();
85  }
86 
87  @Override
88  protected HtmlBasedComponent createEditor(final EditableCell editableCell) {
89  if(editableCell.getColumn() instanceof EditableColumn) {
90  final ProductsItem productsItem = (ProductsItem) editableCell.getRow().getValue();
91  EditableColumn ec = (EditableColumn) editableCell.getColumn();
92  if(ec.getLabel().equals(Application.getString("lReference"))) {
93  Object value = getCellValue(editableCell);
95  orplb.setMold("select");
96  orplb.setAllowNull(true);
97  orplb.setNullAtBottom(true);
98  orplb.setNullLabel("lNewReference");
99  orplb.setWorkOrder(productsItem.getWorkOrder());
100  // line value must update with new selection
101  orplb.addEventListener(Events.ON_SELECT, new EventListener() {
102  @Override
103  public void onEvent(Event event) throws Exception {
104  Integer oref = orplb.getObjectValue();
105  if(oref != null) {
106  productsItem.setOrderRef(oref);
107  Events.postEvent(new Event(Events.ON_CHANGING, ProductsGrid.this, editableCell));
108  }
109  }
110  });
111  if(value != null) orplb.setObjectValue((Integer) value);
112  return orplb;
113  } else if(ec.getJavaClass().equals(Task.class)) {
114  Object value = getCellValue(editableCell);
115  TaskListbox tlb = new TaskListbox();
116  tlb.setMold("select");
117  tlb.setAllowNull(true);
118  tlb.setNullAtBottom(true);
119  tlb.setNullLabel("lNewTask");
120  tlb.setOrderReference(productsItem.getOrderReference());
121  if(value != null) tlb.setObjectValue((Task) value);
122  return tlb;
123  } else if(ec.getJavaClass().equals(IProduct.class)) {
124  Object value = getCellValue(editableCell);
126  if(value != null) pecb.setObjectValue((IProduct) value);
127  return pecb;
128  }
129  }
130  return super.createEditor(editableCell);
131  }
132 
133  @Override
134  protected String formatCell(EditableCell editableCell, Object value) {
135  ProductsItem productsItem = (ProductsItem) editableCell.getRow().getValue();
136  lastWorkOrderId = productsItem.getWorkOrderId();
137  lastOrderRef = productsItem.getOrderRef();
138  lastTask = productsItem.getTask();
139  lastConcept = productsItem.getConcept();
140  if(value instanceof Task) {
141  Task t = ((Task) value);
142  return t.getName();
143  } else if(value instanceof IProduct) {
144  IProduct p = ((IProduct) value);
145  return p.getProductString();
146  }
147  return super.formatCell(editableCell, value);
148  }
149 
150  @Override
151  protected boolean isCellValid(EditableCell editableCell, Object value) {
152  if(editableCell.getColumn() instanceof EditableColumn) {
153  EditableColumn ec = (EditableColumn) editableCell.getColumn();
154  if(ec.getLabel().equals(Application.getString("lWorkOrder"))) {
155  return WorkOrder.getByWorkOrderId((Long) value) != null;
156  }
157  }
158  return super.isCellValid(editableCell, value);
159  }
160 
161  private void addColumns() {
162  try {
163  addColumn(Application.getString("lWorkOrder"), "long", "workOrderId", null, 0, false, false).setWidth("100px");
164  addColumn(Application.getString("lReference"), "int", "orderRef", null, 0, false, false).setWidth("100px");
165  addColumn(Application.getString("lTask"), Task.class, "task", null, 3, false, false).setHflex("1");
166  addColumn(Application.getString("lQuantity"), "double", "units", null, 2, false, false).setWidth("60px");
167  addColumn(Application.getString("lConcept"), String.class, "concept", null, 0, false, false).setHflex("1");
168  addColumn(Application.getString("lProduct"), IProduct.class, "iProduct", null, 0, false, false).setHflex("1");
169  addColumn(Application.getString("lCost"), "double", "cost", null, 2, false, false).setWidth("80px");
170  } catch (ClassNotFoundException ex) {
171  Logger.getLogger(OrderReferenceGrid.class.getName()).log(Level.SEVERE, null, ex);
172  }
173  }
174 
175 }
static String getString(String key)
static WorkOrder getByWorkOrderId(long value)
Definition: WorkOrder.java:365
String formatCell(EditableCell editableCell, Object value)
boolean isValid(ProductsItem v)
void setReceipt(boolean receipt)
void initiateRow(Row row, ProductsItem value)
HtmlBasedComponent createEditor(final EditableCell editableCell)
boolean isCellValid(EditableCell editableCell, Object value)
void setWorkOrderId(long workOrderId)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
Object getCellValue(EditableCell editableCell)
void setNullAtBottom(boolean nullAtBottom)