BrightSide Workbench Full Report + Source Code
ServiceGrid.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.Company;
26 import org.turro.financials.entity.Service;
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 ServiceGrid extends CollectionGrid<Service> implements AfterCompose {
42 
43  private Company company;
44 
45  public ServiceGrid() {
46  }
47 
48  public ServiceGrid(Company company) {
49  super(company.getServices());
50  setDefaultBehavior(false);
51  addColumns();
52  }
53 
54  public void setCompany(Company company) {
55  this.company = company;
56  setCollection(company.getServices());
57  setDefaultBehavior(false);
58  addColumns();
59  }
60 
61  @Override
62  protected void initiateRow(Row row, Service value) {
63  if(value == null) {
64  value = new Service();
65  value.setCompany(company);
66  company.getServices().add(value);
67  }
68  row.setValue(value);
69  addDetail(row);
70  }
71 
72  @Override
73  protected boolean deleteRow(Row row) {
74  Service s = (Service) row.getValue();
75  EntityCollections.entities(company.getServices()).remove(s);
76  s.setCompany(null);
77  return true;
78  }
79 
80  @Override
81  protected boolean isValid(Service v) {
82  return !Strings.isBlank(v.getName()) &&
83  v.getCompany() != null;
84  }
85 
86  @Override
87  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
88  if(editableCell.getColumn() instanceof EditableColumn) {
89  EditableColumn ec = (EditableColumn) editableCell.getColumn();
90  if(ec.getJavaClass().equals(double.class)) {
91  Percentbox pb = new Percentbox();
92  pb.setValue((Double) getCellValue(editableCell));
93  return pb;
94  }
95  }
96  return super.createEditor(editableCell);
97  }
98 
99  @Override
100  protected String formatCell(EditableCell editableCell, Object value) {
101  if(editableCell.getColumn() instanceof EditableColumn) {
102  EditableColumn ec = (EditableColumn) editableCell.getColumn();
103  if(ec.getJavaClass().equals(double.class)) {
104  return DecimalFormats.format(((Double) value) * 100.0) + "%";
105  }
106  }
107  return super.formatCell(editableCell, value);
108  }
109 
110  private void addColumns() {
111  try {
112  addDetailColumn();
113  addColumn(I_.get("Service"), String.class,
114  "name", null, 0, false, false).setImage("/_zul/images/b_service.png");
115  addColumn(I_.get("Structure margin"), "double",
116  "structureMargin", "0.00", 2, false, false).setWidth("160px");
117  addColumn(I_.get("Profit margin"), "double",
118  "profitMargin", "0.00", 2, false, false).setWidth("160px");
119  addColumn(I_.get("Always apply"), "boolean",
120  "alwaysApply", null, 0, false, false).setWidth("160px");
121  addColumn(I_.get("Internal"), "boolean",
122  "internal", null, 0, false, false).setWidth("80px");
123  } catch (ClassNotFoundException ex) {
124  Logger.getLogger(ServiceGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
125  }
126  }
127 
128  private void addDetail(Row row) {
129  row.getChildren().clear();
130  ServiceDetail detail = new ServiceDetail(row, this);
131  row.appendChild(detail);
132  }
133 
134 }
static String format(Number value, String pattern)
String formatCell(EditableCell editableCell, Object value)
void initiateRow(Row row, Service value)
HtmlBasedComponent createEditor(EditableCell editableCell)
void setCompany(Company company)
Definition: Service.java:60
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)
void setDefaultBehavior(boolean defaultBehavior)