BrightSide Workbench Full Report + Source Code
GenericListbox.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.zkoss.input;
19 
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24 import org.zkoss.zk.ui.ext.AfterCompose;
25 import org.zkoss.zul.Listbox;
26 import org.zkoss.zul.Listhead;
27 import org.zkoss.zul.Listheader;
28 import org.zkoss.zul.Listitem;
29 
34 public abstract class GenericListbox<V> extends Listbox implements AfterCompose {
35 
36  private boolean selectFirst = true;
37  private V internalValue;
38  private Collection<V> internalValues;
39  protected boolean populated = false;
40 
41  public GenericListbox() {
42  }
43 
44  @Override
45  public void setDisabled(boolean value) {
46  super.setDisabled(value);
47  for(Listitem li : getItems()) {
48  li.setDisabled(value);
49  }
50  }
51 
52  @Override
53  public void afterCompose() {
54  if(!populated) {
55  populateList();
56  populated = true;
57  if(internalValue != null) {
58  setObjectValue(internalValue);
59  internalValue = null;
60  } else if (internalValues != null) {
61  setObjectValues(internalValues);
62  internalValues = null;
63  } else {
64  setObjectValue(null);
65  }
66  }
67  }
68 
69  public void clearItems() {
70  getItems().clear();
71  populated = false;
72  }
73 
74  public void setObjectValue(V value) {
75  if(!populated) {
76  internalValue = value;
77  }
78  if(value == null && selectFirst && getItemCount() > 0) {
79  setSelectedIndex(0);
80  } else {
81  for(Object item : getItems()) {
82  if(item instanceof Listitem) {
83  if(equals((V) ((Listitem) item).getValue(), (V) value)) {
84  setSelectedItem((Listitem) item);
85  }
86  }
87  }
88  }
89  }
90 
91  public void setObjectValues(Collection<V> collection) {
92  if(!populated) {
93  internalValues = collection;
94  }
95  setSelectedItem(null);
96  setSelectedItems(new ValueListitemAdapter<V>(this, collection) {
97  @Override
98  protected boolean equals(V value, V obj) {
99  return GenericListbox.this.equals(value, obj);
100  }
101  });
102  }
103 
104  public V getObjectValue() {
105  if(!populated) {
106  return internalValue;
107  }
108  Listitem li = getSelectedItem();
109  if(li != null) {
110  return (V) li.getValue();
111  }
112  return null;
113  }
114 
115  public Collection<V> getObjectValues() {
116  if(!populated) {
117  return internalValues;
118  }
119  return new ListitemValueAdapter<V>(getSelectedItems(), true);
120  }
121 
122  public Collection<V> getDeselectedObjectValues() {
123  if(!populated) {
124  return internalValues;
125  }
126  return new ListitemValueAdapter<V>(getDeselectedItems(), false);
127  }
128 
129  public boolean isSelectFirst() {
130  return selectFirst;
131  }
132 
133  public void setSelectFirst(boolean selectFirst) {
134  this.selectFirst = selectFirst;
135  }
136 
137  public Set getDeselectedItems() {
138  Set set = new HashSet();
139  for(Listitem li : (List<Listitem>) getItems()) {
140  if(!li.isSelected()) {
141  set.add(li);
142  }
143  }
144  return set;
145  }
146 
147  public void sort() {
148  Collection c = getHeads();
149  if(c == null || c.isEmpty()) {
150  Listhead lh = new Listhead();
151  Listheader lhr = new Listheader();
152  lhr.setSort("auto");
153  lh.appendChild(lhr);
154  appendChild(lh);
155  c = getHeads();
156  lh.setVisible(false);
157  }
158  for(Object h : c) {
159  if(h instanceof Listhead) {
160  if(((Listhead) h).getFirstChild() instanceof Listheader) {
161  ((Listheader) ((Listhead) h).getFirstChild()).sort(true);
162  break;
163  }
164  }
165  }
166  }
167 
168  protected boolean equals(V value, V obj) {
169  return (value == null && obj == null) ||
170  (value != null && value.equals(obj));
171  }
172 
173  protected abstract void populateList();
174 
175  /* Related entity (zul attribute) */
176 
177  private Object relatedEntity;
178 
179  public Object getRelatedEntity() {
180  return relatedEntity;
181  }
182 
183  public void setRelatedEntity(Object relatedEntity) {
184  this.relatedEntity = relatedEntity;
185  }
186 
187 }
void setObjectValues(Collection< V > collection)
void setSelectFirst(boolean selectFirst)
void setRelatedEntity(Object relatedEntity)