BrightSide Workbench Full Report + Source Code
DocumentRegistersGrid.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.Date;
22 import java.util.HashSet;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.turro.elephant.context.ElephantContext;
26 import org.turro.financials.entity.Document;
27 import org.turro.financials.entity.DocumentRelation;
28 import org.turro.financials.entity.RegisterView;
29 import org.turro.financials.model.document.DocumentWrapper;
30 import org.turro.financials.model.register.RegisterWrapper;
31 import org.turro.financials.register.EntriesGrid;
32 import org.turro.i18n.I_;
33 import org.turro.zkoss.grid.CollectionGrid;
34 import org.turro.zkoss.grid.EditableCell;
35 import org.zkoss.zul.Detail;
36 import org.zkoss.zul.Row;
37 import org.zkoss.zul.Vbox;
38 
43 public class DocumentRegistersGrid extends CollectionGrid<RegisterWrapper> {
44 
45  private Document document;
46 
48  super();
49  }
50 
51  public DocumentRegistersGrid(Document document) {
52  super();
53  setDocument(document);
54  }
55 
56  public Document getDocument() {
57  return document;
58  }
59 
60  public void setDocument(Document document) {
61  this.document = document;
62  }
63 
64  public void setDescendants(Collection<DocumentRelation> descendants) {
65  if(descendants == null) return;
66  if(getCollection() == null) {
67  setCollection(new HashSet<RegisterWrapper>());
68  }
69  for(DocumentRelation dr : descendants) {
70  if(!dr.isEmpty()) {
71  dr.getDescendant().setForcedView(document.getForcedView());
72  getCollection().addAll(new DocumentWrapper(dr.getDescendant()).getRegisters());
73  }
74  }
75  }
76 
77  public void updateDocument(Document document, Collection<DocumentRelation> descendants) {
78  clearRows();
79  clearColumns();
80  if(getCollection() != null) {
81  setCollection(null);
82  }
83  setDocument(document);
84  if(getCollection() == null) {
85  setCollection(new HashSet<RegisterWrapper>());
86  }
87  if(document != null && document.getContract() != null) {
88  getCollection().addAll(new DocumentWrapper(document).getRegisters());
89  }
90  setDescendants(descendants);
91  for(DocumentRelation dr : descendants) {
92  dr.getDescendant().setForcedView(document.getForcedView());
93  }
94  afterCompose();
95  }
96 
97  @Override
98  protected void initiateRow(Row row, RegisterWrapper value) {
99  row.setValue(value);
100  addDetail(row);
101  }
102 
103  @Override
104  protected boolean deleteRow(Row row) {
105  throw new UnsupportedOperationException("Not supported yet.");
106  }
107 
108  @Override
109  protected boolean isValid(RegisterWrapper v) {
110  return true;
111  }
112 
113  @Override
114  protected String formatCell(EditableCell editableCell, Object value) {
115  if(editableCell.getCellIndex() == 1) {
116  if(value instanceof Long) {
117  if(((Long) value) == 0) {
118  return "<auto>";
119  }
120  }
121  } else if(editableCell.getCellIndex() == 3) {
122  if(value instanceof RegisterView) {
123  return ((RegisterView) value).getName();
124  }
125  return "";
126  }
127  return super.formatCell(editableCell, value);
128  }
129 
130  @Override
131  public void afterCompose() {
132  setReadOnly(true);
133  if(document != null && document.getDocumentDefinition() != null) {
134  try {
135  addColumns();
136  } catch (ClassNotFoundException ex) {
137  Logger.getLogger(DocumentRegistersGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
138  }
139  }
140  super.afterCompose();
141  }
142 
143  private void addColumns() throws ClassNotFoundException {
144  //int fractionDigits = document.getCurrency().getDefaultFractionDigits();
145  addDetailColumn();
146  addColumn(I_.get("Register"), "long",
147  "register.idRegister", null, 0, false, false).setWidth("120px");
148  addColumn(I_.get("Date"), Date.class,
149  "register.registerDate", null, 0, true, false).setWidth("120px");
150  addColumn(I_.get("View"), RegisterView.class,
151  "register.view", null, 0, false, false).setWidth("200px");
152  addColumn(I_.get("Books"), String.class,
153  "bookString", null, 0, false, false);
154  }
155 
156  private void addDetail(Row row) {
157  row.getChildren().clear();
158  Detail detail = new Detail();
159  row.appendChild(detail);
160  Vbox vbox = new Vbox();
161  vbox.setWidth("100%");
162  vbox.setStyle("padding:5px");
163  detail.appendChild(vbox);
164  EntriesGrid eg = new EntriesGrid((RegisterWrapper) row.getValue());
165  eg.setDependingRow(row);
166  eg.setReadOnly(isReadOnly());
167  eg.setAllowDeletions(isAllowDeletions());
168  eg.setAllowInsertions(isAllowInsertions());
169  vbox.appendChild(eg);
170  eg.afterCompose();
171  detail.setVisible(!eg.getCollection().isEmpty());
172  detail.setOpen(detail.isVisible());
173  row.setVisible(detail.isVisible());
174  }
175 
176 }
void setDescendants(Collection< DocumentRelation > descendants)
String formatCell(EditableCell editableCell, Object value)
void updateDocument(Document document, Collection< DocumentRelation > descendants)
DocumentDefinition getDocumentDefinition()
Definition: Document.java:167
static String get(String msg)
Definition: I_.java:41
void setCollection(Collection< V > collection)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
void setReadOnly(boolean readOnly)