BrightSide Workbench Full Report + Source Code
zul/contact/AddressGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.contacts.zul.contact;
19 
20 import java.math.BigDecimal;
21 import org.turro.auth.Authentication;
22 import org.turro.contacts.Address;
23 import org.turro.contacts.Contact;
24 import org.turro.elephant.context.Application;
25 import org.turro.elephant.util.Messages;
26 import org.turro.i18n.I_;
27 import org.turro.zkoss.label.LabelTypes;
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.*;
33 
38 public class AddressGrid extends Grid {
39 
40  private Contact contact;
41  private Rows rows;
42  private Toolbar toolbar;
43  private Toolbarbutton addButton;
44 
45  public AddressGrid() {
46  rows = new Rows();
47  appendChild(rows);
48  }
49 
50  public void setContact(Contact contact) {
51  this.contact = contact;
52  rows.getChildren().clear();
53  if(contact != null) {
54  addRows();
55  }
56  }
57 
58  public void setAddToolbar(boolean addToolbar) {
59  if(addToolbar) {
60  toolbar = new Toolbar();
61  getParent().appendChild(toolbar);
62  addToolbarButtons();
63  }
64  }
65 
66  public void addRows() {
67  for(Address a : contact.getAddresses()) {
68  Row row = new Row();
69  row.setValue(a);
70  rows.appendChild(row);
71 
72  addRow(a, row);
73  }
74  }
75 
76  private void addRow(final Address a, final Row parentRow) {
77  GridLayout grid = new GridLayout("min,1");
78  parentRow.appendChild(grid);
79 
80  grid.addRow().addCaption(I_.get("Address"));
81 
82  Hlayout hbox = new Hlayout();
83  hbox.setSclass("z-valign-middle");
84  hbox.setValign("middle");
85  hbox.setSpacing("5px");
86  grid.addComponent(hbox);
87 
88  final AddressNameCombobox name = new AddressNameCombobox();
90  name.addEventListener(Events.ON_CHANGE, new EventListener() {
91  @Override
92  public void onEvent(Event event) throws Exception {
94  }
95  });
96  hbox.appendChild(name);
97 
98  if(Application.getApplication().isInRole("contact-address:delete")) {
99  Image img = new Image("/_zul/images/edit-delete.png");
100  img.setStyle("cursor:pointer");
101  img.addEventListener(Events.ON_CLICK, new EventListener() {
102  @Override
103  public void onEvent(Event event) throws Exception {
104  Messages.confirmDeletion().show(() -> {
105  contact.getAddresses().remove(a);
106  a.setContact(null);
107  parentRow.detach();
108  });
109  }
110  });
111  hbox.appendChild(img);
112  }
113 
114  grid.addRow().addCaption(I_.get("Street"));
115 
116  final Textbox street = new Textbox();
117  street.setWidth("100%");
118  street.setText(a.getStreet());
119  street.addEventListener(Events.ON_CHANGE, new EventListener() {
120  @Override
121  public void onEvent(Event event) throws Exception {
122  a.setStreet(street.getText());
123  }
124  });
125  grid.addComponent(street);
126 
127  grid.addRow().addCaption(I_.get("Zip code"));
128 
129  hbox = new Hlayout();
130  hbox.setSclass("z-valign-middle");
131  hbox.setValign("middle");
132  hbox.setSpacing("5px");
133  grid.addComponent(hbox);
134 
135  final Textbox zipCode = new Textbox();
136  zipCode.setCols(5);
137  zipCode.setText(a.getZipCode());
138  zipCode.addEventListener(Events.ON_CHANGE, new EventListener() {
139  @Override
140  public void onEvent(Event event) throws Exception {
141  a.setZipCode(zipCode.getText());
142  }
143  });
144  hbox.appendChild(zipCode);
145 
146  hbox.appendChild(new Space());
147  hbox.appendChild(LabelTypes.getGridCaptionLabel(I_.get("City")));
148 
149  final Textbox city = new Textbox();
150  city.setCols(35);
151  city.setText(a.getCity());
152  city.addEventListener(Events.ON_CHANGE, new EventListener() {
153  @Override
154  public void onEvent(Event event) throws Exception {
155  a.setCity(city.getText());
156  }
157  });
158  hbox.appendChild(city);
159 
160  hbox.appendChild(new Space());
161  hbox.appendChild(LabelTypes.getGridCaptionLabel(I_.get("Province")));
162 
163  final Textbox province = new Textbox();
164  province.setCols(20);
165  province.setText(a.getProvince());
166  province.addEventListener(Events.ON_CHANGE, new EventListener() {
167  @Override
168  public void onEvent(Event event) throws Exception {
169  a.setProvince(province.getText());
170  }
171  });
172  hbox.appendChild(province);
173 
174  hbox.appendChild(new Space());
175  hbox.appendChild(LabelTypes.getGridCaptionLabel(I_.get("State")));
176 
177  final Textbox state = new Textbox();
178  state.setCols(15);
179  state.setText(a.getState());
180  state.addEventListener(Events.ON_CHANGE, new EventListener() {
181  @Override
182  public void onEvent(Event event) throws Exception {
183  a.setState(state.getText());
184  }
185  });
186  hbox.appendChild(state);
187 
188  grid.addRow().addCaption(I_.get("Longitude"));
189 
190  hbox = new Hlayout();
191  hbox.setSclass("z-valign-middle");
192  hbox.setValign("middle");
193  hbox.setSpacing("5px");
194  grid.addComponent(hbox);
195 
196  final Decimalbox longitude = new Decimalbox(BigDecimal.valueOf(a.getLongitude()));
197  longitude.addEventListener(Events.ON_CHANGE, new EventListener() {
198  @Override
199  public void onEvent(Event event) throws Exception {
200  a.setLongitude(longitude.getValue().doubleValue());
201  }
202  });
203  hbox.appendChild(longitude);
204 
205  hbox.appendChild(new Space());
206  hbox.appendChild(LabelTypes.getGridCaptionLabel(I_.get("Latitude")));
207 
208  final Decimalbox latitude = new Decimalbox(BigDecimal.valueOf(a.getLatitude()));
209  latitude.addEventListener(Events.ON_CHANGE, new EventListener() {
210  @Override
211  public void onEvent(Event event) throws Exception {
212  a.setLatitude(latitude.getValue().doubleValue());
213  }
214  });
215  hbox.appendChild(latitude);
216 
217 // if(ReflectionUtil.classCheck("org.zkoss.gmaps.Gmaps") != null) {
218 //
219 // }
220  }
221 
222 
223  private void addToolbarButtons() {
224  addButton = new Toolbarbutton(
225  I_.get("Add"),
226  "/_zul/images/new.png"
227  );
228  addButton.addEventListener(Events.ON_CLICK, new EventListener() {
229 
230  @Override
231  public void onEvent(Event event) throws Exception {
232  Address a = new Address();
233  a.setContact(contact);
234  a.setOwner(Authentication.getIContact().getId());
235  contact.getAddresses().add(a);
236  setContact(contact);
237  }
238 
239  });
240  toolbar.appendChild(addButton);
241  }
242 
243 }
void setProvince(String province)
Definition: Address.java:130
void setState(String state)
Definition: Address.java:138
void setLatitude(double latitude)
Definition: Address.java:154
void setZipCode(String zipCode)
Definition: Address.java:114
void setCity(String city)
Definition: Address.java:122
void setStreet(String street)
Definition: Address.java:106
void setDescription(String description)
Definition: Address.java:146
void setContact(org.turro.contacts.Contact contact)
Definition: Address.java:170
void setLongitude(double longitude)
Definition: Address.java:162
Set< Address > getAddresses()
Definition: Contact.java:355
static String get(String msg)
Definition: I_.java:41
GridLayout addComponent(HtmlBasedComponent comp)
GridLayout addCaption(String label)