BrightSide Workbench Full Report + Source Code
PublishGroup.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 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.profile;
20 
21 import org.turro.contacts.Contact;
22 import org.turro.i18n.I_;
23 import org.zkoss.zk.ui.event.Events;
24 import org.zkoss.zk.ui.ext.AfterCompose;
25 import org.zkoss.zul.Radio;
26 import org.zkoss.zul.Radiogroup;
27 
32 public class PublishGroup extends Radiogroup implements AfterCompose {
33 
34  private Contact contact;
35  private String element;
36  private ProfileRelation relation;
37 
38  public void setContact(Contact contact) {
39  this.contact = contact;
40  }
41 
42  public void setElement(String element) {
43  this.element = element;
44  }
45 
46  public void setRelation(ProfileRelation relation) {
47  this.relation = relation;
48  }
49 
50  @Override
51  public void afterCompose() {
52  createItems();
53  ProfilePolicies pp = new ProfilePolicies(contact);
54  select(pp.getPolicy(relation, element));
55  addEventListener(Events.ON_CHECK, (event) -> {
56  Radio radio = getSelectedItem();
57  ProfilePolicies policies = new ProfilePolicies(contact);
58  PublishPolicy policy = PublishPolicy.valueOf(radio.getValue());
59  policies.setPolicy(relation == null ? contact : relation.getRelation(), element, policy);
60  policies.save();
61  });
62  }
63 
64  private void select(PublishPolicy policy) {
65  for(Radio item : getItems()) {
66  if(policy.toString().equals(item.getValue())) {
67  setSelectedItem(item);
68  }
69  }
70  }
71 
72  private void createItems() {
73  appendItem(I_.get("Public"), PublishPolicy.PUBLISH_ALL.toString());
74  appendItem(I_.get("User"), PublishPolicy.PUBLISH_USERS.toString());
75  appendItem(I_.get("Following"), PublishPolicy.PUBLISH_FOLLOWING.toString());
76  appendItem(I_.get("Private"), PublishPolicy.PUBLISH_NONE.toString());
77  }
78 
79 }
PublishPolicy getPolicy(Object obj, String element)
void setRelation(ProfileRelation relation)