BrightSide Workbench Full Report + Source Code
ParticipationLink.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2024 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.participation;
20 
21 import org.turro.action.MailSenders;
22 import org.turro.assistant.ParticipationInfo;
23 import org.turro.auth.Authentication;
24 import org.turro.entities.Entities;
25 import org.turro.entities.IElephantEntity;
26 import org.turro.i18n.I_;
27 import org.turro.plugin.contacts.IContact;
28 import org.turro.string.Strings;
29 import org.zkoss.zk.ui.event.Events;
30 import org.zkoss.zk.ui.ext.AfterCompose;
31 import org.zkoss.zul.A;
32 import org.zkoss.zul.Html;
33 
38 public class ParticipationLink extends A implements AfterCompose {
39 
40  private final Html html;
41 
42  private Object entity;
43  private ParticipationReason reason;
44  private String name, description;
45 
46  public ParticipationLink() {
47  html = new Html();
48  appendChild(html);
49  }
50 
51  public void setReason(String reason) {
52  this.reason = ParticipationReason.valueOf(reason);
53  }
54 
55  public void setEntity(Object entity) {
56  this.entity = entity;
57  }
58 
59  public void setName(String name) {
60  this.name = name;
61  }
62 
63  public void setDescription(String description) {
64  this.description = description;
65  }
66 
67  @Override
68  public void afterCompose() {
69  addEventListener(Events.ON_CLICK, (event) -> {
70  execute();
71  adjust();
72  });
73  addEventListener(Events.ON_MOUSE_OVER, (event) -> {
74  adjustHover();
75  });
76  addEventListener(Events.ON_MOUSE_OUT, (event) -> {
77  adjust();
78  });
79  adjust();
80  }
81 
82  public void adjust() {
83  switch(reason) {
84  case REASON_APPLY -> adjustApply();
85  }
86  }
87 
88  public void adjustHover() {
89  switch(reason) {
90  case REASON_APPLY -> adjustHoverApply();
91  }
92  }
93 
94  private void adjustApply() {
95  setIconSclass("hand point up outline icon");
98  iee = Entities.getController(entity);
99  if(!iec.isEmpty() && !iee.isEmpty()) {
100  ParticipationInfo info = new ParticipationInfo(iee.getPath(), iec.getPath(), reason);
101  setTooltiptext(info.getCount() + " " + I_.get("Applying"));
102  if(info.isChecked()) {
103  setSclass("ui green button");
104  html.setContent(I_.get("Applying"));
105  } else {
106  setSclass("ui grey button");
107  html.setContent(I_.get("Apply"));
108  }
109  }
110  }
111 
112  private void adjustHoverApply() {
113  IContact contact = Authentication.getIContact();
114  IElephantEntity iec = Entities.getController(contact.getContact()),
115  iee = Entities.getController(entity);
116  if(!iec.isEmpty() && !iee.isEmpty()) {
117  ParticipationInfo info = new ParticipationInfo(iee.getPath(), iec.getPath(), reason);
118  if(info.isChecked()) {
119  setSclass("ui red button");
120  html.setContent(I_.get("Stop applying"));
121  }
122  }
123  }
124 
125  private void execute() {
126  IContact contact = Authentication.getIContact();
127  IElephantEntity iec = Entities.getController(contact.getContact()),
128  iee = Entities.getController(entity);
129  if(!iec.isEmpty() && !iee.isEmpty()) {
130  ParticipationInfo info = new ParticipationInfo(iee.getPath(), iec.getPath(), reason);
131  boolean selected = info.toggle();
132  if(selected && reason.isMailableOnSelect()) {
133  String subject = iec.getName() + " " + info.getReasonStr() + " " + Strings.isBlank(name, iee.getName());
134  MailSenders.getPool()
135  .addAdministrators()
136  .send(subject, subject + "\n\n" + Strings.isBlank(description, iee.getDescription()));
137  } else if(reason.isMailableOnDeselect()) {
138  String subject = iec.getName() + " NOT " + info.getReasonStr() + " " + Strings.isBlank(name, iee.getName());
139  MailSenders.getPool()
140  .addAdministrators()
141  .send(subject, subject + "\n\n" + Strings.isBlank(description, iee.getDescription()));
142  }
143  }
144  }
145 
146 }
static IElephantEntity getController(String path)
Definition: Entities.java:78
static String get(String msg)
Definition: I_.java:41
IElephantEntity getController(String entityPath)