BrightSide Workbench Full Report + Source Code
SocialGroupListbox.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.social;
20 
21 import java.util.HashSet;
22 import java.util.Set;
23 import java.util.TreeSet;
24 import org.turro.contacts.Contact;
25 import org.turro.security.SecurityGroup;
26 import org.turro.security.SocialGroups;
27 import org.turro.zkoss.input.CollectionListbox;
28 
33 public class SocialGroupListbox extends CollectionListbox<SocialGroupValue> {
34 
35  public void setContact(Contact contact) {
36  if(getCollection() == null || getCollection().isEmpty()) {
37  loadSocialGroups(true);
38  }
39  SocialGroups instance = SocialGroups.instance();
40  Set<SocialGroupValue> selected = new HashSet<>();
41  contact.getSyndications().forEach(syndication -> {
42  SecurityGroup sg = instance.getSocialGroup(syndication.getName());
43  selected.add(new SocialGroupValue(sg.getName(), sg.getId(), sg.getType()));
44  });
45  setObjectValues(selected);
46  }
47 
48  @Override
49  protected String convertToString(SocialGroupValue v) {
50  return v.getName();
51  }
52 
53  public void loadSocialGroups(boolean multiple) {
54  setSelectFirst(false);
55  if(multiple) {
56  setMultiple(true);
57  setCheckmark(true);
58  }
59  TreeSet<SocialGroupValue> set = new TreeSet<>();
60  SocialGroups.allowedSocialGroups().forEach(sg -> {
61  set.add(new SocialGroupValue(sg.getName(), sg.getId(), sg.getType()));
62  });
63  updateCollection(set);
64  }
65 
66  public void setKeyString(String key) {
67  if(getCollection() == null || getCollection().isEmpty()) {
68  loadSocialGroups(false);
69  }
70  setObjectValue(getCollection().stream().filter(sgv -> sgv.getKey().equals(key)).findFirst().orElse(null));
71  }
72 
73  public String getKeyString() {
75  return sgm == null ? null : sgm.getKey();
76  }
77 
78 }
Set< Syndication > getSyndications()
Definition: Contact.java:403
SecurityGroup getSocialGroup(String id)
static Collection< SecurityGroup > allowedSocialGroups()
static SocialGroups instance()
void setObjectValues(Collection< V > collection)
void setSelectFirst(boolean selectFirst)