BrightSide Workbench Full Report + Source Code
BalanceMajorItem.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 org.turro.financials.entity.Account;
21 import org.turro.financials.entity.MajorAccount;
22 import org.turro.financials.menu.FinancialsMenu;
23 import org.zkoss.zk.ui.event.Event;
24 import org.zkoss.zk.ui.event.EventListener;
25 import org.zkoss.zk.ui.event.Events;
26 import org.zkoss.zk.ui.ext.AfterCompose;
27 import org.zkoss.zul.Toolbarbutton;
28 import org.zkoss.zul.Treecell;
29 import org.zkoss.zul.Treechildren;
30 import org.zkoss.zul.Treeitem;
31 import org.zkoss.zul.Treerow;
32 
37 public class BalanceMajorItem extends Treeitem implements AfterCompose {
38 
39  private Treechildren children;
40  private MajorAccount majorAccount;
41  private boolean loadOnDemand;
42 
43  public BalanceMajorItem(MajorAccount majorAccount, boolean loadOnDemand) {
44  super();
45  this.majorAccount = majorAccount;
46  this.loadOnDemand = loadOnDemand;
47  }
48 
49  @Override
50  public BalanceTree getTree() {
51  return (BalanceTree) super.getTree();
52  }
53 
54  public void showContents() {
55  fillFolder();
56  setOpen(true);
57  }
58 
59  public void reloadContents() {
60  children.getChildren().clear();
61  showContents();
62  }
63 
64  private void addCells() {
65  Double[] dA = getTree().getFilter().getAmountsA(majorAccount.getAccount());
66  Treerow row = new Treerow();
67  row.setSclass("majorAccount" + majorAccount.getAccount().length());
68  appendChild(row);
69  row.appendChild(new Treecell(majorAccount.getAccount()));
70  row.appendChild(new Treecell(majorAccount.getDescription()));
71  if(getTree().getFilter().hasTwoGroup()) {
72  Double[] dB = getTree().getFilter().getAmountsB(majorAccount.getAccount());
73  row.appendChild(BalanceTopItem.getGroupMoneyCell(getTree(), dA[0], dB[0]));
74  row.appendChild(BalanceTopItem.getGroupMoneyCell(getTree(), dA[1], dB[1]));
75  row.appendChild(BalanceTopItem.getGroupMoneyCell(getTree(), dA[0] - dA[1], dB[0] - dB[1]));
76  } else {
77  row.appendChild(new Treecell(getTree().formatCurrency(dA[0])));
78  row.appendChild(new Treecell(getTree().formatCurrency(dA[1])));
79  row.appendChild(new Treecell(getTree().formatCurrency(dA[0] - dA[1])));
80  }
81  Toolbarbutton tb = new Toolbarbutton();
82  tb.setImage("/_zul/images/statement.png");
83  tb.addEventListener(Events.ON_CLICK, new EventListener() {
84  @Override
85  public void onEvent(Event event) throws Exception {
86  FinancialsMenu.showStatement(majorAccount.getAccount() + "*");
87  }
88  });
89  Treecell tc = new Treecell();
90  tc.appendChild(tb);
91  row.appendChild(tc);
92  }
93 
94  private void addChildrenSpace() {
95  children = new Treechildren();
96  appendChild(children);
97  if(!loadOnDemand) {
98  fillFolder();
99  setOpen(true);
100  }
101  }
102 
103  public boolean isLoadOnDemand() {
104  return loadOnDemand;
105  }
106 
107  public void setLoadOnDemand(boolean loadOnDemand) {
108  this.loadOnDemand = loadOnDemand;
109  }
110 
111  private void initLoadOnDemand() {
112  if(!loadOnDemand) return;
113 
114  setOpen(false);
115 
116  addEventListener(Events.ON_OPEN, new EventListener() {
117  @Override
118  public void onEvent(Event event) throws Exception {
119  // load files and add folders
120  if(isOpen()) {
121  fillFolder();
122  }
123  }
124  });
125  }
126 
127  private void fillFolder() {
128  if(children.getChildren().isEmpty()) {
129  for(MajorAccount ma : getTree().getFilter().getMajorAccounts(majorAccount.getAccount())) {
130  BalanceMajorItem bmi = new BalanceMajorItem(ma, loadOnDemand);
131  children.appendChild(bmi);
132  bmi.afterCompose();
133  }
134  for(Account a : getTree().getFilter().getAccounts(majorAccount.getAccount())) {
135  BalanceAccountItem bai = new BalanceAccountItem(a);
136  children.appendChild(bai);
137  bai.afterCompose();
138  }
139  }
140  }
141 
142  @Override
144  return (BalanceMajorItem) getParentItem();
145  }
146 
147  @Override
148  public void afterCompose() {
149  initLoadOnDemand();
150  addCells();
151  addChildrenSpace();
152  }
153 
154 }
BalanceMajorItem(MajorAccount majorAccount, boolean loadOnDemand)
static Treecell getGroupMoneyCell(BalanceTree tree, Double a, Double b)