BrightSide Workbench Full Report + Source Code
form/AddressGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 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.contacts.form;
20 
21 import org.turro.contacts.Address;
22 import org.turro.contacts.Contact;
23 import org.turro.i18n.I_;
24 import org.turro.jpa.entity.EntityCollections;
25 import org.turro.zkoss.grid.CollectionGrid;
26 import org.turro.zkoss.grid.EditableCell;
27 import org.zkoss.zk.ui.HtmlBasedComponent;
28 import org.zkoss.zk.ui.ext.AfterCompose;
29 import org.zkoss.zul.Row;
30 
35 public class AddressGrid extends CollectionGrid<Address> implements AfterCompose {
36 
37  private Contact contact;
38 
39  public void setContact(Contact contact) {
40  this.contact = contact;
41  setCollection(contact.getAddresses());
42  }
43 
44  @Override
45  protected boolean isValid(Address v) {
46  return !v.isEmpty();
47  }
48 
49  @Override
50  protected void initiateRow(Row row, Address value) {
51  if(value == null) {
52  value = new Address();
53  value.setContact(contact);
54  contact.getAddresses().add(value);
55  }
56  row.setValue(value);
57  }
58 
59  @Override
60  protected boolean deleteRow(Row row) {
61  Address a = (Address) row.getValue();
62  EntityCollections.entities(contact.getAddresses()).remove(a);
63  a.setContact(contact);
64  return true;
65  }
66 
67  @Override
68  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
69  if(editableCell.getCellIndex() == 0) {
70  Object value = getCellValue(editableCell);
72  cdc.setObjectValue((String) value);
73  return cdc;
74  }
75  return super.createEditor(editableCell);
76  }
77 
78  @Override
79  public void afterCompose() {
80  addColumns();
81  super.afterCompose();
82  }
83 
84  private void addColumns() {
85  addColumn(I_.get("Address"), String.class,
86  "description", null, 0, false, false).setWidth("200px");
87  addColumn(I_.get("Street"), String.class,
88  "street", null, 0, false, false).setHflex("3");
89  addColumn(I_.get("Zip code"), String.class,
90  "zipCode", null, 0, false, false).setWidth("150px");
91  addColumn(I_.get("City"), String.class,
92  "city", null, 0, false, false).setHflex("1");
93  addColumn(I_.get("Province"), String.class,
94  "province", null, 0, false, false).setHflex("1");
95  addColumn(I_.get("State"), String.class,
96  "state", null, 0, false, false).setHflex("1");
97  }
98 
99 }
void setContact(org.turro.contacts.Contact contact)
Definition: Address.java:170
Set< Address > getAddresses()
Definition: Contact.java:355
HtmlBasedComponent createEditor(EditableCell editableCell)
void initiateRow(Row row, Address value)
static String get(String msg)
Definition: I_.java:41
static EntityCollections entities(Collection values)
void setCollection(Collection< V > collection)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
Object getCellValue(EditableCell editableCell)