BrightSide Workbench Full Report + Source Code
IssueRoleCtrl.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.dossier.zul.issue;
19 
20 import java.util.HashSet;
21 import java.util.Set;
22 import org.turro.dossier.entity.IssueParticipantRole;
23 import org.turro.i18n.I_;
24 import org.zkoss.lang.Strings;
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.event.SelectEvent;
29 import org.zkoss.zul.Bandbox;
30 import org.zkoss.zul.Bandpopup;
31 import org.zkoss.zul.Listbox;
32 import org.zkoss.zul.Listhead;
33 import org.zkoss.zul.Listheader;
34 import org.zkoss.zul.Listitem;
35 
40 public class IssueRoleCtrl extends Bandbox {
41 
42  private Set<IssueParticipantRole> roleSet = new HashSet<>();
43  private Listbox role;
44 
45  public IssueRoleCtrl() {
46  setReadonly(true);
47  addPopup();
48  }
49 
50  public Set<IssueParticipantRole> getRoleSet() {
51  return roleSet;
52  }
53 
54  public void setRoleSet(Set<IssueParticipantRole> roleSet) {
55  this.roleSet = roleSet;
56  updateCheckMarks();
57  updateText();
58  }
59 
60  private void addPopup() {
61  Bandpopup popup = new Bandpopup();
62  appendChild(popup);
63 
64  role = new Listbox();
65  role.setWidth("100%");
66  role.setMultiple(true);
67  role.setCheckmark(true);
68  Listhead lh = new Listhead();
69  lh.appendChild(new Listheader(I_.get("Role")));
70  role.appendChild(lh);
71  role.addEventListener(Events.ON_SELECT, new EventListener() {
72  @Override
73  public void onEvent(Event event) throws Exception {
74  Set<Listitem> selected = ((SelectEvent) event).getSelectedItems();
75  if(selected != null) {
76  for(Listitem li : selected) {
77  roleSet.add((IssueParticipantRole) li.getValue());
78  }
79  }
80  Set<Listitem> unselected = ((SelectEvent) event).getUnselectedItems();
81  if(unselected != null) {
82  for(Listitem li : unselected) {
83  roleSet.remove((IssueParticipantRole) li.getValue());
84  }
85  }
86  updateText();
87  }
88  });
89 
90  for(IssueParticipantRole ipr : IssueParticipantRole.values()) {
91  Listitem li = new Listitem(I_.byKey(ipr.toString()));
92  li.setValue(ipr);
93  role.appendChild(li);
94  }
95 
96  popup.appendChild(role);
97  }
98 
99  private void updateCheckMarks() {
100  for(Object obj : role.getChildren()) {
101  if(obj instanceof Listitem) {
102  Listitem li = (Listitem) obj;
103  li.setSelected(roleSet.contains(li.getValue()));
104  }
105  }
106  }
107 
108  private void updateText() {
109  String newText = "";
110  for(Object obj : role.getChildren()) {
111  if(obj instanceof Listitem) {
112  Listitem li = (Listitem) obj;
113  newText += (Strings.isEmpty(newText) ? "" : ",") +
114  (li.isSelected() ? I_.byKey(li.getValue().toString()) : "");
115  }
116  }
117  Events.postEvent(new Event(Events.ON_CHANGE, IssueRoleCtrl.this));
118  setText(newText);
119  }
120 
121 }
void setRoleSet(Set< IssueParticipantRole > roleSet)
Set< IssueParticipantRole > getRoleSet()
static String get(String msg)
Definition: I_.java:41