BrightSide Workbench Full Report + Source Code
DepartmentGrid.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.business;
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.elephant.util.DecimalFormats;
25 import org.turro.financials.entity.Department;
26 import org.turro.financials.entity.Headquarters;
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.turro.zkoss.grid.EditableColumn;
32 import org.turro.zkoss.input.Percentbox;
33 import org.zkoss.zk.ui.HtmlBasedComponent;
34 import org.zkoss.zk.ui.ext.AfterCompose;
35 import org.zkoss.zul.Row;
36 
41 public class DepartmentGrid extends CollectionGrid<Department> implements AfterCompose {
42 
43  public DepartmentGrid(Headquarters headquarters) {
44  super(headquarters.getDepartments());
45  setDefaultBehavior(false);
46  addColumns();
47  }
48 
49  @Override
50  protected void initiateRow(Row row, Department value) {
51  if(value == null) {
52  Headquarters h = (Headquarters) getDependingRow().getValue();
53  value = new Department();
54  value.setHeadquarters(h);
55  h.getDepartments().add(value);
56  }
57  row.setValue(value);
58  addDetail(row);
59  }
60 
61  @Override
62  protected boolean deleteRow(Row row) {
63  Headquarters h = (Headquarters) getDependingRow().getValue();
64  Department d = (Department) row.getValue();
66  d.setHeadquarters(null);
67  return true;
68  }
69 
70  @Override
71  protected boolean isValid(Department v) {
72  return !Strings.isBlank(v.getName()) &&
73  v.getHeadquarters() != null;
74  }
75 
76  @Override
77  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
78  if(editableCell.getColumn() instanceof EditableColumn) {
79  EditableColumn ec = (EditableColumn) editableCell.getColumn();
80  if(ec.getJavaClass().equals(double.class)) {
81  Percentbox pb = new Percentbox();
82  pb.setValue((Double) getCellValue(editableCell));
83  return pb;
84  }
85  }
86  return super.createEditor(editableCell);
87  }
88 
89  @Override
90  protected String formatCell(EditableCell editableCell, Object value) {
91  if(editableCell.getColumn() instanceof EditableColumn) {
92  EditableColumn ec = (EditableColumn) editableCell.getColumn();
93  if(ec.getJavaClass().equals(double.class)) {
94  return DecimalFormats.format(((Double) value) * 100.0) + "%";
95  }
96  }
97  return super.formatCell(editableCell, value);
98  }
99 
100  private void addColumns() {
101  try {
102  addDetailColumn();
103  addColumn(I_.get("Department"), String.class,
104  "name", null, 0, false, false).setImage("/_zul/images/b_department.png");
105  addColumn(I_.get("Structure margin"), "double",
106  "structureMargin", "0.00", 2, false, false).setWidth("160px");
107  addColumn(I_.get("Profit margin"), "double",
108  "profitMargin", "0.00", 2, false, false).setWidth("160px");
109  addColumn(I_.get("Always apply"), "boolean",
110  "alwaysApply", null, 0, false, false).setWidth("160px");
111  } catch (ClassNotFoundException ex) {
112  Logger.getLogger(DepartmentGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
113  }
114  }
115 
116  private void addDetail(Row row) {
117  row.getChildren().clear();
118  DepartmentDetail detail = new DepartmentDetail(row, this);
119  row.appendChild(detail);
120  }
121 
122 }
static String format(Number value, String pattern)
HtmlBasedComponent createEditor(EditableCell editableCell)
void initiateRow(Row row, Department value)
String formatCell(EditableCell editableCell, Object value)
void setHeadquarters(Headquarters headquarters)
Definition: Department.java:66
static String get(String msg)
Definition: I_.java:41
static EntityCollections entities(Collection values)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
Object getCellValue(EditableCell editableCell)
void setDefaultBehavior(boolean defaultBehavior)