BrightSide Workbench Full Report + Source Code
DocumentListbox.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.document;
19 
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.List;
23 import org.turro.string.Strings;
24 import org.turro.elephant.util.DateFormats;
25 import org.turro.elephant.util.DecimalFormats;
26 import org.turro.financials.document.action.DocumentActionMenu;
27 import org.turro.financials.entity.Document;
28 import org.turro.financials.entity.DocumentLine;
29 import org.turro.i18n.I_;
30 import org.turro.zkoss.input.CollectionListbox;
31 import org.zkoss.zul.*;
32 
37 public class DocumentListbox extends CollectionListbox<Document> {
38 
39  private boolean showContract = false,
40  showTotalAmount = false,
41  showStores = false,
42  showConcepts = false;
43  private DocumentActionMenu dam;
44 
45  public DocumentListbox() {
46  }
47 
48  public DocumentListbox(Collection<Document> collection) {
49  super(collection);
50  }
51 
52  public boolean isShowConcepts() {
53  return showConcepts;
54  }
55 
56  public void setShowConcepts(boolean showConcepts) {
57  this.showConcepts = showConcepts;
58  }
59 
60  public boolean isShowContract() {
61  return showContract;
62  }
63 
64  public void setShowContract(boolean showContract) {
65  this.showContract = showContract;
66  }
67 
68  public boolean isShowStores() {
69  return showStores;
70  }
71 
72  public void setShowStores(boolean showStores) {
73  this.showStores = showStores;
74  }
75 
76  public boolean isShowTotalAmount() {
77  return showTotalAmount;
78  }
79 
80  public void setShowTotalAmount(boolean showTotalAmount) {
81  this.showTotalAmount = showTotalAmount;
82  }
83 
84  @Override
85  protected String convertToString(Document v) {
86  if(v == null) return null;
87 
88  String storesStr = "";
89  if(showStores) {
90  HashSet<String> set = new HashSet<String>();
91  for(DocumentLine dl : v.getDocumentLines()) {
92  if(dl.getStore() != null) {
93  set.add(dl.getStore().getPartialDescription());
94  }
95  }
96  for(String s : set) {
97  storesStr += SUBITEM_SEPARATOR + s;
98  }
99  }
100 
101  String conceptsStr = "";
102  if(showConcepts) {
103  HashSet<String> set = new HashSet<String>();
104  for(DocumentLine dl : v.getDocumentLines()) {
105  if(!Strings.isBlank(dl.getConcept())) {
106  set.add(dl.getConcept());
107  }
108  }
109  for(String s : set) {
110  conceptsStr += SUBITEM_SEPARATOR + s;
111  }
112  }
113 
114  return (showContract ? v.getContract().getFullDescription() + SUBITEM_SEPARATOR : "") +
115  v.getDocumentDefinition().getName() + storesStr + conceptsStr + ITEM_SEPARATOR +
116  DateFormats.format(v.getDocumentDate(), true) + SUBITEM_SEPARATOR +
120  }
121 
122  @Override
123  public void afterCompose() {
124  dam = new DocumentActionMenu();
125  getParent().appendChild(dam);
126  addHeaders();
127  super.afterCompose();
128  if(showTotalAmount) {
129  addFooters();
130  }
131  }
132 
133  @Override
134  protected void populateList() {
135  super.populateList();
136  for(Listitem li : (List<Listitem>) getItems()) {
137  li.setContext(dam);
138  li.setAttribute("document", li.getValue());
139  }
140  }
141 
142  private void addHeaders() {
143  if(getListhead() != null) return;
144  Listhead lh = new Listhead();
145  lh.setSizable(true);
146  lh.appendChild(new Listheader(I_.get("Document"), null, "40%"));
147  lh.appendChild(new Listheader(I_.get("Date"), null, "20%"));
148  lh.appendChild(new Listheader(I_.get("Number"), null, "20%"));
149  lh.appendChild(new Listheader(I_.get("Amount"), null, "20%"));
150  appendChild(lh);
151  }
152 
153  private void addFooters() {
154  Listfoot lh = getListfoot();
155  if(lh != null) {
156  lh.detach();
157  }
158  lh = new Listfoot();
159  appendChild(lh);
160  lh.appendChild(new Listfooter());
161  lh.appendChild(new Listfooter());
162  lh.appendChild(new Listfooter());
163  lh.appendChild(new Listfooter(DecimalFormats.format(getTotal())));
164  }
165 
166  private double getTotal() {
167  double total = 0;
168  if(getCollection() != null) {
169  for(Document d : getCollection()) {
170  total += d.getTotalAmount();
171  }
172  }
173  return total;
174  }
175 
176 }
static final String format(Date d, boolean dateOnly)
static String format(Number value, String pattern)
DocumentListbox(Collection< Document > collection)
void setShowTotalAmount(boolean showTotalAmount)
Set< DocumentLine > getDocumentLines()
Definition: Document.java:180
DocumentDefinition getDocumentDefinition()
Definition: Document.java:167
static String get(String msg)
Definition: I_.java:41