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