BrightSide Workbench Full Report + Source Code
GenericCombobox.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2017 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.zkoss.input;
20 
21 import java.lang.reflect.ParameterizedType;
22 import java.util.LinkedList;
23 import org.turro.reflection.Reflections;
24 import org.turro.util.CompareUtil;
25 import org.zkoss.zul.Combobox;
26 import org.zkoss.zul.Comboitem;
27 import org.zkoss.zul.ComboitemRenderer;
28 import org.zkoss.zul.Constraint;
29 
35 public abstract class GenericCombobox<V> extends Combobox implements ComboitemRenderer<Object> {
36 
37  private boolean allowNotInModelValues = true;
38  private V currentValue;
39 
40  public GenericCombobox() {
41  initComponent();
42  }
43 
44  public V getObjectValue() {
45  Class<V> genericClass = (Class<V>) ((ParameterizedType) getClass()
46  .getGenericSuperclass()).getActualTypeArguments()[0];
47  V value = null;
48  if(Reflections.of(genericClass).canCast(String.class) && allowNotInModelValues) {
49  // if String allow new values
50  value = (V) getText();
51  }
52  Comboitem ci = getSelectedItem();
53  if(ci != null) {
54  value = ci.getValue();
55  }
56  if(value == null && allowNotInModelValues && currentValue != null) {
57  if(getText().equals(getTextFromObject(currentValue))) {
58  value = currentValue;
59  }
60  }
61  return value;
62  }
63 
64  public void setObjectValue(V value) {
65  Constraint c = getConstraint();
66  setConstraint((Constraint) null);
67  currentValue = value;
68  if(!selectObjectItem(value)) {
69  setText(value != null ? getTextFromObject(value) : null);
70  }
71  setConstraint(c);
72  }
73 
74  public boolean isAllowNotInModelValues() {
75  return allowNotInModelValues;
76  }
77 
78  public void setAllowNotInModelValues(boolean allowNotInModelValues) {
79  this.allowNotInModelValues = allowNotInModelValues;
80  }
81 
82  public void refreshModel() {
83  Constraint c = getConstraint();
84  setConstraint((Constraint) null);
85  createModel();
86  setConstraint(c);
87  }
88 
89  private void createModel() {
90  GenericComboModel ccm = new GenericComboModel(new Object[0]) {
91  @Override
92  public void populateList(String value, LinkedList list, int nRows) {
93  GenericCombobox.this.populateList(value, list, nRows);
94  }
95  };
96  setModel(ccm);
97  }
98 
99  public abstract void populateList(String value, LinkedList list, int nRows);
100  public abstract String getTextFromObject(V value);
101 
102  private void initComponent() {
103  setItemRenderer(this);
104  createModel();
105  }
106 
107  private boolean selectObjectItem(V value) {
108  if(value != null) {
109  String str = getTextFromObject(value);
110  for(Comboitem item : getItems()) {
111  if(CompareUtil.compare(str, getTextFromObject(item.getValue())) == 0) {
112  setSelectedItem(item);
113  return true;
114  }
115  }
116  }
117  return false;
118  }
119 
120  /* Related entity (zul attribute) */
121 
122  private Object relatedEntity;
123 
124  public Object getRelatedEntity() {
125  return relatedEntity;
126  }
127 
128  public void setRelatedEntity(Object relatedEntity) {
129  this.relatedEntity = relatedEntity;
130  }
131 
132  /* ComboitemRenderer */
133 
134  @Override
135  public void render(Comboitem item, Object data, int index) throws Exception {
136  item.setLabel(getTextFromObject((V) data));
137  item.setValue(data);
138  }
139 
140 }
abstract String getTextFromObject(V value)
void render(Comboitem item, Object data, int index)
void setRelatedEntity(Object relatedEntity)
void setAllowNotInModelValues(boolean allowNotInModelValues)
abstract void populateList(String value, LinkedList list, int nRows)