BrightSide Workbench Full Report + Source Code
EntityTreeItem.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.entities;
20 
21 import java.util.Collections;
22 import java.util.List;
23 import org.zkoss.zk.ui.event.Event;
24 import org.zkoss.zk.ui.event.EventListener;
25 import org.zkoss.zk.ui.event.Events;
26 import org.zkoss.zk.ui.ext.AfterCompose;
27 import org.zkoss.zul.Treechildren;
28 import org.zkoss.zul.Treeitem;
29 
34 public class EntityTreeItem extends Treeitem implements AfterCompose {
35 
36  private Treechildren children;
37  private boolean loadOnDemand;
38 
39  public EntityTreeItem(IElephantEntity entity, boolean loadOnDemand) {
40  super();
41  this.loadOnDemand = loadOnDemand;
42  setValue(entity);
43  setLabel(entity.getName());
44  setTooltiptext(getLabel());
45  }
46 
47  @Override
48  public EntityTree getTree() {
49  return (EntityTree) super.getTree();
50  }
51 
53  return (IElephantEntity) getValue();
54  }
55 
56  public void showContents() {
57  fillCategory();
58  setOpen(true);
59  }
60 
61  public void reloadContents() {
62  if(children != null) {
63  children.getChildren().clear();
64  showContents();
65  }
66  }
67 
68  public boolean isLeaf() {
69  return getEntityChildren().isEmpty();
70  }
71 
72  private void initLoadOnDemand() {
73  if(!loadOnDemand) return;
74 
75  setOpen(false);
76 
77  addEventListener(Events.ON_OPEN, new EventListener() {
78  @Override
79  public void onEvent(Event event) throws Exception {
80  // load files and add folders
81  if(isOpen()) {
82  fillCategory();
83  }
84  }
85  });
86  }
87 
88  public EntityTreeItem addCategory(IElephantEntity category, boolean loadOnDemand) {
89  EntityTreeItem cat = new EntityTreeItem(category, loadOnDemand);
90  children.appendChild(cat);
91  cat.afterCompose();
92  return cat;
93  }
94 
95  private void fillCategory() {
96  if(children != null && children.getChildren().isEmpty()) {
97  for(IElephantEntity cat : getEntityChildren()) {
98  addCategory(cat, loadOnDemand);
99  }
100  }
101  }
102 
103  private void addChildrenSpace() {
104  if(!isLeaf()) {
105  children = new Treechildren();
106  appendChild(children);
107  if(!loadOnDemand) {
108  fillCategory();
109  setOpen(true);
110  }
111  }
112  }
113 
114  public boolean isLoadOnDemand() {
115  return loadOnDemand;
116  }
117 
118  public void setLoadOnDemand(boolean loadOnDemand) {
119  this.loadOnDemand = loadOnDemand;
120  }
121 
122  @Override
123  public void afterCompose() {
124  initLoadOnDemand();
125  addChildrenSpace();
126  }
127 
128  private List<IElephantEntity> entityChildren;
129 
130  private List<IElephantEntity> getEntityChildren() {
131  if(entityChildren == null) {
132  entityChildren = getCategory().getChildren();
133  }
134  if(entityChildren == null) {
135  entityChildren = Collections.EMPTY_LIST;
136  }
137  return entityChildren;
138  }
139 
140 }
EntityTreeItem(IElephantEntity entity, boolean loadOnDemand)
void setLoadOnDemand(boolean loadOnDemand)
EntityTreeItem addCategory(IElephantEntity category, boolean loadOnDemand)
List< IElephantEntity > getChildren()