BrightSide Workbench Full Report + Source Code
ContactTypeBandbox.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 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.contacts.model;
20 
21 import java.util.HashSet;
22 import java.util.Set;
23 import org.turro.contacts.content.DirectoryType;
24 import org.turro.elephant.util.Images;
25 import org.turro.i18n.I_;
26 import org.turro.util.PhraseBuilder;
27 import org.zkoss.zk.ui.event.Event;
28 import org.zkoss.zk.ui.event.EventListener;
29 import org.zkoss.zk.ui.event.Events;
30 import org.zkoss.zk.ui.event.SelectEvent;
31 import org.zkoss.zul.Bandbox;
32 import org.zkoss.zul.Bandpopup;
33 import org.zkoss.zul.Listbox;
34 import org.zkoss.zul.Listhead;
35 import org.zkoss.zul.Listheader;
36 import org.zkoss.zul.Listitem;
37 
42 public class ContactTypeBandbox extends Bandbox {
43 
44  private Set<DirectoryType> typeSet = new HashSet<DirectoryType>();
45  private Listbox types;
46 
47  public ContactTypeBandbox() {
48  setReadonly(true);
49  addPopup();
50  }
51 
52  public Set<DirectoryType> getTypeSet() {
53  return typeSet;
54  }
55 
56  public void setTypeSet(Set<DirectoryType> typeSet) {
57  this.typeSet = typeSet;
58  updateCheckMarks();
59  updateText();
60  }
61 
62  private void addPopup() {
63  Bandpopup popup = new Bandpopup();
64  appendChild(popup);
65 
66  types = new Listbox();
67  types.setWidth("300px");
68  types.setMultiple(true);
69  types.setCheckmark(true);
70  Listhead lh = new Listhead();
71  lh.appendChild(new Listheader(I_.get("Type")));
72  types.appendChild(lh);
73  types.addEventListener(Events.ON_SELECT, new EventListener() {
74  @Override
75  public void onEvent(Event event) throws Exception {
76  Set<Listitem> selected = ((SelectEvent) event).getSelectedItems();
77  if(selected != null) {
78  for(Listitem li : selected) {
79  typeSet.add((DirectoryType) li.getValue());
80  }
81  }
82  Set<Listitem> unselected = ((SelectEvent) event).getUnselectedItems();
83  if(unselected != null) {
84  for(Listitem li : unselected) {
85  typeSet.remove((DirectoryType) li.getValue());
86  }
87  }
88  updateText();
89  }
90  });
91  popup.appendChild(types);
92 
93  for(DirectoryType is : DirectoryType.values()) {
94  Listitem li = new Listitem(I_.byKey(is.toString()));
95  li.setImage(Images.getImage(is.getImage()));
96  li.setValue(is);
97  types.appendChild(li);
98  }
99  }
100 
101  private void updateCheckMarks() {
102  for(Object obj : types.getChildren()) {
103  if(obj instanceof Listitem) {
104  Listitem li = (Listitem) obj;
105  li.setSelected(typeSet.contains(li.getValue()));
106  }
107  }
108  }
109 
110 
111  private void updateText() {
112  PhraseBuilder pb = new PhraseBuilder();
113  for(Object obj : types.getChildren()) {
114  if(obj instanceof Listitem) {
115  Listitem li = (Listitem) obj;
116  if(li.isSelected()) {
117  pb.addWord(I_.byKey(li.getValue().toString()));
118  pb.addPendingSeparator(",");
119  }
120  }
121  }
122  Events.postEvent(new Event(Events.ON_CHANGE, ContactTypeBandbox.this));
123  setText(pb.toString());
124  }
125 }
126 
127 
void setTypeSet(Set< DirectoryType > typeSet)
static String get(String msg)
Definition: I_.java:41