BrightSide Workbench Full Report + Source Code
contacts/src/main/java/org/turro/contacts/zul/fields/FieldValueGrid.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.fields;
19 
20 import java.util.Date;
21 import java.util.List;
22 import org.turro.contacts.Contact;
23 import org.turro.contacts.FieldDef;
24 import org.turro.contacts.FieldValue;
25 import org.turro.contacts.db.ContactsPU;
26 import org.turro.contacts.zul.contact.FieldValueCombobox;
27 import org.turro.elephant.db.WhereClause;
28 import org.turro.i18n.I_;
29 import org.turro.zkoss.input.DateboxShort;
30 import org.zkoss.zk.ui.event.Event;
31 import org.zkoss.zk.ui.event.EventListener;
32 import org.zkoss.zk.ui.event.Events;
33 import org.zkoss.zul.Checkbox;
34 import org.zkoss.zul.Datebox;
35 import org.zkoss.zul.Doublebox;
36 import org.zkoss.zul.Grid;
37 import org.zkoss.zul.Label;
38 import org.zkoss.zul.Longbox;
39 import org.zkoss.zul.Row;
40 import org.zkoss.zul.Rows;
41 
46 public class FieldValueGrid extends Grid {
47 
48  private Contact contact;
49  private Rows rows;
50  private String tabKey;
51 
52  public FieldValueGrid(String tabKey) {
53  this.tabKey = tabKey;
54  rows = new Rows();
55  appendChild(rows);
56  }
57 
58  public void setContact(Contact contact) {
59  this.contact = contact;
60  rows.getChildren().clear();
61  if(contact != null && tabKey != null) {
62  addRows();
63  }
64  }
65 
66  public void addRows() {
67  for(final FieldDef fd : getTabFields()) {
68  final Row row = new Row();
69  row.setValue(fd);
70  rows.appendChild(row);
71 
72  row.appendChild(new Label(I_.get(fd.getLabelKey())));
73 
74  if(fd.getJavaClass().equals(String.class)) {
75  final FieldValueCombobox value = new FieldValueCombobox(fd);
76  value.setCols(50);
77  value.setFieldValue((String) getFieldValue(contact, fd));
78  value.addEventListener(Events.ON_CHANGE, new EventListener() {
79  @Override
80  public void onEvent(Event event) throws Exception {
81  setFieldValue(contact, fd, value.getFieldValue());
82  }
83  });
84  row.appendChild(value);
85  } else if(fd.getJavaClass().equals(Long.class)) {
86  final Longbox value = new Longbox();
87  value.setValue((Long) getFieldValue(contact, fd));
88  value.addEventListener(Events.ON_CHANGE, new EventListener() {
89  @Override
90  public void onEvent(Event event) throws Exception {
91  setFieldValue(contact, fd, value.getValue());
92  }
93  });
94  row.appendChild(value);
95  } else if(fd.getJavaClass().equals(Double.class)) {
96  final Doublebox value = new Doublebox();
97  value.setValue((Double) getFieldValue(contact, fd));
98  value.addEventListener(Events.ON_CHANGE, new EventListener() {
99  @Override
100  public void onEvent(Event event) throws Exception {
101  setFieldValue(contact, fd, value.getValue());
102  }
103  });
104  row.appendChild(value);
105  } else if(fd.getJavaClass().equals(Date.class)) {
106  final Datebox value = new DateboxShort();
107  value.setValue((Date) getFieldValue(contact, fd));
108  value.addEventListener(Events.ON_CHANGE, new EventListener() {
109  @Override
110  public void onEvent(Event event) throws Exception {
111  setFieldValue(contact, fd, value.getValue());
112  }
113  });
114  row.appendChild(value);
115  } else if(fd.getJavaClass().equals(Boolean.class)) {
116  final Checkbox value = new Checkbox();
117  Boolean b = (Boolean) getFieldValue(contact, fd);
118  value.setChecked(b == null ? false : b);
119  value.addEventListener(Events.ON_CHECK, new EventListener() {
120  @Override
121  public void onEvent(Event event) throws Exception {
122  setFieldValue(contact, fd, value.isChecked());
123  }
124  });
125  row.appendChild(value);
126  }
127  }
128  }
129 
130  private Object getFieldValue(Contact contact, FieldDef fd) {
131  for(FieldValue fv : contact.getFieldValues()) {
132  if(fv.getFieldDef().getId().equals(fd.getId())) {
133  return fv.getObjectValue();
134  }
135  }
136  return null;
137  }
138 
139  private void setFieldValue(Contact contact, FieldDef fd, Object value) {
140  for(FieldValue fv : contact.getFieldValues()) {
141  if(fv.getFieldDef().getId().equals(fd.getId())) {
142  fv.setObjectValue(value);
143  return;
144  }
145  }
146  FieldValue fv = new FieldValue();
147  fv.setContact(contact);
148  fv.setFieldDef(fd);
149  fv.setObjectValue(value);
150  contact.getFieldValues().add(fv);
151  }
152 
153  private List<FieldDef> getTabFields() {
154  WhereClause wc = new WhereClause();
155  wc.addClause("select distinct fieldDef");
156  wc.addClause("from FieldDef as fieldDef");
157  wc.addClause("where fieldDef.tabKey = :tabKey");
158  wc.addNamedValue("tabKey", tabKey);
159  return (List<FieldDef>) new ContactsPU().getResultList(wc);
160  }
161 }
162 
Set< FieldValue > getFieldValues()
Definition: Contact.java:427
static String get(String msg)
Definition: I_.java:41