BrightSide Workbench Full Report + Source Code
CustomerOwnerGrid.java
Go to the documentation of this file.
1 package org.turro.crm.zul.customer;
2 
3 import java.util.Date;
4 import org.turro.crm.entity.Customer;
5 import org.turro.crm.entity.CustomerOwner;
6 import org.turro.crm.entity.FormulaType;
7 import org.turro.crm.zul.vendor.FormulaListbox;
8 import org.turro.crm.zul.vendor.VendorCombobox;
9 import org.turro.elephant.util.Messages;
10 import org.turro.i18n.I_;
11 import org.zkoss.zk.ui.event.Event;
12 import org.zkoss.zk.ui.event.EventListener;
13 import org.zkoss.zk.ui.event.Events;
14 import org.zkoss.zul.Grid;
15 import org.zkoss.zul.Hbox;
16 import org.zkoss.zul.Image;
17 import org.zkoss.zul.Label;
18 import org.zkoss.zul.Row;
19 import org.zkoss.zul.Rows;
20 import org.zkoss.zul.Toolbar;
21 import org.zkoss.zul.Toolbarbutton;
22 import org.zkoss.zul.Vbox;
23 
28 public class CustomerOwnerGrid extends Grid {
29 
30  private Customer customer;
31  private boolean editable;
32  private Rows rows;
33  private Toolbar toolbar;
34  private Toolbarbutton addButton;
35 
36  public CustomerOwnerGrid() {
37  rows = new Rows();
38  appendChild(rows);
39  }
40 
41  public void setEditable(boolean editable) {
42  this.editable = editable;
43  if(customer != null) {
44  setCustomer(customer);
45  }
46  updateButtons();
47  }
48 
49  public void setCustomer(Customer customer) {
50  this.customer = customer;
51  rows.getChildren().clear();
52  if(customer != null) {
53  addRows();
54  }
55  }
56 
57  public void setAddToolbar(boolean addToolbar) {
58  if(addToolbar) {
59  toolbar = new Toolbar();
60  getParent().appendChild(toolbar);
61  addToolbarButtons();
62  }
63  }
64 
65  public void addRows() {
66  for(final CustomerOwner co : customer.getOwnedBy()) {
67  final Row row = new Row();
68  row.setValue(co);
69  rows.appendChild(row);
70 
71  Vbox vbox = new Vbox();
72  row.appendChild(vbox);
73 
74  if(editable) {
75  Hbox hbox = new Hbox();
76  vbox.appendChild(hbox);
77 
78  final VendorCombobox vendor = new VendorCombobox();
79  vendor.setObjectValue(co.getVendor());
80  vendor.addEventListener(Events.ON_CHANGE, new EventListener() {
81  @Override
82  public void onEvent(Event event) throws Exception {
83  co.setVendor(vendor.getObjectValue());
84  co.setOwningDate(new Date());
85  }
86  });
87  hbox.appendChild(vendor);
88 
89  final Image img = new Image("/_zul/images/delete.png");
90  img.setStyle("cursor:pointer");
91  img.addEventListener(Events.ON_CLICK, new EventListener() {
92  @Override
93  public void onEvent(Event event) throws Exception {
94  Messages.confirmDeletion().show(() -> {
95  customer.getOwnedBy().remove(co);
96  setCustomer(customer);
97  });
98  }
99  });
100  hbox.appendChild(img);
101 
102  final FormulaListbox comission = new FormulaListbox();
104  comission.setObjectValue(co.getComission());
105  comission.addEventListener(Events.ON_SELECT, new EventListener() {
106  @Override
107  public void onEvent(Event event) throws Exception {
108  co.setComission(comission.getObjectValue());
109  }
110  });
111  vbox.appendChild(comission);
112  comission.afterCompose();
113 
114  final FormulaListbox alert = new FormulaListbox();
116  alert.setObjectValue(co.getAlert());
117  alert.addEventListener(Events.ON_SELECT, new EventListener() {
118  @Override
119  public void onEvent(Event event) throws Exception {
120  co.setAlert(alert.getObjectValue());
121  }
122  });
123  vbox.appendChild(alert);
124  alert.afterCompose();
125 
126  } else {
127  Label label = new Label(co.getVendor() != null ? co.getVendor().getName() : "***");
128  row.appendChild(label);
129 
130  label = new Label(co.getComission().getName());
131  row.appendChild(label);
132 
133  label = new Label(co.getAlert().getName());
134  row.appendChild(label);
135  }
136  }
137  }
138 
139  private void addToolbarButtons() {
140  addButton = new Toolbarbutton(
141  I_.get("Add"),
142  "/_zul/images/new.png"
143  );
144  addButton.addEventListener(Events.ON_CLICK, new EventListener() {
145  @Override
146  public void onEvent(Event event) throws Exception {
147  CustomerOwner co = new CustomerOwner();
148  co.setCustomer(customer);
149  customer.getOwnedBy().add(co);
150  setCustomer(customer);
151  }
152  });
153  toolbar.appendChild(addButton);
154 
155  updateButtons();
156  }
157 
158  private void updateButtons() {
159  if(toolbar != null) {
160  addButton.setVisible(editable);
161  }
162  }
163 }
164 
void setCustomer(Customer customer)
Set< CustomerOwner > getOwnedBy()
Definition: Customer.java:88
void setFormulaType(FormulaType formulaType)
static Messages confirmDeletion()
Definition: Messages.java:87
static String get(String msg)
Definition: I_.java:41