BrightSide Workbench Full Report + Source Code
GroupItItem.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.zul.groupit;
20 
21 import org.turro.string.Strings;
22 import org.turro.contacts.GroupIt;
23 import org.turro.i18n.I_;
24 import org.zkoss.zk.ui.event.Event;
25 import org.zkoss.zk.ui.event.EventListener;
26 import org.zkoss.zk.ui.event.Events;
27 import org.zkoss.zk.ui.ext.AfterCompose;
28 import org.zkoss.zul.Treechildren;
29 import org.zkoss.zul.Treeitem;
30 
35 public class GroupItItem extends Treeitem implements AfterCompose {
36 
37  private Treechildren children;
38  private boolean loadOnDemand;
39  private final String entityPath;
40 
41  public GroupItItem(GroupIt group, boolean loadOnDemand, String entityPath) {
42  super();
43  this.loadOnDemand = loadOnDemand;
44  this.entityPath = entityPath;
45  setValue(group);
46  setLabel(group == null ? I_.get("All") : group.getName());
47  setTooltiptext(getLabel());
48  }
49 
50  @Override
51  public GroupItTree getTree() {
52  return (GroupItTree) super.getTree();
53  }
54 
55  public GroupIt getGroup() {
56  return (GroupIt) getValue();
57  }
58 
59  public void showContents() {
60  fillGroup();
61  setOpen(true);
62  }
63 
64  public void reloadContents() {
65  if(!isLeaf() && children == null) {
66  children = new Treechildren();
67  appendChild(children);
68  }
69  if(children != null) {
70  children.getChildren().clear();
71  showContents();
72  }
73  }
74 
75  public boolean isLeaf() {
76  GroupIt group = getGroup();
77  if(Strings.isBlank(entityPath)) {
78  return group == null ?
79  GroupIt.roots(getTree().getCategory()).isEmpty() :
80  group.children().isEmpty();
81  } else {
82  return group == null ?
83  GroupIt.roots(getTree().getCategory(), entityPath).isEmpty() :
84  group.children(entityPath).isEmpty();
85  }
86  }
87 
88  private void initLoadOnDemand() {
89  if(!loadOnDemand) return;
90 
91  setOpen(false);
92 
93  addEventListener(Events.ON_OPEN, new EventListener() {
94  @Override
95  public void onEvent(Event event) throws Exception {
96  // load files and add folders
97  if(isOpen()) {
98  fillGroup();
99  }
100  }
101  });
102  }
103 
104  public GroupItItem addGroup(GroupIt group, boolean loadOnDemand) {
105  GroupItItem cat = new GroupItItem(group, loadOnDemand, entityPath);
106  children.appendChild(cat);
107  cat.afterCompose();
108  return cat;
109  }
110 
111  private void fillGroup() {
112  if(children != null && children.getChildren().isEmpty()) {
113  GroupIt group = getGroup();
114  if(Strings.isBlank(entityPath)) {
115  for(GroupIt gpr : group == null ?
116  GroupIt.roots(getTree().getCategory()) :
117  group.children()) {
118  addGroup(gpr, loadOnDemand);
119  }
120  } else {
121  for(GroupIt gpr : group == null ?
122  GroupIt.roots(getTree().getCategory(), entityPath) :
123  group.children(entityPath)) {
124  addGroup(gpr, loadOnDemand);
125  }
126  }
127  }
128  }
129 
130  private void addChildrenSpace() {
131  if(!isLeaf()) {
132  children = new Treechildren();
133  appendChild(children);
134  if(!loadOnDemand) {
135  fillGroup();
136  setOpen(true);
137  }
138  }
139  }
140 
141  public boolean isLoadOnDemand() {
142  return loadOnDemand;
143  }
144 
145  public void setLoadOnDemand(boolean loadOnDemand) {
146  this.loadOnDemand = loadOnDemand;
147  }
148 
149  @Override
150  public void afterCompose() {
151  initLoadOnDemand();
152  addChildrenSpace();
153  }
154 
155 }
Collection< GroupIt > children()
Definition: GroupIt.java:148
static Collection< GroupIt > roots(String category)
Definition: GroupIt.java:122
static String get(String msg)
Definition: I_.java:41
void setLoadOnDemand(boolean loadOnDemand)
GroupItItem addGroup(GroupIt group, boolean loadOnDemand)
GroupItItem(GroupIt group, boolean loadOnDemand, String entityPath)