BrightSide Workbench Full Report + Source Code
ValueItGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.zul.fieldit;
20 
21 import java.util.Collection;
22 import java.util.Date;
23 import org.turro.contacts.FieldIt;
24 import org.turro.contacts.ValueIt;
25 import org.turro.fieldit.AutoFillList;
26 import org.turro.fieldit.IValueItEntity;
27 import org.turro.fieldit.StringList;
28 import org.turro.fieldit.ValueItSet;
29 import org.turro.i18n.I_;
30 import org.turro.zkoss.input.DateboxShort;
31 import org.turro.zkoss.layout.GridLayout;
32 import org.zkoss.zk.ui.HtmlBasedComponent;
33 import org.zkoss.zk.ui.event.Event;
34 import org.zkoss.zk.ui.event.EventListener;
35 import org.zkoss.zk.ui.event.Events;
36 import org.zkoss.zul.Checkbox;
37 import org.zkoss.zul.Datebox;
38 import org.zkoss.zul.Doublebox;
39 import org.zkoss.zul.Longbox;
40 import org.zkoss.zul.Textbox;
41 
46 public class ValueItGrid extends GridLayout {
47 
48  private IValueItEntity entity;
49  private ValueItSet values;
50 
51  public void setEntity(IValueItEntity entity) {
52  this.entity = entity;
53  addFields();
54  }
55 
56  public Collection<ValueIt> getValues() {
57  return values;
58  }
59 
60  private void addFields() {
61  getChildren().clear();
62  setColumns("min,1");
63  values = entity.getValues();
64  for(ValueIt vi : values) {
65  addRow();
66  addCaption(I_.get(vi.getFieldIt().getName()));
67  addComponent(getEditor(vi));
68  }
69  }
70 
71  private HtmlBasedComponent getEditor(final ValueIt value) {
72  FieldIt fi = value.getFieldIt();
73  HtmlBasedComponent editor = null;
74  if(fi.getJavaClass().equals(String.class)) {
75  //final FieldValueCombobox comp = new FieldValueCombobox(fi);
76  final Textbox comp = new Textbox();
77  comp.setCols(50);
78  comp.setValue((String) value.getObjectValue());
79  //comp.setFieldValue((String) value.getObjectValue());
80  comp.addEventListener(Events.ON_CHANGE, new EventListener() {
81  @Override
82  public void onEvent(Event event) throws Exception {
83  //value.setObjectValue(comp.getFieldValue());
84  value.setObjectValue(comp.getValue());
85  Events.postEvent(ValueItGrid.this, new Event(Events.ON_CHANGE));
86  }
87  });
88  editor = comp;
89  } else if(fi.getJavaClass().equals(Long.class)) {
90  final Longbox comp = new Longbox();
91  comp.setValue((Long) value.getObjectValue());
92  comp.addEventListener(Events.ON_CHANGE, new EventListener() {
93  @Override
94  public void onEvent(Event event) throws Exception {
95  value.setObjectValue(comp.getValue());
96  Events.postEvent(ValueItGrid.this, new Event(Events.ON_CHANGE));
97  }
98  });
99  editor = comp;
100  } else if(fi.getJavaClass().equals(Double.class)) {
101  final Doublebox comp = new Doublebox();
102  comp.setValue((Double) value.getObjectValue());
103  comp.addEventListener(Events.ON_CHANGE, new EventListener() {
104  @Override
105  public void onEvent(Event event) throws Exception {
106  value.setObjectValue(comp.getValue());
107  Events.postEvent(ValueItGrid.this, new Event(Events.ON_CHANGE));
108  }
109  });
110  editor = comp;
111  } else if(fi.getJavaClass().equals(Date.class)) {
112  final Datebox comp = new DateboxShort();
113  comp.setValue((Date) value.getObjectValue());
114  comp.addEventListener(Events.ON_CHANGE, new EventListener() {
115  @Override
116  public void onEvent(Event event) throws Exception {
117  value.setObjectValue(comp.getValue());
118  Events.postEvent(ValueItGrid.this, new Event(Events.ON_CHANGE));
119  }
120  });
121  editor = comp;
122  } else if(fi.getJavaClass().equals(Boolean.class)) {
123  final Checkbox comp = new Checkbox();
124  Boolean b = (Boolean) value.getObjectValue();
125  comp.setChecked(b == null ? false : b);
126  comp.addEventListener(Events.ON_CHECK, new EventListener() {
127  @Override
128  public void onEvent(Event event) throws Exception {
129  value.setObjectValue(comp.isChecked());
130  Events.postEvent(ValueItGrid.this, new Event(Events.ON_CHANGE));
131  }
132  });
133  editor = comp;
134  } else if(fi.getJavaClass().equals(StringList.class)) {
135  final StringListListbox comp = new StringListListbox(fi);
136  comp.setObjectValue(value.getObjectString());
137  comp.setMold("select");
138  comp.addEventListener(Events.ON_SELECT, new EventListener() {
139  @Override
140  public void onEvent(Event event) throws Exception {
141  value.setObjectValue(comp.getObjectValue());
142  Events.postEvent(ValueItGrid.this, new Event(Events.ON_CHANGE));
143  }
144  });
145  editor = comp;
146  } else if(fi.getJavaClass().equals(AutoFillList.class)) {
147  final AutoFillCombobox comp = new AutoFillCombobox(fi);
148  comp.setObjectValue(value.getValue());
149  comp.addEventListener(Events.ON_SELECT, new EventListener() {
150  @Override
151  public void onEvent(Event event) throws Exception {
152  value.setObjectValue(comp.getObjectValue());
153  Events.postEvent(ValueItGrid.this, new Event(Events.ON_CHANGE));
154  }
155  });
156  comp.addEventListener(Events.ON_CHANGE, new EventListener() {
157  @Override
158  public void onEvent(Event event) throws Exception {
159  value.setObjectValue(comp.getObjectValue());
160  comp.saveValue(comp.getObjectValue());
161  Events.postEvent(ValueItGrid.this, new Event(Events.ON_CHANGE));
162  }
163  });
164  editor = comp;
165  }
166  return editor;
167  }
168 
169 }
static String get(String msg)
Definition: I_.java:41
GridLayout addComponent(HtmlBasedComponent comp)
GridLayout addCaption(String label)
void setEntity(IValueItEntity entity)
Collection< ValueIt > getValues()