BrightSide Workbench Full Report + Source Code
SecPanel.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.security;
20 
21 import java.util.Set;
22 import java.util.stream.Collectors;
23 import org.turro.i18n.I_;
24 import org.zkoss.zk.ui.event.Event;
25 import org.zkoss.zk.ui.event.Events;
26 import org.zkoss.zk.ui.ext.AfterCompose;
27 import org.zkoss.zul.Div;
28 import org.zkoss.zul.Label;
29 import org.zkoss.zul.Separator;
30 
35 public class SecPanel extends Div implements AfterCompose {
36 
37  private SecSet secs;
38  private Div allSecs;
39 
40  public SecPanel() {
41  allSecs = new Div();
42  appendChild(allSecs);
43  appendChild(new Separator("horizontal"));
44  }
45 
46  public Set<SecItem> getSelected() {
47  return secs.getSelected();
48  }
49 
50  public void updateSecs() {
51  secs = Secs.getAvailables();
52  long maxUsage = secs.getMaxUsage();
53  drawSecs(maxUsage);
54  }
55 
56  private void drawSecs(long maxUsage) {
57  allSecs.getChildren().clear();
58  for(SecItem sec : secs) {
59  final Label label = new Label(I_.get(SecurityGroups.get(sec.getSecName()).getName()));
60  String font = sec.isSelected() ? "bold" : "normal";
61  String color = sec.isSelected() ? "#333" : "#156dc6";
62  label.setStyle("cursor:pointer;font-size:" + (int) (11 +
63  (5 * (double)((double)sec.getUsage() / (double)maxUsage))) +
64  "px;font-weight:" + font + ";color:" + color);
65  label.setAttribute("sec", sec);
66  label.addEventListener(Events.ON_CLICK, (Event event) -> {
67  SecItem sec1 = (SecItem) label.getAttribute("sec");
68  sec1.setSelected(!sec1.isSelected());
69  Events.postEvent(new Event(Events.ON_CHANGE, SecPanel.this));
70  drawSecs(maxUsage);
71  });
72  label.setTooltiptext(Secs.getSiblings(
73  Set.of(sec.getSecName())).stream().map(t -> t.getSecName())
74  .collect(Collectors.joining(", ")));
75  allSecs.appendChild(label);
76  allSecs.appendChild(new Separator("vertical"));
77  }
78  }
79 
80  @Override
81  public void afterCompose() {
82  updateSecs();
83  }
84 
85 }
static String get(String msg)
Definition: I_.java:41
Set< SecItem > getSelected()
Definition: SecPanel.java:46
TreeSet< SecItem > getSelected()
Definition: SecSet.java:94
static SecSet getSiblings(Set< String > secNames)
Definition: Secs.java:59
static SecSet getAvailables()
Definition: Secs.java:49
static SecurityGroup get(String id)