BrightSide Workbench Full Report + Source Code
BreakdownGrid.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.reference;
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.Breakdown;
25 import org.turro.erp.entity.OrderReference;
26 import org.turro.erp.entity.RequiredUsage;
27 import org.turro.erp.entity.Task;
28 import org.turro.jpa.entity.EntityCollectionUtil;
29 import org.turro.zkoss.grid.CollectionGrid;
30 import org.zkoss.zul.Column;
31 import org.zkoss.zul.Row;
32 
37 public class BreakdownGrid extends CollectionGrid<Breakdown> {
38 
39  private OrderReference orderReference;
40 
41  public BreakdownGrid() {
42  addColumns();
43  }
44 
46  return orderReference;
47  }
48 
49  public void setOrderReference(OrderReference orderReference) {
50  this.orderReference = orderReference;
51  }
52 
53  public double getAmount() {
54  double amount = 0.0;
55  for(Breakdown b : getValues()) {
56  amount += b.getAmount();
57  }
58  return amount;
59  }
60 
61  public void setMode(int mode) {
62  if(mode == 1) {
63  // Tasks
64  getRows().getChildren().clear();
65  orderReference.getBreakdowns().clear();
66  for(Task t : orderReference.getTasks()) {
67  Row row = appendValue();
68  Breakdown b = (Breakdown) row.getValue();
69  b.setDescription(t.getName());
70  b.setQuantity(0.0d);
71  b.setPrice(0.0d);
72  updateRow(row);
73  }
74  if(isAllowInsertions()) {
75  addNewRow();
76  }
77  } else if (mode == 2) {
78  // Resources
79  getRows().getChildren().clear();
80  orderReference.getBreakdowns().clear();
81  for(Task t : orderReference.getTasks()) {
82  for(RequiredUsage hru : t.getRequiredUsages()) {
83  Row row = appendValue();
84  Breakdown b = (Breakdown) row.getValue();
85  b.setDescription(hru.getName());
86  b.setQuantity(hru.getRealUnits());
87  b.setPrice(hru.getUnitPrice());
88  updateRow(row);
89  }
90  }
91  if(isAllowInsertions()) {
92  addNewRow();
93  }
94  } else if(mode == 3) {
95  getRows().getChildren().clear();
96  orderReference.getBreakdowns().clear();
97  if(isAllowInsertions()) {
98  addNewRow();
99  }
100  }
101  }
102 
103  public void setShowQuantity(boolean show) {
104  ((Column) getColumns().getChildren().get(1)).setVisible(show);
105  }
106 
107  public void setShowPrice(boolean show) {
108  ((Column) getColumns().getChildren().get(3)).setVisible(show);
109  ((Column) getColumns().getChildren().get(4)).setVisible(show);
110  }
111 
112  @Override
113  protected void initiateRow(Row row, Breakdown value) {
114  if(value == null) {
115  value = new Breakdown();
116  orderReference.getBreakdowns().add(value);
117  value.setOrderRef(getRowCount());
118  }
119  row.setValue(value);
120  }
121 
122  @Override
123  protected boolean deleteRow(Row row) {
124  Breakdown t = (Breakdown) row.getValue();
125  EntityCollectionUtil.remove(orderReference.getBreakdowns(), t);
126  return true;
127  }
128 
129  @Override
130  protected boolean isValid(Breakdown v) {
131  return !v.isEmpty();
132  }
133 
134  @Override
135  protected void rowChanged(Row row) {
136  super.rowChanged(row);
137  updateRow(row);
138  }
139 
140  private void addColumns() {
141  try {
142  addColumn("#", "int", "orderRef", null, 0, false, false).setWidth("60px");
143  addColumn(Application.getString("lQuantity"), Double.class, "quantity", null, 3, false, false).setWidth("80px");
144  addColumn(Application.getString("lDescription"), String.class, "description", null, 0, false, false).setHflex("1");
145  addColumn(Application.getString("lPrice"), Double.class, "price", null, 2, false, false).setWidth("100px");
146  addColumn(Application.getString("lAmount"), "double", "amount", null, 2, false, true).setWidth("100px");
147  } catch (ClassNotFoundException ex) {
148  Logger.getLogger(OrderReferenceGrid.class.getName()).log(Level.SEVERE, null, ex);
149  }
150  }
151 
152 }
static String getString(String key)
void setOrderRef(int orderRef)
Definition: Breakdown.java:62
void setDescription(String description)
Definition: Breakdown.java:46
void setPrice(Double price)
Definition: Breakdown.java:70
void setQuantity(Double quantity)
Definition: Breakdown.java:78
void setOrderReference(OrderReference orderReference)
void initiateRow(Row row, Breakdown value)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
Columns getColumns(boolean create)
Rows getRows(boolean create)