BrightSide Workbench Full Report + Source Code
AccountReportGrid.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.report;
19 
20 import org.turro.elephant.util.DecimalFormats;
21 import org.turro.i18n.I_;
22 import org.turro.math.Zero;
23 import org.turro.zkoss.grid.PagingGrid;
24 import org.turro.zkoss.label.LabelTypes;
25 import org.zkoss.zk.ui.Component;
26 import org.zkoss.zk.ui.ext.AfterCompose;
27 import org.zkoss.zul.*;
28 
33 public class AccountReportGrid extends PagingGrid implements AfterCompose {
34 
35  private AccountReport report;
36 
37  public AccountReportGrid() {
38  }
39 
41  return report;
42  }
43 
44  public void setReport(AccountReport report) {
45  this.report = report;
46  }
47 
48  public void incrementPeriod(int amount) {
49  report.getExistingPeriods().incrementPeriod(amount);
50  getColumns().getChildren().clear();
51  getRows().getChildren().clear();
52  addColumns();
53  addRows();
54  }
55 
56  @Override
57  public void afterCompose() {
58  addColumns();
59  addRows();
60  }
61 
62  private void addColumns() {
63  Columns cols = getColumns(true);
64 
65  Column col = new Column(I_.get("Item"));
66  col.setWidth("200px");
67  cols.appendChild(col);
68 
69  for(AccountReportPeriod period : report.getExistingPeriods().getCurrentPeriod(24)) {
70  col = new Column(period.getYear() + "-" + period.getMonth());
71  col.setAlign("right");
72  col.setWidth("120px");
73  col.setValue(period);
74  cols.appendChild(col);
75  }
76 
77  }
78 
79  private void addRows() {
80  Rows rows = getRows(true);
81  for(AccountReportItem item : report.getChildren()) {
82  addItem(item, rows);
83  }
84  if(report.isShowTotal()) {
85  addItem(report.getTotalItem(), rows);
86  }
87  for(AccountReportFormula formula : report.getFormulas()) {
88  addFormula(formula, rows);
89  }
90  }
91 
92  private void addItem(AccountReportItem root, Rows rows) {
93  Row row = new Row();
94  for(Component col : getColumns().getChildren()) {
95  if(((Column)col).getValue() == null) {
96  row.appendChild(getLabelFor(root));
97  } else {
98  row.appendChild(getComponentFor(root.getPeriodFor((AccountReportPeriod) ((Column)col).getValue())));
99  }
100  }
101  rows.appendChild(row);
102  for(AccountReportItem item : root.getChildren()) {
103  addItem(item, rows);
104  }
105  if(root.isShowTotal()) {
106  addItem(root.getTotalItem(), rows);
107  }
108  }
109 
110  private void addFormula(AccountReportFormula formula, Rows rows) {
111  Row row = new Row();
112  for(Component col : getColumns().getChildren()) {
113  if(((Column)col).getValue() == null) {
114  row.appendChild(LabelTypes.getCaptionLabel(I_.get(formula.getLabel())));
115  } else {
116  row.appendChild(LabelTypes.getCaptionLabel(DecimalFormats.format(formula.getValue((AccountReportPeriod) ((Column)col).getValue()))));
117  }
118  }
119  rows.appendChild(row);
120  }
121 
122  private Component getComponentFor(AccountReportPeriod period) {
123  if(period == null || Zero.near(period.getValue(), 0)) {
124  return new Label("");
125  } else if(period.getItem().isTotalItem() || period.getItem().isShowTotal()) {
126  return LabelTypes.getCaptionLabel(DecimalFormats.format(period.getValue()));
127  } else {
128  return new Label(DecimalFormats.format(period.getValue()));
129  }
130  }
131 
132  private Component getLabelFor(AccountReportItem item) {
133  Hbox hbox = new Hbox();
134  hbox.setWidth("100%");
135  hbox.setWidths("98%, 2%");
136  Label l;
137  if(item.isTotalItem() || item.isShowTotal() || item.isTitleItem()) {
138  l = LabelTypes.getCaptionLabel(
139  (item.isTotalItem() ? "Total " : "") +
140  I_.get(item.getLabel()));
141  } else {
142  l = new Label(I_.get(item.getLabel()));
143  }
144  l.setStyle("padding-left:" + (getLevel(item) * 3) + "px");
145  hbox.appendChild(l);
146  if(item.getMode() != null) {
147  hbox.appendChild(LabelTypes.getTinyLabel(I_.get(item.getMode().getLabel())));
148  }
149  return hbox;
150  }
151 
152  private int getLevel(AccountReportItem item) {
153  int level = 0;
154  while(item.getParent() != null) {
155  level++;
156  item = item.getParent();
157  }
158  return level;
159  }
160 
161 }
List< AccountReportItem > getChildren()
List< AccountReportFormula > getFormulas()
PeriodSet getCurrentPeriod(int count)
Definition: PeriodSet.java:60
static String get(String msg)
Definition: I_.java:41
Columns getColumns(boolean create)
Rows getRows(boolean create)