BrightSide Workbench Full Report + Source Code
HeadquartersGrid.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.action.Contacts;
24 import org.turro.contacts.Contact;
25 import org.turro.contacts.util.ContactCombobox;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.elephant.util.DecimalFormats;
28 import org.turro.financials.entity.Company;
29 import org.turro.financials.entity.Headquarters;
30 import org.turro.i18n.I_;
31 import org.turro.jpa.entity.EntityCollections;
32 import org.turro.plugin.contacts.IContact;
33 import org.turro.zkoss.grid.CollectionGrid;
34 import org.turro.zkoss.grid.EditableCell;
35 import org.turro.zkoss.grid.EditableColumn;
36 import org.turro.zkoss.input.Percentbox;
37 import org.zkoss.zk.ui.HtmlBasedComponent;
38 import org.zkoss.zk.ui.ext.AfterCompose;
39 import org.zkoss.zul.Row;
40 
45 public class HeadquartersGrid extends CollectionGrid<Headquarters> implements AfterCompose {
46 
47  private Company company;
48 
49  public HeadquartersGrid() {
50  }
51 
52  public HeadquartersGrid(Company company) {
53  super(company.getHeadquarters());
54  setDefaultBehavior(false);
55  addColumns();
56  }
57 
58  public void setCompany(Company company) {
59  this.company = company;
60  setCollection(company.getHeadquarters());
61  setDefaultBehavior(false);
62  addColumns();
63  }
64 
65  @Override
66  protected void initiateRow(Row row, Headquarters value) {
67  if(value == null) {
68  value = new Headquarters();
69  value.setCompany(company);
70  company.getHeadquarters().add(value);
71  }
72  row.setValue(value);
73  addDetail(row);
74  }
75 
76  @Override
77  protected boolean deleteRow(Row row) {
78  Headquarters h = (Headquarters) row.getValue();
79  EntityCollections.entities(company.getHeadquarters()).remove(h);
80  h.setCompany(null);
81  return true;
82  }
83 
84  @Override
85  protected boolean isValid(Headquarters v) {
86  return !Strings.isBlank(v.getName()) &&
87  !Strings.isBlank(v.getIdContact()) &&
88  v.getCompany() != null;
89  }
90 
91  @Override
92  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
93  if(editableCell.getCellIndex() == 1) {
94  Object value = getCellValue(editableCell);
95  ContactCombobox cdc = new ContactCombobox();
96  if(value != null) cdc.setIdContact(((Headquarters) editableCell.getRow().getValue()).getIdContact());
97  return cdc;
98  }
99  if(editableCell.getColumn() instanceof EditableColumn) {
100  EditableColumn ec = (EditableColumn) editableCell.getColumn();
101  if(ec.getJavaClass().equals(double.class)) {
102  Percentbox pb = new Percentbox();
103  pb.setValue((Double) getCellValue(editableCell));
104  return pb;
105  }
106  }
107  return super.createEditor(editableCell);
108  }
109 
110  @Override
111  protected Object getEditorValue(EditableCell editableCell) {
112  if(editableCell.getCellIndex() == 1) {
113  HtmlBasedComponent hbc = (HtmlBasedComponent) editableCell.getEditor();
114  if(hbc instanceof ContactCombobox) {
115  Contact c = ((ContactCombobox) hbc).getContact();
116  if(c != null) {
117  ((Headquarters) editableCell.getRow().getValue()).setName(c.getName());
118  return c.getId();
119  }
120  }
121  return null;
122  }
123  return super.getEditorValue(editableCell);
124  }
125 
126  @Override
127  protected String formatCell(EditableCell editableCell, Object value) {
128  if(editableCell.getCellIndex() == 1) {
129  IContact ci = Contacts.getContactById((String) value);
130  if(ci.isValid()) {
131  return ci.getName();
132  }
133  return "";
134  }
135  if(editableCell.getColumn() instanceof EditableColumn) {
136  EditableColumn ec = (EditableColumn) editableCell.getColumn();
137  if(ec.getJavaClass().equals(double.class)) {
138  return DecimalFormats.format(((Double) value) * 100.0) + "%";
139  }
140  }
141  return super.formatCell(editableCell, value);
142  }
143 
144  private void addColumns() {
145  try {
146  addDetailColumn();
147  addColumn(I_.get("Headquarters"), String.class,
148  "idContact", null, 0, false, false).setImage("/_zul/images/b_headquarters.png");
149  addColumn(I_.get("Structure margin"), "double",
150  "structureMargin", "0.00", 2, false, false).setWidth("160px");
151  addColumn(I_.get("Profit margin"), "double",
152  "profitMargin", "0.00", 2, false, false).setWidth("160px");
153  addColumn(I_.get("Always apply"), "boolean",
154  "alwaysApply", null, 0, false, false).setWidth("160px");
155  } catch (ClassNotFoundException ex) {
156  Logger.getLogger(HeadquartersGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
157  }
158  }
159 
160  private void addDetail(Row row) {
161  row.getChildren().clear();
162  HeadquartersDetail detail = new HeadquartersDetail(row, this);
163  row.appendChild(detail);
164  }
165 
166 }
static IContact getContactById(String id)
Definition: Contacts.java:72
static String format(Number value, String pattern)
void initiateRow(Row row, Headquarters value)
String formatCell(EditableCell editableCell, Object value)
Object getEditorValue(EditableCell editableCell)
HtmlBasedComponent createEditor(EditableCell editableCell)
Set< Headquarters > getHeadquarters()
Definition: Company.java:80
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)