BrightSide Workbench Full Report + Source Code
UsageTree.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.task.usage;
20 
21 import java.util.Set;
22 import org.turro.command.Command;
23 import org.turro.command.Context;
24 import org.turro.erp.entity.OrderItem;
25 import org.turro.erp.entity.ReceiptItem;
26 import org.turro.erp.entity.RequiredUsage;
27 import org.turro.erp.entity.Task;
28 import org.turro.zkoss.layout.GridLayout;
29 import org.zkoss.zk.ui.event.Event;
30 import org.zkoss.zk.ui.event.EventListener;
31 import org.zkoss.zk.ui.event.Events;
32 import org.zkoss.zul.Vlayout;
33 
38 public class UsageTree extends GridLayout {
39 
40  private Task task;
41  private RequiredUsage requiredUsage;
42  private boolean onlyUsages = false;
43 
44  public UsageTree() {
45  addEventListener(Events.ON_CHANGE, new EventListener() {
46  @Override
47  public void onEvent(Event event) throws Exception {
48  addComponents();
49  }
50  });
51  }
52 
53  public void setOnlyUsages(boolean onlyUsages) {
54  this.onlyUsages = onlyUsages;
55  }
56 
57  public void addHumanUsage() {
58  final RequiredUsage ru = new RequiredUsage();
59  ru.setTask(task);
61  UsageBox.doEdit(ru, new Command() {
62  @Override
63  public Object execute(Context context) {
64  if(!ru.isEmpty()) {
65  task.getRequiredUsages().add(ru);
66  Events.postEvent(new Event(Events.ON_CHANGE, UsageTree.this));
67  }
68  return null;
69  }
70  });
71  }
72 
73  public void addProductUsage() {
74  final RequiredUsage ru = new RequiredUsage();
75  ru.setTask(task);
76  ru.prepareProduct();
77  UsageBox.doEdit(ru, new Command() {
78  @Override
79  public Object execute(Context context) {
80  if(!ru.isEmpty()) {
81  task.getRequiredUsages().add(ru);
82  Events.postEvent(new Event(Events.ON_CHANGE, UsageTree.this));
83  }
84  return null;
85  }
86  });
87  }
88 
90  return requiredUsage;
91  }
92 
93  public void setRequiredUsage(RequiredUsage requiredUsage) {
94  this.requiredUsage = requiredUsage;
95  if(requiredUsage != null) {
96  addComponents();
97  }
98  }
99 
100  public Task getTask() {
101  return task;
102  }
103 
104  public void setTask(Task task) {
105  this.task = task;
106  if(task != null) {
107  addComponents();
108  }
109  }
110 
111  private void addComponents() {
112  if(getColumns() == null) {
113  if(onlyUsages || isProductOrder()) {
114  setColumns("1");
115  } else {
116  setColumns("1,2");
117  }
118  }
119  if(getRows() != null) {
120  getRows().getChildren().clear();
121  }
122  if(task != null) {
123  for(RequiredUsage ru : task.getRequiredUsages()) {
124  ru.getUpdatedOrderItems();
125  addRow();
126  addComponent(new UsageBox(ru));
127  if(!onlyUsages) {
128  GridLayout glo = new GridLayout("1,1");
129  addComponent(glo);
130  appendOrders(glo, ru.getOrderItems());
131  }
132  }
133  } else if(requiredUsage != null) {
134  requiredUsage.getUpdatedOrderItems();
135  addRow();
136  addComponent(new UsageBox(requiredUsage));
137  if(!onlyUsages) {
138  GridLayout glo = new GridLayout("1,1");
139  addComponent(glo);
140  appendOrders(glo, requiredUsage.getOrderItems());
141  }
142  }
143  }
144 
145  private void appendOrders(GridLayout gl, Set<OrderItem> orderItems) {
146  for(OrderItem oi : orderItems) {
147  gl.addRow();
148  gl.addComponent(new OrderBox(oi));
149  appendReceipts(gl, oi.getReceiptItems());
150  if(oi.getRelated() != null) {
151  gl.addRow();
152  gl.addComponent(new OrderBox(oi.getRelated()));
153  appendReceipts(gl, oi.getRelated().getReceiptItems());
154  }
155  }
156  }
157 
158  private void appendReceipts(GridLayout gl, Set<ReceiptItem> receiptItems) {
159  Vlayout vbox = new Vlayout();
160  for(ReceiptItem ri : receiptItems) {
161  vbox.appendChild(new ReceiptBox(ri));
162  }
163  gl.addComponent(vbox);
164  }
165 
166  private boolean isProductOrder() {
167  return (task != null ? task.isProductOrder() : (
168  requiredUsage != null ? requiredUsage.getTask().isProductOrder() : false));
169  }
170 
171 }
static void doEdit(RequiredUsage requiredUsage, Command command)
Definition: UsageBox.java:151
void setRequiredUsage(RequiredUsage requiredUsage)
Definition: UsageTree.java:93
void setOnlyUsages(boolean onlyUsages)
Definition: UsageTree.java:53
GridLayout addComponent(HtmlBasedComponent comp)
Rows getRows(boolean create)