BrightSide Workbench Full Report + Source Code
TagLabel.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.Collection;
22 import org.turro.string.Strings;
23 import org.turro.annotation.ElephantPlugin;
24 import org.turro.elephant.context.Application;
25 import org.turro.entities.Entities;
26 import org.turro.path.Path;
27 import org.turro.zkoss.label.LabelEditable;
28 import org.zkoss.zk.ui.event.Event;
29 import org.zkoss.zk.ui.event.EventListener;
30 import org.zkoss.zk.ui.event.Events;
31 import org.zkoss.zk.ui.ext.AfterCompose;
32 import org.zkoss.zul.Hbox;
33 import org.zkoss.zul.Label;
34 
39 @ElephantPlugin(label="tag-label")
40 public class TagLabel extends Hbox implements ITagCtrl, AfterCompose, EventListener {
41 
42  private TagSet set;
43  private String entityPath, role;
44  private Object entity;
45 
46  public TagLabel() {
47  setSclass("z-valign-middle tagitLabel");
48  setAlign("center");
49  }
50 
51  @Override
52  public void setEntity(Object entity) {
53  this.entity = entity;
54  if(entity instanceof String) {
55  entityPath = (String) entity;
56  } else {
57  entityPath = Entities.getController(entity).getPath();
58  }
59  }
60 
61  @Override
62  public void setEntityPath(String entityPath) {
63  this.entityPath = entityPath;
64  }
65 
66  @Override
67  public void updateControls() {
68  afterCompose();
69  }
70 
71  @Override
72  public void afterCompose() {
74  role = new Path(entityPath).getRoot();
75 // if(!app.isInRole(role + "-tagit:list")) {
76 // setVisible(false);
77 // }
78  if(set == null) {
79  Collection<String> choices = Tags.tagChoices(entityPath);
80  getChildren().clear();
81  set = Tags.getTags(entityPath);
82  LabelEditable le;
83  if(!set.isEmpty()) {
84  appendChild(new Label("["));
85  for(TagItem ti : set) {
86  le = new LabelEditable(ti.getTagName());
87  le.setEditable(app.isInRole(role + "-tagit:edit"));
88  le.setEditorCols(12);
89  le.setValues(choices);
90  appendChild(le);
91  le.addEventListener(Events.ON_CHANGE, this);
92  }
93  appendChild(new Label("]"));
94  }
95  if(app.isInRole(role + "-tagit:new")) {
96  le = new LabelEditable("", "/_zul/images/tag_new.png");
97  le.setEditorCols(12);
98  le.setValues(choices);
99  appendChild(le);
100  le.addEventListener(Events.ON_CHANGE, this);
101  }
102  for(Object obj : getChildren()) {
103  if(obj instanceof AfterCompose) {
104  ((AfterCompose) obj).afterCompose();
105  }
106  }
107  }
108  }
109 
110  @Override
111  public void onEvent(Event event) throws Exception {
112  if(event.getData() instanceof LabelEditable) {
113  LabelEditable le = (LabelEditable) event.getData();
114  if(!Strings.isBlank(le.getLabel())) {
115  if(!Strings.isBlank(le.getImage())) {
116  // add tag
117  Tags.addTag(entityPath, le.getLabel());
118  } else {
119  // change tag
120  Tags.changeTag(entityPath, le.getOldLabel(), le.getLabel());
121  }
122  } else {
123  if(!Strings.isBlank(le.getImage())) {
124  // nothing, insertion cancelled
125  } else {
126  // delete tag
127  if(Application.getApplication().isInRole(role + "-tagit:delete")) {
128  Tags.removeTag(entityPath, le.getOldLabel());
129  }
130  }
131  }
132  }
133  set = null;
134  afterCompose();
135  }
136 
137 }
138 
static IElephantEntity getController(String path)
Definition: Entities.java:78
void setEntity(Object entity)
Definition: TagLabel.java:52
void setEntityPath(String entityPath)
Definition: TagLabel.java:62
void onEvent(Event event)
Definition: TagLabel.java:111
static void addTag(Object entity, String tagName)
Definition: Tags.java:188
static void removeTag(Object entity, String tagName)
Definition: Tags.java:215
static TagSet getTags(Object entity)
Definition: Tags.java:351
static void changeTag(String entityPath, String oldName, String newName)
Definition: Tags.java:202
static Collection< String > tagChoices(Object entity)
Definition: Tags.java:398
void setValues(Collection< String > values)
void setEditable(boolean editable)