BrightSide Workbench Full Report + Source Code
CollectionCheckbox.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 org.turro.zkoss.layout.VboxH;
24 import org.zkoss.zk.ui.Component;
25 import org.zkoss.zk.ui.event.Event;
26 import org.zkoss.zk.ui.event.EventListener;
27 import org.zkoss.zk.ui.event.Events;
28 import org.zkoss.zk.ui.ext.AfterCompose;
29 import org.zkoss.zul.Checkbox;
30 
35 public abstract class CollectionCheckbox<V> extends VboxH implements AfterCompose {
36 
37  private Collection<V> internalValues;
38  protected boolean populated = false;
39  private Collection<V> collection;
40 
41  public CollectionCheckbox() {
42  super(0);
43  }
44 
45  public CollectionCheckbox(Collection<V> collection) {
46  super(0);
47  this.collection = collection;
48  }
49 
50  public void updateCollection() {
51  clear();
52  setCollection(collection);
53  afterCompose();
54  }
55 
56  public void updateCollection(Collection<V> collection) {
57  clear();
58  setCollection(collection);
59  afterCompose();
60  }
61 
62  public Collection<V> getCollection() {
63  return collection;
64  }
65 
66  public void setCollection(Collection<V> collection) {
67  this.collection = collection;
68  }
69 
70  protected void populateList() {
71  //setSizedByContent(true);
72  clear();
73  if(collection == null) return;
74  for(V v : collection) {
75  Checkbox cb = new Checkbox(convertToString(v));
76  cb.setAttribute("obj", v);
77  addComponent(cb);
78  cb.addEventListener(Events.ON_CHECK, new EventListener() {
79  @Override
80  public void onEvent(Event event) throws Exception {
81  Events.postEvent(new Event(Events.ON_CHECK, CollectionCheckbox.this));
82  }
83  });
84  }
85  }
86 
87  protected boolean equals(V value, V obj) {
88  String v1 = value == null ? null : convertToString(value);
89  String v2 = obj == null ? null : convertToString(obj);
90  return (v1 == null && v2 == null) ||
91  (v1 != null && v1.equals(v2));
92  }
93 
94  protected abstract String convertToString(V v);
95 
96  public void setObjectValues(Collection<V> collection) {
97  if(!populated) {
98  internalValues = collection;
99  }
100  for(Component c : (List<Component>) getRealChildren()) {
101  if(c instanceof Checkbox) {
102  Checkbox cb = (Checkbox) c;
103  cb.setChecked(false);
104  for(V v : collection) {
105  if(cb.getLabel().equals(convertToString(v))) {
106  cb.setChecked(true);
107  break;
108  }
109  }
110  }
111  }
112  }
113 
114  public Collection<V> getObjectValues() {
115  if(!populated) {
116  return internalValues;
117  }
118  HashSet<V> set = new HashSet<V>();
119  for(Component c : (List<Component>) getRealChildren()) {
120  if(c instanceof Checkbox) {
121  Checkbox cb = (Checkbox) c;
122  if(cb.isChecked()) {
123  set.add((V) cb.getAttribute("obj"));
124  }
125  }
126  }
127  return set;
128  }
129 
130  @Override
131  public void afterCompose() {
132  if(!populated) {
133  populateList();
134  populated = true;
135  if (internalValues != null) {
136  setObjectValues(internalValues);
137  internalValues = null;
138  }
139  }
140  }
141 
142  @Override
143  public void clear() {
144  super.clear();
145  populated = false;
146  }
147 
148 }
void setCollection(Collection< V > collection)
CollectionCheckbox(Collection< V > collection)
void setObjectValues(Collection< V > collection)
void updateCollection(Collection< V > collection)
void addComponent(Component comp)
Definition: VboxH.java:47