BrightSide Workbench Full Report + Source Code
StatementGrid.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.register;
19 
20 import java.util.Date;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.command.CommandUtil;
24 import org.turro.elephant.context.ElephantContext;
25 import org.turro.elephant.util.DecimalFormats;
26 import org.turro.financials.account.logic.BankEntry;
27 import org.turro.financials.account.logic.BankLabel;
28 import org.turro.financials.account.logic.StatementEntry;
29 import org.turro.i18n.I_;
30 import org.turro.zkoss.grid.CollectionGrid;
31 import org.turro.zkoss.grid.EditableCell;
32 import org.turro.zkoss.label.LabelTypes;
33 import org.turro.zkoss.locale.Currencies;
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.zul.Checkbox;
39 import org.zkoss.zul.Foot;
40 import org.zkoss.zul.Footer;
41 import org.zkoss.zul.Row;
42 import org.zkoss.zul.Space;
43 
48 public class StatementGrid extends CollectionGrid<StatementEntry> {
49 
50  public StatementGrid() {
51  super();
52  try {
53  setReadOnly(true);
54  addColumns();
55  } catch (ClassNotFoundException ex) {
56  Logger.getLogger(StatementGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
57  }
58  }
59 
60  public void refreshRows() {
61  clearFoot();
62  clearRows();
63  afterCompose();
64  }
65 
66  @Override
67  public void afterCompose() {
68  super.afterCompose();
69  double debit = 0.0, credit = 0.0;
70  if(getCollection() != null) {
71  for(StatementEntry se : getCollection()) {
72  if(!(se instanceof BankEntry)) {
73  debit += se.getDebit();
74  credit += se.getCredit();
75  }
76  }
77  Foot foot = createFoot();
78  Footer footer = new Footer("");
79  footer.setSpan(4);
80  foot.appendChild(footer);
81  footer = new Footer();
82  footer.setAlign("right");
83  footer.appendChild(LabelTypes.getCaptionLabel(DecimalFormats.format(debit, "#,##0.00")));
84  foot.appendChild(footer);
85  footer = new Footer();
86  footer.setAlign("right");
87  footer.appendChild(LabelTypes.getCaptionLabel(DecimalFormats.format(credit, "#,##0.00")));
88  foot.appendChild(footer);
89  footer = new Footer();
90  footer.setAlign("right");
91  footer.appendChild(LabelTypes.getCaptionLabel(DecimalFormats.format(debit-credit, "#,##0.00")));
92  foot.appendChild(footer);
93  footer = new Footer("");
94  foot.appendChild(footer);
95  }
96  }
97 
98  @Override
99  protected void initiateRow(Row row, StatementEntry value) {
100  row.setValue(value);
101  if(value instanceof BankEntry) {
102  row.setSclass("invalid");
103  }
104  }
105 
106  @Override
107  protected boolean deleteRow(Row row) {
108  return false;
109  }
110 
111  @Override
112  protected boolean isValid(StatementEntry v) {
113  return true;
114  }
115 
116  @Override
117  protected HtmlBasedComponent createRenderer(EditableCell editableCell) {
118  if(editableCell.getCellIndex() == 3) {
119  final StatementEntry se = (StatementEntry) editableCell.getRow().getValue();
120  if(!(se instanceof BankEntry)) {
122  se.getDocument() == null ? se.getRegister() : se.getDocument())
123  .setLabel(se.getDescription());
124  } else {
125  return new BankLabel(se);
126  }
127  } if(editableCell.getCellIndex() == 7) {
128  final StatementEntry se = (StatementEntry) editableCell.getRow().getValue();
129  if(!(se instanceof BankEntry)) {
130  final Checkbox cb = (Checkbox) super.createRenderer(editableCell);
131  cb.addEventListener(Events.ON_CHECK, new EventListener() {
132  @Override
133  public void onEvent(Event event) throws Exception {
134  se.setConciliated(cb.isChecked());
135  }
136  });
137  return cb;
138  } else {
139  return new Space();
140  }
141  }
142  return super.createRenderer(editableCell);
143  }
144 
145  private void addColumns() throws ClassNotFoundException {
146  int fractionDigits = Currencies.getDefault().getDefaultFractionDigits();
147  addColumn(I_.get("Date"), Date.class,
148  "date", null, 0, true, true).setWidth("90px");
149  addColumn(I_.get("View"), String.class,
150  "view.name", null, 0, false, true).setWidth("90px");
151  addColumn(I_.get("Account"), String.class,
152  "account", null, 0, false, true).setWidth("100px");
153  addColumn(I_.get("Description"), String.class,
154  "description", null, 0, false, true);
155  addColumn(I_.get("Debit"), "double",
156  "debit", null, fractionDigits, false, true).setWidth("100px");
157  addColumn(I_.get("Credit"), "double",
158  "credit", null, fractionDigits, false, true).setWidth("100px");
159  addColumn(I_.get("Balance"), "double",
160  "balance", null, fractionDigits, false, true).setWidth("100px");
161  addColumn("", "boolean",
162  "conciliated", null, 0, false, false).setWidth("35px");
163  }
164 
165 }
static ControlAdapter getAdapterOrSpace(Object entity)
static String format(Number value, String pattern)
void initiateRow(Row row, StatementEntry value)
HtmlBasedComponent createRenderer(EditableCell editableCell)
static String get(String msg)
Definition: I_.java:41
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
void setReadOnly(boolean readOnly)
static Label getCaptionLabel(String value)
Definition: LabelTypes.java:40