BrightSide Workbench Full Report + Source Code
UsageBox.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 org.turro.command.Command;
22 import org.turro.command.CommandUtil;
23 import org.turro.command.Context;
24 import org.turro.elephant.context.Application;
25 import org.turro.elephant.util.DecimalFormats;
26 import org.turro.elephant.util.Images;
27 import org.turro.elephant.util.ZkossUtils;
28 import org.turro.elephant.zkoss.Modal;
29 import org.turro.erp.db.ErpPU;
30 import org.turro.erp.entity.OrderItem;
31 import org.turro.erp.entity.RequiredUsage;
32 import org.turro.jpa.entity.EntityCollectionUtil;
33 import org.turro.zkoss.label.PercentLabel;
34 import org.turro.zkoss.layout.GridLayout;
35 import org.zkoss.zk.ui.Executions;
36 import org.zkoss.zk.ui.event.Event;
37 import org.zkoss.zk.ui.event.EventListener;
38 import org.zkoss.zk.ui.event.Events;
39 import org.zkoss.zul.*;
40 
45 public class UsageBox extends Groupbox {
46 
47  private RequiredUsage requiredUsage;
48  private GridLayout gl;
49 
50  public UsageBox(RequiredUsage requiredUsage) {
51  this.requiredUsage = requiredUsage;
52  setSclass("usageBox");
53  setMold("3d");
54  setOpen(true);
55  addComponents();
56  }
57 
58  private void addComponents() {
59  if(getCaption() != null) {
60  getCaption().setLabel(requiredUsage.getName());
61  getCaption().setImage(Images.getImage(requiredUsage.getType()));
62  gl.getRows().getChildren().clear();
63  } else {
64  appendChild(new Caption(requiredUsage.getName(),
65  Images.getImage(requiredUsage.getType())));
66  gl = new GridLayout("min,right-1,right-1,right-1");
67  gl.setColCaptions(new String[] {
68  "",
69  Application.getString("lEstimated"),
70  Application.getString("lExpected"),
71  Application.getString("lReal")
72  });
73  appendChild(gl);
74  }
75  renderUsage();
76  }
77 
78  private void renderUsage() {
79  if(requiredUsage.getProviderId() > 0) {
80  gl.addRow();
81  gl.addSpannedComponent(CommandUtil.getLabel(requiredUsage.getProvider()), 4);
82  }
83  gl.addRow();
84  gl.addCaption(Application.getString("lUnits"));
85  gl.addComponent(new Label(DecimalFormats.format(requiredUsage.getEstimatedUnits())));
86  gl.addComponent(new Label(DecimalFormats.format(requiredUsage.getExpectedUnits())));
87  gl.addComponent(new Label(DecimalFormats.format(requiredUsage.getRealUnits())));
88  if(requiredUsage.isHumanResourceType()) {
89  gl.addRow();
90  gl.addCaption(Application.getString("lDedication"));
91  gl.addComponent(new PercentLabel(requiredUsage.getDedication()));
92  gl.addComponent(new PercentLabel(requiredUsage.getExpectedDedication()));
93  gl.addComponent(new PercentLabel(requiredUsage.getRealDedication()));
94  }
95  gl.addRow();
96  gl.addCaption(Application.getString("lCost"));
97  gl.addComponent(new Label(DecimalFormats.format(requiredUsage.getEstimatedCost())));
98  gl.addComponent(new Label(DecimalFormats.format(requiredUsage.getExpectedCost())));
99  gl.addComponent(new Label(DecimalFormats.format(requiredUsage.getRealCost())));
100  gl.addRow();
101  Hlayout hbox = new Hlayout();
102  hbox.setStyle("text-align:right");
103  Button edit = new Button(Application.getString("lEdit"));
104  hbox.appendChild(edit);
105  edit.addEventListener(Events.ON_CLICK, new EventListener() {
106  @Override
107  public void onEvent(Event event) throws Exception {
108  doEdit(requiredUsage, new Command() {
109  @Override
110  public Object execute(Context context) {
111  addComponents();
112  return null;
113  }
114  });
115  }
116  });
117  if(requiredUsage.getOrderItems().isEmpty()) {
118  Button delete = new Button(Application.getString("lDelete"));
119  hbox.appendChild(delete);
120  delete.addEventListener(Events.ON_CLICK, new EventListener() {
121  @Override
122  public void onEvent(Event event) throws Exception {
123  ZkossUtils.confirmDeletion(null, new Command() {
124  @Override
125  public Object execute(Context context) {
126  EntityCollectionUtil.remove(requiredUsage.getTask().getRequiredUsages(), requiredUsage);
127  Events.postEvent(new Event(Events.ON_CHANGE, ZkossUtils.getParent(UsageBox.this, UsageTree.class)));
128  return null;
129  }
130  });
131  }
132  });
133  if(requiredUsage.getId() > 0) {
134  Button order = new Button(Application.getString("lNewOrder"));
135  hbox.appendChild(order);
136  order.addEventListener(Events.ON_CLICK, new EventListener() {
137  @Override
138  public void onEvent(Event event) throws Exception {
139  OrderItem oi = requiredUsage.startOrder();
140  if(!oi.isEmpty()) {
141  new ErpPU().saveObject(oi);
142  }
143  Events.postEvent(new Event(Events.ON_CHANGE, ZkossUtils.getParent(UsageBox.this, UsageTree.class)));
144  }
145  });
146  }
147  }
148  gl.addSpannedComponent(hbox, 4);
149  }
150 
151  public static void doEdit(RequiredUsage requiredUsage, Command command) {
152  Window w = (Window) Executions.createComponents("/WEB-INF/_zul/erp/task/editUsage.zul", null, null);
153  w.setMode(Window.MODAL);
154  Events.sendEvent(Events.ON_USER, w, requiredUsage);
155  Modal.doModal(w, command);
156  }
157 
158 }
159 
static String getString(String key)
static String getImage(String image)
Definition: Images.java:36
static int doModal(String file)
Definition: Modal.java:38
static void doEdit(RequiredUsage requiredUsage, Command command)
Definition: UsageBox.java:151
UsageBox(RequiredUsage requiredUsage)
Definition: UsageBox.java:50
void setColCaptions(String captions)
Definition: GridLayout.java:94
GridLayout addComponent(HtmlBasedComponent comp)
Rows getRows(boolean create)
GridLayout addSpannedComponent(HtmlBasedComponent comp, int cols)
GridLayout addCaption(String label)