BrightSide Workbench Full Report + Source Code
TagPanel.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.tags;
20 
21 import java.util.Set;
22 import java.util.stream.Collectors;
23 import org.zkoss.zk.ui.event.Event;
24 import org.zkoss.zk.ui.event.Events;
25 import org.zkoss.zk.ui.ext.AfterCompose;
26 import org.zkoss.zul.Div;
27 import org.zkoss.zul.Label;
28 import org.zkoss.zul.Separator;
29 
34 public class TagPanel extends Div implements AfterCompose {
35 
36  private String entityRoot;
37  private TagSet tags;
38  private Div allTags;
39 
40  public TagPanel() {
41  allTags = new Div();
42  appendChild(allTags);
43  appendChild(new Separator("horizontal"));
44  }
45 
46  public String getEntityRoot() {
47  return entityRoot;
48  }
49 
50  public void setEntityRoot(String entityRoot) {
51  this.entityRoot = entityRoot;
52  }
53 
54  public Set<TagItem> getSelected() {
55  return tags.getSelected();
56  }
57 
58  public void updateTags() {
59  tags = Tags.getAvailables(entityRoot);
60  long maxUsage = tags.getMaxUsage();
61  drawTags(maxUsage);
62  }
63 
64  private void drawTags(long maxUsage) {
65  allTags.getChildren().clear();
66  for(TagItem tag : tags) {
67  final Label label = new Label(tag.getTagName());
68  String font = tag.isSelected() ? "bold" : "normal";
69  String color = tag.isSelected() ? "#333" : "#156dc6";
70  label.setStyle("cursor:pointer;font-size:" + (int) (11 +
71  (5 * (double)((double)tag.getUsage() / (double)maxUsage))) +
72  "px;font-weight:" + font + ";color:" + color);
73  label.setAttribute("tag", tag);
74  label.addEventListener(Events.ON_CLICK, (Event event) -> {
75  TagItem tag1 = (TagItem) label.getAttribute("tag");
76  tag1.setSelected(!tag1.isSelected());
77  Events.postEvent(new Event(Events.ON_CHANGE, TagPanel.this));
78  drawTags(maxUsage);
79  });
80  label.setTooltiptext(Tags.getSiblings(entityRoot,
81  Set.of(tag.getTagName())).stream().map(t -> t.getTagName())
82  .collect(Collectors.joining(", ")));
83  allTags.appendChild(label);
84  allTags.appendChild(new Separator("vertical"));
85  }
86  }
87 
88  @Override
89  public void afterCompose() {
90  updateTags();
91  }
92 
93 }
void setEntityRoot(String entityRoot)
Definition: TagPanel.java:50
Set< TagItem > getSelected()
Definition: TagPanel.java:54
TreeSet< TagItem > getSelected()
Definition: TagSet.java:96
static TagSet getSiblings(Set< String > tagNames)
Definition: Tags.java:82
static TagSet getAvailables()
Definition: Tags.java:65