BrightSide Workbench Full Report + Source Code
CollectionListbox.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 org.turro.i18n.I_;
22 import org.turro.zkoss.label.LabelTypes;
23 import org.zkoss.zul.Listcell;
24 import org.zkoss.zul.Listitem;
25 import org.zkoss.zul.Vlayout;
26 
32 public abstract class CollectionListbox<V> extends GenericListbox<V> {
33 
34  public static final String ITEM_SEPARATOR = "|||",
35  ITEM_SEPARATOR_REGEX = "\\|\\|\\|",
36  SUBITEM_SEPARATOR = "%%%",
37  SUBITEM_SEPARATOR_REGEX = "\\%\\%\\%";
38 
39  private Collection<V> collection;
40  private boolean allowNull, nullAtBottom;
41  private String nullLabel = "None";
42 
43  public CollectionListbox() {
44  }
45 
46  public CollectionListbox(Collection<V> collection) {
47  this.collection = collection;
48  }
49 
50  public void updateCollection() {
51  clearItems();
52  setCollection(collection);
53  afterCompose();
54  }
55 
56  public void updateCollection(Collection<V> collection) {
57  clearItems();
58  setCollection(collection);
59  afterCompose();
60  }
61 
62  public boolean isAllowNull() {
63  return allowNull;
64  }
65 
66  public void setAllowNull(boolean allowNull) {
67  this.allowNull = allowNull;
68  }
69 
70  public Collection<V> getCollection() {
71  return collection;
72  }
73 
74  public void setCollection(Collection<V> collection) {
75  this.collection = collection;
76  }
77 
78  public boolean isNullAtBottom() {
79  return nullAtBottom;
80  }
81 
82  public void setNullAtBottom(boolean nullAtBottom) {
83  this.nullAtBottom = nullAtBottom;
84  }
85 
86  public String getNullLabel() {
87  return nullLabel;
88  }
89 
90  public void setNullLabel(String nullLabel) {
91  this.nullLabel = nullLabel;
92  }
93 
94  @Override
95  protected void populateList() {
96  //setSizedByContent(true);
97  clearItems();
98  if(collection == null) return;
99  if(allowNull && !nullAtBottom) {
100  Listitem li = new Listitem("<" + I_.get(nullLabel) + ">", null);
101  beforeAppend(li);
102  appendChild(li);
103  }
104  int cells = 1;
105  if(getListhead() != null && !getListhead().getChildren().isEmpty()) {
106  cells = getListhead().getChildren().size();
107  }
108  for(V v : collection) {
109  if(cells > 1) {
110  String sa[] = convertToString(v).split(ITEM_SEPARATOR_REGEX);
111  Listitem li = new Listitem();
112  li.setValue(v);
113  for(String s : sa) {
114  String ssa[] = s.split(SUBITEM_SEPARATOR_REGEX);
115  if(ssa.length > 1) {
116  Listcell cell = new Listcell(ssa[0]);
117  li.appendChild(cell);
118  Vlayout vbox = new Vlayout();
119  vbox.setStyle("padding:2px");
120  for(int i = 1; i < ssa.length; i++) {
121  vbox.appendChild(LabelTypes.getSoftLabel(ssa[i]));
122  }
123  cell.appendChild(vbox);
124  } else {
125  if(s != null && s.endsWith(SUBITEM_SEPARATOR)) {
126  s = s.replaceAll(SUBITEM_SEPARATOR_REGEX, "");
127  }
128  li.appendChild(new Listcell(s));
129  }
130  }
131  beforeAppend(li);
132  appendChild(li);
133  } else {
134  Listitem li = new Listitem(convertToString(v), v);
135  beforeAppend(li);
136  appendChild(li);
137  }
138  }
139  if(allowNull && nullAtBottom) {
140  Listitem li = new Listitem("<" + I_.get(nullLabel) + ">", null);
141  beforeAppend(li);
142  appendChild(li);
143  }
144  }
145 
146  @Override
147  protected boolean equals(V value, V obj) {
148  String v1 = value == null ? null : convertToString(value);
149  String v2 = obj == null ? null : convertToString(obj);
150  return (v1 == null && v2 == null) ||
151  (v1 != null && v1.equals(v2));
152  }
153 
154  protected abstract String convertToString(V v);
155 
156  protected void beforeAppend(Listitem li) { /* do nothing */ }
157 
158 }
static String get(String msg)
Definition: I_.java:41
CollectionListbox(Collection< V > collection)
void updateCollection(Collection< V > collection)
void setNullAtBottom(boolean nullAtBottom)
void setCollection(Collection< V > collection)
static Label getSoftLabel(String value)
Definition: LabelTypes.java:28