BrightSide Workbench Full Report + Source Code
BudgetSelector.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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.erp.reference;
20 
21 import java.util.Map;
22 import java.util.TreeMap;
23 import org.turro.elephant.context.Application;
24 import org.turro.elephant.util.Images;
25 import org.turro.erp.entity.Budget;
26 import org.turro.erp.entity.OrderReference;
27 import org.turro.erp.entity.WorkOrder;
28 import org.zkoss.zk.ui.event.Event;
29 import org.zkoss.zk.ui.event.EventListener;
30 import org.zkoss.zk.ui.event.Events;
31 import org.zkoss.zul.Hlayout;
32 import org.zkoss.zul.Toolbarbutton;
33 
38 public class BudgetSelector extends Hlayout {
39 
40  private WorkOrder workOrder;
41  private TreeMap<Long, String> budgets;
42  private boolean hasSelection = false;
43 
44  public void setWorkOrder(WorkOrder workOrder) {
45  this.workOrder = workOrder;
46  addComponents();
47  }
48 
49  public void refresh() {
50  addComponents();
51  }
52 
53  private void addComponents() {
54  getChildren().clear();
55  setSpacing("5px");
56  generateMap();
57  for(final Map.Entry<Long, String> entry : budgets.entrySet()) {
58  Toolbarbutton tbb = new Toolbarbutton(entry.getKey() + "", Images.getImage(entry.getValue()));
59  tbb.addEventListener(Events.ON_CLICK, new EventListener() {
60  @Override
61  public void onEvent(Event event) throws Exception {
62  Events.postEvent(new Event(Events.ON_SELECT, BudgetSelector.this, entry.getKey()));
63  hasSelection = true;
64  }
65  });
66  appendChild(tbb);
67  }
68  Toolbarbutton tbb = new Toolbarbutton(Application.getString("lNew"), Images.getImage("document_new"));
69  tbb.addEventListener(Events.ON_CLICK, new EventListener() {
70  @Override
71  public void onEvent(Event event) throws Exception {
72  Events.postEvent(new Event(Events.ON_SELECT, BudgetSelector.this, 0L));
73  hasSelection = true;
74  }
75  });
76  appendChild(tbb);
77  if(hasSelection) {
78  tbb = new Toolbarbutton(Application.getString("lDelete"), Images.getImage("document_del"));
79  tbb.addEventListener(Events.ON_CLICK, new EventListener() {
80  @Override
81  public void onEvent(Event event) throws Exception {
82  Events.postEvent(new Event(Events.ON_SELECT, BudgetSelector.this, -1L));
83  hasSelection = false;
84  }
85  });
86  appendChild(tbb);
87  }
88  }
89 
90  private void generateMap() {
91  budgets = new TreeMap<Long, String>();
92  for(OrderReference or : workOrder.getOrderReferences()) {
93  if(!or.isEmpty()) {
94  for(Budget b : or.getBudgets()) {
95  String status = budgets.get(b.getBudgetNumber());
96  if(b.isAccepted()) {
97  budgets.put(b.getBudgetNumber(), "ok");
98  } else if(b.isRefused()) {
99  if(!"ok".equals(status)) {
100  budgets.put(b.getBudgetNumber(), "cancel");
101  }
102  } else if(b.isDelivered()) {
103  if(status == null) {
104  budgets.put(b.getBudgetNumber(), "mail_send");
105  }
106  } else {
107  budgets.put(b.getBudgetNumber(), "document");
108  }
109  }
110  }
111  }
112  }
113 
114 }
static String getImage(String image)
Definition: Images.java:36
Set< OrderReference > getOrderReferences()
Definition: WorkOrder.java:124
void setWorkOrder(WorkOrder workOrder)