BrightSide Workbench Full Report + Source Code
ContractFlowTabbox.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 org.turro.string.Strings;
22 import org.turro.command.Command;
23 import org.turro.command.Context;
24 import org.turro.elephant.util.Messages;
25 import org.turro.financials.document.DocumentDefinitionListbox;
26 import org.turro.financials.entity.Contract;
27 import org.turro.financials.entity.ContractFlow;
28 import org.turro.financials.entity.DocumentDefinition;
29 import org.turro.i18n.I_;
30 import org.turro.jpa.entity.EntityCollections;
31 import org.turro.util.Chars;
32 import org.turro.zkoss.dialog.InputDialog;
33 import org.turro.zkoss.dialog.InputField;
34 import org.zkoss.zk.ui.HtmlBasedComponent;
35 import org.zkoss.zk.ui.event.Event;
36 import org.zkoss.zk.ui.event.EventListener;
37 import org.zkoss.zk.ui.event.Events;
38 import org.zkoss.zul.Tab;
39 import org.zkoss.zul.Tabbox;
40 import org.zkoss.zul.Tabpanel;
41 import org.zkoss.zul.Tabpanels;
42 import org.zkoss.zul.Tabs;
43 
48 public class ContractFlowTabbox extends Tabbox {
49 
50  private Contract contract;
51 
52  public void setContract(Contract contract) {
53  this.contract = contract;
54  addTabs();
55  }
56 
57  public void addFlow(final ContractFlow contractFlow) {
59  getPage(),
60  I_.get("Expiry"),
61  new InputField[] {
62  new InputField("Document model", null, null, 0) {
63  @Override
64  protected HtmlBasedComponent createEditor() {
65  DocumentDefinitionListbox ddl = new DocumentDefinitionListbox(contract.getContractDefinition());
66  ddl.setMold("select");
67  if(contractFlow != null) {
68  ddl.setObjectValue(contractFlow.getDocumentDefinition());
69  }
70  return ddl;
71  }
72  },
73  new InputField("Description", contractFlow == null ? "" : contractFlow.getDescription(), null, 0)
74  }, new Command() {
75  @Override
76  public Object execute(Context context) {
77  InputField[] fields = (InputField[]) context.get("fields");
78  if(fields.length > 0) {
79  String description = null;
80  DocumentDefinition documentDefinition = null;
81  for(InputField f : fields) {
82  if("Description".equals(f.getLabel())) {
83  description = (String) f.getValue();
84  } else if("Document model".equals(f.getLabel())) {
85  documentDefinition = (DocumentDefinition) f.getValue();
86  }
87  }
88  if(!Strings.isBlank(description) && documentDefinition != null) {
89  if(contractFlow == null) {
90  ContractFlow cf = new ContractFlow();
91  cf.setDescription(description);
92  cf.setDocumentDefinition(documentDefinition);
93  cf.setContract(contract);
94  contract.getContractFlows().add(cf);
95  } else {
96  contractFlow.setDescription(description);
97  contractFlow.setDocumentDefinition(documentDefinition);
98  }
99  addTabs();
100  }
101  }
102  return null;
103  }
104  });
105  }
106 
107  private void addTabs() {
108  getChildren().clear();
109  Tabs tabs = new Tabs();
110  appendChild(tabs);
111  Tabpanels tabpanels = new Tabpanels();
112  appendChild(tabpanels);
113  for(final ContractFlow cf : contract.getContractFlows()) {
114  cf.clearEmpties();
115  final Tab tab = new Tab(cf.getDocumentDefinition().getName() + Chars.forward().spaced() + cf.getDescription());
116  tab.setClosable(true);
117  tab.addEventListener(9000, Events.ON_CLOSE, new EventListener<Event>() {
118  @Override
119  public void onEvent(Event event) throws Exception {
120  event.stopPropagation();
121  Messages.confirmDeletion().add(cf.getDescription()).show(() -> {
122  tab.close();
123  EntityCollections.entities(contract.getContractFlows()).remove(cf);
124  addTabs();
125  Events.postEvent(new Event(Events.ON_CHANGE, ContractFlowTabbox.this.getParent()));
126  });
127  }
128  });
129  tab.addEventListener(Events.ON_DOUBLE_CLICK, new EventListener<Event>() {
130  @Override
131  public void onEvent(Event event) throws Exception {
132  addFlow(cf);
133  }
134  });
135  tabs.appendChild(tab);
136  Tabpanel panel = new Tabpanel();
137  ContractExpiriesGrid ceg = new ContractExpiriesGrid(cf);
138  ceg.addEventListener(Events.ON_CHANGE, new EventListener<Event>() {
139  @Override
140  public void onEvent(Event event) throws Exception {
141  Events.postEvent(new Event(Events.ON_CHANGE, ContractFlowTabbox.this.getParent()));
142  }
143  });
144  ceg.setVflex(true);
145  ceg.setReadOnly(false);
146  ceg.setAllowDeletions(true);
147  ceg.setAllowInsertions(true);
148  panel.appendChild(ceg);
149  tabpanels.appendChild(panel);
150  ceg.afterCompose();
151  }
152  Tab addFlow = new Tab("+");
153  tabs.appendChild(addFlow);
154  addFlow.addEventListener(Events.ON_CLICK, new EventListener<Event>() {
155  @Override
156  public void onEvent(Event event) throws Exception {
157  addFlow(null);
158  Events.postEvent(new Event(Events.ON_CHANGE, ContractFlowTabbox.this.getParent()));
159  }
160  });
161  }
162 
163 }
void addFlow(final ContractFlow contractFlow)
Set< ContractFlow > getContractFlows()
Definition: Contract.java:137
static String get(String msg)
Definition: I_.java:41
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)