BrightSide Workbench Full Report + Source Code
ContractExpiriesGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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 
19 package org.turro.financials.contract;
20 
21 import java.text.DateFormatSymbols;
22 import java.util.Collection;
23 import java.util.Set;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.turro.string.Strings;
27 import org.turro.elephant.context.Application;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.util.DecimalFormats;
30 import org.turro.financials.document.ConceptCombobox;
31 import org.turro.financials.document.DocumentDefinitionListbox;
32 import org.turro.financials.entity.ContractExpiry;
33 import org.turro.financials.entity.ContractFlow;
34 import org.turro.financials.entity.DocumentDefinition;
35 import org.turro.financials.entity.LineType;
36 import org.turro.financials.linetype.LineTypeListbox;
37 import org.turro.i18n.I_;
38 import org.turro.jpa.entity.EntityCollections;
39 import org.turro.scheduler.constraints.WeekDay;
40 import org.turro.scheduler.zul.constraints.WeekDayListbox;
41 import org.turro.usual.UsualUsages;
42 import org.turro.zkoss.grid.CollectionGrid;
43 import org.turro.zkoss.grid.EditableCell;
44 import org.turro.zkoss.input.DaysOfMonthBandbox;
45 import org.turro.zkoss.input.DaysOfMonthListbox;
46 import org.turro.zkoss.input.MonthBandbox;
47 import org.turro.zkoss.input.MonthListbox;
48 import org.turro.zkoss.input.Percentbox;
49 import org.zkoss.zk.ui.HtmlBasedComponent;
50 import org.zkoss.zk.ui.ext.AfterCompose;
51 import org.zkoss.zul.Row;
52 
57 public class ContractExpiriesGrid extends CollectionGrid<ContractExpiry> implements AfterCompose {
58 
59  private ContractFlow contractFlow;
60 
62  super();
63  }
64 
65  public ContractExpiriesGrid(ContractFlow contractFlow) {
66  super(contractFlow.getContractExpiries());
67  this.contractFlow = contractFlow;
68  addColumns();
69  }
70 
71  public void setContractFlow(ContractFlow contractFlow) {
72  this.contractFlow = contractFlow;
73  setCollection(contractFlow.getContractExpiries());
74  addColumns();
75  }
76 
77  @Override
78  protected boolean isValid(ContractExpiry v) {
79  return !v.isEmpty();
80  }
81 
82  @Override
83  protected void initiateRow(Row row, ContractExpiry value) {
84  if(value == null) {
85  value = new ContractExpiry();
86  value.setDayOfWeek(-1);
87  value.setContractFlow(contractFlow);
88  contractFlow.getContractExpiries().add(value);
89  }
90  row.setValue(value);
91  }
92 
93  @Override
94  protected boolean deleteRow(Row row) {
95  ContractExpiry ce = (ContractExpiry) row.getValue();
96  EntityCollections.entities(contractFlow.getContractExpiries()).remove(ce);
97  ce.setContractFlow(null);
98  return true;
99  }
100 
101  @Override
102  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
103  if(editableCell.getCellIndex() == 0) {
104  Object value = getCellValue(editableCell);
106  contractFlow.getContract().getContractDefinition(),
107  contractFlow.getDocumentDefinition());
108  ddl.setMold("select");
109  if(value != null) ddl.setObjectValue((DocumentDefinition) value);
110  return ddl;
111  } else if(editableCell.getCellIndex() == 1) {
112  Object value = getCellValue(editableCell);
113  ContractExpiry ce = editableCell.getRow().getValue();
114  ConceptCombobox slb = new ConceptCombobox();
117  if(value != null) slb.setObjectValue((String) value);
118  return slb;
119  } else if(editableCell.getCellIndex() == 3) {
120  Object value = getCellValue(editableCell);
122  if(value != null) dmb.setObjectValue((Collection<Integer>) value);
123  return dmb;
124  } else if(editableCell.getCellIndex() == 4) {
125  Object value = getCellValue(editableCell);
126  WeekDayListbox wdl = new WeekDayListbox();
127  wdl.setAllowNull(true);
128  wdl.setMold("select");
129  if(value != null) wdl.setDayIndex((Integer) value);
130  return wdl;
131  } else if(editableCell.getCellIndex() == 5) {
132  Object value = getCellValue(editableCell);
133  MonthBandbox dmb = new MonthBandbox();
134  if(value != null) dmb.setObjectValue((Collection<Integer>) value);
135  return dmb;
136  } else if(editableCell.getCellIndex() == 6) {
137  Object value = getCellValue(editableCell);
138  Percentbox pc = new Percentbox();
139  if(value != null) pc.setValue((Double) value);
140  return pc;
141  } else if(editableCell.getCellIndex() == 8) {
142  Object value = getCellValue(editableCell);
143  ContractExpiry ce = editableCell.getRow().getValue();
145  if(value != null) {
146  ltl.setObjectValue((LineType) value);
147  }
148  ltl.setMold("select");
149  return ltl;
150  }
151  return super.createEditor(editableCell);
152  }
153 
154  @Override
155  protected Object getEditorValue(EditableCell editableCell) {
156  if(editableCell.getCellIndex() == 4) {
157  WeekDayListbox wdl = (WeekDayListbox) editableCell.getEditor();
158  return wdl.getDayIndex();
159  }
160  return super.getEditorValue(editableCell);
161  }
162 
163  @Override
164  protected String formatCell(EditableCell editableCell, Object value) {
165  if(editableCell.getCellIndex() == 0) {
167  if(dd != null) {
168  return dd.getName();
169  }
170  return "";
171  } else if(editableCell.getCellIndex() == 3) {
172  if(value != null) {
173  return DaysOfMonthListbox.getStringValues((Collection<Integer>) value);
174  }
175  } else if(editableCell.getCellIndex() == 4) {
176  if(value != null) {
177  DateFormatSymbols dfs = new DateFormatSymbols(Application.getUsedLocale());
178  int wd = (Integer) value;
179  if(wd > -1) {
180  return Strings.capitalize(dfs.getWeekdays()[WeekDay.map(wd)]);
181  }
182  }
183  return "";
184  } else if(editableCell.getCellIndex() == 5) {
185  if(value != null) {
186  return MonthListbox.getStringValues((Collection<Integer>) value);
187  }
188  } else if(editableCell.getCellIndex() == 6) {
189  return DecimalFormats.format(((Double) value) * 100.0) + "%";
190  } else if(editableCell.getCellIndex() == 8) {
191  if(value != null) {
192  return ((LineType) value).getName();
193  }
194  return "";
195  }
196  return super.formatCell(editableCell, value);
197  }
198 
199  private void addColumns() {
200  try {
201  addColumn(I_.get("Document model"), DocumentDefinition.class, "documentDefinition", null, 0, false, false).setHflex("2");
202  addColumn(I_.get("Concept"), String.class, "concept", null, 0, false, false).setHflex("2");
203  addColumn(I_.get("+Days"), "int", "daysToAdd", null, 0, false, false).setWidth("80px");
204  addColumn(I_.get("Day of month"), Set.class, "daysOfMonth", null, 0, false, false).setWidth("130px");
205  addColumn(I_.get("Day of week"), "int", "dayOfWeek", null, 0, false, false).setWidth("130px");
206  addColumn(I_.get("Excluded months"), Set.class, "excludedMonths", null, 0, false, false).setWidth("130px");
207  addColumn(I_.get("Percent"), "double", "percentValue", null, 2, false, false).setWidth("130px");
208  addColumn(I_.get("Amount"), "double", "amountValue", null, 2, false, false).setWidth("130px");
209  addColumn(I_.get("Line type"), LineType.class, "lineType", null, 2, false, false).setHflex("1");
210  } catch (ClassNotFoundException ex) {
211  Logger.getLogger(ContractTaxesGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
212  }
213  }
214 
215 }
static String format(Number value, String pattern)
String formatCell(EditableCell editableCell, Object value)
HtmlBasedComponent createEditor(EditableCell editableCell)
void setDocumentDefinition(DocumentDefinition documentDefinition)
void setContractFlow(ContractFlow contractFlow)
Set< ContractExpiry > getContractExpiries()
ContractDefinition getContractDefinition()
Definition: Contract.java:125
String getUsualPath(DocumentDefinition documentDefinition)
Definition: Contract.java:488
static String get(String msg)
Definition: I_.java:41
static EntityCollections entities(Collection values)
static String getMostUsedvalue(String path)
void setCollection(Collection< V > collection)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
Object getCellValue(EditableCell editableCell)
void setObjectValue(Collection< Integer > v)
void setObjectValue(Collection< Integer > v)